I've been trying to port this immediate mode(glBegin/glEnd) code to direct mode(VAs) for rendering a plane. Please let me know if the direct mode code will exactly work as the immediate mode code.
Note: consider a 50X50 mesh Immediate mode code:
int once=0, a=0,b=0;
for(int j=0; j<50-1; j++)
{
once=0;
for(int i=0; i<50; i++)
{
a=i+j*(50);
b=i+(j+1)*50;
if(once)
{
glBegin(GL_TRIANGLE_STRIP);
once=1;
}
else
{
glTexCoord2f(Texture[a].x, Texture[a].y);
glVertex2f(Mesh[a].x, Mesh[a].y);
glTexCoord2f(Texture[a].x, Texture[a].y);
glVertex2f(Mesh[b].x, Mesh[b].y);
}
}
if(once)
{
glEnd();
}
}
Direct mode code:
unsigned int indexArray[50*50];
int idx=0;
for(int j=0; j<50-1; j++)
{
for(int i=0; i<50; i++)
{
a=i+j*(50);
b=i+(j+1)*50;
indexArray[idx]=a;
indexArray[idx+1]=b;
idx+=2;
}
}
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(2dPoint), Texture);
glVertexPointer(3, GL_FLOAT, sizeof(2dPoint), Mesh);
glDrawElements(GL_TRIANGLE_STRIP, (50-1)*(50-1)*2, GL_UNSIGNED_INT, indexArray);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Note: 2dPoint is a structure for 2 floating point values holding x and y
Update After correcting the glVertexPointer() for 2-d co-ordinates. I observed the triangulation happening the following way:
With glBegin()-glEnd():
/\ /\ /\ /\ /
/ \ / \ / \ / \ /
\ / \ / \ / \ / \ /
\ / \ / \ / \/ \ /
\/ \ \ / /\ \ /
/\ / \ \/ / \ \/
/ \ / \ /\ / \ /\
/ \ / \ / \ / \ / \
\ / \ / \ / \ / \
\ / \ / \/ \/ \
\ / \ \ /\ /\ \
\ / \ / \ / \ / \ \
\/ \ / \ / \ / \ \
/\ \ / \ / \ / \ \
/ \ \ / / \ / \ \
/ \ / / \ \/ \ \
/ \ / \ / \ /\ \ \
\ / \ / \ / \ \ \
\/ \ / \ / \ \ \
/\ / \ / \ \ \
With glDrawElements():
/\ /\ /\ /\ /
/ \ / \ / \ / \ /
\ / \ / \ / \ / \ /
\ / \ / \ / \/ \ /
--\/--------\--------\--/-------/\-------\--/
/\ / \ \/ / \ \/
/ \ / \ /\ / \ /\
/ \ / \ / \ / \ / \
------\-/-------\---/----\--/--------\--/----\
\ / \ / \/ \/ \
\ / \ \ /\ /\ \
\ / \ / \ / \ / \ \
\/ \ / \ / \ / \ \
/\ \ / \ / \ / \ \
/ \ \ / / \ / \ \
-/----\----- \-------/-\--------\/----------\-------\
/ \ / \ / \ /\ \ \
\ / \ / \ / \ \ \
\/ \ / \ / \ \ \
/\ / \ / \ \ \
Sorry for the alignment issues in the illustration. but as you can see, with the index array and glDrawElements(), the number of triangles increased. how can i modify the index array to match the winding similar to the results of glBegin()/glEnd()?
glVertexPointer
line looks wrong, since you only have two floats per vertex but tell OpenGL to read three of them. In general: What is the problem with this code? – BDL