Introduction à opengl

Contenu du snippet

petit programme très simple implementant quelque fonctions de opengl, pour débuter ds le domaine de la 3D.

Source / Exemple :


//main.c

/***************************************************************** 
/  BoBRobert
/***************************************************************** 

/*Une petite "démo" pour commencer 
Bon ttes les fct ne st pas utils mais le but est de faire un moteur d'affichage réutilisable*/ 
//#include <windows.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <GL/glut.h> 
#include <math.h> 
#include <time.h> 

//Mes objets perso 
#include "formes.h" 
//#include "readconf.h" 

void affichage(); 
void clavier(unsigned char touche, int x, int y); 
void reshape(int, int); 
void mouse(int,int,int,int); 
void mousemotion(int,int); 

char presseL, presseD;/*varible globale pour l'état de la sourie*/ 

int px,py,pz; 

/*Structure permettant la définition d'un point (ca peut kan meme servir lol)*/ 
typedef struct{ 
   float x; 
   float y; 
   float z; 
   float r; 
   float g; 
   float b; 
}point; 

/*fonction d'initialisation de la fenêtre de rendu*/ 
/*paramètres en entrée : 
          - Position de la fenêtre x,y 
         - taille de la fenêtre x,y 
         - nom de la fenêtre (string) max 19 caractères 

  • /
void Glut_Window(int xPos,int yPos,int xTaille,int yTaille,char WinName[20]){ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowPosition(xPos,yPos);/*position de la fenetre*/ glutInitWindowSize(xTaille,yTaille);/*taille de la fenetre*/ glutCreateWindow(WinName);/*création de la fenetre*/ } /*fonction de gestion du redimentionnement de la fenetre de rendu(pas super o point)*/ void reshape(int xWin,int yWin){ if(xWin<yWin) glViewport(0,(yWin-xWin)/2,xWin,xWin); else glViewport((xWin-yWin)/2,0,yWin,yWin); } /*Paramètres d'initialisation de la belle scène*/ void Glut_Init_Screen(){ glClearColor(0.0,1.0,1.0,0.0);//efface tt glColor3f(1.0,1.0,1.0);//initialise la couleure de font glPointSize(2.0);//taille des points glEnable(GL_DEPTH_TEST); } /*gère les entrées sortie*/ void Glut_IO(){ glutDisplayFunc(affichage);//No Comment lol glutKeyboardFunc(clavier); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMotionFunc(mousemotion); } /*gestion des évennement clavier*/ void clavier(unsigned char touche,int x,int y) { switch (touche){ case 'p': /*carre plein */ glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glutPostRedisplay(); break; case 'f': /*fil de fer */ glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glutPostRedisplay(); break; case 's': /* sommets*/ glPolygonMode(GL_FRONT_AND_BACK,GL_POINT); glutPostRedisplay(); break; /*case 'z': px++; glutPostRedisplay(); break; case 'd': px--; glutPostRedisplay(); break;*/ case 'q' : /*quitte */ exit(0); } } /*fonction déterminant l'état des boutons de la sourie*/ void mouse(int button, int stat, int x, int y){ //pour le bouton gauche //détermine si le bouton gauche est pressé if(button == GLUT_LEFT_BUTTON &stat == GLUT_DOWN){ presseL=1;//état du bouton xold=x; yold=y; } //si relaché if(button ==GLUT_LEFT_BUTTON &stat == GLUT_UP) presseL=0; //pour le bouton droit } void mousemotion(int x, int y){ if(presseL==1){ anglex=anglex+(x-xold); angley=angley+(y-yold); glutPostRedisplay(); } xold=x; yold=y; } /*fct d'affichage de la scène*/ void affichage() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Coeur3D(); //glTranslatef(-px,0.0,-pz); cube(); //Disque(1,1,1,1); //glutWireSphere(1,20,20); glFlush(); glutSwapBuffers(); } //Le Bo main int main(int argc, char **argv){ printf("Welcome in THE opengl World\n"); //read_conf(); glutInit(&argc,argv);/*initialise GLUT*/ Glut_Window(200,200,500,500,"OpenGL Power!!!!!!");/*initialisation de la fenetre*/ Glut_Init_Screen();/*paramètres de la scène*/ Glut_IO();/*entrées sorties*/ glutMainLoop(); return 0; } //formes.h a mettre ds un autre ficher /*lib de ttes les formes (lol)*/ int anglex, angley, x, y, xold, yold;//variables globales de gestion de la position de la sourie /*définition d'objets divers et variée*/ void rotation(){ glLoadIdentity(); glRotatef(-angley,1.1,0.0,0.0); glRotatef(-anglex,0.0,1.0,0.0); } //carré 2D void Carre2D(){ //carré glBegin(GL_POLYGON); glColor3f(1.0,0.0,0.0); glVertex2f(-0.5,-0.5); glColor3f(0.0,1.0,0.0); glVertex2f(0.5,-0.5); glColor3f(0.0,0.0,1.0); glVertex2f(0.5,0.5); glColor3f(1.0,1.0,1.0); glVertex2f(-0.5,0.5); glEnd(); } //cube void cube(){ rotation(); glBegin(GL_POLYGON); glColor3f(1.0,0.0,0.0); glVertex3f(-0.5,-0.5,-0.5); glColor3f(0.0,1.0,0.0); glVertex3f(0.5,-0.5,-0.5); glColor3f(0.0,0.0,1.0); glVertex3f(0.5,0.5,-0.5); glColor3f(1.0,1.0,1.0); glVertex3f(-0.5,0.5,-0.5); glEnd(); glBegin(GL_POLYGON); glColor3f(1.0,0.0,0.0); glVertex3f(-0.5,-0.5,0.5); glColor3f(0.0,1.0,0.0); glVertex3f(0.5,-0.5,0.5); glColor3f(0.0,0.0,1.0); glVertex3f(0.5,0.5,0.5); glColor3f(1.0,1.0,1.0); glVertex3f(-0.5,0.5,0.5); glEnd(); glBegin(GL_POLYGON); glColor3f(1.0,0.0,0.0); glVertex3f(0.5,0.5,-0.5); glColor3f(0.0,1.0,0.0); glVertex3f(-0.5,0.5,-0.5); glColor3f(0.0,0.0,1.0); glVertex3f(-0.5,0.5,0.5); glColor3f(1.0,1.0,1.0); glVertex3f(0.5,0.5,0.5); glEnd(); glBegin(GL_POLYGON); glColor3f(1.0,0.0,0.0); glVertex3f(0.5,-0.5,-0.5); glColor3f(0.0,1.0,0.0); glVertex3f(-0.5,-0.5,-0.5); glColor3f(0.0,0.0,1.0); glVertex3f(-0.5,-0.5,0.5); glColor3f(1.0,1.0,1.0); glVertex3f(0.5,-0.5,0.5); glEnd(); glBegin(GL_POLYGON); glColor3f(1.0,0.0,0.0); glVertex3f(0.5,-0.5,-0.5); glColor3f(0.0,1.0,0.0); glVertex3f(0.5,0.5,-0.5); glColor3f(0.0,0.0,1.0); glVertex3f(0.5,0.5,0.5); glColor3f(1.0,1.0,1.0); glVertex3f(0.5,-0.5,0.5); glEnd(); glBegin(GL_POLYGON); glColor3f(1.0,0.0,0.0); glVertex3f(-0.5,-0.5,-0.5); glColor3f(0.0,1.0,0.0); glVertex3f(-0.5,0.5,-0.5); glColor3f(0.0,0.0,1.0); glVertex3f(-0.5,0.5,0.5); glColor3f(1.0,1.0,1.0); glVertex3f(-0.5,-0.5,0.5); glEnd(); } //disque void Disque(int rayon,int R,int G, int B){ int i; glBegin(GL_POLYGON); for(i=0;i<361;i++){ glColor3f(R,G,B); glVertex3f(rayon*cos(i*3.14/180),rayon*sin(i*3.14/180),0); } glEnd(); } //Arc de disque void ArcDisque(int rayon,int debut,int fin,int R,int G, int B){ int i; glBegin(GL_POLYGON); glVertex3f(0,0,0); for(i=debut;i<fin;i++){ glColor3f(R,G,B); glVertex3f(rayon*cos(i*3.14/180),rayon*sin(i*3.14/180),0); } glEnd(); } //coeur en 2D(o comme il est bo :$) void Coeur2D(){ glBegin(GL_POLYGON); //droite glColor3f(1.0,0.0,0.0); glVertex2f(0.0,-0.6);//a0 glColor3f(1.0,0.0,0.0); glVertex2f(0.1,-0.5);//a1 glColor3f(1.0,0.0,0.0); glVertex2f(0.2,-0.4);//a2 glColor3f(1.0,0.0,0.0); glVertex2f(0.3,-0.25);//a3 glColor3f(1.0,0.0,0.0); glVertex2f(0.4,-0.05);//a4 glColor3f(1.0,0.0,0.0); glVertex2f(0.5,0.15);//a5 glColor3f(1.0,0.0,0.0); glVertex2f(0.55,0.25);//a6 glColor3f(1.0,0.0,0.0); glVertex2f(0.5,0.35);//a7 glColor3f(1.0,0.0,0.0); glVertex2f(0.4,0.5);//a8 glColor3f(1.0,0.0,0.0); glVertex2f(0.25,0.55);//a9 glColor3f(1.0,0.0,0.0); glVertex2f(0.1,0.5);//a10 glColor3f(1.0,0.0,0.0); glVertex2f(0.05,0.45);//a11 glColor3f(1.0,0.0,0.0); glVertex2f(0.0,0.3);//a12 //gauche glColor3f(1.0,0.0,0.0); glVertex2f(-0.0,-0.6);//-a0 glColor3f(1.0,0.0,0.0); glVertex2f(-0.1,-0.5);//-a1 glColor3f(1.0,0.0,0.0); glVertex2f(-0.2,-0.4);//-a2 glColor3f(1.0,0.0,0.0); glVertex2f(-0.3,-0.25);//-a3 glColor3f(1.0,0.0,0.0); glVertex2f(-0.4,-0.05);//-a4 glColor3f(1.0,0.0,0.0); glVertex2f(-0.5,0.15);//-a5 glColor3f(1.0,0.0,0.0); glVertex2f(-0.55,0.25);//-a6 glColor3f(1.0,0.0,0.0); glVertex2f(-0.5,0.35);//-a7 glColor3f(1.0,0.0,0.0); glVertex2f(-0.4,0.5);//-a8 glColor3f(1.0,0.0,0.0); glVertex2f(-0.25,0.55);//-a9 glColor3f(1.0,0.0,0.0); glVertex2f(-0.1,0.5);//-a10 glColor3f(1.0,0.0,0.0); glVertex2f(-0.05,0.45);//-a11 glColor3f(1.0,0.0,0.0); glVertex2f(-0.0,0.3);//-a12 glEnd(); } //coeur en 3D encore plus bo void Coeur3D(){ //annimation rotation(); glBegin(GL_POLYGON); //droite glColor3f(1.0,1.0,0.0); glVertex3f(0.0,-0.6,0.25);//a0 glColor3f(1.0,1.0,0.0); glVertex3f(0.1,-0.5,0.25);//a1 glColor3f(1.0,1.0,0.0); glVertex3f(0.2,-0.4,0.25);//a2 glColor3f(1.0,0.0,0.0); glVertex3f(0.3,-0.25,0.25);//a3 glColor3f(1.0,0.0,0.0); glVertex3f(0.4,-0.05,0.25);//a4 glColor3f(1.0,0.0,0.0); glVertex3f(0.5,0.15,0.25);//a5 glColor3f(1.0,0.0,0.0); glVertex3f(0.55,0.25,0.25);//a6 glColor3f(1.0,0.0,0.0); glVertex3f(0.5,0.35,0.25);//a7 glColor3f(1.0,0.0,0.0); glVertex3f(0.4,0.5,0.25);//a8 glColor3f(1.0,0.0,0.0); glVertex3f(0.25,0.55,0.25);//a9 glColor3f(1.0,0.0,0.0); glVertex3f(0.1,0.5,0.25);//a10 glColor3f(1.0,0.0,0.0); glVertex3f(0.05,0.45,0.25);//a11 glColor3f(1.0,0.0,0.0); glVertex3f(0.0,0.3,0.25);//a12 //gauche glColor3f(1.0,1.0,0.0); glVertex3f(-0.0,-0.6,0.25);//-a0 glColor3f(1.0,1.0,0.0); glVertex3f(-0.1,-0.5,0.25);//-a1 glColor3f(1.0,1.0,0.0); glVertex3f(-0.2,-0.4,0.25);//-a2 glColor3f(1.0,0.0,0.0); glVertex3f(-0.3,-0.25,0.25);//-a3 glColor3f(1.0,0.0,0.0); glVertex3f(-0.4,-0.05,0.25);//-a4 glColor3f(1.0,0.0,0.0); glVertex3f(-0.5,0.15,0.25);//-a5 glColor3f(1.0,0.0,0.0); glVertex3f(-0.55,0.25,0.25);//-a6 glColor3f(1.0,0.0,0.0); glVertex3f(-0.5,0.35,0.25);//-a7 glColor3f(1.0,0.0,0.0); glVertex3f(-0.4,0.5,0.25);//-a8 glColor3f(1.0,0.0,0.0); glVertex3f(-0.25,0.55,0.25);//-a9 glColor3f(1.0,0.0,0.0); glVertex3f(-0.1,0.5,0.25);//-a10 glColor3f(1.0,0.0,0.0); glVertex3f(-0.05,0.45,0.25);//-a11 glColor3f(1.0,0.0,0.0); glVertex3f(-0.0,0.3,0.25);//-a12 glEnd(); glBegin(GL_POLYGON); //droite glColor3f(1.0,1.0,0.0); glVertex3f(0.0,-0.6,-0.25);//a0 glColor3f(1.0,1.0,0.0); glVertex3f(0.1,-0.5,-0.25);//a1 glColor3f(1.0,1.0,0.0); glVertex3f(0.2,-0.4,-0.25);//a2 glColor3f(1.0,0.0,0.0); glVertex3f(0.3,-0.25,-0.25);//a3 glColor3f(1.0,0.0,0.0); glVertex3f(0.4,-0.05,-0.25);//a4 glColor3f(1.0,0.0,0.0); glVertex3f(0.5,0.15,-0.25);//a5 glColor3f(1.0,0.0,0.0); glVertex3f(0.55,0.25,-0.25);//a6 glColor3f(1.0,0.0,0.0); glVertex3f(0.5,0.35,-0.25);//a7 glColor3f(1.0,0.0,0.0); glVertex3f(0.4,0.5,-0.25);//a8 glColor3f(1.0,0.0,0.0); glVertex3f(0.25,0.55,-0.25);//a9 glColor3f(1.0,0.0,0.0); glVertex3f(0.1,0.5,-0.25);//a10 glColor3f(1.0,0.0,0.0); glVertex3f(0.05,0.45,-0.25);//a11 glColor3f(1.0,0.0,0.0); glVertex3f(0.0,0.3,-0.25);//a12 //gauche glColor3f(1.0,1.0,0.0); glVertex3f(-0.0,-0.6,-0.25);//-a0 glColor3f(1.0,1.0,0.0); glVertex3f(-0.1,-0.5,-0.25);//-a1 glColor3f(1.0,1.0,0.0); glVertex3f(-0.2,-0.4,-0.25);//-a2 glColor3f(1.0,0.0,0.0); glVertex3f(-0.3,-0.25,-0.25);//-a3 glColor3f(1.0,0.0,0.0); glVertex3f(-0.4,-0.05,-0.25);//-a4 glColor3f(1.0,0.0,0.0); glVertex3f(-0.5,0.15,-0.25);//-a5 glColor3f(1.0,0.0,0.0); glVertex3f(-0.55,0.25,-0.25);//-a6 glColor3f(1.0,0.0,0.0); glVertex3f(-0.5,0.35,-0.25);//-a7 glColor3f(1.0,0.0,0.0); glVertex3f(-0.4,0.5,-0.25);//-a8 glColor3f(1.0,0.0,0.0); glVertex3f(-0.25,0.55,-0.25);//-a9 glColor3f(1.0,0.0,0.0); glVertex3f(-0.1,0.5,-0.25);//-a10 glColor3f(1.0,0.0,0.0); glVertex3f(-0.05,0.45,-0.25);//-a11 glColor3f(1.0,0.0,0.0); glVertex3f(-0.0,0.3,-0.25);//-a12 glEnd(); }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.