# HG changeset patch # User Radek Brich # Date 1197624871 -3600 # Node ID 5d043eeb09d9da534d95273377f7966cfb427edc # Parent 5f954c0d34fcd17bead661de714e262fe3fd3506 realtime_dragon demo: now fullsize model + octree realtime_bunny demo: bigger resolution Box, Sphere: implemented AABB intersection new stop condition for octree building (when number of shapes in children >= 6x shapes in parent node) fixes for octree traversal diff -r 5f954c0d34fc -r 5d043eeb09d9 ccdemos/realtime_bunny.cc --- a/ccdemos/realtime_bunny.cc Fri Dec 14 00:05:54 2007 +0100 +++ b/ccdemos/realtime_bunny.cc Fri Dec 14 10:34:31 2007 +0100 @@ -6,8 +6,8 @@ #include #include -int w = 320; -int h = 200; +int w = 640; +int h = 400; Float *render_buffer; Raytracer rt; diff -r 5f954c0d34fc -r 5d043eeb09d9 ccdemos/realtime_dragon.cc --- a/ccdemos/realtime_dragon.cc Fri Dec 14 00:05:54 2007 +0100 +++ b/ccdemos/realtime_dragon.cc Fri Dec 14 10:34:31 2007 +0100 @@ -1,7 +1,7 @@ #include #include "raytracer.h" -#include "kdtree.h" +#include "octree.h" #include #include @@ -41,8 +41,7 @@ // read vertices Vector3 P; - int num = vertex_num; - while (num--) + for (int i = 0; i < vertex_num; i++) { f >> P.x >> P.y >> P.z; P.x = -scale*P.x; @@ -56,7 +55,7 @@ // read faces Triangle *face; int v1, v2, v3; - while (face_num--) + for (int i = 0; i < face_num; i++) { f >> num; if (num != 3) @@ -77,7 +76,7 @@ vertex_face_num.at(v3)++; } - for (int i; i < vertex_num; i++) + for (int i = 0; i < vertex_num; i++) { normals.at(i) /= vertex_face_num.at(i); normals.at(i).normalize(); @@ -140,10 +139,10 @@ /* initialize raytracer and prepare scene */ render_buffer = (Float *) malloc(w*h*3*sizeof(Float)); - rt.setThreads(1); - rt.setMaxDepth(3); + rt.setThreads(2); + rt.setMaxDepth(0); - KdTree top; + Octree top; rt.setTop(&top); Light light1(Vector3(-5.0, 2.0, 8.0), Colour(0.9, 0.3, 0.6)); @@ -155,13 +154,11 @@ //rt.addlight(&light2); Material mat(Colour(0.9, 0.9, 0.9)); - load_ply("../models/dragon/dragon_vrip_res4.ply", &mat, 29); + load_ply("../models/dragon/dragon_vrip.ply", &mat, 29); rt.setCamera(&cam); cam.setEye(Vector3(0,0,10)); - /* build kd-tree */ - top.setMaxDepth(30); top.optimize(); /* loop... */ diff -r 5f954c0d34fc -r 5d043eeb09d9 demos/buddha.py --- a/demos/buddha.py Fri Dec 14 00:05:54 2007 +0100 +++ b/demos/buddha.py Fri Dec 14 10:34:31 2007 +0100 @@ -12,7 +12,7 @@ rt = Raytracer() mat = Material(colour=(0.9, 0.9, 0.9)) -LoadStanfordPlyFile(rt, "../models/happy/happy_vrip_res4.ply", +LoadStanfordPlyFile(rt, "../models/happy/happy_vrip_res2.ply", mat, smooth=True, scale=20.0, trans=(0,-3,0)) light = Light(position=(-5.0, 2.0, 8.0), colour=(0.9, 0.3, 0.6)) diff -r 5f954c0d34fc -r 5d043eeb09d9 demos/bunny.py --- a/demos/bunny.py Fri Dec 14 00:05:54 2007 +0100 +++ b/demos/bunny.py Fri Dec 14 10:34:31 2007 +0100 @@ -6,7 +6,7 @@ import sys sys.path.append(open('ModulePath').read().strip()) -from raytracer import Raytracer, Light, Box, Triangle, NormalVertex, Material +from raytracer import Raytracer, Light, Box, Triangle, Material from plyreader import LoadStanfordPlyFile import Image diff -r 5f954c0d34fc -r 5d043eeb09d9 include/scene.h --- a/include/scene.h Fri Dec 14 00:05:54 2007 +0100 +++ b/include/scene.h Fri Dec 14 10:34:31 2007 +0100 @@ -156,7 +156,7 @@ center(acenter), radius(aradius) { material = amaterial; } bool intersect(const Ray &ray, Float &dist) const; bool intersect_all(const Ray &ray, Float dist, vector &allts) const; - bool intersect_bbox(const BBox &bbox) const { return true; }; + bool intersect_bbox(const BBox &bbox) const; const Vector3 normal(const Vector3 &P) const { return (P - center) * inv_radius; }; BBox get_bbox() const; }; @@ -175,7 +175,7 @@ }; bool intersect(const Ray &ray, Float &dist) const; bool intersect_all(const Ray &ray, Float dist, vector &allts) const { return false; }; - bool intersect_bbox(const BBox &bbox) const { return true; }; + bool intersect_bbox(const BBox &bbox) const; const Vector3 normal(const Vector3 &P) const; BBox get_bbox() const { return BBox(L, H); }; }; diff -r 5f954c0d34fc -r 5d043eeb09d9 src/octree.cc --- a/src/octree.cc Fri Dec 14 00:05:54 2007 +0100 +++ b/src/octree.cc Fri Dec 14 10:34:31 2007 +0100 @@ -55,7 +55,8 @@ } } - if (shapes->size() <= 8 && shapenum > 2*shapes->size()) + if ((shapes->size() <= 8 && shapenum > 2*shapes->size()) + || shapenum >= 6*shapes->size()) { // bad subdivision, revert delete[] children; @@ -78,7 +79,6 @@ ShapeList::iterator shape; for (shape = shapes.begin(); shape != shapes.end(); shape++) root->addShape(*shape); - root->subdivide(bbox, max_depth); built = true; } @@ -184,7 +184,7 @@ st_cur->next = -1; Shape *nearest_shape = NULL; - while (nearest_shape == NULL) + for (;;) { if (st_cur->next == -1) { @@ -196,17 +196,16 @@ if (node->isLeaf()) { ShapeList::iterator shape; - register Float mindist = max3(tx0,ty0,tz0); - /* correct & slow */ - //Float dist = min(min3(tx1,ty1,tz1),nearest_distance); - /* faster */ - register Float dist = nearest_distance; + //register Float mindist = max3(tx0,ty0,tz0); + register Float dist = min(nearest_distance, min3(tx1,ty1,tz1)); for (shape = node->shapes->begin(); shape != node->shapes->end(); shape++) - if (*shape != origin_shape && (*shape)->intersect(ray, dist) && dist >= mindist) + if (*shape != origin_shape && (*shape)->intersect(ray, dist)) { nearest_shape = *shape; nearest_distance = dist; } + if (nearest_shape != NULL) + return nearest_shape; } else { @@ -327,5 +326,4 @@ } st_cur->next = -1; } - return nearest_shape; } diff -r 5f954c0d34fc -r 5d043eeb09d9 src/scene.cc --- a/src/scene.cc Fri Dec 14 00:05:54 2007 +0100 +++ b/src/scene.cc Fri Dec 14 10:34:31 2007 +0100 @@ -144,19 +144,31 @@ return false; } +bool Sphere::intersect_bbox(const BBox &bbox) const +{ + register float dmin = 0; + for (int i = 0; i < 3; i++) + { + if (center[i] < bbox.L[i]) + dmin += (center[i] - bbox.L[i])*(center[i] - bbox.L[i]); + else + if (center[i] > bbox.H[i]) + dmin += (center[i] - bbox.H[i])*(center[i] - bbox.H[i]); + } + if (dmin <= sqr_radius) + return true; + return false; +}; + BBox Sphere::get_bbox() const { - BBox bbox = BBox(); - bbox.L = center - radius; - bbox.H = center + radius; - return bbox; + return BBox(center - radius, center + radius); } bool Box::intersect(const Ray &ray, Float &dist) const { Float a,b; - bool res = get_bbox().intersect(ray, a, b); - if (res && a < dist) + if (get_bbox().intersect(ray, a, b) && a < dist) { dist = a; return true; @@ -165,6 +177,14 @@ return false; } +bool Box::intersect_bbox(const BBox &bbox) const +{ + return ( + H.x > bbox.L.x && L.x < bbox.H.x && + H.y > bbox.L.y && L.y < bbox.H.y && + H.z > bbox.L.z && L.z < bbox.H.z); +} + const Vector3 Box::normal(const Vector3 &P) const { Vector3 N;