Tangrim Des bisous et des nounours ! | Bonjour à vous.
Je m'amuse à apprendre à programmer en opengl (et SDL2) et je bloque un peu.
D'abord, du code:
Code :
- #include <stdio.h>
- #include <stdbool.h>
- #include <gl/gl.h>
- #include <gl/glu.h>
- #include <SDL.h>
- #define WIDTH 640
- #define HEIGHT 480
- SDL_Window* main_window = NULL;
- void resize_window(int width, int height)
- {
- GLfloat ratio;
- if (height == 0) // évite une division par zéro
- {
- height = 1;
- }
- glViewport(0, 0, width, height);
- ratio = (GLfloat)width / height;
- glMatrixMode(GL_PROJECTION); // matrice de projection
- glLoadIdentity(); // remise à 0
- gluPerspective(45.0f, ratio, 0.1f, 100.0f);
- glMatrixMode(GL_MODELVIEW); // Choisir la matrice de modélisation
- glLoadIdentity(); // Remettre a zéro la matrice de modélisation
- }
- void dessine_scene()
- {
- // rotation
- static GLfloat rot_triangle;
- // fps
- static GLint t0 = 0;
- static GLint frames = 0;
- // effaçage
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity(); // remettre à zéro
- // Bouge de 7.0 unités vers l'exterieur de l'écran
- glTranslatef(0.0f, 0.0f, -7.0f);
- // rotation
- glRotatef(rot_triangle, 0.0f, 1.0f, 0.0f);
- glBegin(GL_TRIANGLES);
- glColor3f(1.0f, 0.0f, 0.0f); // rouge
- glVertex3f( 0.0f, 1.0f, 0.0f);
- glVertex3f( -1.0f, -1.0f, 1.0f);
- glVertex3f( 1.0f, -1.0f, 1.0f);
- glColor3f(0.0f, 1.0f, 0.0f); // vert
- glVertex3f( 0.0f, 1.0f, 0.0f);
- glVertex3f( 1.0f, -1.0f, 1.0f);
- glVertex3f( 1.0f, -1.0f, -1.0f);
- glColor3f(0.0f, 0.0f, 1.0f); // bleu
- glVertex3f( 0.0f, 1.0f, 0.0f);
- glVertex3f( 1.0f, -1.0f, -1.0f);
- glVertex3f( -1.0f, -1.0f, -1.0f);
- glColor3f(1.0f, 0.5f, 0.0f); // orange
- glVertex3f( 0.0f, 1.0f, 0.0f);
- glVertex3f( -1.0f, -1.0f, -1.0f);
- glVertex3f( -1.0f, -1.0f, 1.0f);
- glEnd();
- /* Draw it to the screen */
- SDL_GL_SwapWindow(main_window);
- frames++;
- {
- GLint t = SDL_GetTicks();
- if (t - t0 >= 5000)
- {
- GLfloat seconds = (t - t0) / 1000.0;
- GLfloat fps = frames / seconds;
- printf("%d frames in %g seconds = %g FPS\n", frames, seconds, fps);
- t0 = t;
- frames = 0;
- }
- }
- /* move your body */
- rot_triangle += 0.9f;
- }
- int main(int N, char* T[])
- {
- bool quitte = false;
- bool visible = true;
- SDL_GLContext glcontexte = NULL;
- SDL_Event event;
- // init SDL
- if (SDL_Init(SDL_INIT_VIDEO != 0))
- {
- fprintf(stderr, "SDL, c'est plus fort que toi !\n" );
- SDL_Quit();
- return -1;
- }
- // init OpenGL
- glShadeModel(GL_SMOOTH); // ombrage
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // fond noir
- glClearDepth(1.0f); // tampon de profondeur
- glEnable(GL_DEPTH_TEST); // test de pofondeur
- glDepthFunc(GL_LEQUAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // c'est joli tout plein
- // création fenêtre SDL
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
- main_window = SDL_CreateWindow("Hellos world !",
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- WIDTH,
- HEIGHT,
- SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
- if (main_window == NULL)
- {
- fprintf(stdout, "Échec de création de fenêtre SDL (%s)\n", SDL_GetError());
- SDL_Quit();
- return -1;
- }
- glcontexte = SDL_GL_CreateContext(main_window);
- if (glcontexte == NULL)
- {
- fprintf(stdout, "Échec de création de contexte OpenGL (%s)\n", SDL_GetError());
- SDL_Quit();
- return -1;
- }
- // sync buffer swap with monitor's vertical refresh rate
- SDL_GL_SetSwapInterval(1);
- /* resize the initial window */
- resize_window(640, 480);
- while (quitte == false)
- {
- while (SDL_PollEvent(&event))
- {
- switch (event.type)
- {
- case SDL_WINDOWEVENT:
- switch(event.window.event)
- {
- case SDL_WINDOWEVENT_RESIZED:
- /* handle resize event */
- resize_window(event.window.data1, event.window.data2);
- break;
- case SDL_WINDOWEVENT_HIDDEN:
- visible = false;
- break;
- case SDL_WINDOWEVENT_SHOWN:
- visible = true;
- break;
- }
- break;
- case SDL_KEYDOWN:
- if (event.key.keysym.sym == SDLK_ESCAPE)
- quitte = true;
- break;
- case SDL_QUIT:
- quitte = true;
- break;
- default:
- break;
- }
- }
- if (visible == true)
- {
- dessine_scene();
- }
- }
- SDL_DestroyWindow(main_window);
- SDL_GL_DeleteContext(glcontexte);
- return 0;
- (void)N; (void)T;
- }
|
Ça compile sur tous projets incluant SDL2.
Et comme vous voyez, la pyramide est bizarre.
J'ai l'impression que les triangles restent à leur plan d'origine, un problème de Z-BUFFER en gros.
Comment régler ça ?
Merci  Message édité par Tangrim le 29-04-2014 à 14:07:38 ---------------
Des Bisous et des nounours ! | Internet 2025 | Dungeon-Generator
|