21 #include <Qt3DExtras/QDiffuseSpecularMaterial>
22 #include <Qt3DRender/QAttribute>
23 #include <Qt3DRender/QBuffer>
24 #include <Qt3DRender/QGeometry>
25 #include <Qt3DRender/QParameter>
26 #include <Qt3DRender/QEffect>
27 #include <Qt3DRender/QTechnique>
28 #include <Qt3DRender/QGraphicsApiFilter>
34 return QStringLiteral(
"phong" );
70 mShininess = elem.attribute( QStringLiteral(
"shininess" ) ).toFloat();
71 mOpacity = elem.attribute( QStringLiteral(
"opacity" ), QStringLiteral(
"1.0" ) ).toFloat();
81 elem.setAttribute( QStringLiteral(
"shininess" ), mShininess );
82 elem.setAttribute( QStringLiteral(
"opacity" ), mOpacity );
100 return dataDefinedMaterial();
103 Qt3DExtras::QDiffuseSpecularMaterial *material =
new Qt3DExtras::QDiffuseSpecularMaterial;
104 material->setDiffuse( QColor( mDiffuse.red(), mDiffuse.green(), mDiffuse.blue(),
opacity ) );
105 material->setAmbient( QColor( mAmbient.red(), mAmbient.green(), mAmbient.blue(),
opacity ) );
106 material->setSpecular( QColor( mSpecular.red(), mSpecular.green(), mSpecular.blue(),
opacity ) );
107 material->setShininess( mShininess );
110 material->setAlphaBlendingEnabled(
true );
130 QMap<QString, QString> parameters;
131 parameters[ QStringLiteral(
"Kd" ) ] = QStringLiteral(
"%1 %2 %3" ).arg( mDiffuse.redF() ).arg( mDiffuse.greenF() ).arg( mDiffuse.blueF() );
132 parameters[ QStringLiteral(
"Ka" ) ] = QStringLiteral(
"%1 %2 %3" ).arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
133 parameters[ QStringLiteral(
"Ks" ) ] = QStringLiteral(
"%1 %2 %3" ).arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
134 parameters[ QStringLiteral(
"Ns" ) ] = QString::number( mShininess );
140 Qt3DRender::QParameter *ambientParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ka" ), QColor::fromRgbF( 0.05f, 0.05f, 0.05f, 1.0f ) );
141 Qt3DRender::QParameter *diffuseParameter =
new Qt3DRender::QParameter( QStringLiteral(
"kd" ), QColor::fromRgbF( 0.7f, 0.7f, 0.7f, 1.0f ) );
142 Qt3DRender::QParameter *specularParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ks" ), QColor::fromRgbF( 0.01f, 0.01f, 0.01f, 1.0f ) );
143 Qt3DRender::QParameter *shininessParameter =
new Qt3DRender::QParameter( QStringLiteral(
"shininess" ), 150.0f );
145 diffuseParameter->setValue( mDiffuse );
146 ambientParameter->setValue( mAmbient );
147 specularParameter->setValue( mSpecular );
148 shininessParameter->setValue( mShininess );
150 effect->addParameter( ambientParameter );
151 effect->addParameter( diffuseParameter );
152 effect->addParameter( specularParameter );
153 effect->addParameter( shininessParameter );
163 array.resize(
sizeof(
unsigned char ) * 9 );
164 unsigned char *fptr =
reinterpret_cast<unsigned char *
>( array.data() );
166 *fptr++ =
static_cast<unsigned char>(
diffuse.red() );
167 *fptr++ =
static_cast<unsigned char>(
diffuse.green() );
168 *fptr++ =
static_cast<unsigned char>(
diffuse.blue() );
170 *fptr++ =
static_cast<unsigned char>(
ambient.red() );
171 *fptr++ =
static_cast<unsigned char>(
ambient.green() );
172 *fptr++ =
static_cast<unsigned char>(
ambient.blue() );
174 *fptr++ =
static_cast<unsigned char>(
specular.red() );
175 *fptr++ =
static_cast<unsigned char>(
specular.green() );
176 *fptr++ =
static_cast<unsigned char>(
specular.blue() );
185 Qt3DRender::QBuffer *dataBuffer =
new Qt3DRender::QBuffer( geometry );
187 Qt3DRender::QAttribute *diffuseAttribute =
new Qt3DRender::QAttribute( geometry );
188 diffuseAttribute->setName( QStringLiteral(
"dataDefinedDiffuseColor" ) );
189 diffuseAttribute->setVertexBaseType( Qt3DRender::QAttribute::UnsignedByte );
190 diffuseAttribute->setVertexSize( 3 );
191 diffuseAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
192 diffuseAttribute->setBuffer( dataBuffer );
193 diffuseAttribute->setByteStride( 9 *
sizeof(
unsigned char ) );
194 diffuseAttribute->setByteOffset( 0 );
195 diffuseAttribute->setCount( vertexCount );
196 geometry->addAttribute( diffuseAttribute );
198 Qt3DRender::QAttribute *ambientAttribute =
new Qt3DRender::QAttribute( geometry );
199 ambientAttribute->setName( QStringLiteral(
"dataDefinedAmbiantColor" ) );
200 ambientAttribute->setVertexBaseType( Qt3DRender::QAttribute::UnsignedByte );
201 ambientAttribute->setVertexSize( 3 );
202 ambientAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
203 ambientAttribute->setBuffer( dataBuffer );
204 ambientAttribute->setByteStride( 9 *
sizeof(
unsigned char ) );
205 ambientAttribute->setByteOffset( 3 *
sizeof(
unsigned char ) );
206 ambientAttribute->setCount( vertexCount );
207 geometry->addAttribute( ambientAttribute );
209 Qt3DRender::QAttribute *specularAttribute =
new Qt3DRender::QAttribute( geometry );
210 specularAttribute->setName( QStringLiteral(
"dataDefinedSpecularColor" ) );
211 specularAttribute->setVertexBaseType( Qt3DRender::QAttribute::UnsignedByte );
212 specularAttribute->setVertexSize( 3 );
213 specularAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
214 specularAttribute->setBuffer( dataBuffer );
215 specularAttribute->setByteStride( 9 *
sizeof(
unsigned char ) );
216 specularAttribute->setByteOffset( 6 *
sizeof(
unsigned char ) );
217 specularAttribute->setCount( vertexCount );
218 geometry->addAttribute( specularAttribute );
220 dataBuffer->setData( data );
223 Qt3DRender::QMaterial *QgsPhongMaterialSettings::dataDefinedMaterial()
const
225 Qt3DRender::QMaterial *material =
new Qt3DRender::QMaterial;
227 Qt3DRender::QEffect *eff =
new Qt3DRender::QEffect( material );
229 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
230 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
231 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
232 technique->graphicsApiFilter()->setMajorVersion( 3 );
233 technique->graphicsApiFilter()->setMinorVersion( 3 );
234 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey();
235 filterKey->setName( QStringLiteral(
"renderingStyle" ) );
236 filterKey->setValue( QStringLiteral(
"forward" ) );
237 technique->addFilterKey( filterKey );
239 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass();
240 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram();
243 const QUrl urlVert( QStringLiteral(
"qrc:/shaders/phongDataDefined.vert" ) );
244 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
245 const QUrl urlFrag( QStringLiteral(
"qrc:/shaders/phongDataDefined.frag" ) );
246 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Fragment, Qt3DRender::QShaderProgram::loadSource( urlFrag ) );
248 renderPass->setShaderProgram( shaderProgram );
249 technique->addRenderPass( renderPass );
251 technique->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"shininess" ), mShininess ) );
253 eff->addTechnique( technique );
254 material->setEffect( eff );