remove forgotten noise.h includes pyrit
authorRadek Brich <radek.brich@devl.cz>
Wed, 23 Apr 2008 19:35:03 +0200
branchpyrit
changeset 80 907929fa9b59
parent 79 062b1c4143f7
child 81 9dbb9c8c115b
remove forgotten noise.h includes common_ply.h: ignore invalid faces with duplicated points (this solves visual flaws in dragon model) extend loadShape for loading triangles realtime_dragon.cc demo: add kd-tree dump/load functionality, add colored perlin cloud texture
ccdemos/common_ply.h
ccdemos/realtime_dragon.cc
include/kdtree.h
include/material.h
include/scene.h
include/serialize.h
src/kdtree.cc
src/serialize.cc
--- a/ccdemos/common_ply.h	Wed Apr 23 14:39:33 2008 +0200
+++ b/ccdemos/common_ply.h	Wed Apr 23 19:35:03 2008 +0200
@@ -57,7 +57,17 @@
 			continue;
 		}
 		f >> v1 >> v2 >> v3;
-		face = new Triangle(vertices.at(v1), vertices.at(v3), vertices.at(v2), mat);
+
+		// check for invalid faces and ignore them
+		if (vertices[v1]->P == vertices[v2]->P
+		 || vertices[v1]->P == vertices[v3]->P
+		 || vertices[v2]->P == vertices[v3]->P)
+		{
+			f.ignore(1000,'\n');
+			continue;
+		}
+
+		face = new Triangle(vertices[v1], vertices[v3], vertices[v2], mat);
 		rt.addShape(face);
 
 		normals.at(v1) += face->getNormal();
--- a/ccdemos/realtime_dragon.cc	Wed Apr 23 14:39:33 2008 +0200
+++ b/ccdemos/realtime_dragon.cc	Wed Apr 23 19:35:03 2008 +0200
@@ -1,5 +1,6 @@
 #include "raytracer.h"
 #include "kdtree.h"
+#include "serialize.h"
 
 #include "common_sdl.h"
 #include "common_ply.h"
@@ -12,6 +13,8 @@
 
 	rt.setMaxDepth(0);
 	rt.setTop(&top);
+	rt.setCamera(&cam);
+	cam.setEye(Vector3(0,0,10));
 
 	Light light1(Vector3(-5.0, 2.0, 8.0), Colour(0.9, 0.3, 0.6));
 	light1.castShadows(false);
@@ -23,15 +26,33 @@
 
 	Material mat(Colour(0.9, 0.9, 0.9));
 	mat.setSmooth(true);
-	load_ply(rt, "../models/ply/dragon/dragon_vrip.ply", &mat, Vector3(-29,29,-29), Vector3(0,-3.6,0));
+	LinearColourMap cmap(Colour(0.5, 0.0, 0.0), Colour(1.0, 1.0, 0.5));
+	//LinearColourMap cmap(Colour(1.0, 0.2, 0.0), Colour(0.3, 0.9, 0.2));
+	mat.setTexture(new CloudTexture(10, &cmap));
 
-	rt.setCamera(&cam);
-	cam.setEye(Vector3(0,0,10));
+	ifstream fin("realtime_dragon.kdtreedump");
+	if (!fin)
+	{
+		load_ply(rt, "../models/ply/dragon/dragon_vrip.ply",
+			&mat, Vector3(-29,29,-29), Vector3(0,-3.6,0));
+		top.optimize();
+	}
+	else
+		top.load(fin, &mat);
 
-	top.optimize();
+	for (int i = 1; i < argc; i++)
+	{
+		if (strcmp(argv[1], "-dump") == 0)
+		{
+			resetSerializer();
+			ofstream fout("realtime_dragon.kdtreedump");
+			fout << top << endl;
+			fout.close();
+		}
 
-	if (argc == 2 && strcmp(argv[1], "-buildonly") == 0)
-		return 0;
+		if (strcmp(argv[1], "-buildonly") == 0)
+			return 0;
+	}
 
 	loop_sdl(rt, cam);
 }
--- a/include/kdtree.h	Wed Apr 23 14:39:33 2008 +0200
+++ b/include/kdtree.h	Wed Apr 23 19:35:03 2008 +0200
@@ -93,7 +93,7 @@
 	void setMaxDepth(int md) { max_depth = md; };
 
 	ostream & dump(ostream &st);
-	istream & load(istream &st);
+	istream & load(istream &st, Material *mat);
 };
 
 #endif
--- a/include/material.h	Wed Apr 23 14:39:33 2008 +0200
+++ b/include/material.h	Wed Apr 23 19:35:03 2008 +0200
@@ -29,7 +29,6 @@
 
 #include "common.h"
 #include "vector.h"
-#include "noise.h"
 
 /**
  * perlin noise
@@ -291,6 +290,7 @@
 	void setTransmissivity(const Float trans, const Float rindex)
 		{ transmissivity = trans; refract_index = rindex; };
 	void setSmooth(bool sm) { smooth = sm; };
+	void setTexture(Texture *tex) { texture = tex; };
 };
 
 #endif
--- a/include/scene.h	Wed Apr 23 14:39:33 2008 +0200
+++ b/include/scene.h	Wed Apr 23 19:35:03 2008 +0200
@@ -32,7 +32,6 @@
 
 #include "common.h"
 #include "sampler.h"
-#include "noise.h"
 #include "vector.h"
 #include "quaternion.h"
 
--- a/include/serialize.h	Wed Apr 23 14:39:33 2008 +0200
+++ b/include/serialize.h	Wed Apr 23 19:35:03 2008 +0200
@@ -47,7 +47,7 @@
 extern Indexer vertex_index, shape_index;
 
 void resetSerializer();
-Shape *loadShape(istream &st);
+Shape *loadShape(istream &st, Material *mat);
 
 ostream & operator<<(ostream &st, Shape &o);
 ostream & operator<<(ostream &st, Vertex &o);
--- a/src/kdtree.cc	Wed Apr 23 14:39:33 2008 +0200
+++ b/src/kdtree.cc	Wed Apr 23 19:35:03 2008 +0200
@@ -412,7 +412,7 @@
 	}
 }
 
-istream & KdTree::load(istream &st)
+istream & KdTree::load(istream &st, Material *mat)
 {
 	string s;
 	istringstream is;
@@ -421,6 +421,8 @@
 	if (s.compare("(kdtree") != 0)
 		return st;
 
+	dbgmsg(1, "* loading kd-tree\n");
+
 	shapes.clear();
 	if (root) delete root;
 	root = new KdNode();
@@ -433,7 +435,7 @@
 	Shape *shape;
 	for (int i = 0; i < shape_count; i++)
 	{
-		shape = loadShape(st);
+		shape = loadShape(st, mat);
 		Container::addShape(shape);
 		getline(st, s, ',');
 	}
--- a/src/serialize.cc	Wed Apr 23 14:39:33 2008 +0200
+++ b/src/serialize.cc	Wed Apr 23 19:35:03 2008 +0200
@@ -25,15 +25,18 @@
  */
 
 #include "serialize.h"
+#include <vector>
 #include <string>
 #include <sstream>
 
 Indexer vertex_index, shape_index;
+vector<Vertex*> vertices;
 
 void resetSerializer()
 {
 	vertex_index.reset();
 	shape_index.reset();
+	vertices.clear();
 }
 
 bool Indexer::get(void *o, int &retidx)
@@ -53,32 +56,78 @@
 	}
 }
 
-Shape *loadShape(istream &st)
+Shape *loadShape(istream &st, Material *mat)
 {
 	string s;
 	istringstream is;
-	getline(st, s, ',');
-	trim(s);
-	if (s.compare("(box") == 0)
+	for (;;)
 	{
-		Vector3 L,H;
-		st >> L;
 		getline(st, s, ',');
-		st >> H;
-		getline(st, s, ')');
-		return new Box(L, H, new Material(Colour(1,1,1)));
+		trim(s);
+
+		// Vertex
+		if (s.compare("(v") == 0)
+		{
+			Vector3 P;
+			st >> P;
+			getline(st, s, ')');
+			vertices.push_back(new Vertex(P));
+			getline(st, s, ',');
+			continue;
+		}
+
+		// NormalVertex
+		if (s.compare("(vn") == 0)
+		{
+			Vector3 P,N;
+			st >> P;
+			getline(st, s, ',');
+			st >> N;
+			getline(st, s, ')');
+			vertices.push_back(new NormalVertex(P,N));
+			getline(st, s, ',');
+			continue;
+		}
+
+		// Triangle
+		if (s.compare("(t") == 0)
+		{
+			int a,b,c;
+			st >> a;
+			getline(st, s, ',');
+			st >> b;
+			getline(st, s, ',');
+			st >> c;
+			getline(st, s, ')');
+			return new Triangle(vertices[a], vertices[b], vertices[c], mat);
+		}
+
+		// box
+		if (s.compare("(box") == 0)
+		{
+			Vector3 L,H;
+			st >> L;
+			getline(st, s, ',');
+			st >> H;
+			getline(st, s, ')');
+			return new Box(L, H, mat);
+		}
+
+		// Sphere
+		if (s.compare("(sphere") == 0)
+		{
+			Vector3 center;
+			Float radius;
+			st >> center;
+			getline(st, s, ',');
+			st >> radius;
+			getline(st, s, ')');
+			return new Sphere(center, radius, mat);
+		}
+
+		// else...
+		return NULL;
 	}
-	if (s.compare("(sphere") == 0)
-	{
-		Vector3 center;
-		Float radius;
-		st >> center;
-		getline(st, s, ',');
-		st >> radius;
-		getline(st, s, ')');
-		return new Sphere(center, radius, new Material(Colour(1,1,1)));
-	}
-	return NULL;
 }
 
 ostream & operator<<(ostream &st, Shape &o)