/* Placage de texture sur une sphere */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2010 */ #include #include #include #include #include #include #ifndef M_PI #define M_PI 3.14159265358979323846264338327950288 #endif /* Variables et constantes globales */ /* pour les angles et les couleurs utilises */ static float rx = 0.0F; static float ry = 0.0F; static float rz = 0.0F; static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F }; static const float jaune[] = { 1.0F,1.0F,0.0F,1.0F }; static const float rouge[] = { 1.0F,0.0F,0.0F,1.0F }; static const float vert[] = { 0.0F,1.0F,0.0F,1.0F }; static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F }; static GLubyte *image; GLubyte *makeImage(void) { GLubyte *tab =(GLubyte *) malloc(8*8*3*sizeof(GLubyte)); GLubyte *t = tab; for ( int i = 0 ; i < 8 ; i++ ) for ( int j = 0 ; j < 8 ; j++ ) { t[0] = t[1] = t[2] = ( (i+j)%2 ) ? 0x00 : 0xFF; t += 3; } return(tab); } 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); image = makeImage(); glPixelStorei(GL_UNPACK_ALIGNMENT,1); glTexImage2D(GL_TEXTURE_2D,0,3,8,8,0,GL_RGB,GL_UNSIGNED_BYTE,image); 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 solidSphere(float rayon,int nlat,int nlon) { for ( int i = 0 ; i < nlat ; i++ ) { float a1 = -M_PI/2.0F + i*M_PI/nlat ; float a2 = a1 + M_PI/nlat ; float cs1 = cos(a1); float cs2 = cos(a2); float sn1 = sin(a1); float sn2 = sin(a2); float s1 =(float) i/nlat; float s2 =(float) (i+1)/nlat; glBegin(GL_QUAD_STRIP); for ( int j = 0 ; j <= nlon ; j++ ) { float a = j*2*M_PI/nlon; float cs = cos(a); float sn = sin(a); float x1 = cs1*cs; float z1 = cs1*sn; float x2 = cs2*cs; float z2 = cs2*sn; float t =(float) j/nlon; glNormal3f(x1,sn1,z1); glTexCoord2f(s1,t); glVertex3f(rayon*x1,rayon*sn1,rayon*z1); glNormal3f(x2,sn2,z2); glTexCoord2f(s2,t); glVertex3f(rayon*x2,rayon*sn2,rayon*z2); } glEnd(); } } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(rx,1.0F,0.0F,0.0F); glRotatef(ry,0.0F,1.0F,0.0F); glRotatef(rz,0.0F,0.0F,1.0F); solidSphere(3.0,18,36); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Erreur OpenGL\n",error); } /* Fonction executee lorsqu'aucun evenement */ /* n'est en file d'attente */ void idle(void) { rx += 0.1256F; ry += 0.2117F; rz += 0.1174F; glutPostRedisplay() ; } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ void reshape(int x,int y) { glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION) ; glLoadIdentity() ; gluPerspective(20.0F,(float) x/y,1.0,40.0) ; glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; gluLookAt(0.0,0.0,20.0,0.0,0.0,0.0,0.0,1.0,0.0); } /* Fonction executee lors de l'appui */ /* d'une touche alphanumerique du clavier */ void keyboard(unsigned char key,int x,int y) { switch (key) { case 0x0D : { static int anim = 0; anim = !anim; glutIdleFunc(( anim ) ? idle : NULL); } break; case 0x1B : exit(0); break; } } /* Fonction executee lors de l'appui */ /* d'une touche de curseur ou d'une touche */ /* page up ou page down */ void special(int key,int x,int y) { switch(key) { case GLUT_KEY_UP : rx++; glutPostRedisplay() ; break; case GLUT_KEY_DOWN : rx--; glutPostRedisplay() ; break; case GLUT_KEY_LEFT : ry++; glutPostRedisplay() ; break; case GLUT_KEY_RIGHT : ry--; glutPostRedisplay() ; break; case GLUT_KEY_PAGE_UP : rz++; glutPostRedisplay() ; break; case GLUT_KEY_PAGE_DOWN : rz--; glutPostRedisplay() ; break; } } static int clean(void) { free(image); return(0); } int main(int argc,char **argv) { onexit(clean); glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(260,260); glutInitWindowPosition(50,50); glutCreateWindow("Une sphere avec texture"); myinit(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutDisplayFunc(display); glutMainLoop(); return(0); }