PDA

View Full Version : Vertex Arrays and lightmapping question.



HellRaiZer
05-25-2003, 12:13 AM
I have a little question on vertex arrays.
I'm trying to figure out how can i render multitextured lightmapped triangles using vertex arrays, as fast as possible!!!

I found a demo on the net, about lightmapping using vertex arrays. It just shows a cube with different texture on every face, and different lightmap on every face (face = QUAD). The problem with that demo is that the author uses the same texture coordinates for both texture0 and texture1. So the only thing he had to do is :

glClientActiveTextureARB(GL_TEXTURE0_ARB);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, cube.vert); // float *cube.vert
glTexCoordPointer(2, GL_FLOAT, 0,cube.tex); // float *cube.tex

if (lightmap)
{
glClientActiveTextureARB(GL_TEXTURE1_ARB);
glTexCoordPointer(2, GL_FLOAT, 0, cube.tex); // he uses the same tex coords!!!
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}

for (cnt = 0; cnt < cube.numFaces; cnt++)
{
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, cube.tmapId[cnt]);
glEnable(GL_TEXTURE_2D);

if (lightmap)
{
glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, cube.lmapId[cnt]);
glEnable(GL_TEXTURE_2D);
}

// this way, he can use glDrawElements!!!!!!
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, cube.index + (4 * cnt));

if (lightmap)
{
glActiveTextureARB(GL_TEXTURE1_ARB);
glDisable(GL_TEXTURE_2D);
}

glActiveTextureARB(GL_TEXTURE0_ARB);
glDisable(GL_TEXTURE_2D);
}

if (lightmap)
{
glActiveTextureARB(GL_TEXTURE1_ARB);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

glActiveTextureARB(GL_TEXTURE0_ARB);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);;

In my case, its nearly impossible to have the same tex coords for texture0 and texture1!!! So the only solution i can thing right now is:

1) pass all vertex and texture0 tex coords to opengl. A vertex struct in the form
struct Vertex
{
float u,v;
float x,y,z;
};
can be possible!!!

2) for every polygon (triangle) do :

// Assuming lightmapping always, and the same texture0 for all triangles!!!
glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, currentTriangle->Lightmap);
glBegin(GL_TRIANGLES);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, curTriangle->LMCoord.u,curTriangle->LMCoord.v);
glArrayElement(curTriangle->VertexIndex);
// the same procedure for the rest of triangle's vertices.
glEnd();

3) Reset TEXTURE_2D for texture0 and texture1!!!

But this way, the usefullness of vertex arrays is gone!!!!

Can this be fixed?????
Is there any other way for passing the appropriate data to OpenGL, with a descent speed??? With descent speed, i mean that the way i showed above is approaching immediate mode!! so why bothering with vertex arrays?

If somebody can help, please do it!!!

And something last. I dont know if the above code works!!!!!!

Thanks in advance!

Btw, how can i put a code list in my post?????

HellRaiZer


[This message has been edited by HellRaiZer (edited 05-25-2003).]

nbkolchin
05-25-2003, 01:34 AM
You can use different data array for TEXTURE1.
I.e. glTexCoordPointer(2, GL_FLOAT, 0, cube.tex2)

Read glspec section 2.8