Aller au contenu

[OpenGL]Problème d'illumination


Premium

Messages recommandés

Salut,

j'ai un problème au niveau de l'illumination de la scene.

J'ai calculé les normales mais ça ne m'affiche pas ce que je souhaite

Voici le code complet

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159263

int angle;

void calcNormal(GLfloat p1[3], GLfloat p2[3], GLfloat p3[3],GLfloat normal[3]);

GLfloat jarre_2d[]={0,0,
		3,0,
		4,2,
		4,5,
		3,7,
		1,9,
		1,11,
		3,13};


void lathe(int nbr_vertices, GLfloat profile[], float angle, int nbr_sections) {
 int s, v;
 float theta1, theta2, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
 GLfloat p1[3],p2[3],p3[3],normal[3];

 for(s=0; s<nbr_sections; s++) {

theta1 = (s*1.0)*angle/nbr_sections*PI/180;
theta2 = (s+1.0)*angle/nbr_sections*PI/180;

for(v=0; v<nbr_vertices-1; v++) {
  x1 = profile[v*2]*cos(theta1);
  y1 = profile[v*2+1];
  z1 = profile[v*2]*sin(theta1);

  x2 = profile[v*2+2]*cos(theta1);
  y2 = profile[v*2+3];
  z2 = profile[v*2+2]*sin(theta1);

  x3 = profile[v*2+2]*cos(theta2);
  y3 = profile[v*2+3];
  z3 = profile[v*2+2]*sin(theta2);

  x4 = profile[v*2]*cos(theta2);
  y4 = profile[v*2+1];
  z4 = profile[v*2]*sin(theta2);



  calcNormal(p1,p2,p3,normal);
  glBegin(GL_POLYGON);
  glNormal3fv(normal);
  glVertex3f(x1,y1,z1);
  glVertex3f(x2,y2,z2);
  glVertex3f(x3,y3,z3);
  glVertex3f(x4,y4,z4);
  glEnd();

}   
 }
}


void normalize(GLfloat *vector){

 GLfloat length = sqrt(vector[0]*vector[0] + vector[1]*vector[1] + vector[2]* vector[2]);

 if( length == 0.0 )
length = 1.0;

 vector[0] /= length;
 vector[1] /= length;
 vector[2] /= length;
}


void calcNormal(GLfloat *p1, GLfloat *p2, GLfloat *p3, GLfloat *n){

 GLfloat v1[3];
 GLfloat v2[3];

 v1[0] = p2[0]-p1[0];
 v1[1] = p2[1]-p1[1];
 v1[2] = p2[2]-p1[2];  
 v2[0] = p3[0]-p2[0];
 v2[1] = p3[1]-p2[1];
 v2[2] = p3[2]-p2[2];

 n[0] = v1[1] * v2[2] - v1[2] * v2[1];
 n[1] = v1[2] * v2[0] - v1[0] * v2[2];
 n[2] = v1[0] * v2[1] - v1[1] * v2[0]; 

 normalize(n);
}


void display(void)
{
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // Clear the colour and depth buffer

 glLoadIdentity(); // Clear matrix stack
 // We set the camera in position 50,50,100 and we look at origo
 gluLookAt(50,50,100,0,0,0,0,1,0);
 //jarre
 glPushMatrix();
 glColor3f(0.6,0.2,1.0);
 glTranslatef(18,0,4);
 glScalef(3,3,3);
 lathe(8, jarre_2d, 360.0, 16);

 glColor3f(0.6,0.2,1.0);  
 glTranslatef(0,5,17);
 glScalef(5,5,5);
 glutSolidCube(1);

 /*glColor3f(1.0,0.5,0.5);
glPushMatrix();
glTranslated( 0, 15, 0 );
glScaled(20,20,20);
glutSolidTeapot(1.0);
glPopMatrix();*/

 glFlush(); // Makes sure that we output the model to the graphics card
 glutSwapBuffers();
}

// Called when a key is pressed
void key(unsigned char k, int x, int y)
{
 switch(k){
 case 'f':
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glutPostRedisplay();
break;
 case 'p':
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glutPostRedisplay();
break;
 case 'q':
exit(0);
 }
}

void reshape(int width,int height)
{
 glViewport(0,0,width,height); // Reset The Current Viewport

 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
 glLoadIdentity(); // Reset The Projection Matrix

 // Calculate The Aspect Ratio Of The Window
 gluPerspective(45.0f,(float)640/(float)480,0.1f,1000.0f);
 // Always keeps the same aspect as a 640 wide and 480 high window

 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
 glLoadIdentity(); // Reset The Modelview Matrix
}

void init()
{
 glClearColor(0,0,0,0);
 glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
 glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
 glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
}

void setupRC(){
 glDepthFunc(GL_LESS);
 glEnable(GL_DEPTH_TEST);
 glFrontFace(GL_CCW);

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glEnable(GL_LIGHTING);

 GLfloat diffuseLight[] = {0.7,0.7,0.7,1.0};
 glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
 glEnable(GL_LIGHT0);

 glEnable(GL_COLOR_MATERIAL);
 glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);

 GLfloat ambientLight[] = {0.05,0.05,0.05,1.0};
 glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);

 GLfloat specularLight [] = {0.7,0.7,0.7,1.0};
 GLfloat spectre [] = {1.0,1.0,1.0,1.0};
 glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight);
 glMaterialfv(GL_FRONT, GL_SPECULAR, spectre);
 glMaterialf(GL_FRONT,GL_SHININESS,128);

 glShadeModel(GL_SMOOTH);

 GLfloat lightPosition [] = {600,10,200,1};
 glLightfv(GL_LIGHT0,GL_POSITION,lightPosition);
}

int main(int argc, char *argv[])
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); // We want rgb display functionality
 glutInitWindowSize(640,480); // Set the window dimensions
 glutInitWindowPosition(0,0); // Set the window starting point
 glutCreateWindow("Exercise 1"); // Set the caption and launch the window

 angle = 0;


 init();
 setupRC();

 // Last things before rendering starts
 glutDisplayFunc(display); // This will be called every frame
 glutReshapeFunc(reshape); // Reshape the window when something changes
 glutKeyboardFunc(key); // Callback for input

 glutMainLoop(); // Starts the main program

 // We will not reach this point unless exit(0) is called (see function keyCB)

 return 0;
}

Ce que j'obtiens :

http://img222.imageshack us/my.php?image=capturezm3.png

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...