PDA

View Full Version : Draw an arc



AeroSujan
09-18-2011, 03:58 AM
Dear Friends
I am trying to draw an arc in openGL......Given are the centerX,centerY,radius,start_angle and end_angle.....How can I draw an arc with this information. Please help me someone. Thanks Sujan

-Mil0-
09-18-2011, 11:50 PM
Dear Friends
I am trying to draw an arc in openGL......Given are the centerX,centerY,radius,start_angle and end_angle.....How can I draw an arc with this information. Please help me someone. Thanks Sujan

One way to do it is to simply generate a number of vertices positioned on the arc, and then draw lines between them using glDrawArrays(GL_LINE_STRIP, ...).

Here's a quick function I whipped up to generate those verts (C code):



struct vec4{
float x, y, z, w;
};

float* getArcVerts(float pos_x, float pos_y,
float radius,
float angle_start, float angle_end,
unsigned int detail_level)
{
float* verts = malloc(sizeof(struct vec4) * detail_level);

float angle_delta = angle_end - angle_start;
float angle_step = angle_delta / (detail_level - 1);
float angle = angle_start;

struct vec4* v = (struct vec4*)verts;
while(detail_level){
v->x = pos_x + (cos(angle) * radius);
v->y = pos_y + (sin(angle) * radius);
v->z = 0.0f;
v->w = 1.0f;

angle += angle_step;
++v;

--detail_level;
}

return verts;

}


The detail_level is the additional thing to note: it's essentially the number of vertices that comprise your arc, so the higher it is, the smoother the arc will seem.

I'm sure there are other ways to do this - maybe using a fragment shader to paint only pixels that satisfy the equation of an arc (which is something I would like to see) - but for now, I only know the vertex approach.

Hope that helps.

AeroSujan
09-19-2011, 03:29 AM
HI Mil0
The following error I am getting for the line of

float* verts = malloc(sizeof(struct vec4) * detail_level);


c:\users\dasmahapatras\projects\bhagavan_cadem\rev olutionproj_profile\revolutionproj\revolutionprojv iew.cpp(588) : error C2440: 'initializing' : cannot convert from 'void *' to 'float *'

Please tell me how to resolve this error. Thanks Sujan

AleaIactaEst
09-19-2011, 03:44 AM
You need to cast the result of malloc (which is always of type void*) to the desired type, in this case float*


float* verts = (float*)malloc(sizeof(struct vec4) * detail_level);

AeroSujan
09-19-2011, 03:53 AM
Thats giving linking error

AleaIactaEst
09-19-2011, 04:43 AM
That's a different error ... you're providing too few information on your problem.

AeroSujan
09-19-2011, 06:52 AM
Now its working fine Friend. Thanks a lot....

-Mil0-
09-19-2011, 11:53 AM
HI Mil0
The following error I am getting for the line of

float* verts = malloc(sizeof(struct vec4) * detail_level);


c:\users\dasmahapatras\projects\bhagavan_cadem\rev olutionproj_profile\revolutionproj\revolutionprojv iew.cpp(588) : error C2440: 'initializing' : cannot convert from 'void *' to 'float *'

Please tell me how to resolve this error. Thanks Sujan

Well, as the error clearly states, and as AlealactaEst already pointed out, the pointer returned by malloc cannot be converted automatically, so you have to do it yourself.

I'm on Linux, using gcc, and I was compiling with -Wall, but I didn't get any warnings, so I just left it like that.

I'll try to be more explicit in the future.

Anyway, I'm glad it's working now.