Research on Qt3D (4): Specify the rendering material and effect.
Research on Qt3D (4): Specify the rendering material and effect
In the previous article, I learned how to simply display the model. Qt3D has a built-in obj model parser to load and display a simple obj model. In fact, Qt3D is still very powerful in rendering configuration. By setting the material, we can get a very good rendering Effect. In addition, we can set Effect to load our own designated shader, let Qt 3D and OpenGL compile to get the desired rendering effect.
Jiang caiyang's original article, first published at: http://blog.csdn.net/gamesdev/article/details/43983493. Welcome to the discussion.
Based on the QML Code introduced in the previous article, we simply add a material. The Code is as follows:
Entity { Mesh { id: mesh objectName: "toyPlane" source: "qrc:/toyplane.obj" } //! [4] PhongMaterial { id: phongMaterial } //! [4] components: [ mesh, phongMaterial ] }
The running result is as follows:
Here we are //! [4] added a common material: Phong. The Phong illumination model is a common illumination model that reveals how to generate highlights. In the past, my blog also introduced and implemented such a lighting model. Due to its extensiveness, Qt 3D encapsulates a class for convenient operation. The figure shows that the Phong illumination model is used to make the toy airplane smooth and transparent. We can also set specific parameters for different lighting effects: set the global light (ambient), diffuse, specular, and shininess ). The added code is as follows:
import Qt3D 2.0import Qt3D.Render 2.0Entity{ id: root Camera { id: camera position: Qt.vector3d( 0.0, 0.0, 40.0 ) projectionType: CameraLens.PerspectiveProjection fieldOfView: 45 aspectRatio: 16.0 / 9.0 nearPlane : 0.1 farPlane : 1000.0 upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) } components: FrameGraph { ForwardRenderer { clearColor: Qt.rgba( 0, 0, 0, 1 ) camera: camera } } Entity { Mesh { id: mesh objectName: "toyPlane" source: "qrc:/toyplane.obj" } //! [4] PhongMaterial { id: phongMaterial ambient: Qt.rgba( 0.6, 0.2, 0.1, 1 ) diffuse: Qt.rgba( 0.2, 0.6, 0.1, 1 ) specular: Qt.rgba( 0.2, 0.9, 0.1, 1 ) shininess: 0.6 } //! [4] components: [ mesh, phongMaterial ] } Configuration { controlledCamera: camera }}
The program running result is as follows:
Here we present a shiny model of a toy airplane.
If we want to write our own shader to color our model, Qt 3D also provides the corresponding method, which can be implemented without using C ++ code and directly specified in QML. Here we need to set Effect (Effect), Technique (OpenGL technology used), RenderPass (rendering times), and ShaderProgram (coloring tool ). The following is the QML code:
import Qt3D 2.0import Qt3D.Render 2.0Entity{ id: root Camera { id: camera position: Qt.vector3d( 0.0, 0.0, 40.0 ) projectionType: CameraLens.PerspectiveProjection fieldOfView: 45 aspectRatio: 16.0 / 9.0 nearPlane : 0.1 farPlane : 1000.0 upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) } components: FrameGraph { ForwardRenderer { clearColor: Qt.rgba( 0, 0, 0, 1 ) camera: camera } } Entity { Mesh { id: mesh source: "qrc:/toyplane.obj" } //! [5] Material { id: material effect: effect Effect { id: effect techniques: [ technique ] Technique { id: technique openGLFilter { api: OpenGLFilter.Desktop profile: OpenGLFilter.None majorVersion: 2 minorVersion: 0 } renderPasses: [ renderPass ] RenderPass { id: renderPass shaderProgram: simpleSP ShaderProgram { id: simpleSP vertexShaderCode: loadSource( "qrc:/Simple.vert" ) fragmentShaderCode: loadSource( "qrc:/Simple.frag" ) } } } } } //! [5] components: [ mesh, material ] } Configuration { controlledCamera: camera }}
The newly added part of the code is in //! [5. Here we do not use the default PhoneMaterial, but use its parent class Material and specify the Effect to implement the rendering Effect we need. Technique in Effect indicates the OpenGL technology used, Because OpenGL has multiple versions, including OpenGL and OpenGL ES, core profile and compatibility profile, which makes various technologies complicated. Therefore, Qt 3D proposed the Technique concept. Specify the api, profile, majorVersion, and minorVersion to use the OpenGL API we need. The next step is RenderPass rendering (this translation always feels bad), which indicates the number of rendering operations required for a rendering. For example, in the shadow map technology, rendering must be performed more than once. Therefore, you must specify two RenderPass. The RenderPass must be specified for each RenderPass. Therefore, we load the corresponding coloring tool file to let the system compile and link, and finally render it. In this example, we implement a Simple coloring tool Simple. vert and Simple. frag, as shown below:
// Simple.vert#version 100attribute vec3 vertexPosition;uniform mat4 mvp;void main( void ){ gl_Position = mvp * vec4( vertexPosition, 1.0 );}
// Simple.frag#version 100void main( void ){ gl_FragColor = vec4( 1.0, 0.8, 0.2, 1.0 );}
It should be noted that Qt 3D specifies the default attribute variables and the consistency (uniform) variables in MeshData. They are as follows:
Attribute variable |
Consistency (uniform) Variable |
VertexPosition |
ModelMatrix |
VertexTexCoord |
ViewMatrix |
VertexNormal |
ProjectionMatrix |
VertexColor |
ModelView |
VertexTangent |
ModelViewProjection |
|
Mvp |
|
InverseModelMatrix |
|
InverViewMatrix |
|
InverseProjectionMatrix |
|
InverseModelView |
|
InverseModelViewProjection |
|
ModelNormalMatrix |
|
ModelViewNormal |
|
ViewportMatrix |
|
InverseViewportMatrix |
|
Time |
※These variables are not listed in the document and may vary with version changes.
This is a very simple coloring tool that outputs in a single color. Run the following command:
By gradually enriching the content of the coloring tool, the program can get a better rendering effect.