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
| #include "GL/gl.h"
#include "GL/glut.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <iostream>
#define WIDTH 640
#define HEIGHT 480
void Display();
void Reshape(int,int);
void Init();
SDL_Surface *fenetre;
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
fenetre = SDL_SetVideoMode(640,480,32, SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE | SDL_RESIZABLE | SDL_HWSURFACE
| SDL_HWACCEL);
Init();
Reshape(640,480);
bool quit = false;
while(!quit)
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_VIDEORESIZE:
Reshape(event.resize.w,event.resize.h);
}
}
Display();
}
return 0;
}
void Display()
{
glClearColor(0,0,0,0); // selectionne la couleur noire (qui est celle par défaut)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(4,3,3,0,0,0,0,1,0);
GLuint texture_name;
GLuint objet;
SDL_Surface * texture;
// ---- Création d'un objet de texture. ------------------------------------------------
glGenTextures (1, & texture_name);
glBindTexture (GL_TEXTURE_2D, texture_name);
// Paramétrage de la texture.
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Chargement du fichier.
texture = IMG_Load ("SDL/small_tux.bmp" );
// Jonction entre OpenGL et SDL.
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, texture->w, texture->h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, texture-> pixels);
// ---- Fin de création de l'objet de texture. -----------------------------------------
// ...
// Création d'un objet par l'utilisation d'une liste d'affichage.
objet = glGenLists (1);
glNewList (objet, GL_COMPILE);
// Avant d'appeler les fonction glVertex..., on indique quelle texture va être associée à l'objet.
// On active l'application de texture...
glEnable (GL_TEXTURE_2D);
// ... et on indique quelle texture utiliser (celle qui a été créée précédemment).
glBindTexture (GL_TEXTURE_2D, texture_name);
// On peut enfin créer un objet (ici un simple carré).
glBegin (GL_QUADS);
glTexCoord2f (0.0, 0.0); glNormal3f (1.0, 1.0, 0.0); glVertex3f (1.0, 1.0, -2.0);
glTexCoord2f (0.0, 5.0); glNormal3f (1.0, 1.0, 0.0); glVertex3f (-1.0, 1.0, -2.0);
glTexCoord2f (5.0, 5.0); glNormal3f (1.0, 1.0, 0.0); glVertex3f (-1.0, -1.0, -2.0);
glTexCoord2f (5.0, 0.0); glNormal3f (1.0, 1.0, 0.0); glVertex3f (1.0, -1.0, -2.0);
glEnd ();
// Fin de l'application de la texture.
glDisable (GL_TEXTURE_2D);
// Fin de la liste d'affichage.
glEndList ();
glEnd();
glCallList(1);
SDL_GL_SwapBuffers();
}
void Reshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION); // Choisit la matrice de projection
glLoadIdentity();
gluPerspective(45.0,(float) w/h,1.,10.);
}
void Init()
{
glShadeModel( GL_SMOOTH );
glClearColor(0.0,0.0,0.0,0.0);
glClearDepth(1.0);
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
} |
Partager