PDA

View Full Version : Problem rotating an object



Saulo Okada
10-06-2009, 12:02 PM
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.

Fugitive
10-07-2009, 09:43 AM
I dont meant to sound condescending, but i have three suggestions:

1)state 'exactly' what your problem is, rather than simplying saying you cant do it. Is it giving an error? Are the arms rotating in the wrong direction?

2)Provide a screen-shot if you can.

3)Since this seems to be an elementary problem, post this in the beginners section rather than advanced.

Kind regards,
Fugi

MaxH
10-07-2009, 11:06 AM
As 'Fugitive' says, you haven't told us what happens when you run this program? How close is it to what you want? Do you get a snowman with arms that don't rotate? Exactly what shows up in the graphics window? Anything?

MarkZ3
10-07-2009, 12:31 PM
http://mark.omnialabs.net/snow_man_lol.png

I compiled it and it looks like this. You are rotating the sphere first, which doesn't do much since its a sphere, then it's scaled. I think you mean to scale then rotate:

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

Also, if you ask another question, it would be faster if you added a little bit of english comments in your code where you think the problem is.

MaxH
10-08-2009, 11:50 AM
You have even more basic problems than what 'MarkZ3' mentioned. Try printing out the value of 'anguloPernaDireita' just before you call it with glRotate (in the routine 'desenhaBoneco'). You will see that it is zero. In fact, 'desenhaBoneco' is only called once. It is not called when you push the 'd' key. There is a problem with the flow of your code. 'desenhaBoneco' must be directly or indirectly called from 'renderScene'.