opengl - glBegin/glEnd to glDrawElements() -
i've been trying port immediate mode(glbegin/glend) code direct mode(vas) rendering plane. please let me know if direct mode code work immediate mode code.
note: consider 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 structure 2 floating point values holding x , y
update after correcting glvertexpointer() 2-d co-ordinates. observed triangulation happening following way:
with glbegin()-glend():
/\ /\ /\ /\ / / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \/ \ / \/ \ \ / /\ \ / /\ / \ \/ / \ \/ / \ / \ /\ / \ /\ / \ / \ / \ / \ / \ \ / \ / \ / \ / \ \ / \ / \/ \/ \ \ / \ \ /\ /\ \ \ / \ / \ / \ / \ \ \/ \ / \ / \ / \ \ /\ \ / \ / \ / \ \ / \ \ / / \ / \ \ / \ / / \ \/ \ \ / \ / \ / \ /\ \ \ \ / \ / \ / \ \ \ \/ \ / \ / \ \ \ /\ / \ / \ \ \
with gldrawelements():
/\ /\ /\ /\ / / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \/ \ / --\/--------\--------\--/-------/\-------\--/ /\ / \ \/ / \ \/ / \ / \ /\ / \ /\ / \ / \ / \ / \ / \ ------\-/-------\---/----\--/--------\--/----\ \ / \ / \/ \/ \ \ / \ \ /\ /\ \ \ / \ / \ / \ / \ \ \/ \ / \ / \ / \ \ /\ \ / \ / \ / \ \ / \ \ / / \ / \ \ -/----\----- \-------/-\--------\/----------\-------\ / \ / \ / \ /\ \ \ \ / \ / \ / \ \ \ \/ \ / \ / \ \ \ /\ / \ / \ \ \
sorry alignment issues in illustration. can see, index array , gldrawelements(), number of triangles increased. how can modify index array match winding similar results of glbegin()/glend()?
since have 2d coordinates, glvertexpointer
call wrong.
glvertexpointer(3, gl_float, sizeof(2dpoint), mesh);
this line tell opengl read 3 floats per vertex, if have 2 of them have change to:
glvertexpointer(2, gl_float, sizeof(2dpoint), mesh); ^
Comments
Post a Comment