PDA

View Full Version : Array of shapes



Nightwingvyse
03-12-2010, 02:45 AM
I'm trying to create an array of similar shapes that rotate around the cursor, but all of different speeds, distances and colours.

I've experimented by creating two of this shape and giving them two different distances from the cursor. The program builds fine, but the window doesn't load properly. The empty window just sits there and doesn't respond.

My code for this bit is:

for(int i=2; i<=4; i+2){
glTranslatef(i,i,0); }
float PI = 3.14159265358979323846264338327950288;
for(int i=0; i<=1; i++){
glBegin(GL_TRIANGLE_FAN);
for(int i=0; i <= 360; i++){
glVertex3f(sin(i*PI/180)*1,cos(i*PI/180)*1, 1);}}
glEnd();

What am i doing wrong? Can anyone help? Thanks.

Nightwingvyse
03-12-2010, 03:04 AM
Ok, i've solved this problem by moving some things round. I got this:

for(int i=1; i<=2; i++){
glTranslatef(mouse_x,mouse_y,0); //Positions circle at cursor.
glColor3f(0.4*i,0.4*i,0.4*i); //Gives colour of circle.
glRotatef(spin, 0.0, 0.0, 0.5*i); //Gives spin of circle.
glTranslatef(2*i,2*i,0); //Positions the original location of circle in relation to cursor before spin is applied.
glBegin(GL_TRIANGLE_FAN);
for(int i=0; i <= 360; i++){
glVertex3f(sin(i*PI/180)*1, cos(i*PI/180)*1, 1);}}
glEnd()


But now it only seems to create the first circle that would be in the loop. I'd have thought this would be okay. Can anyone Help?

ZbuffeR
03-12-2010, 04:56 AM
try adding glPushMatrix(GL_MODELVIEW); right after the { and glPopMatrix(GL_MODELVIEW); right before the }

Nightwingvyse
03-12-2010, 05:07 AM
I get "'glPushMatrix' : function does not take 1 arguments" when i do that, but i remember this vaguely. I'll look it up and see what needs to change.

ZbuffeR
03-12-2010, 06:01 AM
Yeah sorry, no need for an argument, pop/push is done implicitely on current matrix.
http://www.opengl.org/sdk/docs/man/xhtml/glPushMatrix.xml

Pziko
03-12-2010, 07:59 AM
You have nested loops with the same control variable name. And if you want to have an arbitary number of objects rotating, try using a precalculated look up table instead of 360 sin(i*PI/180) & cos(i*PI/180) per object per frame.

ZbuffeR
03-12-2010, 08:41 AM
@Pziko : precalc tables are not so good nowadays, as CPU computing speeds have increased a lot more more than memory bandwidth. It may have uses but a careful benchmark should validate it.

First make it work, then optimize performance.

dorbie
03-12-2010, 04:36 PM
That's why you stick your precalc in an attribute array (prefferably a VBO) and forget about it.

If you use individual vertex dispatch with some trig between, it's not good.

I agree with the philosophy of get it working then cache though.

Nightwingvyse
03-15-2010, 03:42 AM
Okay, so can anyone tell me what i can do to make this test work?

ZbuffeR
03-15-2010, 05:11 AM
You already had a number of advices.
- Did you put the push/pop at the right places ?
- Used a different variable to avoid aliasing "i" ?
- How is your code now ?
- Does it still not work ?

dorbie
03-15-2010, 04:06 PM
Your loops aren't nested.

And when you nest them you'll have problems since you use 'i' in both as Zb said.

P.S. they weren't nested in first version and so Zb has already given you the answer, it is definitely the 'i' namespace collision.