You are on page 1of 16

#include <stdlib.

h>
#include <math.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <iostream>
using namespace std;
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif
#define LI 64
#define LH 64

float x=1.0;
float y=0.7;
int selection;
static int objet = 1 ;
static int mode = 1 ;
static float fact = 1.0F;
static GLfloat view_rotx = 20.0F ;
static GLfloat view_roty = 30.0F ;
static GLfloat view_rotz = 0.0F ;
GLfloat lum_ambiante[]={0.0,1,1.0,2.0};
GLfloat lum_diffuse[]={ 1, 1, 0, 0};
GLfloat lum_speculaire[]= {2,2,0,0};

struct coord3D {
float x;
float y;
float z; } ;

struct coord2D {
float x;
float y; } ;
GLubyte image[LI][LH][3];

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 solidSphere(float r,int lat,int lon) {


int npt = 2 + (lat-1)*lon;
int ntx = (lat+1)*(lon+1);
coord3D *pts =(coord3D *) calloc(npt,sizeof(coord3D));
coord2D *tex =(coord2D *) calloc(ntx,sizeof(coord2D));
pts[0].x = 0.0f;
pts[0].y = 0.0f;

pts[0].z = 1.0f;
pts[1].x = 0.0f;
pts[1].y = 0.0f;
pts[1].z = -1.0f;
int n = 0 ;
int i,j ;
for ( j = 0 ; j < lat+1 ; j++ ) {
for ( i = 0 ; i < lon+1 ; i++ ) {
tex[n].x =(float) i/lon;
tex[n].y =(float) j/lat;
n++; } }
n=2;
for ( j = 0 ; j < (lat-1) ; j++ ) {
double aa = M_PI/2.0-(j+1)*M_PI/lat;
double c = cos(aa);
double s = sin(aa);
for ( i = 0 ; i < lon ; i++ ) {
double a = i*M_PI*2.0/lon;
pts[n].x = c*cos(a);
pts[n].y = c*sin(a);
pts[n].z = s;
n++; } }
glBegin(GL_QUADS);
for ( i = 0 ; i < lon ; i++ ) {
glTexCoord2fv((float *) &tex[i]);
glNormal3fv((float *) &pts[0]);
glVertex3fv((float *) &pts[0]);

glTexCoord2fv((float *) &tex[i+lon+1]);
glNormal3fv((float *) &pts[2+i]);
glVertex3fv((float *) &pts[2+i]);
glTexCoord2fv((float *) &tex[i+lon+2]);
glNormal3fv((float *) &pts[2+(i+1)%lon]);
glVertex3fv((float *) &pts[2+(i+1)%lon]);
glTexCoord2fv((float *) &tex[i+1]);
glNormal3fv((float *) &pts[0]);
glVertex3fv((float *) &pts[0]); }
for ( j = 0 ; j < lat-2 ; j++ ) {
for ( i = 0 ; i < lon ; i++ ) {
glTexCoord2fv((float *) &tex[i+(j+1)*(lon+1)]);
glNormal3fv((float *) &pts[2+i+j*lon]);
glVertex3fv((float *) &pts[2+i+j*lon]);
glTexCoord2fv((float *) &tex[i+1+(j+1)*(lon+1)]);
glNormal3fv((float *) &pts[2+(i+1)%lon+j*lon]);
glVertex3fv((float *) &pts[2+(i+1)%lon+j*lon]);
glTexCoord2fv((float *) &tex[i+1+(j+2)*(lon+1)]);
glNormal3fv((float *) &pts[2+(i+1)%lon+(j+1)*lon]);
glVertex3fv((float *) &pts[2+(i+1)%lon+(j+1)*lon]);
glTexCoord2fv((float *) &tex[i+(j+2)*(lon+1)]);
glNormal3fv((float *) &pts[2+i+(j+1)*lon]);
glVertex3fv((float *) &pts[2+i+(j+1)*lon]); } }
for ( i = 0 ; i < lon ; i++ ) {
glTexCoord2fv((float *) &tex[i+lat*(lon+1)]);
glNormal3fv((float *) &pts[1]);
glVertex3fv((float *) &pts[1]);

glTexCoord2fv((float *) &tex[i+(lat-1)*(lon+1)]);
glNormal3fv((float *) &pts[2+(lat-2)*lon+i]);
glVertex3fv((float *) &pts[2+(lat-2)*lon+i]);
glTexCoord2fv((float *) &tex[i+1+(lat-1)*(lon+1)]);
glNormal3fv((float *) &pts[2+(lat-2)*lon+(i+1)%lon]);
glVertex3fv((float *) &pts[2+(lat-2)*lon+(i+1)%lon]);
glTexCoord2fv((float *) &tex[i+1+lat*(lon+1)]);
glNormal3fv((float *) &pts[1]);
glVertex3fv((float *) &pts[1]); }
glEnd();
free(tex);
free(pts);
}
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 solidCube(void) {
glBegin(GL_QUADS);
glNormal3f(0.0F,0.0F,1.0F);
glTexCoord3f(0.99F,0.99F,0.99F);
glVertex3f(0.99F,0.99F,0.99F);
glTexCoord3f(-0.99F,0.99F,0.99F);
glVertex3f(-0.99F,0.99F,0.99F);
glTexCoord3f(-0.99F,-0.99F,0.99F);
glVertex3f(-0.99F,-0.99F,0.99F);
glTexCoord3f(0.99F,-0.99F,0.99F);
glVertex3f(0.99F,-0.99F,0.99F);
glNormal3f(0.0F,0.0F,-1.0F);
glTexCoord3f(0.99F,0.99F,-0.99F);
glVertex3f(0.99F,0.99F,-0.99F);

glTexCoord3f(-0.99F,0.99F,-0.99F);
glVertex3f(-0.99F,0.99F,-0.99F);
glTexCoord3f(-0.99F,-0.99F,-0.99F);
glVertex3f(-0.99F,-0.99F,-0.99F);
glTexCoord3f(0.99F,-0.99F,-0.99F);
glVertex3f(0.99F,-0.99F,-0.99F);
glNormal3f(0.0F,1.0F,0.0F);
glTexCoord3f(0.99F,0.99F,0.99F);
glVertex3f(0.99F,0.99F,0.99F);
glTexCoord3f(-0.99F,0.99F,0.99F);
glVertex3f(-0.99F,0.99F,0.99F);
glTexCoord3f(-0.99F,0.99F,-0.99F);
glVertex3f(-0.99F,0.99F,-0.99F);
glTexCoord3f(0.99F,0.99F,-0.99F);
glVertex3f(0.99F,0.99F,-0.99F);
glNormal3f(0.0F,-1.0F,0.0F);
glTexCoord3f(0.99F,-0.99F,0.99F);
glVertex3f(0.99F,-0.99F,0.99F);
glTexCoord3f(-0.99F,-0.99F,0.99F);
glVertex3f(-0.99F,-0.99F,0.99F);
glTexCoord3f(-0.99F,-0.99F,-0.99F);
glVertex3f(-0.99F,-0.99F,-0.99F);
glTexCoord3f(0.99F,-0.99F,-0.99F);
glVertex3f(0.99F,-0.99F,-0.99F);
glNormal3f(1.0F,0.0F,0.0F);
glTexCoord3f(0.99F,0.99F,0.99F);
glVertex3f(0.99F,0.99F,0.99F);

glTexCoord3f(0.99F,-0.99F,0.99F);
glVertex3f(0.99F,-0.99F,0.99F);
glTexCoord3f(0.99F,-0.99F,-0.99F);
glVertex3f(0.99F,-0.99F,-0.99F);
glTexCoord3f(0.99F,0.99F,-0.99F);
glVertex3f(0.99F,0.99F,-0.99F);
glNormal3f(-1.0F,0.0F,0.0F);
glTexCoord3f(-0.99F,0.99F,0.99F);
glVertex3f(-0.99F,0.99F,0.99F);
glTexCoord3f(-0.99F,-0.99F,0.99F);
glVertex3f(-0.99F,-0.99F,0.99F);
glTexCoord3f(-0.99F,-0.99F,-0.99F);
glVertex3f(-0.99F,-0.99F,-0.99F);
glTexCoord3f(-0.99F,0.99F,-0.99F);
glVertex3f(-0.99F,0.99F,-0.99F);
glEnd();
}

void selectTexture(int selection){


switch(selection){
case 1 : glEnable(GL_TEXTURE_2D);
break;
case 2 : glDisable(GL_TEXTURE_2D);

break;}
glutPostRedisplay();
}

void selectMode(int selection) {


switch (selection) {
case 11 : mode = 1 ;
break ;
case 12 : mode = 0 ;
break ; }
glutPostRedisplay();
}

void selectObjet(int selection) {


switch (selection) {
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :objet = selection ;
break ;
case 0 : exit(0); }
glutPostRedisplay();
}

void select(int selection) {


switch (selection) {

case 0 : exit(0); }
glutPostRedisplay();
}
void selectZoom(int selection){
switch(selection){
case 1 : x+=0.2;
y+=0.1;
glutPostRedisplay();
break;
case 2 : x-=0.2;

y-=0.1;

glutPostRedisplay();
break;}

}
void selectlight(int selection){

switch(selection){
case 1 : glLightfv( GL_LIGHT0, GL_AMBIENT, lum_ambiante);
break;
case 2 : glLightfv( GL_LIGHT0, GL_DIFFUSE, lum_diffuse);
break;
case 3 : glLightfv( GL_LIGHT0, GL_SPECULAR, lum_speculaire);
break;
case 4 : glDisable( GL_LIGHT0 );

break;
case 5 : glEnable (GL_LIGHT0 );
break;
glutPostRedisplay();
}
}

void redraw() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(view_rotx, 1.0, 0.0, 0.0);
glRotatef(view_roty, 0.0, 1.0, 0.0);
glRotatef(view_rotz, 0.0, 0.0, 1.0);
if ( mode ) {
switch(objet) {
case 1 :glutSolidSphere(x,20,20);
break;
case 2 : glutSolidCube(x);
break;
case 3 : glutSolidCone(x,1.3,20,20) ;
break ;
case 4 : glutSolidTorus(y,1.4,20,20) ;
break ;
case 5 : solidSphere(x,20,20);
break;
case 6 : solidTore(y,1.4,20,20);
break;

case 7 : solidCube();
break;
}}
else {
switch(objet) {
case 1 : glutWireSphere(x,20,20) ;
break ;
case 2 : glutWireCube(x);
break;
case 3 : glutWireCone(x,1.3,20,20) ;
break ;
case 4 : glutWireTorus(y,1.4,20,20) ;
break ; } }
glPopMatrix();
glutSwapBuffers();
}

void reshape(int w,int h) {


glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.,(float)w/(float)h,1.0,20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0F,0.0F,-5.0F) ;
}

void key(unsigned char key,int x,int y) {


switch ( key ) {
case 'z' : view_rotz += 2.0;
break;
case 'Z' : view_rotz -= 2.0;
break;
case '\033' : exit(0);
break ; }
}

static void special(int k, int x, int y) {


switch (k) {
case GLUT_KEY_UP : view_rotx += 2.0;
break;
case GLUT_KEY_DOWN : view_rotx -= 2.0;
break;
case GLUT_KEY_LEFT : view_roty += 2.0;
break;
case GLUT_KEY_RIGHT : view_roty -= 2.0;
break; }
glutPostRedisplay();
}

int main(int argc,char **argv) {


glutInit(&argc,argv);
glutInitWindowSize(300,300);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);

glutCreateWindow("Menus avec GLUt");


glClearColor(0.8F,0.8F,0.8F,0.0);
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_CLAMP);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

glShadeModel(GL_SMOOTH);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glutDisplayFunc(redraw);
glutReshapeFunc(reshape);
int menuMode = glutCreateMenu(selectMode);
glutAddMenuEntry("Plein",11);
glutAddMenuEntry("Filaire",12);
int menuObjet = glutCreateMenu(selectObjet);
glutAddMenuEntry("Sphere",1);
glutAddMenuEntry("Cube",2);
glutAddMenuEntry("Cone",3);
glutAddMenuEntry("Tore",4);
glutAddMenuEntry("Sphere avec texture",5);
glutAddMenuEntry("Tore avec texture",6);
glutAddMenuEntry("cube avec texture",7);

int menuZoom = glutCreateMenu(selectZoom);


glutAddMenuEntry("IN",1);
glutAddMenuEntry("OUT",2);
int menuLight = glutCreateMenu(selectlight);
glutAddMenuEntry("ambiante",1);
glutAddMenuEntry("diffuse",2);
glutAddMenuEntry("speculaire",3);
glutAddMenuEntry("Eteindre la lumire",4);
glutAddMenuEntry("allumer la lumire",5);
int menuTexture = glutCreateMenu(selectTexture);
glutAddMenuEntry("activer",1);
glutAddMenuEntry("dsactiver",2);
glutCreateMenu(select);
glutAddSubMenu("Mode",menuMode);
glutAddSubMenu("Objet",menuObjet);
glutAddSubMenu("Zoom",menuZoom);
glutAddSubMenu("Eclairage",menuLight);
glutAddSubMenu("Texture",menuTexture);

glutAddMenuEntry("Quitter",0);
glutAttachMenu(GLUT_LEFT_BUTTON);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glutMainLoop();

return(0);
}

You might also like