March 2, 2012

akhil akhil
Lab Rat
13 posts

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.

  1. void GLWindow::paintGL() {
  2.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                             // Clear the background and depth-buffer for this frame
  3.  
  4.     GLuint vertexAttr = m_sampleProgram.attributeLocation("vertex");
  5.     GLuint normalAttr = m_sampleProgram.attributeLocation("normal");
  6.     GLuint texAttr = m_sampleProgram.attributeLocation("texCoord");
  7.     GLuint matrixAttr = m_sampleProgram.uniformLocation("matrix");
  8.     GLuint projAttr = m_sampleProgram.uniformLocation("proj");
  9.  
  10.     m_sampleProgram.bind();
  11.     vertexBuffer.bind();
  12.     indexBuffer.bind();
  13.  
  14.     m_sampleProgram.enableAttributeArray(vertexAttr);
  15.     m_sampleProgram.setAttributeBuffer(vertexAttr, GL_FLOAT, 0 * sizeof(GLfloat), 3, 5 * sizeof(GLfloat));
  16.     m_sampleProgram.enableAttributeArray(texAttr);
  17.     m_sampleProgram.setAttributeBuffer(texAttr,    GL_FLOAT, 3 * sizeof(GLfloat), 2, 6 * sizeof(GLfloat));
  18.     m_sampleProgram.enableAttributeArray(normalAttr);
  19.     m_sampleProgram.setAttributeBuffer(normalAttr, GL_FLOAT, 5 * sizeof(GLfloat), 3, 5 * sizeof(GLfloat));
  20.  
  21.     QMatrix4x4 projection;
  22.     projection.perspective(45.0f, (float)width()/(float)height(), 0.01f, 1000.0f );    // Use perspective projection
  23.  
  24.  
  25.     m_sampleProgram.setUniformValue( projAttr, projection );                        // Set projection to the shader
  26.  
  27.     // Orientation for the <object> we are about to render
  28.     QMatrix4x4 orientation;
  29.     orientation.setToIdentity();
  30.     orientation.translate(0.0f, 0.0f, -10.0f );                                      // 5 units away from camera
  31.     static float ang = 0.0f;
  32.     ang+=0.1f;
  33.     orientation.rotate(ang, 1.0f, 0.0f, 0.0f );
  34.     orientation.rotate(ang*0.7f, 0.0f, 1.0f, 0.0f );
  35.     orientation.rotate(ang*1.4f, 0.0f, 0.0f, 1.0f );
  36.     m_sampleProgram.setUniformValue( matrixAttr, orientation );                     // Set orientation to the shader
  37.  
  38.     glEnable(GL_DEPTH_TEST);
  39.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  40.     glDrawElements(GL_TRIANGLES, faceCount, GL_UNSIGNED_SHORT, 0);          // draw the 3d <object>
  41.  
  42.     m_sampleProgram.disableAttributeArray(vertexAttr);                              // Disable the program
  43.     m_sampleProgram.disableAttributeArray(texAttr);
  44.     m_sampleProgram.disableAttributeArray(normalAttr);
  45.  
  46.     vertexBuffer.release();
  47.     indexBuffer.release();
  48.  
  49.     m_sampleProgram.release();
  50.     swapBuffers();                                                                  // Swap buffers manually since automatic swapping was disabled in a constructor.
  51. };

1 reply

April 5, 2012

minimoog77 minimoog77
Lab Rat
130 posts

  1. m_sampleProgram.enableAttributeArray(vertexAttr);
  2.     m_sampleProgram.setAttributeBuffer(vertexAttr, GL_FLOAT, 0 * sizeof(GLfloat), 3, 5 * sizeof(GLfloat));
  3.     m_sampleProgram.enableAttributeArray(texAttr);
  4.     m_sampleProgram.setAttributeBuffer(texAttr,    GL_FLOAT, 3 * sizeof(GLfloat), 2, 6 * sizeof(GLfloat));
  5.     m_sampleProgram.enableAttributeArray(normalAttr);
  6.     m_sampleProgram.setAttributeBuffer(normalAttr, GL_FLOAT, 5 * sizeof(GLfloat), 3, 5 * sizeof(GLfloat));

Looks like vertex strides are wrong.

 
  ‹‹ Problem in running cube example: ’glActiveTexture’ was not declared in this scope’      Using Mouseevent together with Keypressedevents with OPENGL&QT ››

You must log in to post a reply. Not a member yet? Register here!