I want to draw a circle that starts out dark at the top and slower gets lighter towards the bottom. I am using gl_lines to draw out the circle and change each lines color.
Are we discussing a problem with the shading of the circle, or programming language features and coding style?
Assuming it’s the later: Both the points on the circle and your shading effect can be calculated. It’s definitely better generating the points in a loop.
On the one hand you can controll a lot of parameters (e.g. how many line segments should the circle have, etc…) without code changes and on the
other hand, a loop is definitely going to be smaller than a wall of glVertex* calls.
Perhaps, GL_LINE_LOOP might be an easier choice for programmatical generation of a circle. When using GL_LINE_LOOP, every point you specify by calling glVertex*
is connected to the last point specified and when you call glEnd( ), the last point is automatically connected to the first point you specified.
You could for instance write a simple for loop that loops over degress from 0 to <360 and compute a position on the circle. Here’s some pseudo-C for that simplistic approach:
If the color is bright at the bottom and dark at the top, it must obviously be a function of y.
Why not try something linear:
int i, step=10;
float x, y, c;
glBegin( GL_LINE_LOOP );
for( i=0; i<360; i+=step )
{
x = cos( ((float)i)*DEGTORAD );
y = sin( ((float)i)*DEGTORAD );
c = y * (-0.5f) + 0.5f;
glColor3f( c, c, c );
glVertex2f( x, y );
}
glEnd( );
float x, y, z;
glBegin(GL_POLYGON);
for (float i = 0.0; i < 360; i+=1) {
x = cos(((float) i) * (3.1415/180));
y = sin(((float) i) * (3.1415/180));
z = y * (-0.5f) + (0.5f);
glColor3f(z, z, z);
glVertex2f(x, y);
}
It does exactly what i want it to do but location and size of the circle is not where i want them to be.
My window is currently 500 by 500 and the circle takes up the entire window.
What i want to know is how would i change the radius and how do i change the location of the circle? I would like to place more circles in the same window.
How familiar are you with the following things:
[ul]
[li]OpenGL®[/li][li]The C programming language[/li][li]Trigonometry[/li][li]Linear algebra[/li][/ul]
There are many ways to resize/move the circle in the image. If it fills the entire window, you obviously have your transformation and projection matrices set to identity. You could multiply your x and y values with the desired radius and add an offset, or you could use glTranslatef/glRotatef to do the same thing (basics of legacy OpenGL®). When you resize your window to a non rectangular shape, you will probably notice that the circle is no longer a circle, since your projection matrix is identity.
Basic knowledge of linear algebra and the legacy OpenGL® API you are using can be very helpfull here. In this case, a little knowledge of trignometry got you the solution of how to programmatically generate circle points.
You initially asked if you should use a for-loop or a while-loop and replaced the loop counter with a float, that you compare against an integer and add an integer to and then cast in to a float. How familiar are you with the C programming language you are using? (Or programming languages in general, since pretty much every imparative programming language has for and while loops). When you want to do OpenGL® based 3D graphics, fundamental understanding of the programming language you use is essential.
I am not familiar with OpenGL. I am new to it and I think that is what I was confused with. I have programed in C++ but not in a while. I have a good understanding of for loops and while loops. I just didn’t think of using cos and sin to create a circle. That made it much more simple. I was trying to pick coordinates and drawing individual lines, which was making it confusing and more difficult. I was trying to figure out a simpler way and using sin and cos was it.