1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include <stdio.h>
#include <stdlib.h>
#include <gl/glut.h>
typedef struct point{
float x; float y;float z;
};point pl;
point zero={-0.5, -0.5, 0.5}, un={0.5, -0.5, 0.5},deux={-0.5, 0.5 ,0.5},
trois={0.5, 0.5, 0.5},quatre={-0.5, 0.5 ,-0.5},cinq={0.5, 0.5, -0.5},six={-0.5, -0.5 ,-0.5},sept={0.5, -0.5, -0.5};
// variables du mouvement
static int scaling = 0;
static int translating = 0;
static int rotating = 0;
static float scale = 1.0;
static float center[3] = { 0.0, 0.0, 0.0 };
static float rotation[3] = { 0.0, 0.0, 0.0 };
static float translation[3] = { 0.0, 0.0, -4.0 };
//variables de la camera
float ex=1,ey=1,ez=1;
void face(point a,point b,point c,point d){
glBegin(GL_POLYGON);
glVertex3f(a.x,a.y,a.z);
glVertex3f(b.x,b.y,b.z);
glVertex3f(c.x,c.y,c.z);
glVertex3f(d.x,d.y,d.z);
glEnd();
}
void drawCube(){
// Setup viewing transformation
glLoadIdentity(); glScalef(scale, scale, scale); glTranslatef(translation[0], translation[1], 0.0);
// Set projection transformation
glMatrixMode(GL_PROJECTION); glLoadIdentity();
gluPerspective(45.0, 1, 0.1, 100.0);
// Set camera transformation
glMatrixMode(GL_MODELVIEW);glLoadIdentity();
glTranslatef(translation[0], translation[1], translation[2]);
glScalef(scale, scale, scale);glRotatef(rotation[0], 1.0, 0.0, 0.0);
glRotatef(rotation[1], 0.0, 1.0, 0.0);glRotatef(rotation[2], 0.0, 0.0, 1.0);
glTranslatef(-center[0], -center[1], -center[2]);
gluLookAt((GLfloat)ex,(GLfloat)ey,(GLfloat)ez,0,0,0,0,1,0);
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,.0,.0);face(zero,un,sept,six);
glColor3f(.0,1.0,.0);face(sept,cinq,quatre,six);
glColor3f(.0,.0,1.0);face(cinq,quatre,deux,trois);
glColor3f(1.0,1.0,1.0);face(deux,trois,un,zero);
glColor3f(1.0,1.0,.0);face(un,sept,cinq,trois);
glColor3f(.0,1.0,1.0);face(zero,six,quatre,deux);
glPopMatrix();
glFlush();
/********************
traitement de glrearpixel();
********************/
}
void init(){glClearColor(.0,.0,.0,.0);}
void clavier(unsigned char k,int x,int y){
switch(k){
case 'q':{ exit(0);break;}
case 'a': {ex =7;glutPostRedisplay();break;}
case 'z': {ex =-7;glutPostRedisplay();break;}
case 'e': {ey =5;glutPostRedisplay();break;}
case 'r': {ey =-5;glutPostRedisplay();break;}
case 't': {ez =6;glutPostRedisplay();break;}
case 'y': {ez =-6;glutPostRedisplay();break;}
//case 27: exit(0);break;
}
}
void main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(450,450);
glutInitWindowPosition(300,100);
glutCreateWindow("azer");
init();
glutDisplayFunc(drawCube);
glutKeyboardFunc(clavier);
glutMainLoop();
} |
Partager