PDA

View Full Version : help me!!! rotate around my model



matt
12-05-2001, 05:26 AM
i have a model and i want to rotate around the damn thing, im at my wits end here is the code for the model which is at the origin, could you tell me how to rotate around the model

#include <windows.h>
#include <stdio.h>
#include <gl/glut.h>


void sheep(void) //draw scene
{

glClear(GL_COLOR_BUFFER_BIT);




//sheep's body
glPushMatrix();
glScalef(0.8f,1.0f,1.0f);
glColor3f(1.0f,1.0f,1.0f);
GLUquadricObj *sheepBody;
sheepBody = gluNewQuadric();
gluQuadricNormals(sheepBody, GLU_SMOOTH);
gluSphere(sheepBody,20, 20, 50);
glPopMatrix();


//sheep's head
glPushMatrix();
glTranslatef(0.0f,25.0f,0.0f);
glRotatef(-90.0f,1.0f,0.0f,0.0f);
glScalef(1.0f,0.7f,1.0f);
glColor3f(0.0f,0.0f,0.0f);
GLUquadricObj *sheepHead;
sheepHead = gluNewQuadric();
gluQuadricNormals(sheepHead, GLU_SMOOTH);
gluSphere(sheepHead,9, 9, 20);
glPopMatrix();


/*sheep's left ear
glPushMatrix();
glTranslatef(15.0f,0.0f,0.0f);
glRotatef(-170.0f,0.0f,0.0f,1.0f);
glScalef(0.0f,0.0f,0.0f);
glColor3f(1.0f,0.0f,0.0f);
GLUquadricObj *sheepLeftEar;
sheepLeftEar = gluNewQuadric();
gluDisk(sheepLeftEar,0.0, 50.0, 20, 1);
glPopMatrix();*/


//sheep left arm
glPushMatrix();
glTranslatef(-14.0f,11.0f,0.0f);
glRotatef(-90.0f,0.0f,1.0f,0.0f);
glRotatef(60.0f,1.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,0.0f);
GLUquadricObj *sheepLeftArm;
sheepLeftArm = gluNewQuadric();
gluCylinder(sheepLeftArm,1.5, 1.5, 20, 20, 2);
glPopMatrix();


//sheep right arm
glPushMatrix();
glTranslatef(14.0f,11.0f,0.0f);
glRotatef(-90.0f,0.0f,1.0f,0.0f);
glRotatef(120.0f,1.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,0.0f);
GLUquadricObj *sheepRightArm;
sheepRightArm = gluNewQuadric();
gluCylinder(sheepRightArm,1.5, 1.5, 20, 20, 2);
glPopMatrix();


//sheep left leg
glPushMatrix();
glTranslatef(-9.0f,-16.0f,0.0f);
glRotatef(-90.0f,0.0f,1.0f,0.0f);
glRotatef(90.0f,1.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,0.0f);
GLUquadricObj *sheepLeftLeg;
sheepLeftLeg = gluNewQuadric();
gluCylinder(sheepLeftLeg,1.5, 1.5, 20, 20, 2);
glPopMatrix();


//sheep right leg
glPushMatrix();
glTranslatef(9.0f,-16.0f,0.0f);
glRotatef(-90.0f,0.0f,1.0f,0.0f);
glRotatef(90.0f,1.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,0.0f);
GLUquadricObj *sheepRightLeg;
sheepRightLeg = gluNewQuadric();
gluCylinder(sheepRightLeg,1.5, 1.5, 20, 20, 2);
glPopMatrix();

glFlush();
}

void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(330,TimerFunc,1);
}


void SetupRC(void) //setup rendering state
{
glClearColor(0.0f,0.0f,1.0f,1.0f);


//glEnable(GL_DEPTH_TEST);
//glEnable(GL_CCW);
//glEnable(GL_CULL_FACE);
}

void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat nRange = 100.0f;

glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();


if (w <= h)
glOrtho (-nRange,nRange,-nRange*h/w,nRange*h/w,-nRange,nRange);
else
glOrtho (-nRange*w/h,nRange*w/h,-nRange,nRange,-nRange,nRange);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


}

void main(void) //program entry point
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Sheep");
glutDisplayFunc(sheep);
glutReshapeFunc(ChangeSize);
SetupRC();

//glutTimerFunc(330,TimerFunc,1);
glutMainLoop();
}

Rob The Bloke
12-05-2001, 12:01 PM
Originally posted by matt:

#include <windows.h>
#include <stdio.h>
#include <gl/glut.h>

// little initial rotation value
float rot=0;
int xLast = 0;

void sheep(void) //draw scene
{

glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glRotatef(rot,0,1,0);

glPushMatrix();

//sheep's body
glPushMatrix();
glScalef(0.8f,1.0f,1.0f);
glColor3f(1.0f,1.0f,1.0f);
GLUquadricObj *sheepBody;
sheepBody = gluNewQuadric();
gluQuadricNormals(sheepBody, GLU_SMOOTH);
gluSphere(sheepBody,20, 20, 50);
glPopMatrix();


//sheep's head
glPushMatrix();
glTranslatef(0.0f,25.0f,0.0f);
glRotatef(-90.0f,1.0f,0.0f,0.0f);
glScalef(1.0f,0.7f,1.0f);
glColor3f(0.0f,0.0f,0.0f);
GLUquadricObj *sheepHead;
sheepHead = gluNewQuadric();
gluQuadricNormals(sheepHead, GLU_SMOOTH);
gluSphere(sheepHead,9, 9, 20);
glPopMatrix();


/*sheep's left ear
glPushMatrix();
glTranslatef(15.0f,0.0f,0.0f);
glRotatef(-170.0f,0.0f,0.0f,1.0f);
glScalef(0.0f,0.0f,0.0f);
glColor3f(1.0f,0.0f,0.0f);
GLUquadricObj *sheepLeftEar;
sheepLeftEar = gluNewQuadric();
gluDisk(sheepLeftEar,0.0, 50.0, 20, 1);
glPopMatrix();*/


//sheep left arm
glPushMatrix();
glTranslatef(-14.0f,11.0f,0.0f);
glRotatef(-90.0f,0.0f,1.0f,0.0f);
glRotatef(60.0f,1.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,0.0f);
GLUquadricObj *sheepLeftArm;
sheepLeftArm = gluNewQuadric();
gluCylinder(sheepLeftArm,1.5, 1.5, 20, 20, 2);
glPopMatrix();


//sheep right arm
glPushMatrix();
glTranslatef(14.0f,11.0f,0.0f);
glRotatef(-90.0f,0.0f,1.0f,0.0f);
glRotatef(120.0f,1.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,0.0f);
GLUquadricObj *sheepRightArm;
sheepRightArm = gluNewQuadric();
gluCylinder(sheepRightArm,1.5, 1.5, 20, 20, 2);
glPopMatrix();


//sheep left leg
glPushMatrix();
glTranslatef(-9.0f,-16.0f,0.0f);
glRotatef(-90.0f,0.0f,1.0f,0.0f);
glRotatef(90.0f,1.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,0.0f);
GLUquadricObj *sheepLeftLeg;
sheepLeftLeg = gluNewQuadric();
gluCylinder(sheepLeftLeg,1.5, 1.5, 20, 20, 2);
glPopMatrix();


//sheep right leg
glPushMatrix();
glTranslatef(9.0f,-16.0f,0.0f);
glRotatef(-90.0f,0.0f,1.0f,0.0f);
glRotatef(90.0f,1.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,0.0f);
GLUquadricObj *sheepRightLeg;
sheepRightLeg = gluNewQuadric();
gluCylinder(sheepRightLeg,1.5, 1.5, 20, 20, 2);
glPopMatrix();

glPopMatrix();

glFlush();
}

void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(330,TimerFunc,1);
}


void SetupRC(void) //setup rendering state
{
glClearColor(0.0f,0.0f,1.0f,1.0f);


//glEnable(GL_DEPTH_TEST);
//glEnable(GL_CCW);
//glEnable(GL_CULL_FACE);
}

void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat nRange = 100.0f;

glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();


if (w <= h)
glOrtho (-nRange,nRange,-nRange*h/w,nRange*h/w,-nRange,nRange);
else
glOrtho (-nRange*w/h,nRange*w/h,-nRange,nRange,-nRange,nRange);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


}

void mouseMove( int x, int y )
{
int stepX = xLast - x;
rot += stepX*0.01;
glutPostRedisplay();
}

void main(void) //program entry point
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Sheep");
glutDisplayFunc(sheep);
glutMotionFunc(mouseMove);
glutReshapeFunc(ChangeSize);
SetupRC();

//glutTimerFunc(330,TimerFunc,1);
glutMainLoop();
}

The above should work, prolly want to add extra stuff to get rotations in more than one direction.

matt
12-05-2001, 01:18 PM
thank you