Le source : TD-TextureSurFacette2.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2004 */
/* Une facette texture 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 GLubyte *image1 ;
static GLubyte *image2 ;
static int w2 ;
static int h2 ;
static int mode = 0;
static int damier = 0;
static int clamp = 0;
static int f1;
static int f2;
static float factS = 1.0F;
static float factT = 1.0F;
GLubyte *makeImage1() {
GLubyte *img =(GLubyte *) calloc(2*2*3,sizeof(GLubyte));
img[0] = img[4] = img[8] = img[9] = img[10] =(GLbyte) 0xFF;
return(img);
}
GLubyte *makeImage2() {
GLubyte *img =(GLubyte *) calloc(8*8*3,sizeof(GLubyte));
for ( int i = 0 ; i < 8 ; i++ )
for ( int j = 0 ; j < 8 ; j++ )
if ( ((i+j)%2) == 1 ) {
int ind = 3*(i*8+j);
img[ind] = img[ind+1] = img[ind+2] = 0xFF; }
return(img);
}
void display2() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRasterPos2i(w2/2-32,h2/2-32);
if ( damier ) {
glPixelZoom(8.0F,8.0F);
glDrawPixels(8,8,GL_RGB,GL_UNSIGNED_BYTE,image2); }
else {
glPixelZoom(32.0F,32.0F);
glDrawPixels(2,2,GL_RGB,GL_UNSIGNED_BYTE,image1); }
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 hexagone1(void) {
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();
}
void hexagone2(void) {
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*11.0,factT*sn*11.0);
glVertex2f(4.0F*cs,4.0F*sn); }
glEnd();
}
void display1() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
if ( damier )
glTexImage2D(GL_TEXTURE_2D,0,3,8,8,0,GL_RGB,GL_UNSIGNED_BYTE,image2);
else
glTexImage2D(GL_TEXTURE_2D,0,3,2,2,0,GL_RGB,GL_UNSIGNED_BYTE,image1);
if ( clamp ) {
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);}
else {
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); }
glPushMatrix();
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glPushMatrix();
glTranslatef(-2.2F,2.0F,0.0F);
glScalef(0.5F,0.5F,0.5F);
manipulateurSouris();
manipulateurClavier();
hexagone1();
glPopMatrix();
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glPushMatrix();
glTranslatef(2.2F,2.0F,0.0F);
glScalef(0.5F,0.5F,0.5F);
manipulateurSouris();
manipulateurClavier();
hexagone1();
glPopMatrix();
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glPushMatrix();
glTranslatef(-2.2F,-2.0F,0.0F);
glScalef(0.5F,0.5F,0.5F);
manipulateurSouris();
manipulateurClavier();
hexagone2();
glPopMatrix();
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glPushMatrix();
glTranslatef(2.2F,-2.0F,0.0F);
glScalef(0.5F,0.5F,0.5F);
manipulateurSouris();
manipulateurClavier();
hexagone2();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 'd' :
case 'D' : damier = !damier;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
break;
case ' ' : clamp = !clamp;
glutPostWindowRedisplay(f1);
break;
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() {
image1 = makeImage1();
image2 = makeImage2();
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}
void myInit2() {
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitWindowSize(300,250);
glutInitWindowPosition(50,50);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f1 = glutCreateWindow("Hexagone texture");
creationMenuBasique();
myInit1();
setParametresOrthoBasique(-4.0,4.0,-4.0,4.0,-50.0,50.0);
setManipulateurDistance(10.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutDisplayFunc(display1);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutInitWindowPosition(450,100);
glutInitWindowSize(150,150);
f2 = glutCreateWindow("Texture");
creationMenuBasique();
myInit2();
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutMainLoop();
return(0);
}