Exemple 6: utiliser une texture pour le ciel
Ce programme charge six textures formant les faces d’un cube qui se recollent, de façon à simuler un paysage panoramique autour de la caméra. Les fichiers de texture doivent être copiés dans la même répertoire que l’exécutable.Les fichiers de texture proviennent du site:
Fichier : examples/prog6.cpp /*
Simulation d'une "skysphere" par un cube éloigné. La classe Bitmap
fournie est utilisée pour lire la texture depuis un fichier.
La fenêtre est affichée en plein écran, et la souris sert à
controller 2 axes de rotation.
*/
#include <math.h>
#include "glut_import.h"
#include "application.h"
#include "texture.h"
#include "interleavedarray.h"
class DemoWindow:public Window{
Texture_2D tex[6];
InterleavedArray_T2F_V3F faces[6];
float theta,phi;
public:
DemoWindow():
Window("Fenêtre de démonstration",windowStyle(GLUT_RGBA,WC_KEYBOARD | WC_MOUSE,1)){};
void onMouseMotion(int passive,int x,int y){
theta=(float) x/getWidth();
phi=(float) y/getHeight();
redisplay();
};
void addCubeFace(InterleavedArray_T2F_V3F *array,Vect3<float> O,Vect3<float> U,Vect3<float> V){
T2F_V3F vertex;
vertex.T=vect(0,0);
vertex.V=O;
array->add(vertex);
vertex.T=vect(0,1);
vertex.V=O+V;
array->add(vertex);
vertex.T=vect(1,1);
vertex.V=O+U+V;
array->add(vertex);
vertex.T=vect(1,0);
vertex.V=O+U;
array->add(vertex);
}
void onDisplay(){
float t=0.03*glutGet(GLUT_ELAPSED_TIME);
int i;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70,(float) getWidth()/getHeight(),0.1,1000);
gluLookAt(0,0,2,0,0,0,0,1,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(180*phi-90,1,0,0);
glRotatef(360*theta,0,1,0);
for (i=0;i<6;i++){
tex[i].bind();
faces[i].drawElements(GL_QUADS);
}
glFlush();
glutSwapBuffers();
};
void onCreate(){
int i,D=500;
glEnable(GL_TEXTURE_2D);
for (i=0;i<6;i++){
tex[i].setWrapS(GL_CLAMP);
tex[i].setWrapT(GL_CLAMP);
}
tex[0].loadBitmapFile("lostvalley_up.bmp");
tex[1].loadBitmapFile("lostvalley_north.bmp");
tex[2].loadBitmapFile("lostvalley_west.bmp");
tex[3].loadBitmapFile("lostvalley_south.bmp");
tex[4].loadBitmapFile("lostvalley_east.bmp");
tex[5].loadBitmapFile("lostvalley_down.bmp");
addCubeFace(faces,vect(-D,D,-D),vect(2*D,0,0),vect(0,0,2*D));
addCubeFace(faces+1,vect(D,-D,D),vect(-2*D,0,0),vect(0,2*D,0));
addCubeFace(faces+2,vect(D,-D,-D),vect(0,0,2*D),vect(0,2*D,0));
addCubeFace(faces+3,vect(-D,-D,-D),vect(2*D,0,0),vect(0,2*D,0));
addCubeFace(faces+4,vect(-D,-D,D),vect(0,0,-2*D),vect(0,2*D,0));
addCubeFace(faces+5,vect(-D,-D,D),vect(2*D,0,0),vect(0,0,-2*D));
};
};
int main(){
DemoWindow w;
Application::run();
} Ce document a été traduit de LaTeX par HeVeA |