PDA

View Full Version : Problem rotating an object



Saulo Okada
10-06-2009, 10:01 AM
Hi.
I'm learning OpenGL and I'm traying to build 2 arms to my snowMan. But I can't rotate them. I know that my snow man its ugly but i'm still learning. =P
here is the code that I built.

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

/* *Variaveis para construção do Boneco */
float posPernaDireita = 0, anguloPernaDireita = 0, countPernaDireita;
float posPernaEsquerda = 0, anguloPernaEsquerda = 0, countPernaEsquerda;
float posPeDireitoX = -0.5, posPeDireitoY = -1, posPeDireitoZ = -6, anguloPeDireito = 0, countPeDireito;
float posPeEsquerdoX = 0.5, posPeEsquerdoY = -1,posPeEsquerdoZ = -6, anguloPeEsquerdo = 0, countPeEsquerdo;
float posBracoDireito = 0, anguloBracoDireito = 0, countBracoDireito;
float posBracoEsquerdo = 0, anguloBracoEsquerdo = 0, countBracoEsquerdo;
float posCorpo = 0, anguloCorpo = 0, countCorpo;
float posCabeca = 0, anguloCabeca = 0, countCabeca;

GLUquadricObj *pernaDireita;
GLUquadricObj *pernaEsquerda;
GLUquadricObj *bracoDireito;
GLUquadricObj *bracoEsquerdo;

/* *Variaveis para controle da Camera */
float angle = 0.0, deltaAngle = 0.0, ratio;
float x = 0.0f,y = 1.75f,z = 5.0f;
float lx = 0.0f,ly = 0.0f,lz = -1.0f;
GLint lista_boneco;
int deltaMove = 0;

/* GLUT callback Handlers */

static void resize( int width, int height )
{
/* *Previnir a Divisao por zero */
if( height == 0 )
height = 1;

ratio = (float) width / (float) height;

/* *Resetando o sistema de coordenadas antes de modifica-la */
glMatrixMode( GL_PROJECTION );
glLoadIdentity();

/* *Setando a porta de visão para a ser a janela inteira */
glViewport( 0, 0, width, height );

/* *Estabelecendo o recorte */
gluPerspective( 45, ratio, 1, 1000 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( x, y, z,
x + lx, y + ly, z + lz,
0.0f,1.0f,0.0f );

/* *Inicializando os objetos que farao parte do boneco */
pernaDireita = gluNewQuadric();
pernaEsquerda = gluNewQuadric();
bracoDireito = gluNewQuadric();
bracoEsquerdo = gluNewQuadric();
}

void desenhaBoneco()
{
glColor3f( 1.0f, 1.0f, 1.0f );

/* *Desenha o Corpo do Boneco*/
glTranslatef( 0.0f, 0.75f, 0.0f );
glutSolidSphere( 0.75f, 20, 20 );

/* *Desenha a Cabeca do Boneco */
glTranslatef( 0.0f, 1.0f, 0.0f );
glutSolidSphere( 0.25f, 20, 20 );

/* *Desenha os Olhos do Boneco */
glPushMatrix();
glColor3f( 0.0f, 0.0f, 0.0f );
glTranslatef( 0.05f, 0.10f, 0.18f );
glutSolidSphere( 0.05f, 10, 10 );
glTranslatef( -0.1f, 0.0f, 0.0f );
glutSolidSphere( 0.05f, 10, 10 );
glPopMatrix();

/* *Desenha o Nariz do Boneco*/
glColor3f( 1.0f, 0.5f , 0.5f );
glRotatef( 0.0f,1.0f, 0.0f, 0.0f );
glutSolidCone( 0.08f, 0.5f, 10, 2 );

/* *Perna DIREITA do boneco */
glPushMatrix();
glTranslatef( 0.3f, -0.7f, 1 );
glScalef( 0.3f, 1.2f, 0.3f );
glRotatef( anguloPernaDireita, 1, 0, 0 );
glutSolidSphere( 0.3f, 20, 20 );
glPopMatrix();

/* *Perna ESQUERDA do boneco */
glPushMatrix();
glTranslatef( -0.3f, -0.7f, 1 );
glScalef( 0.3f, 1.2f, 0.3f );
glRotatef( anguloPernaEsquerda, 1, 0, 0 );
glutSolidSphere( 0.3f, 20, 20 );
glPopMatrix();

/* *Pe DIREITO do Boneco */
glPushMatrix();
glTranslatef( posPeDireitoX, posPeDireitoY, posPeDireitoZ );
glScalef( 1, 0.4, 1.5 );
glRotatef( anguloPeDireito, 1, 0 ,0 );
glutSolidSphere( 0.05f, 10, 10 );
glPopMatrix();

/* *Pe ESQUERDO do Boneco */
glPushMatrix();
glTranslatef( posPeEsquerdoX, posPeEsquerdoY, posPeEsquerdoZ );
glScalef( 1, 0.4, 1.5 );
glRotatef( anguloPeEsquerdo, 1, 0, 0 );
glutSolidSphere( 0.05f, 10, 10 );
glPopMatrix();

}

GLuint createDL() {
GLuint bonecoDL;

/* *Cria um ID para a lista */
bonecoDL = glGenLists(1);

/* *Inicializa a lista */
glNewList( bonecoDL, GL_COMPILE );

/* *Chama a funcao que contem os comandos de renderização */
desenhaBoneco();

/* *Termina a lista */
glEndList();

return( bonecoDL );
}

void initScene() {
glEnable( GL_DEPTH_TEST );
lista_boneco = createDL();
}

void orientMe( float ang )
{
lx = sin( ang );
lz = -cos( ang );
glLoadIdentity();
gluLookAt( x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f );
}

void moveMeFlat( int i )
{
x = x + i * (lx) * 0.1;
z = z + i * (lz) * 0.1;
glLoadIdentity();
gluLookAt( x, y, z,
x + lx, y + ly, z + lz,
0.0f,1.0f,0.0f );
}

void renderScene(void)
{
if( deltaMove )
moveMeFlat( deltaMove );
if ( deltaAngle )
{
angle += deltaAngle;
orientMe( angle );
}

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

/* *Desenha o chao */
glColor3f( 0.9f, 0.9f, 0.9f );
glBegin( GL_QUADS );
glVertex3f( -100.0f, 0.0f, -100.0f );
glVertex3f( -100.0f, 0.0f, 100.0f );
glVertex3f( 100.0f, 0.0f, 100.0f );
glVertex3f( 100.0f, 0.0f, -100.0f );
glEnd();

/* *Desenha o Boneco */
glPushMatrix();
glCallList( lista_boneco );
glPopMatrix();
glutSwapBuffers();
}

void pressKey( int key, int x, int y )
{
switch( key )
{
case GLUT_KEY_LEFT :
deltaAngle = -0.01f;
break;
case GLUT_KEY_RIGHT :
deltaAngle = 0.01f;
break;
case GLUT_KEY_UP :
deltaMove = 1;
break;
case GLUT_KEY_DOWN :
deltaMove = -1;
break;
}
}

void releaseKey( int key, int x, int y )
{
switch(key)
{
case GLUT_KEY_LEFT :
case GLUT_KEY_RIGHT :
deltaAngle = 0.0f;
break;
case GLUT_KEY_UP :
case GLUT_KEY_DOWN :
deltaMove = 0;
break;
}
}

void processNormalKeys( unsigned char key, int x, int y )
{
switch( key )
{
case 27: /* *Tecla ESC */
exit(0);
break;
case 'd':
anguloPernaDireita += 5;
break;

}
}

void mouse( int button, int state, int x, int y )
{
//if (button == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN) {
}

static void idle( void )
{
glutPostRedisplay();
}

const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{
glutInit( &amp;argc, argv );
glutInitWindowSize( 800, 600 );
glutInitWindowPosition( 150, 150 );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );

glutCreateWindow( "Animacao" );

initScene();

glutIgnoreKeyRepeat( 1 );
glutKeyboardFunc( processNormalKeys );
glutSpecialFunc( pressKey );
glutSpecialUpFunc( releaseKey );

// glutMouseFunc(mouse);

glutDisplayFunc( renderScene );
glutIdleFunc( idle );

glutReshapeFunc( resize );

glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

glutMainLoop();

return EXIT_SUCCESS;
}

Thanks.
Hope for Answer.