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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| #include <iostream>
#include <string>
#include "sdlglutils.h"
#include <GL/glu.h>
GLfloat lightAmbient[] = {0.5, 0.5, 0.5, 1.f };
GLfloat lightDiffuse[] = {1, 1, 1, 1.f };
GLfloat lightPosition[] = {0.f, 0.f,10 };
GLuint textureCaisseCote;
GLuint textureCaisseFace;
GLuint textureSol;
double angle1 = 0;
double angle2 = 0;
double fovy = 0;
void dessiner_scene()
{
// Vider l'écran.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Placer la camera.
gluLookAt(2+fovy, 1+fovy, 3+fovy, 0, 0, 0, 0, 0, 1);
glRotated(angle1, 1, 0, 0);
glRotated(angle2, 0, 0, 1);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
// Sol.
glBindTexture(GL_TEXTURE_2D, textureSol);
glBegin(GL_QUADS);
glNormal3f(0, 0, -1);
glTexCoord2d(0, 0); glVertex3d(5, -5, -0.5);
glTexCoord2d(5, 0); glVertex3d(5, 5, -0.5);
glTexCoord2d(5, 5); glVertex3d(-5, 5, -0.5);
glTexCoord2d(0, 5); glVertex3d(-5, -5, -0.5);
glEnd();
glBindTexture(GL_TEXTURE_2D, textureCaisseFace);
glBegin(GL_QUADS);
// Face avant.
glNormal3f(1, 0, 0);
glTexCoord2d(0, 0); glVertex3d(1, -0.5, -0.5);
glTexCoord2d(1, 0); glVertex3d(1, 0.5, -0.5);
glTexCoord2d(1, 1); glVertex3d(1, 0.5, 0.5);
glTexCoord2d(0, 1); glVertex3d(1, -0.5, 0.5);
// Face arriere.
glNormal3f(-1, 0, 0);
glTexCoord2d(0, 0); glVertex3d(-1, 0.5, -0.5);
glTexCoord2d(1, 0); glVertex3d(-1, -0.5, -0.5);
glTexCoord2d(1, 1); glVertex3d(-1, -0.5, 0.5);
glTexCoord2d(0, 1); glVertex3d(-1, 0.5, 0.5);
glEnd();
glBindTexture(GL_TEXTURE_2D, textureCaisseCote);
glBegin(GL_QUADS);
// Cote Droit.
glNormal3f(0, 1, 0);
glTexCoord2d(0, 0); glVertex3d(-1, 0.5, -0.5);
glTexCoord2d(1, 0); glVertex3d(1, 0.5, -0.5);
glTexCoord2d(1, 1); glVertex3d(1, 0.5, 0.5);
glTexCoord2d(0, 1); glVertex3d(-1, 0.5, 0.5);
// Cote Gauche.
glNormal3f(0, -1, 0);
glTexCoord2d(0, 0); glVertex3d(-1, -0.5, -0.5);
glTexCoord2d(1, 0); glVertex3d(1, -0.5, -0.5);
glTexCoord2d(1, 1); glVertex3d(1, -0.5, 0.5);
glTexCoord2d(0, 1); glVertex3d(-1, -0.5, 0.5);
// Cote Haut.
glNormal3f(0, 0, 1);
glTexCoord2d(0, 0); glVertex3d(1, 0.5, 0.5);
glTexCoord2d(1, 0); glVertex3d(-1, 0.5, 0.5);
glTexCoord2d(1, 1); glVertex3d(-1, -0.5, 0.5);
glTexCoord2d(0, 1); glVertex3d(1, -0.5, 0.5);
glEnd();
// Proceder à l'affichage.
glFlush();
SDL_GL_SwapBuffers();
}
int main(int argc, char** argv)
{
// Initialisation de la SDL.
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "Error - Impossible d'initialiser la SDL." << std::endl;
return EXIT_FAILURE;
}
// Parametrage de la fenêtre.
SDL_WM_SetCaption("OpenGL -- Caisse", NULL);
// Ouverture de la fenêtre avec un contexte OpenGL.
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
if (screen == NULL)
{
std::cerr << "Error - " << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
// Parametrage de la matrice de projection.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, (double)(640/480), 1, 1000);
// Activation du texturage et de la profondeur.
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
// Chargement de la texture.
textureCaisseCote = loadTexture("crate06_side_top.jpg");
textureCaisseFace = loadTexture("crate06_front.jpg");
textureSol = loadTexture("sol.jpg");
SDL_Event event;
bool running = true;
SDL_EnableKeyRepeat(10, 10);
while (running)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_LEFT)
angle1--;
if (event.key.keysym.sym == SDLK_RIGHT)
angle1++;
if (event.key.keysym.sym == SDLK_UP)
angle2++;
if (event.key.keysym.sym == SDLK_DOWN)
angle2--;
if (event.key.keysym.sym == SDLK_SPACE)
fovy += 0.1;
if (event.key.keysym.sym == SDLK_ESCAPE)
fovy -= 0.1;
break;
}
dessiner_scene();
}
}
// Fermer SDL.
SDL_Quit();
return EXIT_SUCCESS;
} |
Partager