Again with OpenGL

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Again with OpenGL

Post 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).
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

Thanks!

So, the push and pop helps matrix selection?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Again with OpenGL

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post 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?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Again with OpenGL

Post 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).
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

Well when I put the rotate within the push and pop it won't rotate.

EDIT: This is using the "glutMainLoop();".
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

I think I understand the different ways it rotates, I just want to rotate one shape while another stays still
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Again with OpenGL

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

Thank you.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post by Benjamin100 »

Well, it seems the rotation doesn't do anything when inside of the push and pop.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Again with OpenGL

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post 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?
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Again with OpenGL

Post 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.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Again with OpenGL

Post 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.
Post Reply