Jump to content
  • Advertisement
Sign in to follow this  
ChrALX

OpenGL Trying to render a textured cube via indexed VBO

This topic is 3291 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, spending hours, i'm trying to draw a textured cube via OpenGl with VBO. First, the cube will be rendered. But there are only two sides of my cube textured. Here's my vertices/indices/texture coord setup:
GLfloat vertices [] ={
	-1.0f, 1.0f, 0.0f,
	-1.0f,-1.0f, 0.0f,
	 1.0f,-1.0f, 0.0f,
	 1.0f, 1.0f, 0.0f,

	-1.0f, 1.0f, -2.0f,	     
	-1.0f,-1.0f, -2.0f,
	 1.0f,-1.0f, -2.0f,
         1.0f, 1.0f, -2.0f,
};

GLfloat tex[] ={ 
	1.0f,1.0f, // 1
	0.0f,1.0f,
	0.0f,0.0f,
    1.0f,0.0f,

    
	1.0f,1.0f, // 2
    0.0f,1.0f,    
	0.0f,0.0f,
	1.0f,0.0f,

	1.0f,1.0f, // 3
    0.0f,1.0f,    
	0.0f,0.0f,
	1.0f,0.0f,

	1.0f,0.0f, // 4
    0.0f,0.0f,
    0.0f,1.0f,
	1.0f,1.0f,

	1.0f,0.0f, // 5 
    0.0f,0.0f,
    0.0f,1.0f,
	1.0f,1.0f,

	1.0f,0.0f, // 6
    0.0f,0.0f,
    0.0f,1.0f,
	1.0f,1.0f,
};



GLuint indices[] = {
		
	0,1,2,3,
	4,5,6,7,

	3,2,6,7,
	0,1,5,4,

	4,0,3,7,
	5,1,2,6,

	

}; 


GLuint triangleBuffer;
GLuint indexBuffer;

#define BUFFER_OFFSET(i) ((char *)NULL + (i))
#define NUM_VERTICES 24
My window get's initialized via SFML and the texture is loaded. After that i do OpenGL setup stuff and then initialize the VBO Buffers:
    glColor4f(1.f, 1.f, 1.f, 1.f);
	
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	
    glGenBuffers(1,&indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 24*sizeof(GLuint), indices,  
                                                             GL_STREAM_DRAW);
	glGenBuffers(1, &triangleBuffer);
	glBindBuffer(GL_ARRAY_BUFFER, triangleBuffer);
	glBufferDataARB(GL_ARRAY_BUFFER, NUM_VERTICES*(sizeof(GLfloat))+24*2*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
	glBufferSubData(GL_ARRAY_BUFFER,0,NUM_VERTICES*sizeof(GLfloat),vertices);
	//
	glBufferSubData(GL_ARRAY_BUFFER, NUM_VERTICES*(sizeof(GLfloat) ), 24*2*sizeof(GLfloat), tex);

	glVertexPointer(3, GL_FLOAT, 0,0);
	glTexCoordPointer(2, GL_FLOAT, 0, 
					  BUFFER_OFFSET(NUM_VERTICES*(sizeof(GLfloat))));
    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(1.f, 1.f, 1.f, 500.f);
	
    // Start game loop
    while (App.IsOpened())
    {

	glClear( GL_COLOR_BUFFER_BIT);
        // Apply some transformations
		glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
	glTranslatef(0.f, 0.f, -200.f);
	glRotatef(Clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
	glRotatef(Clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
	glRotatef(Clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
		
	// draw it !
	glDrawElements(GL_QUADS,  6*4, GL_UNSIGNED_INT, BUFFER_OFFSET( 0 ));
		
        // Finally, display rendered frame on screen
        App.Display();
    } // Game Loop
Did I miss something ? Has anyone a clue ? Thanks in advance, Chris

Share this post


Link to post
Share on other sites
Advertisement
You have shared vertices but not shared texture coordinates. If you share a vertex, you must share the texture coordinate as well, because the same index is used to reference all enabled attribute arrays. You should immediately know something is wrong when you have attribute arrays of different size; your vertex array contains 8 vertices but the texture coordinate array contains 24 texture coordinates.

You will, however, find that the cube is the worst case with no shared vertices (or possible a handful, depending on how you orient your texture on each face), and you need 24 unique vertices.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!