Correction sur les normales, éviter les bâtiments trop petits.

This commit is contained in:
Georges Dupéron 2011-11-29 23:58:11 +01:00
parent 46fec292f9
commit 917d8d81d6
11 changed files with 48 additions and 26 deletions

View File

@ -26,3 +26,12 @@ int hashInRange(int seed, int n, int a, int b) {
int newSeed(int seed, int n) { int newSeed(int seed, int n) {
return hash2(seed, n); return hash2(seed, n);
} }
int random_seed() {
static bool initialized = false;
if (!initialized) {
initialized = true;
srand(time(NULL));
}
return rand();
}

View File

@ -3,6 +3,8 @@
#include "all_includes.hh" #include "all_includes.hh"
int random_seed();
unsigned int hash2(unsigned int a, unsigned int b); unsigned int hash2(unsigned int a, unsigned int b);
unsigned int hash3(unsigned int seed, int x, int y); unsigned int hash3(unsigned int seed, int x, int y);
int hashInRange(int seed, int n, int a, int b); // Renvoie le n-ième nombre aléatoire dérivé de seed entre a et b. int hashInRange(int seed, int n, int a, int b); // Renvoie le n-ième nombre aléatoire dérivé de seed entre a et b.

View File

@ -18,7 +18,7 @@ int main() {
// Générer une tile de base // Générer une tile de base
Vertex ne(50, 50, 0); Vertex ne(50, 50, 0);
Vertex sw(0, 0, 0); Vertex sw(0, 0, 0);
Chose* c = new RectangleRoutes(ne, sw); Chose* c = new RectangleRoutes(ne,sw);//new RectangleRoutes(ne, sw);
recursiveSubdivide(c); recursiveSubdivide(c);
new View(c); new View(c);

View File

@ -31,10 +31,10 @@ void Batiment::triangulation() {
Vertex toit = (ah + bh + ch + dh) / 4 + Vertex(0,0,h/2); Vertex toit = (ah + bh + ch + dh) / 4 + Vertex(0,0,h/2);
// 4 Murs // 4 Murs
addTriangle(new Triangle(a,bh,ah,0xf1,0xe3,0xad)); addTriangle(new Triangle(a,b,bh,0xf1,0xe3,0xad)); // a-b-bh-ah addTriangle(new Triangle(ah,bh,a,0xf1,0xe3,0xad)); addTriangle(new Triangle(bh,b,a,0xf1,0xe3,0xad)); // a-b-bh-ah
addTriangle(new Triangle(b,dh,bh,0xf1,0xe3,0xad)); addTriangle(new Triangle(b,d,dh,0xf1,0xe3,0xad)); // b-d-dh-bh addTriangle(new Triangle(bh,dh,b,0xf1,0xe3,0xad)); addTriangle(new Triangle(dh,d,b,0xf1,0xe3,0xad)); // b-d-dh-bh
addTriangle(new Triangle(d,ch,dh,0xf1,0xe3,0xad)); addTriangle(new Triangle(d,c,ch,0xf1,0xe3,0xad)); // d-c-ch-dh addTriangle(new Triangle(dh,ch,d,0xf1,0xe3,0xad)); addTriangle(new Triangle(ch,c,d,0xf1,0xe3,0xad)); // d-c-ch-dh
addTriangle(new Triangle(c,ah,ch,0xf1,0xe3,0xad)); addTriangle(new Triangle(c,a,ah,0xf1,0xe3,0xad)); // c-a-ah-ch addTriangle(new Triangle(ch,ah,c,0xf1,0xe3,0xad)); addTriangle(new Triangle(ah,a,c,0xf1,0xe3,0xad)); // c-a-ah-ch
// 1 Toit // 1 Toit
addTriangle(new Triangle(ah,toit,bh,0x8a,0x48,0x3c)); addTriangle(new Triangle(ah,toit,bh,0x8a,0x48,0x3c));

View File

@ -20,6 +20,6 @@ std::ostream& operator<<(std::ostream& os, const Carrefour& c) {
void Carrefour::triangulation() { void Carrefour::triangulation() {
triangles.reserve(2); triangles.reserve(2);
addTriangle(new Triangle(sw, nw, ne, 0x80, 0x80, 0x80)); addTriangle(new Triangle(ne, nw, sw, 0x80, 0x80, 0x80));
addTriangle(new Triangle(sw, se, ne, 0x80, 0x80, 0x80)); addTriangle(new Triangle(sw, se, ne, 0x80, 0x80, 0x80));
} }

View File

@ -34,3 +34,5 @@ void Chose::display() {
} }
} }
} }
unsigned int Chose::initialSeed = random_seed();

View File

@ -6,7 +6,7 @@
// RectangleRoutes est un quadrilatère de routes avec des angles aux coins égaux à 90°. // RectangleRoutes est un quadrilatère de routes avec des angles aux coins égaux à 90°.
class Chose { class Chose {
public: public:
static const unsigned int initialSeed = 42; static unsigned int initialSeed;
unsigned int seed; unsigned int seed;
std::vector<Chose*> children; std::vector<Chose*> children;
std::vector<Triangle*> triangles; std::vector<Triangle*> triangles;

View File

@ -11,9 +11,13 @@ int RectangleRoutes::height() { return std::abs(this->ne.y - this->sw.y); }
bool RectangleRoutes::subdivide() { bool RectangleRoutes::subdivide() {
children.reserve(9); children.reserve(9);
int splitXMin = this->sw.x + std::max(4, this->width()*1/4);
int splitXMax = this->ne.x - std::max(4, this->width()*1/4);
int splitYMin = this->sw.y + std::max(4, this->height()*1/4);
int splitYMax = this->ne.y - std::max(4, this->height()*1/4);
Vertex split( Vertex split(
hashInRange(this->seed, 0, this->sw.x + this->width()*1/4, this->sw.x + this->width()*3/4), hashInRange(this->seed, 0, splitXMin, splitXMax),
hashInRange(this->seed, 1, this->sw.y + this->height()*1/4, this->sw.y + this->height()*3/4), hashInRange(this->seed, 1, splitYMin, splitYMax),
0 // TODO 0 // TODO
); );
// TODO : addChild(…); // TODO : addChild(…);
@ -45,7 +49,7 @@ void RectangleRoutes::triangulation() {
triangles.reserve(2); triangles.reserve(2);
Vertex nw(this->sw.x, this->ne.y, 0); Vertex nw(this->sw.x, this->ne.y, 0);
Vertex se(this->ne.x, this->sw.y, 0); Vertex se(this->ne.x, this->sw.y, 0);
addTriangle(new Triangle(this->sw, nw, this->ne, 0xc0, 0xc0, 0xc0)); addTriangle(new Triangle(this->ne, nw, this->sw, 0xc0, 0xc0, 0xc0));
addTriangle(new Triangle(this->sw, se, this->ne, 0xc0, 0xc0, 0xc0)); addTriangle(new Triangle(this->sw, se, this->ne, 0xc0, 0xc0, 0xc0));
} }

View File

@ -21,6 +21,6 @@ std::ostream& operator<<(std::ostream& os, const Route& r) {
void Route::triangulation() { void Route::triangulation() {
triangles.reserve(2); triangles.reserve(2);
addTriangle(new Triangle(sw, nw, ne, 0x6c, 0x6c, 0x6c)); addTriangle(new Triangle(ne, nw, sw, 0x6c, 0x6c, 0x6c));
addTriangle(new Triangle(sw, se, ne, 0x6c, 0x6c, 0x6c)); addTriangle(new Triangle(sw, se, ne, 0x6c, 0x6c, 0x6c));
} }

View File

@ -2,8 +2,7 @@
Triangle::Triangle(Vertex v1, Vertex v2, Vertex v3, unsigned char r, unsigned char g, unsigned char b): v1(v1), v2(v2), v3(v3), r(r), g(g), b(b) { Triangle::Triangle(Vertex v1, Vertex v2, Vertex v3, unsigned char r, unsigned char g, unsigned char b): v1(v1), v2(v2), v3(v3), r(r), g(g), b(b) {
// TODO : calcul de la normale. // TODO : calcul de la normale.
normal = this->normalVector(v1,v2,v3); normal = normalVector(v1,v2,v3);
} }
std::ostream& operator<<(std::ostream& os, const Triangle* t) { std::ostream& operator<<(std::ostream& os, const Triangle* t) {
@ -26,7 +25,7 @@ Vertexf Triangle::normalVector(Vertex v1, Vertex v2, Vertex v3) {
float x = (float)((ay * bz) - (az * by)); float x = (float)((ay * bz) - (az * by));
float y = (float)((az * bx) - (ax * bz)); float y = (float)((az * bx) - (ax * bz));
float z = -(float)((ax * by) - (ay * bx)); float z = (float)((ax * by) - (ay * bx));
float length = sqrt(x*x + y*y + z*z); float length = sqrt(x*x + y*y + z*z);
normal.x = x/length; normal.x = x/length;
@ -37,14 +36,15 @@ Vertexf Triangle::normalVector(Vertex v1, Vertex v2, Vertex v3) {
} }
void Triangle::display() { void Triangle::display() {
glDisable(GL_LIGHTING); // glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D); // glDisable(GL_TEXTURE_2D);
glBegin(GL_LINES); // glBegin(GL_LINES);
glColor3ub(255,255,0); // glColor3ub(255,255,0);
glVertex3d(v1.x*10,v1.y*10,v1.z*10); // Vertex v = (v1 + v2 + v3) / 3;
glVertex3d(v1.x*10+normal.x*50,v1.y*10+normal.y*50,v1.z*10+normal.z*50); // glVertex3d(v.x*10,v.y*10,v.z*10);
glEnd( ); // glVertex3d(v.x*10+normal.x*50,v.y*10+normal.y*50,v.z*10+normal.z*50);
glEnable(GL_LIGHTING); // glEnd( );
// glEnable(GL_LIGHTING);
View::setColor(r,g,b); View::setColor(r,g,b);
glNormal3d(normal.x,normal.y,normal.z); glNormal3d(normal.x,normal.y,normal.z);

View File

@ -1,6 +1,6 @@
#include "all_includes.hh" #include "all_includes.hh"
View::View(Chose* root) : root(root), cameraCenter(120,-120,50), xAngle(135), yAngle(102), moveDist(4) { View::View(Chose* root) : root(root), cameraCenter(127,14,128), xAngle(44), yAngle(101), moveDist(4) {
cameraSight = cameraCenter + Vertex::fromSpherical(100, yAngle, xAngle); cameraSight = cameraCenter + Vertex::fromSpherical(100, yAngle, xAngle);
initWindow(); initWindow();
mainLoop(); mainLoop();
@ -23,7 +23,7 @@ void View::initWindow() {
float MatSpec[4] = {0.0f, 0.0f, 0.0f, 1.0f}; float MatSpec[4] = {0.0f, 0.0f, 0.0f, 1.0f};
float MatDif[4] = {0.5f, 0.5f, 0.5f, 1.0f}; float MatDif[4] = {0.5f, 0.5f, 0.5f, 1.0f};
float MatAmb[4] = {0.0f, 0.0f, 0.0f, 1.0f}; float MatAmb[4] = {0.4f, 0.4f, 0.4f, 1.0f};
float shininess = 128.0f; float shininess = 128.0f;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,MatSpec); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,MatSpec);
@ -36,10 +36,10 @@ void View::initWindow() {
} }
void View::setLight() { void View::setLight() {
float Light1Pos[4] = {0.5f, 1.0f, 0.0f, 0.0f}; float Light1Pos[4] = {0.5f, 1.0f, 1.0f, 0.0f};
float Light1Dif[4] = {1.0f, 1.0f, 1.0f, 1.0f}; float Light1Dif[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float Light1Spec[4] = {0.0f, 0.0f, 0.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 Light1Amb[4] = {0.2f, 0.2f, 0.2f, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, Light1Dif); glLightfv(GL_LIGHT0, GL_DIFFUSE, Light1Dif);
glLightfv(GL_LIGHT0, GL_SPECULAR, Light1Spec); glLightfv(GL_LIGHT0, GL_SPECULAR, Light1Spec);
@ -124,6 +124,11 @@ void View::mainLoop() {
continuer = 0; continuer = 0;
break; break;
default: default:
if (SDL_GetKeyName(event.key.keysym.sym)[0] == 'q')
continuer = 0;
if (SDL_GetKeyName(event.key.keysym.sym)[0] == 'p') { // _Print _Position
std::cout << "Camera = " << cameraCenter << " xAngle = " << xAngle << " yAngle = " << yAngle << std::endl;
}
break; break;
} }
break; break;