2 Replies - 3319 Views - Last Post: 22 January 2011 - 10:15 AM Rate Topic: -----

#1 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 18-March 10

OpenGL VBO problem

Posted 20 January 2011 - 06:19 PM

Hi, I'm currently having problems with changes my code standard vertex arrays to VBO's, it all works fine except for the texturing, I've checked the code over a lot and still can't think what could be going wrong... when I just use vertex arrays everything including textures work fine but when using openGL's vertex buffer objects the texture doesn't apply properly i.e. the object is the same color of one of the pixels of the texture, I thought it might be something to do with the uv coordinates but the same values as far as i know are being passed to the buffer as whats in the array. does anyone know what might be/is causing this? the only parts of my code that change between using vertex buffers standard and VBO's is the following:

/* my original render function */
glEnableClientState(GL_VERTEX_ARRAY); //Enable the vertex array
	glEnableClientState( GL_NORMAL_ARRAY );
	glEnableClientState(GL_TEXTURE_COORD_ARRAY );
    glVertexPointer(3, GL_FLOAT, 0, &m_vertices[0]); //Tell OpenGL where the vertices are
	glNormalPointer( GL_FLOAT, 0, &m_normals[0] );
	glTexCoordPointer( 2, GL_FLOAT, 0, &m_texCoords[0] );

    //Draw the triangles, we pass in the number of indices, the data type of the index array
    // (GL_UNSIGNED_INT) and then the pointer to the start of the array
	//glLineWidth(10.0);
    glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, &m_indices[0]);
	//glDrawElements(GL_LINES, m_indices.size(), GL_UNSIGNED_INT, &m_indices[0]);
    //Finally disable the vertex array
    glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState( GL_NORMAL_ARRAY );
	glDisableClientState(GL_TEXTURE_COORD_ARRAY );

/* to the new one */
//Bind the vertex array and set the vertex pointer to point at it
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
    //glVertexPointer(3, GL_FLOAT, 0, 0);
    glVertexAttribPointer((GLint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    
    //Bind the color array
    glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
    //glColorPointer(3, GL_FLOAT, 0, 0);
    glVertexAttribPointer((GLint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, m_textureBuffer);
    glVertexAttribPointer((GLint)2, 2, GL_FLOAT, GL_FALSE, 0, 0);

    //Bind the index array
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);

    //Draw the triangles
    glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, 0);

/* anywhere that calculates positions ect has a call to set the buffers up as follows */
glGenBuffers(1, &m_vertexBuffer); //Generate a buffer for the vertices
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); //Bind the vertex buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * m_vertices.size() * 3, &m_vertices[0], GL_STATIC_DRAW); //Send the data to OpenGL

glGenBuffers(1, &m_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * m_indices.size(), &m_indices[0], GL_STATIC_DRAW);

glGenBuffers(1, &m_textureBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_textureBuffer); //Bind the vertex buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * m_texCoords.size() * 2, &m_texCoords[0], GL_STATIC_DRAW); //Send the data to OpenGL

/* and this goes after my texture is loaded into memory */
glEnable(GL_TEXTURE_2D);							//Enable texturing!
	glActiveTexture( GL_TEXTURE0 );
	glBindTexture(GL_TEXTURE_2D, m_grassTexID);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexImage2D(GL_TEXTURE_2D,0, GL_RGB8, m_grassTexture.getWidth(), 
                      m_grassTexture.getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, 
                      m_grassTexture.getImageData());

	glEnableVertexAttribArray(0); //Enable the vertex attribute
    glEnableVertexAttribArray(1); //Enable the color attribute
    glEnableVertexAttribArray(2); //Enable the texture coordinate attribute




everything else in the code is that same as before adding VBO which is why I cannot find why this problem is occurring and due to where the problem is the debugger is useless to get information.
any help is muchly appreciates.
thanks
Dave

Is This A Good Question/Topic? 0
  • +

Replies To: OpenGL VBO problem

#2 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: OpenGL VBO problem

Posted 21 January 2011 - 12:54 PM

You might have the vertex attribute indices wrong. I can't say for sure because I do VBOs a different way, but this seems to suggest 0 for vertices, 2 for normals, and I guess 8 for texture coordinates?

Anyway this is how I bind VBOs.

// VBOs
GLuint tex, verts, norms;
...
glBindBuffer(GL_ARRAY_BUFFER, tex);
glTexCoordPointer(2, GL_FLOAT, 0, NULL);

glBindBuffer(GL_ARRAY_BUFFER, norms);
glNormalPointer(GL_FLOAT, 0, NULL);

glBindBuffer(GL_ARRAY_BUFFER, verts);
glVertexPointer(3, GL_FLOAT, 0, NULL);
...
glDrawArrays(GL_TRIANGLES, 0, numVerts);


Was This Post Helpful? 2
  • +
  • -

#3 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 18-March 10

Re: OpenGL VBO problem

Posted 22 January 2011 - 10:15 AM

Thanks Tom, I manages to solve the problem from you code snippet :) all is working as should be now, thank you again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1