Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a filled envelop like a cone on OpenGL (using GLUT)?

I am using freeglut for opengl rendering...

I need to draw an envelop looking like a cone (2D) that has to be filled with some color and some transparency applied.

Is the freeglut toolkit equipped with such an inbuilt functionality to draw filled geometries(or some trick)? or is there some other api that has an inbuilt support for filled up geometries..

Edit1: just to clarify the 2D cone thing... the envelop is the graphical interpretation of the coverage area of an aircraft during interception(of an enemy aircraft)...that resembles a sector of a circle..i should have mentioned sector instead..

and glutSolidCone doesnot help me as i want to draw a filled sector of a circle...which i have already done...what remains to do is to fill it with some color... how to fill geometries with color in opengl?

Edit2: All the answers posted to this questions can work for my problem in a way.. But i would definitely would want to know a way how to fill a geometry with some color. Say if i want to draw an envelop which is a parabola...in that case there would be no default glut function to actually draw a filled parabola(or is there any?).. So to generalise this question...how to draw a custom geometry in some solid color?

Edit3: The answer that mstrobl posted works for GL_TRIANGLES but for such a code:

glBegin(GL_LINE_STRIP);

glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(200.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(200.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);

glEnd();

which draws a square...only a wired square is drawn...i need to fill it with blue color.

anyway to do it?

if i put some drawing commands for a closed curve..like a pie..and i need to fill it with a color is there a way to make it possible...

i dont know how its possible for GL_TRIANGLES... but how to do it for any closed curve?

like image 486
ashishsony Avatar asked Dec 31 '22 08:12

ashishsony


1 Answers

I'm not sure what you mean by "an envelop", but a cone is a primitive that glut has:

glutSolidCone(radius, height, number_of_slices, number_of_stacks)

The easiest way to fill it with color is to draw it with color. Since you want to make it somewhat transparent, you need an alpha value too:

glColor4f(float red, float green, float blue, float alpha)
// rgb and alpha run from 0.0f to 1.0f; in the example here alpha of 1.0 will
// mean no transparency, 0.0 total transparency. Call before drawing.

To render translucently, blending has to be enabled. And you must set the blending function to use. What you want to do will probably be achieved with the following. If you want to learn more, drop me a comment and I will look for some good pointers. But here goes your setup:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Call that before doing any drawing operations, possibly at program initialization. :)

like image 169
mstrobl Avatar answered Jan 13 '23 15:01

mstrobl