Le source : TD-TextureSurFacette.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Mars 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 GLbyte *image ;
static int w2 ;
static int h2 ;
static int mode = 0;
static int clamp = 0;
static int f1;
static int f2;
static float factS = 1.0F;
static float factT = 1.0F;
GLbyte *makeImage() {
GLbyte *img =(GLbyte *) calloc(12,sizeof(GLbyte));
img[0] = img[1] = img[3] = img[8] = img[10] =(GLbyte) 0xFF;
return(img);
}
void display2() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRasterPos2i(w2/2-32,h2/2-32);
glPixelZoom(32.0F,32.0F);
glDrawPixels(2,2,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 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 ( 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 ' ' : 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() {
image = makeImage();
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D,0,3,2,2,0,GL_RGB,GL_UNSIGNED_BYTE,image);;
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);
}