#include #include #include #include #include #include #include static QString vertexShader = "#version 100\n" "\n" "attribute vec3 vertexPosition;\n" "attribute vec3 vertexNormal;\n" "attribute vec3 vertexColor;\n" "attribute vec2 texCoord2d;\n" "\n" "uniform mat4 modelViewMatrix;\n" "uniform mat3 normalMatrix;\n" "uniform mat4 projectionMatrix;\n" "\n" "struct LightSource\n" "{\n" " vec3 ambient;\n" " vec3 diffuse;\n" " vec3 specular;\n" " vec3 position;\n" "};\n" "uniform LightSource lightSource;\n" "\n" "struct LightModel\n" "{\n" " vec3 ambient;\n" "};\n" "uniform LightModel lightModel;\n" "\n" "struct Material {\n" " vec3 emission;\n" " vec3 specular;\n" " float shininess;\n" "};\n" "uniform Material material;\n" "\n" "varying vec3 v_color;\n" "varying vec2 v_texCoord2d;\n" "\n" "void main()\n" "{\n" " vec3 normal = normalize(normalMatrix * vertexNormal); // normal vector \n" " vec3 position = vec3(modelViewMatrix * vec4(vertexPosition, 1)); // vertex pos in eye coords \n" " vec3 halfVector = normalize(lightSource.position + vec3(0,0,1)); // light half vector \n" " float nDotVP = dot(normal, normalize(lightSource.position)); // normal . light direction \n" " float nDotHV = max(0.f, dot(normal, halfVector)); // normal . light half vector \n" " float pf = mix(0.f, pow(nDotHV, material.shininess), step(0.f, nDotVP)); // power factor \n" "\n" " vec3 ambient = lightSource.ambient;\n" " vec3 diffuse = lightSource.diffuse * nDotVP;\n" " vec3 specular = lightSource.specular * pf;\n" " vec3 sceneColor = material.emission + vertexColor * lightModel.ambient;\n" "\n" " v_color = clamp(sceneColor + \n" " ambient * vertexColor + \n" " diffuse * vertexColor + \n" " specular * material.specular, 0.f, 1.f );\n" "\n" " v_texCoord2d = texCoord2d;\n" "\n" " gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1);\n" "}\n" ; static QString fragmentShader = "#version 100\n" "precision lowp vec3;\n" "precision lowp vec2;\n" "uniform sampler2D texUnit;\n" "\n" "varying vec3 v_color;\n" "varying vec2 v_texCoord2d;\n" "\n" "void main()\n" "{\n" " gl_FragColor = vec4(v_color, 1) * texture2D(texUnit, v_texCoord2d);\n" "}\n" ; /* * Texture copied and modifided modified from: * key()) { case Qt::Key_Escape: exit(0); break; default: QOpenGLWindow::keyPressEvent(ev); break; } } QMatrix4x4 m_projection, m_view; QOpenGLShaderProgram m_pgm; QOpenGLVertexArrayObject m_vao; QOpenGLBuffer m_vbo; QOpenGLBuffer m_ibo; GLuint m_tex; GLsizei m_cnt; }; int main(int argc, char *argv[]) { QGuiApplication a(argc,argv); Window w; w.setWidth(640); w.setHeight(480); w.show(); return a.exec(); }