1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| #ifdef WIN32
#include <windows.h>
#endif
#include <iostream>
#include <fstream>
#include <math.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
/* variables globales pour modéliser le maillon*/
GLuint *tabInd;
GLfloat *tabPts;
int nbreInd;
static GLfloat rotz=0.0;
GLint id_maillon;
/* position de la source */
static GLfloat pos_source[4] = {-10.0f,0.0f,10.0f,1.0f};
void maillon(double rx, double ry, double rz, int nx, int ny, GLfloat **tabGeom,GLuint **tabIndices,int *nbreIndices);
using namespace std;
/*********************************************************/
/* fonction de dessin de la scène à l'écran */
static void drawFunc(void)
{
/* initialisation des buffers : couleur et ZBuffer */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* modification de la matrice de la scène */
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
/* placement de la caméra */
gluLookAt(-10.0,0.0,5.0,0.0,0.0,0.0,0.0,0.0,1.0);
/* placement de la source n°0 */
glLightfv(GL_LIGHT0, GL_POSITION, pos_source );
/* placement des objets dans la scène*/
glRotated(rotz,0.0,0.0,1.0);
glCallList(id_maillon);
glPopMatrix();
/* fin de la définition de la scène */
glFinish();
/* changement de buffer d'affichage */
glutSwapBuffers();
}
/*********************************************************/
/* fonction de changement de dimension de la fenetre */
/* paramètres : */
/* - width : largeur (x) de la zone de visualisation */
/* - height : hauteur (y) de la zone de visualisation */
static void reshapeFunc(int width,int height)
{ GLfloat h = (GLfloat) width / (GLfloat) height ;
/* dimension de l'écran GL */
glViewport(0, 0, (GLint)width, (GLint)height);
/* construction de la matrice de projection */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* définition de la camera */
gluPerspective( 60.0, h, 1.0, 60.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/*********************************************************/
/* fonction d'initialisation des paramètres de rendu et */
/* des objets de la scènes. */
static void init()
{ //static GLfloat c0[4] = { 0.0f, 0.0f, 0.0f, 1.0f};
//static GLfloat c1[4] = { 0.0f, 0.8f, 0.2f, 1.0f};
static GLfloat c2[4] = { 0.7f, 0.7f, 0.2f, 1.0f};
static GLfloat c3[4] = { 0.5f, 0.5f, 0.5f, 1.0f};
/* activation du modèle d'illuminaion */
glEnable(GL_LIGHTING);
/* activation de la source n°0 */
glEnable(GL_LIGHT0);
/* placement de la source n°0 */
glLightfv(GL_LIGHT0, GL_POSITION, pos_source );
/* couleur du fond (noir) */
glClearColor(0.3,0.3,0.3,0.0);
/* activation du ZBuffer */
glEnable( GL_DEPTH_TEST);
glEnable( GL_NORMALIZE);
/* ZBuffer actif pour les objets non convexes */
glDisable(GL_CULL_FACE);
/* lissage des couleurs sur les facettes */
glShadeModel(GL_SMOOTH);
maillon(1.0,0.5,0.15,20,10,&tabPts,&tabInd,&nbreInd);
/* construction de l'objet id_maillon */
id_maillon = glGenLists(1);
glNewList(id_maillon, GL_COMPILE);
/* couleur de l'objet */
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,c2);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,c3);
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,5.0f);
glEnable(GL_LIGHTING);
glEnableClientState(GL_VERTEX_ARRAY);
glInterleavedArrays(GL_T2F_N3F_V3F,0,tabPts);
glDrawElements(GL_QUADS,nbreInd,GL_UNSIGNED_INT,tabInd);
// dessin du repère
glDisable(GL_LIGHTING);
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3i(0,0,0);
glVertex3i(1,0,0);
glColor3f(0,1,0);
glVertex3i(0,0,0);
glVertex3i(0,1,0);
glColor3f(0,0,1);
glVertex3i(0,0,0);
glVertex3i(0,0,1);
glEnd();
glEndList();
}
int main(int argc, char** argv)
{ /* traitement des paramètres du programme propres à GL */
glutInit(&argc, argv);
/* initialisation du mode d'affichage : */
/* RVB + ZBuffer + Double buffer. */
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
/* placement et dimentions originales de la fenêtre */
glutInitWindowPosition(0, 0);
glutInitWindowSize(500, 500);
/* ouverture de la fenêtre */
if (glutCreateWindow("Exemple 1") == GL_FALSE)
{ return 1;
}
init();
/* association de la fonction de dimensionnement */
glutReshapeFunc(reshapeFunc);
/* association de la fonction d'affichage */
glutDisplayFunc(drawFunc);
/* boucle principale de gestion des événements */
glutMainLoop();
return 0;
}
void maillon(double rx, double ry, double rz, int nx, int ny, GLfloat **tabGeom,GLuint **tabIndices,int *nbreIndices)
{ double theta,phi,vx,vy,d;
int ix,iy;
GLfloat *ptr;
GLuint *ind;
*nbreIndices = 4*nx*ny;
*tabGeom = (GLfloat*)malloc(8*nx*ny*sizeof(GLfloat));
*tabIndices = (GLuint*)malloc(*nbreIndices*sizeof(GLuint));
ptr = *tabGeom;
for (ix=0,theta=0.0; ix<nx; ix++,theta+=2.0*M_PI/nx)
{ for (iy=0,phi=0.0; iy<ny; iy++,phi+=2.0*M_PI/ny)
{ vx = ry*cos(theta);
vy = rx*sin(theta);
d = sqrt(vx*vx+vy*vy);
vx/=d;
vy/=d;
// texture au point
*ptr++ = 6.0*ix/nx;
*ptr++ = 2.0*iy/ny;
// normale au point
*ptr++ = vx*cos(phi);
*ptr++ = vy*cos(phi);
*ptr++ = sin(phi);
// position du point
*ptr++ = rx*cos(theta)+rz*vx*cos(phi);
*ptr++ = ry*sin(theta)+rz*vy*cos(phi);
*ptr++ = rz*sin(phi);
}
}
// construction du tableau d'indices
ind = *tabIndices;
int indice;
for (ix=0; ix<nx-1; ix++)
{ for (iy=0; iy<ny-1; iy++)
{ // cas général
// indices de la facette
indice = ix*ny+iy;
*ind++ = indice;
*ind++ = indice+1;
*ind++ = indice+ny+1;
*ind++ = indice+ny;
}
// fermetures (iy=ny-1)
indice = ix*ny+iy;
*ind++ = indice;
*ind++ = ix*ny;
*ind++ = (ix+1)*ny;
*ind++ = indice+ny;
}
// fermetures (ix=nx-1)
for (iy=0; iy<ny-1; iy++)
{ indice = ix*ny+iy;
*ind++ = indice;
*ind++ = indice+1;
*ind++ = iy+1;
*ind++ = iy;
}
// fermetures (ix=nx-1 && iy=ny-1)
*ind++ = ix*ny+iy;
*ind++ = ix*ny;
*ind++ = 0;
*ind++ = ny-1;
} |
Partager