From c0b15da78312ba421c00597be60a29c1516a04b2 Mon Sep 17 00:00:00 2001 From: Yoann Date: Mon, 3 Oct 2011 10:16:29 +0200 Subject: [PATCH] Ajout du remplissage dus triangles et essai de shade en fonction de la normale. --- display.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++-------- display.h | 3 +++ roam.c | 2 +- roam.h | 3 +++ 4 files changed, 67 insertions(+), 11 deletions(-) diff --git a/display.c b/display.c index 71870f0..e7d103d 100644 --- a/display.c +++ b/display.c @@ -12,18 +12,20 @@ int initWindow() { glEnable(GL_LIGHT0); // Active la lumière 0; glewInit(); - float MatSpec[4] = {1.0f, 1.0f, 1.0f, 1.0f}; - float MatDif[4] = {1.0f, 1.0f, 1.0f, 1.0f}; - float MatAmb[4] = {0.3f, 0.3f, 0.3f, 1.0f}; + float MatSpec[4] = {0.0f, 0.0f, 0.0f, 1.0f}; + float MatDif[4] = {0.0f, 1.0f, 0.0f, 1.0f}; + float MatAmb[4] = {0.1f, 0.1f, 0.1f, 1.0f}; float Light1Pos[4] = {0.0f, 0.0f, -1.0f, 0.0f}; float Light1Dif[4] = {1.0f, 1.0f, 1.0f, 1.0f}; - float Light1Spec[4] = {1.0f, 1.0f, 1.0f, 1.0f}; - float Light1Amb[4] = {0.5f, 0.5f, 0.5f, 1.0f}; + float Light1Spec[4] = {0.0f, 0.0f, 0.0f, 1.0f}; + float Light1Amb[4] = {0.4f, 0.4f, 0.4f, 1.0f}; + float shininess = 100.0f; glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,MatSpec); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,MatDif); glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,MatAmb); + glMaterialfv(GL_FRONT,GL_SHININESS,&shininess); glLightfv(GL_LIGHT0, GL_DIFFUSE, Light1Dif); glLightfv(GL_LIGHT0, GL_SPECULAR, Light1Spec); @@ -62,6 +64,12 @@ int mainLoop() { xCamera+=moveDist; xSight+=moveDist; break; + case SDLK_y: + yAngle += 8; + break; + case SDLK_x: + xAngle += 8; + break; default: break; } @@ -106,6 +114,8 @@ void renderScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ; drawAxes(); + glRotated(yAngle,0,1,0); + glRotated(xAngle,1,0,0); //displayTree2(); displayTree(t); @@ -156,12 +166,47 @@ void displayTree2() { glDrawArrays(GL_LINE_LOOP,0, nbVertex*3); } +void setNormals(Triangle *t) { + if(t->tLeftChild == NULL) { + int ax = t->vLeft->x - t->vApex->x; + int ay = t->vLeft->y - t->vApex->y; + int az = t->vLeft->z - t->vApex->z; + int bx = t->vApex->x - t->vRight->x; + int by = t->vApex->y - t->vRight->y; + int bz = t->vApex->z - t->vRight->z; + + int x = (ay * bz) - (az * by); + int y = (az * bx) - (ax * bz); + int z = (ax * by) - (ay * bx); + int length = sqrt((x^2) + (y^2) + (z^2)); + + length = length; + x = x/1000; + y = y/1000; + z = z/1000; + + printf("%d %d %d\n",x,y,z); + t->vLeft->xNormal = x; + t->vLeft->yNormal = y; + t->vLeft->zNormal = z; + t->vRight->xNormal = x; + t->vRight->yNormal = y; + t->vRight->zNormal = z; + t->vApex->xNormal = x; + t->vApex->yNormal = y; + t->vApex->zNormal = z; + } + else { + setNormals(t->tLeftChild); + setNormals(t->tRightChild); + } +} void displayTree(Triangle *t) { if(t->tLeftChild == NULL) { - glNormal3d(0,1,0); - glBegin(GL_LINE_LOOP); - glColor3ub(255,255,255); + glNormal3d(t->vLeft->xNormal,t->vLeft->yNormal,t->vLeft->zNormal); + //glNormal3d(0,10000,0); + glBegin(GL_TRIANGLES); glVertex3d(t->vLeft->x,t->vLeft->y,t->vLeft->z); glVertex3d(t->vApex->x,t->vApex->y,t->vApex->z); glVertex3d(t->vRight->x,t->vRight->y,t->vRight->z); @@ -177,9 +222,14 @@ void displayTree(Triangle *t) { int main() { initWindow(); t = initDefaultExample(); - vertices = (int*) malloc(sizeof(int) * nbTriangles(t)*9+1); - insertValues(t,vertices); + // Calcul des normales des traingles. + setNormals(t); + + // Réorganisation des sommets pour l'affichage optimisé. + //vertices = (int*) malloc(sizeof(int) * nbTriangles(t)*9+1); + //insertValues(t,vertices); + printf("nombre de triangles : %d\n",nbVertex); mainLoop(); diff --git a/display.h b/display.h index 6f92b34..67ea19d 100644 --- a/display.h +++ b/display.h @@ -6,6 +6,7 @@ int initWindow(); int mainLoop(); void renderScene(); +void setNormals(Triangle *t); void displayTree(Triangle *t); void displayTree2(); void Draw_Axes (); @@ -21,4 +22,6 @@ int zCamera = 600; int xSight = 1024; int ySight = 512; int zSight = 0; +int yAngle = 0; +int xAngle = 0; int moveDist = 64; diff --git a/roam.c b/roam.c index 1747e92..ca550bc 100644 --- a/roam.c +++ b/roam.c @@ -326,7 +326,7 @@ Triangle* initDefaultExample() { t->tRightNeighbor = NULL; t->tParent = NULL; - recursiveSplit(t, 13); + recursiveSplit(t, 6); /* triangle_split(t); */ /* triangle_split(t->tLeftChild); */ /* triangle_split(t->tLeftChild->tLeftChild); */ diff --git a/roam.h b/roam.h index ac9fd5a..bf3e984 100644 --- a/roam.h +++ b/roam.h @@ -6,6 +6,9 @@ typedef struct Vertex { int x; int y; int z; + int xNormal; + int yNormal; + int zNormal; /* Ajouter des champs ici. */ } Vertex;