void PaintWidget::resizeGL(int width, int height) { // Set the viewport size. viewport(0, 0, width, height); // Set projection matrix to some fixed values WMatrix4x4 proj; proj.perspective(45, ((double)width)/height, 1, 40); uniformMatrix4(pMatrixUniform_, proj); }
void paintGL() { // Drawing starts here! //clearColor(0.8, 0.8, 0.8, 1); clearColor(0, 0, 0, 1); clearDepth(1); disable(DEPTH_TEST); disable(CULL_FACE); depthFunc(LEQUAL); viewport(0, 0, 640, 360); clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT); WMatrix4x4 proj; proj.perspective(90, 1, 1, 40); useProgram(shaderProgram_); // Set projection matrix uniformMatrix4(pMatrixUniform_, proj); uniformMatrix4(cMatrixUniform_, jsMatrix_); // Draw the scene #if 0 bindBuffer(ARRAY_BUFFER, triangleVertexPositionBuffer_); vertexAttribPointer(vertexPositionAttribute_, 3, FLOAT, false, 0, 0); bindBuffer(ARRAY_BUFFER, triangleVertexColorBuffer_); vertexAttribPointer(vertexColorAttribute_, 4, FLOAT, false, 0, 0); WMatrix4x4 modelMatrix1( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -2.0, 0, 0, 1 ); modelMatrix1.rotate(45, 0, 1, 0); uniformMatrix4fv(mvMatrixUniform_, false, modelMatrix1); drawArrays(TRIANGLES, 0, 3); #endif bindBuffer(ARRAY_BUFFER, squareVertexPositionBuffer_); vertexAttribPointer(vertexPositionAttribute_, 3, FLOAT, false, 0, 0); bindBuffer(ARRAY_BUFFER, squareVertexColorBuffer_); vertexAttribPointer(vertexColorAttribute_, 4, FLOAT, false, 0, 0); WMatrix4x4 modelMatrix2( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ); //modelMatrix2.rotate(45, 1, 0, 0); //modelMatrix2.translate(0, 0, -2); uniformMatrix4(mvMatrixUniform_, modelMatrix2); //drawArrays(TRIANGLE_STRIP, 0, 4); bindBuffer(ELEMENT_ARRAY_BUFFER, squareElementBuffer_); drawElements(TRIANGLE_STRIP, 4, DT_UNSIGNED_BYTE, 0); #if 0 // now draw something textured useProgram(texShaderProgram_); uniformMatrix4fv(texPMatrixUniform_, false, projectionMatrix); //WMatrix4x4 mm3( // 2, 0, 0, 0, // 0, 1, 0, -2.5, // 0, 0, 1, 0, // 0, 0, 0, 1 // ); WMatrix4x4 mm3; //mm3.scale(.5, 2, 1); mm3.rotate(45, 0, 0, 1); uniformMatrix4fv(texMvMatrixUniform_, false, mm3.data()); bindBuffer(ARRAY_BUFFER, textureCoordBuffer_); vertexAttribPointer(texCoordAttribute_, 2, FLOAT, false, 0, 0); activeTexture(TEXTURE0); bindTexture(TEXTURE_2D, texture_); uniform1i(texSamplerUniform_, 0); bindBuffer(ARRAY_BUFFER, squareVertexPositionBuffer_); vertexAttribPointer(texVertexPositionAttribute_, 3, FLOAT, false, 0, 0); drawArrays(TRIANGLE_STRIP, 0, 4); #endif }
void DrawGLWidget::paintGL() { clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT); // Reset Z-buffer, enable Z-buffering // Draw Cube // set perspective matrix WMatrix4x4 pMatrix; pMatrix.perspective(45,1,0.1,100.0); uniformMatrix4(pMatrixUniform_, pMatrix); //set model-view matrix WMatrix4x4 mvMatrix; mvMatrix.setToIdentity(); mvMatrix.translate(0.0,0.0,zDepth); //WMatrix4x4 rotMatrix; //rotMatrix.rotate(angleX,-1.0, 0.0, 0.0); //rotMatrix.rotate(angleY, 0.0, 1.0, 0.0); uniformMatrix4(cMatrixUniform_, rotMatrix); //set model-view matrix uniformMatrix4(mvMatrixUniform_, mvMatrix); // uniformMatrix4(nMatrixUniform_, rotMatrix.inverted().transposed()); activeTexture(TEXTURE0); bindTexture(TEXTURE_2D,texture); uniform1i(samplerUniform_,0); uniform1i(useLightingUniform_,useLighting); uniform3f(ambientColorUniform_,ambientColor.values[0],ambientColor.values[1],ambientColor.values[2]); uniform3f(lightingDirectionUniform_,lightDirection.values[0],lightDirection.values[1],lightDirection.values[2]); uniform3f(directionalColorUniform_,lightColor.values[0],lightColor.values[1],lightColor.values[2]); bindBuffer(ARRAY_BUFFER, cubeVertexPositionBuffer); vertexAttribPointer(vertexPositionAttribute_, cubeVertices.itemsize_, // size: Every vertex has an X, Y and Z component FLOAT, // type: They are floats false, // normalized: Please, do NOT normalize the vertices 0, // stride: The first byte of the next vertex is located this // amount of bytes further. The format of the VBO is // vx, vy, vz, nx, ny, nz and every element is a // Float32, hence 4 bytes large 0); // offset: The byte position of the first vertex in the buffer bindBuffer(ARRAY_BUFFER, cubeVertexNormalBuffer); vertexAttribPointer(vertexNormalAttribute_, cubeNormals.itemsize_, // size: Every vertex has an X, Y and Z component FLOAT, // type: They are floats false, // normalized: Please, do NOT normalize the vertices 0, // stride: The first byte of the next vertex is located this // amount of bytes further. The format of the VBO is // vx, vy, vz, nx, ny, nz and every element is a // Float32, hence 4 bytes large 0); // offset: The byte position of the first vertex in the buffer bindBuffer(ARRAY_BUFFER, cubeTextureCoordBuffer); vertexAttribPointer(textureCoordAttribute_, cubeTexCoord.itemsize_, // size: Every vertex has RGBA components FLOAT, // type: They are floats false, // normalized: Please, do NOT normalize the vertices 0, // stride: The first byte of the next vertex is located this // amount of bytes further. The format of the VBO is // vx, vy, vz, nx, ny, nz and every element is a // Float32, hence 4 bytes large 0); // offset: The byte position of the first vertex in the buffer*/ bindBuffer(ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer); drawElements(TRIANGLES,cubeIndices.numel_,UNSIGNED_SHORT,0); }
void DrawGLWidget::paintGL() { clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT); // Reset Z-buffer, enable Z-buffering bindBuffer(ARRAY_BUFFER, triangleVertexPositionBuffer); // set perspective matrix WMatrix4x4 pMatrix; pMatrix.perspective(45,1,0.1,100.0); jsMatrix_ = createJavaScriptMatrix4(); setJavaScriptMatrix4(jsMatrix_, pMatrix); uniformMatrix4(pMatrixUniform_, jsMatrix_); //set model-view matrix WMatrix4x4 mvMatrix; mvMatrix.setToIdentity(); mvMatrix.translate(-1.5,0.0,-7.0); setJavaScriptMatrix4(jsMatrix_, mvMatrix); uniformMatrix4(mvMatrixUniform_, jsMatrix_); vertexAttribPointer(vertexPositionAttribute_, 3, // size: Every vertex has an X, Y and Z component FLOAT, // type: They are floats false, // normalized: Please, do NOT normalize the vertices 3*4, // stride: The first byte of the next vertex is located this // amount of bytes further. The format of the VBO is // vx, vy, vz, nx, ny, nz and every element is a // Float32, hence 4 bytes large 0); // offset: The byte position of the first vertex in the buffer bindBuffer(ARRAY_BUFFER, triangleVertexColorBuffer); vertexAttribPointer(vertexColorAttribute_, 4, // size: Every vertex has an X, Y and Z component FLOAT, // type: They are floats false, // normalized: Please, do NOT normalize the vertices 4*4, // stride: The first byte of the next vertex is located this // amount of bytes further. The format of the VBO is // vx, vy, vz, nx, ny, nz and every element is a // Float32, hence 4 bytes large 0); // offset: The byte position of the first vertex in the buffer drawArrays(TRIANGLES, 0, 3); bindBuffer(ARRAY_BUFFER, squareVertexPositionBuffer); // set perspective matrix setJavaScriptMatrix4(jsMatrix_, pMatrix); uniformMatrix4(pMatrixUniform_, jsMatrix_); //set model-view matrix mvMatrix.translate(3.0,0.0,0.0); setJavaScriptMatrix4(jsMatrix_, mvMatrix); uniformMatrix4(mvMatrixUniform_, jsMatrix_); vertexAttribPointer(vertexPositionAttribute_, 3, // size: Every vertex has an X, Y and Z component FLOAT, // type: They are floats false, // normalized: Please, do NOT normalize the vertices 3*4, // stride: The first byte of the next vertex is located this // amount of bytes further. The format of the VBO is // vx, vy, vz, nx, ny, nz and every element is a // Float32, hence 4 bytes large 0); // offset: The byte position of the first vertex in the buffer bindBuffer(ARRAY_BUFFER, squareVertexColorBuffer); vertexAttribPointer(vertexColorAttribute_, 4, // size: Every vertex has an X, Y and Z component FLOAT, // type: They are floats false, // normalized: Please, do NOT normalize the vertices 4*4, // stride: The first byte of the next vertex is located this // amount of bytes further. The format of the VBO is // vx, vy, vz, nx, ny, nz and every element is a // Float32, hence 4 bytes large 0); // offset: The byte position of the first vertex in the buffer drawArrays(TRIANGLE_STRIP, 0, 4); // What are you going to draw, Offset , Number of elements }