/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Une facette texturee avec un damier */
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
/* Variables globales */
static int dm = 0;
static float rx = 0.0F;
static float ry = 0.0F;
static int mouvement = 0;
static int button = 0;
static int mx;
static int my;
void damier1() {
static GLubyte image[] = { 0x00,0x00,0x00,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0x00,0x00,0x00 };
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D,0,3,2,2,0,GL_RGB,GL_UNSIGNED_BYTE,image);
glEnable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT );
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
}
void damier2() {
static GLubyte image[] = { 0xFF,0x00,0x00,0xFF,0xFF,0x00,
0x00,0xFF,0xFF,0x00,0x00,0xFF };
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D,0,3,2,2,0,GL_RGB,GL_UNSIGNED_BYTE,image);
glEnable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT );
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
}
void display() {
static int odm = -1;
if ( dm != odm ) {
odm = dm;
switch (dm) {
case 0 :
damier1();
break;
case 1 :
damier2();
break; } }
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(rx,1.0F,0.0F,0.0F);
glRotatef(ry,0.0F,1.0F,0.0F);
glBegin(GL_POLYGON);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-4.0f,-4.0f,0.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(-4.0f,4.0f,0.0f);
glTexCoord2f(1.0f,1.0f);
glVertex3f(4.0f,4.0f,0.0f);
glTexCoord2f(1.0f,0.0f);
glVertex3f(4.0f,-4.0f,0.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void init() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}
/* Fonction executee lors de l'appui */
/* d'une touche de la souris */
void mouse(int bouton,int etat,int x,int y) {
button = bouton;
mx = x;
my = y;
if ( etat == GLUT_DOWN ) {
mouvement = 1; }
if ( etat == GLUT_UP ) {
mouvement = 0; }
}
/* Fonction executee lors du deplacement */
/* de la souris devant la fenetre */
/* avec le bouton appuye */
void motion(int x,int y) {
switch ( button ) {
case GLUT_LEFT_BUTTON :
if ( mouvement == 1 ) {
rx += (y-my);
ry += (x-mx);
mx = x;
my = y;
glutPostRedisplay(); }
break; }
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
/* Configuration d'une camera de visualisation */
/* en projection en perspective */
void reshape(int tx,int ty) {
glViewport(0,0,tx,ty);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float ratio =(float) tx/ty;
gluPerspective(6.302541F,ratio,1.0,1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-0.0F,-0.0F,-100.0F);
}
/* Fonction executee lors de la frappe */
/* d'une touche alphanumerique du clavier */
void keyboard(unsigned char key,int x,int y) {
switch ( key ) {
case 0x0D : dm = !dm;
glutPostRedisplay();
break;
case 0x1B : exit(0); }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitWindowSize(250,250);
glutInitWindowPosition(50,50);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
int f1 = glutCreateWindow("Facette texturee");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return(0);
}