PDA

View Full Version : Trying to transform 2 quads



cinderella1990
12-19-2010, 08:57 AM
Hello, I'm trying to transform quads : first one should rotate, second one should move. I am completely stuck with this one: either both quads are rotating and moving at once, either transformation terminates.
my code:


#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
int x;
int y;
void basicInt(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClearColor(0,0,0,0);
gluOrtho2D(0,950,0,600);
}

void drawQ(int x, int y)
{
glColor3f(1,1,0);
glBegin(GL_QUADS);
glVertex2d( x, y );
glColor3f(1,1,1);
glVertex2d( x, y+75 );
glVertex2d( x+50, y+75 );
glVertex2d(x+50, y );
glEnd( );
}

void refresh(int count)
{
glutPostRedisplay();

}
void myView(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);

glPushMatrix();
glRotated(1,0,0,1);
drawQ(400, 200); //first quad should rotate
glPopMatrix();


glPushMatrix();
glTranslated(1,1,0);
drawQ(600, 200); //second quad should change its position
glPopMatrix();

glutSwapBuffers();
glutTimerFunc(33,refresh,1);
}

int main(int argc,char* argv[])
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(950,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Test");
basicInt();
glutDisplayFunc(myView);
glutMainLoop();

}

I assume something is not right with glPushMatrix/glPopMatrix
please help!

Alfonse Reinheart
12-19-2010, 10:49 AM
glRotated(1,0,0,1);
drawQ(400, 200); //first quad should rotate


That will cause it to rotate 1 degree. That's not very much. Try something you'll notice, like 30 degrees.

cinderella1990
12-19-2010, 10:57 AM
it does rotate and it is noticeable when I delete lines with glPopMatrix/glPushMatrix

ravage
12-19-2010, 05:19 PM
void drawQ(int x, int y)
{
glColor3f(1,1,0);
glBegin(GL_QUADS);
glVertex2d( x, y );
glColor3f(1,1,1);
glVertex2d( x, y+75 );
glVertex2d( x+50, y+75 );
glVertex2d(x+50, y );
glEnd( );
}



This is the reason for your problem. It's like your rotating along the edge of the rectangle which causes it to rotate and move. The other doesn't have that problem since your just moving it. I would change it to this:



void drawQ(int x, int y)
{
glColor3f(1,1,0);
glBegin(GL_QUADS);
glVertex2d( -(x/2), -(y/2) );
glColor3f(1,1,1);
glVertex2d( -(x/2), (y/2) );
glVertex2d( (x/2), (y/2) );
glVertex2d((x/2), -(y/2) );
glEnd( );
}


That way in objects center is 0 instead of somewhere else. Your going to have to move your objects in this case as they will be kinda of screen since your quad center is at [0,0]



glPushMatrix();
glTranslated(400,200,0);
glRotated(dt,0,0,1);
drawQ(400, 200); //first quad should rotate
glPopMatrix();


glPushMatrix();
glTranslated(650,500,0);
drawQ(600, 200); //second quad should change its position
glPopMatrix();


this works out nicely.