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
|
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <iostream>
using namespace std;
SDL_Surface* ecran;
GLuint tex;
SDL_Surface* texture;
bool init();
void reshape(int, int);
void display();
int main(int argc, char** argv)
{
if (!init())
return -1;
SDL_Event event;
reshape(640, 480);
while (true)
{
SDL_PollEvent(&event);
if (event.type == SDL_QUIT)
{
SDL_Quit();
exit(0);
}
display();
}
return 0;
}
bool init()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "Impossible de charger SDL." << endl;
return false;
}
atexit(SDL_Quit);
if ((ecran = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_HWSURFACE)) == NULL)
{
cout << "Impossible d'initialiser l'ecran." << endl;
return false;
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
texture = SDL_LoadBMP("dice.bmp");
glEnable(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture->w, texture->h, 0, GL_BGR, GL_UNSIGNED_BYTE, texture->pixels);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
return true;
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glTexCoord2i(0, 0);
glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2i(1, 0);
glVertex3f(1.0, -1.0, 0.0);
glTexCoord2i(1, 1);
glVertex3f(1.0, 1.0, 0.0);
glTexCoord2i(0, 1);
glVertex3f(-1.0, 1.0, 0.0);
glEnd();
SDL_GL_SwapBuffers();
} |
Partager