🔒 Locked
Rotating objects
Advertisement
Original Post
Put glTranslate before glRotate, for example:
glLoadIdentity();
static int i = 0;
glTranslatef(0,0,-10);
glRotatef(++i,0,1,0);
DisplayModel();
and ensure that DisplayModel() hasn't any glTranslate and the model is centered.
[Edited by - Lord_Vader on February 21, 2007 3:52:18 PM]
glLoadIdentity();
static int i = 0;
glTranslatef(0,0,-10);
glRotatef(++i,0,1,0);
DisplayModel();
and ensure that DisplayModel() hasn't any glTranslate and the model is centered.
[Edited by - Lord_Vader on February 21, 2007 3:52:18 PM]
#include <gl.h>
#include <glut.h>
#include <cstdlib>
float ypos = 0.0,xpos = 0.0,rotate=0.0;
void drawterrain(){
glColor3f(0,256,0);
glBegin(GL_POLYGON);
glVertex3f(0.1,0.9,0.0);
glVertex3f(0.9,0.9,0.0);
glVertex3f(0.9,0.1,0.0);
glVertex3f(0.1,0.1,0.0);
glEnd();
}
void drawobject(){
glColor3f(256,256,0);
glBegin(GL_POLYGON);
glVertex3f(0.4,0.6,0);
glVertex3f(0.6,0.6,0);
glVertex3f(0.6,0.4,0);
glVertex3f(0.4,0.4,0);
glEnd();
glColor3f(256,0,0);
glBegin(GL_POLYGON);
glVertex3f(0.4,0.6,0);
glVertex3f(0.6,0.6,0);
glVertex3f(0.6,0.6,0.1);
glVertex3f(0.4,0.6,0.1);
glEnd();
}
void display()
{
glLoadIdentity();
glClearColor(256,256,256,0);
glClear(GL_COLOR_BUFFER_BIT);
drawterrain();
glPushMatrix();
glTranslatef(xpos,ypos,0.0);
glRotatef(rotate,0.0,0.0,0.0);
drawobject();
glPopMatrix();
glFlush();
}
void init(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(0.0,1.0,-0.1,1.0,-1.0,0.0);
GLfloat lightpos[] = {0.5,0.5,-0.2,0.0};
GLfloat lightdir[] = {0,0,1};
GLfloat red[]={256,0,0};
GLfloat blue[]={0,0,256};
GLfloat white[]={256,256,256};
}
void keyboard(unsigned char key, int x,int y)
{
switch(key){
case 'z':
ypos = ypos+0.01;
display();
break;
case 's':
ypos = ypos - 0.01;
display();
break;
case 'q':
xpos =xpos - 0.01;
display();
break;
case 'd':
xpos =xpos + 0.01;
display();
break;
case 'a':
rotate = rotate +1.0;
display();
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutInitWindowSize(800,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Titel");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
#include <glut.h>
#include <cstdlib>
float ypos = 0.0,xpos = 0.0,rotate=0.0;
void drawterrain(){
glColor3f(0,256,0);
glBegin(GL_POLYGON);
glVertex3f(0.1,0.9,0.0);
glVertex3f(0.9,0.9,0.0);
glVertex3f(0.9,0.1,0.0);
glVertex3f(0.1,0.1,0.0);
glEnd();
}
void drawobject(){
glColor3f(256,256,0);
glBegin(GL_POLYGON);
glVertex3f(0.4,0.6,0);
glVertex3f(0.6,0.6,0);
glVertex3f(0.6,0.4,0);
glVertex3f(0.4,0.4,0);
glEnd();
glColor3f(256,0,0);
glBegin(GL_POLYGON);
glVertex3f(0.4,0.6,0);
glVertex3f(0.6,0.6,0);
glVertex3f(0.6,0.6,0.1);
glVertex3f(0.4,0.6,0.1);
glEnd();
}
void display()
{
glLoadIdentity();
glClearColor(256,256,256,0);
glClear(GL_COLOR_BUFFER_BIT);
drawterrain();
glPushMatrix();
glTranslatef(xpos,ypos,0.0);
glRotatef(rotate,0.0,0.0,0.0);
drawobject();
glPopMatrix();
glFlush();
}
void init(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(0.0,1.0,-0.1,1.0,-1.0,0.0);
GLfloat lightpos[] = {0.5,0.5,-0.2,0.0};
GLfloat lightdir[] = {0,0,1};
GLfloat red[]={256,0,0};
GLfloat blue[]={0,0,256};
GLfloat white[]={256,256,256};
}
void keyboard(unsigned char key, int x,int y)
{
switch(key){
case 'z':
ypos = ypos+0.01;
display();
break;
case 's':
ypos = ypos - 0.01;
display();
break;
case 'q':
xpos =xpos - 0.01;
display();
break;
case 'd':
xpos =xpos + 0.01;
display();
break;
case 'a':
rotate = rotate +1.0;
display();
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutInitWindowSize(800,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Titel");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
You didn't rotate on any axis.
Here's a working one:
void display(){glLoadIdentity();glClearColor(256,256,256,0);glClear(GL_COLOR_BUFFER_BIT);drawterrain();glPushMatrix();glTranslatef(xpos,ypos,0.0);glRotatef(rotate,0.0,0.0,0.0); // this is the culpritdrawobject();glPopMatrix();glFlush();}Here's a working one:
sub display() glLoadIdentity() glClearColor(0,0,0,0) glClear(GL_COLOR_BUFFER_BIT) glTranslatef(0,0,-5.0) drawterrain() glPushMatrix() glTranslatef(xpos,ypos,1.0) glRotatef(rotate,1.0,1.0,0.0) drawobject() glPopMatrix() glFlush()end sub
I had this problem as well I hope what I'm about to tell you will help you...
You have the 3D cartesian system which has 3 axes, now when you spin around the axes, you take the closest vertex to the axe that you want to spin and spin the entire object while using this vertex as the pivot point, as far as I understand it, correct me if I'm wrong... now what you can do is make sure that the object is centered on the (0, 0, 0) point all the time, meaning, move it to the point you want to and then rotate it. Ok I'm not sure I'm clear enough so let's try some code:
My perspective is:
gluPerspective(45.f, (GLfloat)640/ (GLfloat)480, 1.f, 400.f);
So the basic idea is to keep the object centered on the axes...
Hope that helped...
You have the 3D cartesian system which has 3 axes, now when you spin around the axes, you take the closest vertex to the axe that you want to spin and spin the entire object while using this vertex as the pivot point, as far as I understand it, correct me if I'm wrong... now what you can do is make sure that the object is centered on the (0, 0, 0) point all the time, meaning, move it to the point you want to and then rotate it. Ok I'm not sure I'm clear enough so let's try some code:
float xRot = 0;float yRot = 0;float zRot = 0;// advance x, y, z rot every few milliseconds. . .//-------------------------------------------// Rendering codeglLoadIdentity();glClearColor(.0f, .0f, .5f, 1.0f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();glLoadIdentity();glRotatef(45.0f + xRot, 1.0f, .0f, .0f);glRotatef(45.0f + yRot, .0f, 1.0f, .0f);glRotatef(45.0f + zRot, .0f, .0f, 1.0f);glBegin(GL_QUADS); glVertex3f(-0.5f, 0.5f, -10.0f); glVertex3f( 0.5f, 0.5f, -10.0f); glVertex3f( 0.5f, -0.5f, -10.0f); glVertex3f(-0.5f, -0.5f, -10.0f); glEnd();glPopMatrix();My perspective is:
gluPerspective(45.f, (GLfloat)640/ (GLfloat)480, 1.f, 400.f);
So the basic idea is to keep the object centered on the axes...
Hope that helped...
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement