How To: Convert from OpenGL to GLES -


i have following code...

void draw_polygon(struct vector2d* center, int num_points,               struct vector2d* points, int mode) {     int i;      if(mode == gl_triangle_fan)       glvertex2f(center->x, center->y);      for(i = 0; < num_points; i++)       glvertex2f(points[i].x, points[i].y);      glvertex2f(points[0].x, points[0].y); } 

and trying convert opengles 1.1 compat. other posts thought looked need similar this...

void draw_polygon(struct vector2d* center, int num_points,               struct vector2d* points, int mode) {   int i;   int offset=0;   gl_float *arr;   if(mode == gl_triangle_fan)i{     *arr = (gl_float *)malloc(sizeof(gl_float) * ((num_points+1)*2));     arr[0]=center->x;     arr[1]=center->y;     offset = 2;   }   else{     *arr = (int *)malloc(sizeof(gl_float) * (num_points*2)) ;   }    for(i = 0; < num_points; i++){     int g = i+offset;     arr[g]=points[i].x;     arr[g+1]=points[i].y;     i++;   } } 

but of course doesn't compile. can me understand proper way handle in gles?

firs not compiling:

struct vector2d {     glfloat x, y; }; void draw_polygon(struct vector2d* center, int num_points,                   struct vector2d* points, int mode) { //1st gl_float not type, use glfloat     int i;     int offset=0;     glfloat *arr;     if(mode == gl_triangle_fan) { //what "i" doing here         arr = (glfloat *)malloc(sizeof(glfloat) * ((num_points+1)*2)); //no "*" before arr         arr[0]=center->x;         arr[1]=center->y;         offset = 2;     }     else{         arr = (glfloat *)malloc(sizeof(glfloat) * (num_points*2)) ;  //no "*" before arr; why typcast "(int *)"?     }      for(i = 0; < num_points; i++){         int g = (i*2)+offset;//i*2         arr[g]=points[i].x;         arr[g+1]=points[i].y;         i++;     } } 

as doing whole code. gles not work begin/end calls have call draw call. @ point code above did create array of vertex data, need push gpu (or set pointer data) , call gldrawarrays.

so need add:

    glenableclientstate(gl_vertex_array); //this set once unless want disable @ point unlikelly reason     glvertexpointer(2, gl_float, 0, arr); //set pointer (alternative use vbo , copy data directly gpu)     gldrawarrays(mode, 0, num_points+offset/2);      free(arr); //you need free allocated memory 

in same function.

to make method bit less complicated can try this:

void draw_polygon2(struct vector2d* center, int num_points, struct vector2d* points, int mode) {     if(mode == gl_triangle_fan) {         vector2d *newbuffer = (vector2d *)malloc(sizeof(vector2d)*(num_points+1));//create new buffer append center beginning         newbuffer[0] = *center; //copy center         memcpy(newbuffer+1, points, sizeof(vector2d)*num_points); //copy other points after center         glenableclientstate(gl_vertex_array);         glvertexpointer(2, gl_float, sizeof(vector2d), newbuffer);         gldrawarrays(mode, 0, num_points+1);         free(newbuffer);     }     else {         glenableclientstate(gl_vertex_array);         glvertexpointer(2, gl_float, sizeof(vector2d), points);         gldrawarrays(mode, 0, num_points);     } } 

do note since function uses directly vector2d structure considered, x , y first 2 parameters in vector2d structure. if added 'z' 3rd parameter code should work.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -