views:

2981

answers:

6

Hi,

I am relatively new to OpenGL programming...currently involved in a project that uses 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..

Thanks.

Best Regards.

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??

Thanks.

Edit2: Ok thanks for replying...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??

Thanks.

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??

Thanks.

+2  A: 

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. :)

mstrobl
I never remember those blending parameters.
Cheery
+2  A: 

I remember there was a subroutine for that. But it's neither too hard to do by yourself.

But I don't understand the 2D -thing. Cone in 2D? Isn't it just a triangle?

Anyway, here's an algorithm to drawing a cone in opengl

First take a circle, subdivision it evenly so that you get a nice amount of edges. Now pick the center of the circle, make triangles from the edges to the center of the circle. Then select a point over the circle and make triangles from the edges to that point.

The size shape and orientation depends about the values you use to generate the circle and two points. Every step is rather simple and shouldn't cause trouble for you.

First just subdivision a scalar value. Start from [0-2] -range. Take the midpoint ((start+end)/2) and split the range with it. Store the values as pairs. For instance, subdividing once should give you: [(0,1), (1,2)] Do this recursively couple of times, then calculate what those points are on the circle. Simple trigonometry, just remember to multiply the values with π before proceeding. After this you have a certain amount of edges. 2^n where n is the amount of subdivisions. Then you can simply turn them into triangles by giving them one vertex point more. Amount of triangles ends up being therefore: 2^(n+1). (The amounts are useful to know if you are doing it with fixed size arrays.

Edit: What you really want is a pie. (Sorry the pun)

It's equally simple to render. You can again use just triangles. Just select scalar range [-0.25 - 0.25], subdivide, project to circle, and generate one set of triangles.

The scalar - circle projection is simple as: x=cos(v*pi)r, y=sin(vpi)*r where (x,y) is the resulting vertex point, r is a radius, and trigonometric functions work on radiances, not degrees. (if they work with degrees, replace pi with 180)

Use vertex buffers or lists to render it yourself.

Edit: About the coloring question. glColor4f, if you want some parts of the geometry to be different by its color, you can assign a color for each vertex in vertex buffer itself. I don't right now know all the API calls to do it, but API reference in opengl is quite understandable.

Cheery
+2  A: 

Since you reclarified your question to ask for a pie: there's an easy way to draw that too using opengl primitives:

You'd draw a solid sphere using gluSolidSphere(). However, since you only want to draw part of it, you just clip the unwanted parts away:

void glClipPlane(GLenum plane, const GLdouble * equation);

With plane being GL_CLIPPLANE0 to GL_CLIPPLANEn and equation being a plane equation in normal form (a*x + b*y + c*z + d = 0 would mean equation would hold the values { a, b, c, d }. Please note that those are doubles and not floats.

mstrobl
+2  A: 

On the edit on colors:

OpenGL is actually a state machine. This means that the current material and/or color position is used when drawing. Since you probably won't be using materials, ignore that for now. You want colors.

glColor3f(float r, float g, float b) // draw with r/g/b color and alpha of 1
glColor4f(float r, float g, float b, float alpha)

This will affect the colors of any vertices you draw, of any geometry you render - be it glu's or your own - after the glColorXX call has been executed. If you draw a face with vertices and change the color inbetween the glVertex3f/glVertex2f calls, the colors are interpolated.

Try this:

glBegin(GL_TRIANGLES);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-3.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 3.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(3.0, 0.0, 0.0);
glEnd();

But I pointed at glColor4f already, so I assume you want to set the colors on a per-vertex basis. And you want to render using display lists.

Just like you can display lists of vertices, you can also make them have a list of colors: all you need to do is enable the color lists and tell opengl where the list resides. Of course, they need to have the same outfit as the vertex list (same order).

If you had

glEnableClientState(GL_VERTEX_ARRAY);   
glVertexPointer(3, GL_FLOAT, 0, vertices_);
glDisableClientState(GL_VERTEX_ARRAY);

you should add colors this way. They need not be float; in fact, you tell it what format it should be. For a color list with 1 byte per channel and 4 channels (R, G, B and A) use this:

glEnableClientState(GL_VERTEX_ARRAY);   
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices_);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors_);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

EDIT: Forgot to add that you then have to tell OpenGL which elements to draw by calling glDrawElements.

mstrobl
+2  A: 

On Edit3: The way I understand your question is that you want to have OpenGL draw borders and anything between them should be filled with colors.

The idea you had was right, but a line strip is just that - a strip of lines, and it does not have any area.

You can, however, have the lines connect to each other to define a polygon. That will fill out the area of the polygon on a per-vertex basis. Adapting your code:

glBegin(GL_POLYGON);

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();

Please note however, that drawing a polygon this way has two limitations:

  • The polygon must be convex.
  • This is a slow operation.

But I assume you just want to get the job done, and this will do it. For the future you might consider just triangulating your polygon.

mstrobl
Note that you don't need the multiple glColor3f() statements. OpenGL is a state machine, and the state is retained. So one glColor3f()-statement will suffice.
mstrobl
ThankYou mstrobl,this is really what i wanted...ur solution made my day!
ashishsony
A: 

plx hlpme through dis i want to create bubbles which shoud be moving from top to bottom.........ofcrs it wd be a kind ov tranprnt circles plx ghlp me owt abt dis..............