Open GL ES2.0 - glDrawelements()
-
I am not able to use glDrawElements() command properly. I have checked with my buffers( both vertex and index) with my vertex and index data. Both come out to be same. And what i feel is that the main problem lies in drawing the object. I am giving my code below. Can anyone point the mistake.
@void GLWindow::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the background and depth-buffer for this frameGLuint vertexAttr = m_sampleProgram.attributeLocation("vertex"); GLuint normalAttr = m_sampleProgram.attributeLocation("normal"); GLuint texAttr = m_sampleProgram.attributeLocation("texCoord"); GLuint matrixAttr = m_sampleProgram.uniformLocation("matrix"); GLuint projAttr = m_sampleProgram.uniformLocation("proj"); m_sampleProgram.bind(); vertexBuffer.bind(); indexBuffer.bind(); m_sampleProgram.enableAttributeArray(vertexAttr); m_sampleProgram.setAttributeBuffer(vertexAttr, GL_FLOAT, 0 * sizeof(GLfloat), 3, 5 * sizeof(GLfloat)); m_sampleProgram.enableAttributeArray(texAttr); m_sampleProgram.setAttributeBuffer(texAttr, GL_FLOAT, 3 * sizeof(GLfloat), 2, 6 * sizeof(GLfloat)); m_sampleProgram.enableAttributeArray(normalAttr); m_sampleProgram.setAttributeBuffer(normalAttr, GL_FLOAT, 5 * sizeof(GLfloat), 3, 5 * sizeof(GLfloat)); QMatrix4x4 projection; projection.perspective(45.0f, (float)width()/(float)height(), 0.01f, 1000.0f ); // Use perspective projection m_sampleProgram.setUniformValue( projAttr, projection ); // Set projection to the shader // Orientation for the <object> we are about to render QMatrix4x4 orientation; orientation.setToIdentity(); orientation.translate(0.0f, 0.0f, -10.0f ); // 5 units away from camera static float ang = 0.0f; ang+=0.1f; orientation.rotate(ang, 1.0f, 0.0f, 0.0f ); orientation.rotate(ang*0.7f, 0.0f, 1.0f, 0.0f ); orientation.rotate(ang*1.4f, 0.0f, 0.0f, 1.0f ); m_sampleProgram.setUniformValue( matrixAttr, orientation ); // Set orientation to the shader glEnable(GL_DEPTH_TEST); glBindTexture(GL_TEXTURE_2D, texture[0]); glDrawElements(GL_TRIANGLES, faceCount, GL_UNSIGNED_SHORT, 0); // draw the 3d <object> m_sampleProgram.disableAttributeArray(vertexAttr); // Disable the program m_sampleProgram.disableAttributeArray(texAttr); m_sampleProgram.disableAttributeArray(normalAttr); vertexBuffer.release(); indexBuffer.release(); m_sampleProgram.release(); swapBuffers(); // Swap buffers manually since automatic swapping was disabled in a constructor.
};
@
-
@m_sampleProgram.enableAttributeArray(vertexAttr);
m_sampleProgram.setAttributeBuffer(vertexAttr, GL_FLOAT, 0 * sizeof(GLfloat), 3, 5 * sizeof(GLfloat));
m_sampleProgram.enableAttributeArray(texAttr);
m_sampleProgram.setAttributeBuffer(texAttr, GL_FLOAT, 3 * sizeof(GLfloat), 2, 6 * sizeof(GLfloat));
m_sampleProgram.enableAttributeArray(normalAttr);
m_sampleProgram.setAttributeBuffer(normalAttr, GL_FLOAT, 5 * sizeof(GLfloat), 3, 5 * sizeof(GLfloat));@Looks like vertex strides are wrong.