commit 2a6622e265546b733def0623529e4af7e27c0f0e
parent f585c345875e527739d825f8bf4fce502978d22a
Author: Georges Dupéron <jahvascriptmaniac+github@free.fr>
Date: Mon, 3 Oct 2011 10:28:37 +0200
Merge branch 'master' of github:jsmaniac/2011-m2s3-city-builder
Conflicts:
display.c
Diffstat:
| M | display.c | | | 114 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------- |
| A | display.h | | | 27 | +++++++++++++++++++++++++++ |
| M | roam.c | | | 2 | +- |
| M | roam.h | | | 3 | +++ |
4 files changed, 116 insertions(+), 30 deletions(-)
diff --git a/display.c b/display.c
@@ -1,27 +1,4 @@
-#include <SDL/SDL.h>
-#include <GL/glew.h>
-#include <GL/glu.h>
-#include "roam.h"
-
-int initWindow();
-int mainLoop();
-void renderScene();
-void displayTree(Triangle *t);
-void displayTree2();
-void Draw_Axes ();
-
-Triangle *t;
-int *vertices;
-int windowWidth = 1024;
-int nbVertex = 0;
-int windowHeight = 768;
-int xCamera = 1024;
-int yCamera = -800;
-int zCamera = 600;
-int xSight = 1024;
-int ySight = 512;
-int zSight = 0;
-int moveDist = 64;
+#include "display.h"
int initWindow() {
SDL_Init(SDL_INIT_VIDEO);
@@ -31,18 +8,41 @@ int initWindow() {
glLoadIdentity();
gluPerspective(70,(double)windowWidth/windowHeight,1,10000);
glEnable(GL_DEPTH_TEST);
+ glEnable(GL_LIGHTING); // Active l'éclairage
+ glEnable(GL_LIGHT0); // Active la lumière 0;
glewInit();
+
+ 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] = {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);
+ glLightfv(GL_LIGHT0, GL_AMBIENT, Light1Amb);
+ glLightfv(GL_LIGHT0, GL_POSITION, Light1Pos);
+
return 0;
}
+
int mainLoop() {
short continuer = 1;
SDL_Event event;
while (continuer) {
SDL_WaitEvent(&event);
-
switch(event.type) {
case SDL_QUIT:
continuer = 0;
@@ -64,6 +64,12 @@ int mainLoop() {
xCamera+=moveDist;
xSight+=moveDist;
break;
+ case SDLK_y:
+ yAngle += 8;
+ break;
+ case SDLK_x:
+ xAngle += 8;
+ break;
default:
break;
}
@@ -77,6 +83,7 @@ int mainLoop() {
return 0;
}
+
void drawAxes() {
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINES);
@@ -98,6 +105,7 @@ void drawAxes() {
glEnd( );
}
+
void renderScene() {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
@@ -107,6 +115,8 @@ void renderScene() {
//glClearColor(1,1,1,1); // pour un fond blanc
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;
drawAxes();
+ glRotated(yAngle,0,1,0);
+ glRotated(xAngle,1,0,0);
//displayTree2();
displayTree(t);
@@ -114,6 +124,7 @@ void renderScene() {
SDL_GL_SwapBuffers();
}
+
int nbTriangles(Triangle *t) {
int sum = 0;
@@ -128,6 +139,7 @@ int nbTriangles(Triangle *t) {
return sum;
}
+
void insertValues(Triangle *t,int *vertices) {
if(t->tLeftChild == NULL) {
vertices[9*nbVertex] = t->vLeft->x;
@@ -147,6 +159,7 @@ void insertValues(Triangle *t,int *vertices) {
}
}
+
void displayTree2() {
glVertexAttribPointer(0, 3, GL_INT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0);
@@ -154,10 +167,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) {
- 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);
@@ -169,12 +219,18 @@ 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
@@ -0,0 +1,27 @@
+#include <SDL/SDL.h>
+#include <GL/glew.h>
+#include <GL/glu.h>
+#include "roam.h"
+
+int initWindow();
+int mainLoop();
+void renderScene();
+void setNormals(Triangle *t);
+void displayTree(Triangle *t);
+void displayTree2();
+void Draw_Axes ();
+
+Triangle *t;
+int *vertices;
+int windowWidth = 1024;
+int nbVertex = 0;
+int windowHeight = 768;
+int xCamera = 1024;
+int yCamera = -800;
+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
@@ -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
@@ -6,6 +6,9 @@ typedef struct Vertex {
int x;
int y;
int z;
+ int xNormal;
+ int yNormal;
+ int zNormal;
/* Ajouter des champs ici. */
} Vertex;