Le source : TD-TextureHexagone.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Mars 2003 */
/* Une facette texturee avec un damier */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
#ifndef M_PI
#define M_PI 3.14159F
#endif
static GLbyte *image ;
static int w2 ;
static int h2 ;
static int mode = 0;
static int f1;
static int f2;
static float factS = 1.0F;
static float factT = 1.0F;
GLbyte *makeRasterImage() {
GLbyte *image =(GLbyte *) malloc(16*16*3*sizeof(GLbyte));
for ( int i = 0 ; i < 16 ; i++ )
for ( int j = 0 ; j < 16 ; j++ ) {
int p = 3*(j+i*16);
if ( i%2 )
image[p] = image[p+1] = image[p+2] = ( j%2 ) ? 0xFF : 0x00 ;
else
switch (j%4) {
case 0 : image[p] = image[p+1] =(GLbyte) 0xFF;
image[p+2] = 0x00;
break;
case 1 : image[p] =(GLbyte) 0xFF;
image[p+2] = image[p+1]= 0x00;
break;
case 2 : image[p+1] =(GLbyte) 0xFF;
image[p+2] = image[p]= 0x00;
break;
case 3 : image[p+2] =(GLbyte) 0xFF;
image[p] = image[p+1]= 0x00;
break; } }
return(image);
}
void display2() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRasterPos2i(w2/2-8,h2/2-8);
glDrawPixels(16,16,GL_RGB,GL_UNSIGNED_BYTE,image);
glPopMatrix();
glutSwapBuffers();
}
void reshape2(int w,int h) {
w2 = w;
h2 = h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0,h,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void display1() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glBegin(GL_POLYGON);
for ( int i = 0 ; i < 6 ; i++ ) {
float a = i*M_PI/3;
float cs = cos(a);
float sn = sin(a);
glTexCoord2f(factS*cs,factT*sn);
glVertex2f(4.0F*cs,4.0F*sn); }
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 45 : switch (mode) {
case 0 : factS *= 1.01F;
break;
case 1 : factT *= 1.01F;
break; }
glutPostWindowRedisplay(f1);
break;
case 43 : switch (mode) {
case 0 : factS /= 1.01F;
break;
case 1 : factT /= 1.01F;
break; }
glutPostWindowRedisplay(f1);
break;
case 0x0D : mode = !mode;
break; }
}
void myInit1() {
image = makeRasterImage();
glTexImage2D(GL_TEXTURE_2D,0,3,16,16,0,GL_RGB,GL_UNSIGNED_BYTE,image);;
glEnable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}
void myInit2() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitWindowSize(350,250);
glutInitWindowPosition(50,50);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f1 = glutCreateWindow("Hexagone texture");
creationMenuBasique();
myInit1();
setParametresPerspectiveBasique(45.0F,1.0F,1.0F,20.0F,0.0F,0.0F,-10.0F);
setManipulateurDistance(10.0F);
glutReshapeFunc(reshapePerspectiveBasique);
glutDisplayFunc(display1);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutInitWindowPosition(450,100);
glutInitWindowSize(150,50);
f2 = glutCreateWindow("Texture");
creationMenuBasique();
myInit2();
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutMainLoop();
return(0);
}