Jump to content
  • Advertisement
Sign in to follow this  
cpatuzzo

OpenGL Textures using OpenGL2's glDrawArrays

This topic is 2906 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

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, front

GLubyte 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: north

GLdouble 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!