Bonjour, en fait j'aurais un petit soucis, et je crois que mon problème ne doit pas être posé tous les jours...
En fait je cherche à crée un programme qui affiche un cube qui tourne sous OpenGL (je m'entraine pour l'instant...).
Alors du coup faut que je fasse une animation, et je souhaiterai rappeler la fonction dessiner() toutes les 1000/60 milisecondes, pour ce au lieu de crée un timer qui met en pause ma fonction, je préfère utiliser SDL_AddTimer() pour rappeler la fonction dessin()...
main.h
main.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 #ifndef _MAIN_ #define _MAIN_ #define LARGEUR_ECRAN 800 #define HAUTEUR_ECRAN 600 #endif
objets.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <iostream> #include <SDL/SDL.h> #include <GL/gl.h> #include <GL/glu.h> #include "objets.h" #include "main.h" using namespace std; int main(int argc, char *argv[]) { SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER); //on initialise la vide et les timers en SDL... atexit(SDL_Quit); SDL_Surface *ecran = NULL; double Angle=0; //on crée un variable angle ecran = SDL_SetVideoMode(LARGEUR_ECRAN, HAUTEUR_ECRAN, 32, SDL_OPENGL); //on initialise l'écran glEnable(GL_DEPTH_TEST); //on initialise le Z-Buffer glMatrixMode(GL_PROJECTION); //on se place dans la bonne matrice glLoadIdentity(); //on initialise la matrice gluPerspective(70, (double)(LARGEUR_ECRAN/HAUTEUR_ECRAN), 1, 1000); //on initialise la camera SDL_TimerID timer = SDL_AddTimer(1000/60, dessin, &Angle); //on crée le callback /*On attend l'évènement soit on appuie sur la croix, sur sur echap*/ SDL_Event event; while(1) { SDL_PollEvent(&event); switch(event.type) { case SDL_QUIT: exit(EXIT_SUCCESS); break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: exit(EXIT_SUCCESS); break; default: break; } break; } Angle++; //dessin(3*Angle, 7*Angle); } /*On vide la mémoire*/ SDL_RemoveTimer(timer); SDL_Quit(); return EXIT_SUCCESS; }
objets.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 #ifndef _OBJETS_ #define _OBJETS_ #include "main.h" #define COTE 1 //sert à dessiner le cube #define TAILLE 1 //sert à dessiner le cube Uint32 dessin(Uint32, void*); #endif
Et voila mon MakeFile:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <stdio.h> #include <stdlib.h> #include <SDL/SDL.h> #include <GL/gl.h> #include <GL/glu.h> #include "objets.h" Uint32 dessin(Uint32 interval, void* parametre) { double* Angle=(double*)parametre; //On prend le paramètre, et on le place dans un variable adaptée glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //on vide le buffer glMatrixMode( GL_MODELVIEW ); //on se place dans la bonne matrice glLoadIdentity( ); //On réinitialise la matrice gluLookAt(4,3,2,0,0,0,0,0,1); //on place la camera glRotated(*AngleZ, 0, 0, 1); //on fait tourner le cube selon l'axe Z glRotated(*AngleY, 1, 0, 0); //on fait tourner le cube selon l'axe X /*On dessine le cube*/ glBegin(GL_QUADS); glColor3ub(255,0,0); //face +x glVertex3d( COTE, TAILLE, TAILLE); glVertex3d( COTE,-TAILLE, TAILLE); glVertex3d( COTE,-TAILLE,-TAILLE); glVertex3d( COTE, TAILLE,-TAILLE); glColor3ub(0,255,0); //face +y glVertex3d( TAILLE, COTE, TAILLE); glVertex3d(-TAILLE, COTE, TAILLE); glVertex3d(-TAILLE, COTE,-TAILLE); glVertex3d( TAILLE, COTE,-TAILLE); glColor3ub(0,0,255); //face -x glVertex3d(-COTE, TAILLE, TAILLE); glVertex3d(-COTE,-TAILLE, TAILLE); glVertex3d(-COTE,-TAILLE,-TAILLE); glVertex3d(-COTE, TAILLE,-TAILLE); glColor3ub(255,255,0); //face +z glVertex3d( TAILLE, TAILLE, COTE); glVertex3d(-TAILLE, TAILLE, COTE); glVertex3d(-TAILLE,-TAILLE, COTE); glVertex3d( TAILLE,-TAILLE, COTE); glColor3ub(0,255,255); //face -y glVertex3d( TAILLE,-COTE, TAILLE); glVertex3d(-TAILLE,-COTE, TAILLE); glVertex3d(-TAILLE,-COTE,-TAILLE); glVertex3d( TAILLE,-COTE,-TAILLE); glColor3ub(255,0,255); //face -z glVertex3d( TAILLE, TAILLE,-COTE); glVertex3d(-TAILLE, TAILLE,-COTE); glVertex3d(-TAILLE,-TAILLE,-COTE); glVertex3d( TAILLE,-TAILLE,-COTE); glEnd(); /*Puis on rafraîchis l'écran*/ glFlush(); SDL_GL_SwapBuffers(); return interval; }
Cependant tout ce qui s'affiche, c'est un écran noir...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 OBJECTS = main.o objets.o install: all clean all: $(OBJECTS) g++ -Wall -o main $^ -lmingw32 -lopengl32 -lglu32 -lSDL -lSDLmain -lSDL.dll -LC:/SDL/lib main.o: main.cpp main.h objets.h g++ -Wall -o $@ -c $< -IC:/SDL/include objets.o: objets.cpp main.h objets.h g++ -Wall -o $@ -c $< -IC:/SDL/include clean: del *.o *~
Je comprend pas, la fonction callback doit rappeler dessin toutes les 1000/60 millisecondes, Mais apparemment il ne fait rien , et si je place dessin dans le while ça marche pourtant...
Je comprend pas. Aidez moi SVP...
Merci d'avance...
Partager