Page 2 of 3

Re: Again with OpenGL

Posted: Mon Jan 09, 2012 9:08 pm
by Benjamin100
which object is the "glRotatef" function rotating?
How do I specify one object as opposed to another?
Do I just use the "push" and "pop" to change between objects I'm controlling?

EDIT: Evidently not.

Re: Again with OpenGL

Posted: Mon Jan 09, 2012 9:28 pm
by Ginto8
glRotate multiplies the currently selected matrix by a rotation matrix. When called as glRotatef(a,b,c,d), you can conceptually think of this as rotating the current origin a degrees around the vector (b,c,d).

Re: Again with OpenGL

Posted: Mon Jan 09, 2012 9:30 pm
by Benjamin100
Thanks!

So, the push and pop helps matrix selection?

Re: Again with OpenGL

Posted: Mon Jan 09, 2012 9:46 pm
by Ginto8
push and pop operate on the matrix stack, which can be considered a "backlog" of previous matrices. For more information on how stacks like this work: Wikipedia is your friend.

Re: Again with OpenGL

Posted: Mon Jan 09, 2012 10:37 pm
by Benjamin100

Code: Select all

glPushMatrix();
	glRotatef(0.1, 0.0, 1.0, 0.0); 
	glBegin(GL_POLYGON);
	     glColor3f(0.0, 0.0, 1.0);
		     glVertex3f(0.3, 0.8, 0.0);
			 glVertex3f(0.3, 0.2, 0.0);
			 glVertex3f(0.8, 0.2, 0.0);
			 glVertex3f(0.8, 0.8, 0.0);
	glEnd();
	glPopMatrix();
Shouldn't this rotate my rectangle and nothing else?

Re: Again with OpenGL

Posted: Tue Jan 10, 2012 9:17 am
by Ginto8
Yes, but since the vector (0.0,1.0,0.0) points up, not out toward the viewer, it would rotate around a "pole" going from the top of the screen top to the bottom, which I'm guessing isn't what you're looking to do. Also, using GL_POLYGON instead of GL_QUAD is isn't necessarily a good idea, because OpenGL is faster with quads (and even faster with triangles) than with polygons (when it knows they're quads/triangles, and doesn't have to treat them like polygons).

Re: Again with OpenGL

Posted: Tue Jan 10, 2012 4:02 pm
by Benjamin100
Well when I put the rotate within the push and pop it won't rotate.

EDIT: This is using the "glutMainLoop();".

Re: Again with OpenGL

Posted: Tue Jan 10, 2012 4:12 pm
by Benjamin100
I think I understand the different ways it rotates, I just want to rotate one shape while another stays still

Re: Again with OpenGL

Posted: Tue Jan 10, 2012 10:38 pm
by Ginto8
Here's the general formula for working with transformations and the matrix stack:

Code: Select all

[general transformations]
[render scene based on general transforms]
[push]
    [scene 1 transformations]
    [scene 1 rendering]
[pop]
[push]
    [scene 2 transformations]
    [scene 2 rendering]
[pop]
[render second scene based on general transforms]
Most parts of that can be removed, but the indented scenes require the [push][pop] surrounding them to prevent them from affecting other scenes.

Re: Again with OpenGL

Posted: Tue Jan 10, 2012 11:19 pm
by Benjamin100
Thank you.

Re: Again with OpenGL

Posted: Wed Jan 11, 2012 1:47 pm
by Benjamin100
Well, it seems the rotation doesn't do anything when inside of the push and pop.

Re: Again with OpenGL

Posted: Wed Jan 11, 2012 6:09 pm
by Ginto8
Why don't you post the code? Since you say it "doesn't do anything", I'm assuming that you popped the matrix again before actually using the rotation for rendering, but I can't be sure without code.

Re: Again with OpenGL

Posted: Wed Jan 11, 2012 10:02 pm
by Benjamin100
Wait a minute, a couple things...
So, first I think I get that the RENDERING, not JUST the transformations, need to be within the "push()" and "pop()".

Now, is this not supposed to be in the display function? Is this a separate function?
I mean, the push and pop regions are not the same ones as the ones with the vertex data?

Re: Again with OpenGL

Posted: Wed Jan 11, 2012 10:13 pm
by Benjamin100
I've done some editing on it, but now it just makes the rectangle(EDIT: Triangle) flash.

Code: Select all

void rect_display(void)   
{
	glClearColor(0, 0, 0, 0);    
	glClear(GL_COLOR_BUFFER_BIT);  
	glMatrixMode(GL_MODELVIEW);
    
	glPushMatrix();
	glBegin(GL_POLYGON);
	     glColor3f(0.0, 0.0, 1.0);  
		     glVertex3f(0.3, 0.8, 0.0);
			 glVertex3f(0.3, 0.2, 0.0);
			 glVertex3f(0.8, 0.2, 0.0);
			 glVertex3f(0.8, 0.8, 0.0);
    glEnd();
	glRotatef(0.1, 0.0, 0.0, 1.0);
	glFlush();
	glutSwapBuffers();
	glutPostRedisplay();
    glPopMatrix();


	glPushMatrix(); 
	glBegin(GL_TRIANGLES);
	     glColor3f(0.0, 1.0, 0.0);
		     glVertex3f(0.5, 0.9, 0.0);
			 glVertex3f(0.1, 0.1, 0.0);
			 glVertex3f(0.9, 0.1, 0.0);
	glEnd();  
	glFlush();
	glPopMatrix();


	
	
	
}
So there is the display function. It originally just displayed the rectangle, hence the name.

Please tell me EVERYTHING that is wrong with it. And if there should even be a display function.

Re: Again with OpenGL

Posted: Thu Jan 12, 2012 12:37 am
by qpHalcy0n
Rotate before submitting draw calls. This will ensure that the specified transform is applied to all subsequent render calls. This is the purpose for push/pop, so that you can SAVE the current transform state, and RESTORE it when you're done rendering that "thing" such that not ALL subsequent objects rendered have that same transform applied to them. This would be an undesired side effect.

So Push (Save the current transform)
apply new transform
render
Pop (Restore old transform)

Remember this paradigm. Change render state? Clean it up when you're done.