Qt3d's Study (iv): Specifying the rendered material and effect

Source: Internet
Author: User

Qt3d's Study (iv): Specifying the rendered material and effect

In the previous article I learned how to simply display the model. The Qt3d contains a parser for the OBJ model, so that a simple obj model can be loaded and displayed. In fact, Qt3d for the rendering of the configuration is very strong, by setting the material, we can get a very good rendering effect, and can be set by the effect, the specified shader load, let Qt and OpenGL compile, get their own desired rendering effect.

Saiyang original article, starting address: http://blog.csdn.net/gamesdev/article/details/43983493. Welcome to come to discuss the peer.

On the basis of the QML code introduced in the previous article, we simply added 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 results of the operation are as follows:

Here we are in//! [4] A commonly used material is added: Phong material. Phong illumination model is a common illumination model, and he reveals the method of generating high light. In the past, my blog has introduced and implemented such a lighting model. The reason for its universality is that Qt 3D encapsulates a class that is easy to operate. The picture shows the use of the Phong illumination model, which gives the toy aircraft a smooth and transparent effect. We can also set specific parameters to give the light a different effect: Set global light (ambient), diffuse (diffuse), specular (specular), and self-luminous brightness (shininess). The code that is added 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.vec  Tor3d (0.0, 0.0, 0.0)} components:framegraph {forwardrenderer {ClearColor:Qt.rgba ( 0, 0, 0, 1) Camera:camera}} Entity {Mesh {id:mesh o Bjectname: "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 results of the program run are as follows:

This presents a glittering model of toy airplanes.

If we want to write our own shader to color our model, Qt 3D also provides a method that can be implemented without the use of C + + code, and is specified directly in the QML. Here we need to set the effect (effect), the technique (OpenGL technology used), the Renderpass (number of render passes), the Shaderprogram (shader). Here 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.vec  Tor3d (0.0, 0.0, 0.0)} components:framegraph {forwardrenderer {ClearColor:Qt.rgba ( 0, 0, 0, 1) Camera:camera}} Entity {Mesh {Id:mesh s Ource: "Qrc:/toyplane.obj"}//!                [5] Material {id:material Effect:effect effect { Id:effect techniques: [technique] Technique {Id:te Chnique Openglfilter {api:openglfiltEr. Desktop Profile:OpenGLFilter.None majorversion:2 mi                    norversion:0} renderpasses: [Renderpass] Renderpass {Id:renderpass Shaderprogram:simplesp Sh Aderprogram {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]. Instead of using the default phonematerial, we use its parent class, material, and implement the rendering you need by specifying effect. The technique contained in effect represents the OpenGL technology used, because OpenGL has multiple versions, sub-OpenGL and OpenGL ES, and core profile and compatibility profile, This makes all kinds of technology become very complicated. So Qt 3D puts forward the concept of technique. Use the OpenGL API we need by specifying the API, profile, and MajorVersion and minorversion versions. The Renderpass is then rendered (the translation is not always good), which indicates how many times the rendering operation is required for a rendering. For example, in the shadow map technique, you need to render more than one time, so specify two renderpass. For each renderpass, the shader program needs to be specified. Therefore, we load the corresponding shader file to let the system compile and link it, and finally render it. In this example, we implement one of the simplest shader Simple.vert and Simple.frag, which are as follows:

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 explained here that Qt 3D specifies the default attribute (attribute) and consistency (uniform) variables in Meshdata, which are as follows:

Attribute (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 become different as the version changes

This is a very simple shader, it is output in monochrome. Here's how to run:

By gradually enriching the shader's content, you can let the program get a more beautiful rendering effect.

Qt3d Study (iv): Specify the material to render and the effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.