PDA

View Full Version : about texture mapping



hapcafe
06-05-2002, 06:41 PM
I am writing some code like the following:

glBegin(GL_TRIANGLES);
for (int i=0; i<LENGTH-1; i++) {
for (int j=1; j<WIDTH; j++) {
glColor3f(1.0f, 1.0f, 1.0f);

glTexCoord2f(p[i][j].u, p[i][j].v);
glVertex3f(p[i][j].x, p[i][j].y, p[i]j].z);

glTexCoord2f(p[i][j-1].u, p[i][j-1].v);
glVertex3f(p[i][j-1].x, p[i][j-1].y, p[i][j-1].z);

glTexCoord2f(p[i+1][j-1].u, p[i+1][j-1].v);
glVertex3f(p[i+1][j-1].x, p[i+1][j-1].y, p[i+1][j-1].z);

glTexCoord2f(p[i][j].u, p[i][j].v);
glVertex3f(p[i][j].x, p[i][j].y, p[i][j].z);

glTexCoord2f(p[i+1][j-1].u, p[i+1][j-1].v);
glVertex3f(p[i+1][j-1].x, p[i+1][j-1].y, p[i+1][j-1].z);

glTexCoord2f(p[i+1][j].u, p[i+1][j].v);
glVertex3f(p[i+1][j].x, p[i+1][j].y, p[i+1][j].z);
}
}
glEnd();

But the thing is glTexCoord2f() seems to be very time-consuming, killing the FPS. Is there any technique to solve this problem? I think displaylist is not a good way because p[i][j] has to be changed each frame.
Any reply will be appreciated.

Jambolo
06-05-2002, 06:51 PM
Use a triangle strip instead of individual triangles. That will make a huge difference.

secnuop
06-05-2002, 10:24 PM
Creating a triangle strip will make a difference, but not nearly as big of a difference as vertex arrays will. It will involve some re-arranging of your data, but the results will most likely be worthwile.

Basically, I think you could put all of your texture coordinates and vertex data in arrays, and index them similar to the way you're doing now.

If you did a little more work you could probably do both by create vertex arrays and use them to draw triangle strips instead of triangles. However I'd guess that this probably isn't worthwhile since drivers should be pretty optimized for drawing triangles using vertex arrays (this is what Quake 3 does http://www.opengl.org/discussion_boards/ubb/smile.gif).

hapcafe
06-06-2002, 12:34 AM
to Jambolo and secnuop:
I really appreciate your advice.
I will try it asap.

hapcafe
06-08-2002, 06:39 AM
secnuop,
I have tried triangle strip and vertex array.
Triangle strip helps a lot but vertex array doesn't. The following are some important pseudo-code fragments. Is there anything wrong?

1.
struct NODEPARA {
float x;
float y;
float z;
float u;
float v;
};

2.
pVtx=new NODEPARA[MAX][MAX]

3.
glEnableClientStateGL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(NODEPARA), &pVtx1[0][0].u);
glVertexPointer(3, GL_FLOAT, sizeof(NODEPARA), &pVtx1[0][0].x);

4.
for (int i=0; i<LENGTH-1; i++) {
for (int j=1; j<WIDTH; j++) {
glArrayElement(i*MAXNETSIZE+j);
glArrayElement((i+1)*MAXNETSIZE+j);
glArrayElement(i*MAXNETSIZE+(j+1));
glArrayElement((i+1)*MAXNETSIZE+(j+1));
}
}

IronRaph
06-08-2002, 12:08 PM
just fill 3 matrix vertex texcoord color

and after:
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_FLOAT, 0, Matrix_color);
glTexCoordPointer(2, GL_SHORT,0, Matrix_tex);
glVertexPointer(3, GL_INT, 0, Matrix_vertex);

and draw every thing with 1 call:
glDrawArrays( GL_QUADS,0,nbrofelements);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

secnuop
06-09-2002, 10:29 AM
Everything looks good except number 4. One of the biggest advantages of vertex arrays is that you can draw everything with just one function call. You can either do this with DrawArrays() or DrawElements(). I think DrawElements() is going to be the one you're interested in.

Basically where you do your doubly nested loop and call ArrayElement() now, instead build up an index list. Then, pass your index list into DrawElements() and everything gets drawn in one go.

This should cut down your function call overhead significantly.

Scorpion
06-09-2002, 11:56 AM
Just a speed note about texture mapping:
When use mipmapped textures you will gain (quite) more FPS when you render relatively small polygons with big textures. But more memory usage of course will occur.

hapcafe
06-09-2002, 08:56 PM
Thanks a lot to all of you.
I have found a big bug in my code. I need not to draw four vertices in a loop when in GL_TRIANGLE_STRIP mode and drawing only two vertices is enough. This greatly improve FPS.
I am now using glDrawArrays and I am going to use mipmap to see what will happen further.