/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2003 */
/* Placage de texture sur un tore */
#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.14159265358979323846264338327950288
#endif
#define LI 64
#define LH 64
struct coord3D {
float x;
float y;
float z; } ;
struct coord2D {
float x;
float y; } ;
static float fact = 1.0F;
static GLubyte image[LI][LH][3];
static float ri = 0.5F;
static float re = 1.0F;
void makeImage(void) {
int i,j,c;
for( i = 0 ; i < LI ; i++ ) {
for( j = 0 ; j < LH ; j++ ) {
c =(((i&0x4)==0)^((j&0x4)==0))*255;
image[i][j][0] =(GLubyte) c;
image[i][j][1] =(GLubyte) c;
image[i][j][2] =(GLubyte) c; } }
}
void myinit(void) {
glClearColor(0.8F,0.8F,0.8F,0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
makeImage();
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D,0,3,LI,LH,0,GL_RGB,GL_UNSIGNED_BYTE,&image[0][0][0]);
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);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}
void solidTore(double ri,double re,int nbi,int nbe) {
for ( int i = 0 ; i < nbi ; i++ ) {
float alphai = 2*M_PI*i/nbi;
float alphaj = alphai+2*M_PI/nbi;
float cosalphai = cos(alphai);
float sinalphai = sin(alphai);
float cosalphaj = cos(alphaj);
float sinalphaj = sin(alphaj);
glBegin(GL_QUAD_STRIP);
for ( int j = 0 ; j <= nbe ; j++ ) {
float beta = 2*M_PI*j/nbe;
float cosbeta = cos(beta);
float sinbeta = sin(beta);
float x1 = (re+ri*cosbeta)*cosalphai;
float y1 = (re+ri*cosbeta)*sinalphai;
float z1 = ri*sinbeta;
glTexCoord2f((double) i/nbi*fact,(double) j/nbe*fact);
glNormal3f(cosbeta*cosalphai,cosbeta*sinalphai,sinbeta);
glVertex3f(x1,y1,z1);
float x2 = (re+ri*cosbeta)*cosalphaj;
float y2 = (re+ri*cosbeta)*sinalphaj;
float z2 = ri*sinbeta;
glTexCoord2f((double) (i+1)/nbi*fact,(double) j/nbe*fact);
glNormal3f(cosbeta*cosalphaj,cosbeta*sinalphaj,sinbeta);
glVertex3f(x2,y2,z2); }
glEnd(); }
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
solidTore(ri,re,72,36);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 'i' : ri += 0.01F;
glutPostRedisplay();
break;
case 'I' : ri -= 0.01F;
glutPostRedisplay();
break;
case 'e' : re += 0.01F;
glutPostRedisplay();
break;
case 'E' : re -= 0.01F;
glutPostRedisplay();
break;
case 43 : fact *= 1.01F ;
glutPostRedisplay();
break;
case 45 : fact /= 1.01F ;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Un tore avec texture");
myinit();
creationMenuBasique();
setParametresPerspectiveBasique(43.0F,1.5F,1.0F,30.0F,0.0F,0.0F,-3.6F);
setManipulateurDistance(3.6F);
glutReshapeFunc(reshapePerspectiveBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutIdleFunc(idleBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}