Dépendances (dans l'archive) :
mur.bmp
#include <windows.h> #include <math.h> #include <GL/gl.h> #include <GL/glu.h> #include <SDL/SDL.h> #pragma comment(lib,"sdl.lib") #pragma comment(lib,"sdlmain.lib") #pragma comment(lib, "opengl32.lib" ) #pragma comment(lib, "glu32.lib" ) #define XRESOL 800 #define YRESOL 600 int initGL() { // init glShadeModel(GL_FLAT); glClearColor(0.0f,0.0f,0.0f,0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); // cadrage glViewport(0,0,XRESOL,YRESOL); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,4.0/3.0,0.1f,100.0f); glEnable(GL_TEXTURE_2D); return 1; } int KeyControl(float* pe) { Uint8* ks = SDL_GetKeyState(NULL); SDL_PumpEvents(); if (ks[SDLK_ESCAPE]) return 1; if (ks[SDLK_UP]) (*pe)+=0.01f; if (ks[SDLK_DOWN]) (*pe)-=0.01f; return 0; } int Quad(float x,float y,float z,GLuint texture) { glBindTexture(GL_TEXTURE_2D,texture); glBegin(GL_QUADS); glTexCoord2f(0.0,1.0);glVertex3f(x,y,z); glTexCoord2f(1.0,1.0);glVertex3f(x+0.99,y,z); glTexCoord2f(1.0,0.0);glVertex3f(x+0.99,y+0.99,z); glTexCoord2f(0.0,0.0);glVertex3f(x,y+0.99,z); glEnd(); return 0; } GLuint ParametrerTexture(SDL_Surface* tex32,int mipmap,GLint min,GLint max) { GLuint res; glGenTextures(1,&res); glBindTexture(GL_TEXTURE_2D,res); if (mipmap) gluBuild2DMipmaps(GL_TEXTURE_2D,4, tex32->w, tex32->h,GL_BGRA_EXT,GL_UNSIGNED_BYTE,tex32->pixels); else glTexImage2D(GL_TEXTURE_2D,0,GL_RGB, tex32->w, tex32->h,0,GL_BGRA_EXT, GL_UNSIGNED_BYTE, tex32->pixels); glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER,min); glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER,max); return res; } int InitTextures(GLuint textures[8]) { SDL_Surface* tex = SDL_LoadBMP("mur.bmp"); SDL_Surface* tex32 = SDL_CreateRGBSurface(SDL_SWSURFACE,tex->w,tex->h,32,0,0,0,0); SDL_BlitSurface(tex,NULL,tex32,NULL); SDL_FreeSurface(tex); // bord gauche textures[0] = ParametrerTexture(tex32,0,GL_NEAREST,GL_NEAREST); textures[4] = ParametrerTexture(tex32,1,GL_NEAREST,GL_NEAREST); // bord droit textures[3] = ParametrerTexture(tex32,0,GL_LINEAR,GL_LINEAR); textures[7] = ParametrerTexture(tex32,1,GL_LINEAR,GL_LINEAR); // milieu textures[1] = ParametrerTexture(tex32,1,GL_NEAREST_MIPMAP_NEAREST,GL_NEAREST); textures[2] = ParametrerTexture(tex32,1,GL_NEAREST_MIPMAP_LINEAR,GL_NEAREST); textures[5] = ParametrerTexture(tex32,1,GL_LINEAR_MIPMAP_NEAREST,GL_LINEAR); textures[6] = ParametrerTexture(tex32,1,GL_LINEAR_MIPMAP_LINEAR,GL_LINEAR); SDL_FreeSurface(tex32); return 0; } int main(int argc, char **argv) { GLuint textures[8]; SDL_Surface *surface; float e = 0.0f; SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE); surface = SDL_SetVideoMode(XRESOL,YRESOL,32,SDL_HWSURFACE|SDL_OPENGL); initGL(); InitTextures(textures); while(!KeyControl(&e)) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt(0,0,5,0,0,4,0,1,0); Quad(-2,0,e,textures[0]); Quad(-1,0,e,textures[1]); Quad(0,0,e,textures[2]); Quad(1,0,e,textures[3]); Quad(-2,-1,e,textures[4]); Quad(-1,-1,e,textures[5]); Quad(0,-1,e,textures[6]); Quad(1,-1,e,textures[7]); SDL_GL_SwapBuffers( ); } SDL_Quit(); return 0; }
No explanations yet.