HI,
I’m brand new with OpenGL. I’m trying to add a finger to a forearm using glPushMatrix and glPopMatrix but I can’t get it working well. Can somebody help me? Here’s my code so far. Actually…the arm was already made…I just have to add fingers.
#include <GL/glut.h>
#include <stdlib.h>
static int epaule = 20, coude = 20, doigt1 = 20, doigt2 = 20, doigt3 = 20;
GLfloat light_diffuse[] = {1.0, 1.0, 0.0, 0.0};/Initialiser la couleur de la lumière/
GLfloat light_position[] = {0.0, 0.0, 1.50, 1.0};/Initialiser la position de la lumière/
GLUquadricObj *qobj;
void init(void)
{
glClearColor (0.5, 0.5, 0.5, 0.0);
glShadeModel (GL_SMOOTH);
}
void display(void) /* Créer un arbre d’objets */
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);;
glPushMatrix();
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) epaule, 0.0, 1.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 0.40);
glEnable(GL_LIGHTING);
glutSolidCube (1.0);
glDisable(GL_LIGHTING);
glPopMatrix();
glPushMatrix();
glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) coude, 1.0, 1.0, 0.0);
glTranslatef (0.70, 0.0, 0.0);
glScalef (2.0, 0.4, 0.4);
glEnable(GL_LIGHTING);
glutSolidCube (0.70);
glDisable(GL_LIGHTING);
glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) doigt1, 0.0, 1.0, 0.0);
glTranslatef (-0.5,0.5, 0.0);
glScalef (1.0, 1.0, 1.0);
glEnable(GL_LIGHTING);
glutSolidCube (0.30);
glDisable(GL_LIGHTING);
glPopMatrix();
/*
//Doigt #2
glPushMatrix();
glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) doigt2, 1.0, 0.0, 1.0);
glTranslatef (1.70, 0.0, 0.0);
glScalef (3.0, 0.4, 0.4);
glEnable(GL_LIGHTING);
glutSolidCube (0.20);
glDisable(GL_LIGHTING);
glPopMatrix();
//Doigt #3
glPushMatrix();
glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) doigt3, 1.0, 0.0, 1.0);
glTranslatef (1.70, -0.17, 0.0);
glScalef (3.0, 0.4, 0.4);
glEnable(GL_LIGHTING);
glutSolidCube (0.20);
glDisable(GL_LIGHTING);
glPopMatrix();*/
glPopMatrix();
glutSwapBuffers();
glFlush();
}
void reshape (int w, int h) /* Projection */
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(75.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -7.0);
}
/Transformations selon les commandes du clavier/
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case ‘e’:
epaule = (epaule + 5) % 360;
glutPostRedisplay();
break;
case 'E':
epaule = (epaule - 5) % 360;
glutPostRedisplay();
break;
case 'c':
coude = (coude + 5) % 360;
glutPostRedisplay();
break;
case 'C':
coude = (coude - 5) % 360;
glutPostRedisplay();
break;
case 'x':
doigt1 = (doigt1 - 5) % 360;
glutPostRedisplay();
break;
case 'X':
doigt1 = (doigt1 + 5) % 360;
glutPostRedisplay();
break;
case 'y':
doigt2 = (doigt2- 5) % 360;
glutPostRedisplay();
break;
case 'Y':
doigt2 = (doigt2 + 5) % 360;
glutPostRedisplay();
break;
case 'z':
doigt3 = (doigt3 - 5) % 360;
glutPostRedisplay();
break;
case 'Z':
doigt3 = (doigt3 + 5) % 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (“Robot”);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); /définir et activer la lumière 0/
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}