Textures using OpenGL2's glDrawArrays

Started by
7 comments, last by cpatuzzo 13 years, 3 months ago
Hey there, I'm trying to write some skybox code.

It works fine at the moment, but it is using OpenGL1's glBegin(GL_QUADS) function. I need it to use OpenGL2's glDrawArrays or glDrawElements.

The code that needs updating is this:
void drawSkybox() {	for (int i = 0; i < 6; i++) {		glBindTexture(GL_TEXTURE_2D, skybox);		glBegin(GL_QUADS);		for (int j = 0; j < 4; j++) {			glTexCoord2dv(texture[j]);			glVertex3fv(vertices[faces[j]]);		}		glEnd();	}}


The textures are loading fine in another function and their GLuint id's are stored in the skybox array. The arrays for vertices, faces and textures are defined as:

GLfloat vertices[8][3] =	{{-0.5, -0.5, -0.5}, // 0: left,  bottom, back	 {-0.5, -0.5, +0.5}, // 1: left,  bottom, front	 {-0.5, +0.5, -0.5}, // 2: left,  top,    back	 {-0.5, +0.5, +0.5}, // 3: left,  top,    front	 {+0.5, -0.5, -0.5}, // 4: right, bottom, back	 {+0.5, -0.5, +0.5}, // 5: right, bottom, front	 {+0.5, +0.5, -0.5}, // 6: right, top,    back	 {+0.5, +0.5, +0.5}};// 7: right, top,    frontGLubyte faces[6][4] =	{{1, 0, 2, 3}, // 0: west	 {4, 5, 7, 6}, // 1: east	 {4, 0, 1, 5}, // 2: below	 {7, 3, 2, 6}, // 3: above	 {5, 1, 3, 7}, // 4: south	 {0, 4, 6, 2}};// 5: northGLdouble texture[4][2] =	{{0.0, 0.0}, // 0: left,  bottom	 {1.0, 0.0}, // 1: right, bottom	 {1.0, 1.0}, // 2: right, top	 {0.0, 1.0}};// 3: left,  top


I've been struggling with this for hours. I keep getting strange tearing accross the surfaces of the cube. Can anyone help me?

Many thanks,
Chris
Advertisement
You need to post any/all code you have regarding your vertex buffers so they can be debugged, can't tell much from the info you have given.

Also if your image has a strange result, it usually helps to post the picture.

If you post these I'll take a look at your problem.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
As requested, here is the code for trying to display using vertex buffers (I think).

void drawSkybox() {	/*for (int i = 0; i < 6; i++) {		glBindTexture(GL_TEXTURE_2D, skybox);		glBegin(GL_QUADS);		for (int j = 0; j < 4; j++) {			glTexCoord2dv(texture[j]);			glVertex3fv(vertices[faces[j]]);		}		glEnd();	}*/	GLfloat west[4][3] = {{vertices[faces[0][0]][0], vertices[faces[0][0]][1], vertices[faces[0][0]][2]},			      {vertices[faces[0][1]][0], vertices[faces[0][1]][1], vertices[faces[0][1]][2]},			      {vertices[faces[0][2]][0], vertices[faces[0][2]][1], vertices[faces[0][2]][2]},			      {vertices[faces[0][3]][0], vertices[faces[0][3]][1], vertices[faces[0][3]][2]}};	glBindTexture(GL_TEXTURE_2D, skybox[0]); //I don't think this line is correct, or maybe it's the texture array.	glVertexPointer(3, GL_FLOAT, 0, west);	glTexCoordPointer(3, GL_FLOAT, 0, texture);	glDrawArrays(GL_QUADS, 0, 4);}



Here is the image when it's displayed correctly (using the commented out code).
http://uploadpad.com/files/bar.png

And here is the image using the code above, OpenGL2.
http://uploadpad.com/files/foo.png

I think it's either the line I commented in the source, or the texture array that's wrong, can you help?

Many thanks,
Chris
glTexCoordPointer(3, GL_FLOAT, 0, texture);

should be

glTexCoordPointer(2, GL_FLOAT, 0, texture);

as you only have 2 floats per texcoord, not 3. Not sure if that's your only issue, but it's definitely one of them.

Also you need to have the same number of texcoords as you have vertices. If you have 8 vertices you need to have 8 texcoords.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
The array 'west' is 4 x 3 - 4 vertices, 3 dimensions (x, y, z).
The array 'texture' is 4 x 2 - 4 vertices, 2 dimensions (x, y).
Therefore, I think they have the same number of vertices.

I changed the texcoord buffer take 2 at a time, my buffers are now:
glVertexPointer(3, GL_FLOAT, 0, west);glTexCoordPointer(2, GL_FLOAT, 0, texture);


I'm still getting the tearing effect. Do I need to use something like:
glClientActiveTexture(skybox[0]) and glEnableClientState(GL_TEXTURE_COORD_ARRAY) instead of: glBindTexture(GL_TEXTURE_2D, skybox[0]) ? I'm not sure how?

Many thanks,
Chris
Assuming that skybox[0] is a texture id created with glGenTexture, then what you have is correct for glBindTexture.

You do need glEnableClientState(GL_TEXTURE_COORD_ARRAY), but I've never heard of glClientActiveTexture and you don't need it here.

[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
OK, I solved it..... -.-

glTexCoordPointer(2, GL_FLOAT, 0, texture);
should be:
glTexCoordPointer(2, GL_DOUBLE, 0, texture);

2+ hours wasted on that. At least it works now.

Thanks so much for the help,
Chris

[Edited by - cpatuzzo on December 30, 2010 4:10:28 PM]
FYI, you should change your buffers to use floats. Not many GPU support double precision, so you could be killing your performance with that.

Just upload your texcoords as floats and use GL_FLOAT for the pointer, it's better that way.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Changed, thanks. =]

This topic is closed to new replies.

Advertisement