Text archives Help
- From: rocky@sci.utah.edu
- To: manta@sci.utah.edu
- Subject: [MANTA] r673 - branches/itanium2/Model/Groups
- Date: Thu, 27 Oct 2005 17:39:50 -0600 (MDT)
Author: rocky
Date: Thu Oct 27 17:39:46 2005
New Revision: 673
Modified:
branches/itanium2/Model/Groups/KDTree.cc
branches/itanium2/Model/Groups/KDTree.h
branches/itanium2/Model/Groups/KDTreeLoader.cc
branches/itanium2/Model/Groups/TransparentKDTree.cc
branches/itanium2/Model/Groups/TransparentKDTree.h
Log:
Added code to loader to read in normal file. Merged in Abe's smooth normal
changes in KDTree code and fixed up order of barycentric coords.
Modified: branches/itanium2/Model/Groups/KDTree.cc
==============================================================================
--- branches/itanium2/Model/Groups/KDTree.cc (original)
+++ branches/itanium2/Model/Groups/KDTree.cc Thu Oct 27 17:39:46 2005
@@ -94,7 +94,7 @@
{
int i;
RayTriIntersectUserData *ud = (RayTriIntersectUserData*)userData;
- // float nearest_u, nearest_v;
+ float nearest_u, nearest_v;
int nearest_tri = -1;
Vectorf direction = ray->direction();
@@ -129,8 +129,8 @@
// Check to see if the t value is closer.
if (t < maxDist) {
maxDist = t;
- // nearest_u = u;
- // nearest_v = v;
+ nearest_u = u;
+ nearest_v = v;
nearest_tri = triIdx;
}
}
@@ -140,8 +140,8 @@
if (nearest_tri >= 0) {
ud->rayHit.t = maxDist;
- // ud->rayHit.u = nearest_u;
- // ud->rayHit.v = nearest_v;
+ ud->rayHit.u = nearest_u;
+ ud->rayHit.v = nearest_v;
ud->rayHitTriIndex = nearest_tri;
return 1;
@@ -349,7 +349,18 @@
// e.normal = normals[isectData.rayHitTriIndex];
- e.hitInfo.scratchpad<ScratchPadInfo>().normal =
normals[isectData.rayHitTriIndex];
+ //e.hitInfo.scratchpad<ScratchPadInfo>().normal =
normals[isectData.rayHitTriIndex];
+
+ // Interpolate between the three vertices.
+ Vectorf &n0 = normals[isectData.rayHitTriIndex][0];
+ Vectorf &n1 = normals[isectData.rayHitTriIndex][1];
+ Vectorf &n2 = normals[isectData.rayHitTriIndex][2];
+
+ float u = isectData.rayHit.u;
+ float v = isectData.rayHit.v;
+
+ e.hitInfo.scratchpad<ScratchPadInfo>().normal = (n1 * u) + (n2 *
v) + (n0 * (1.0f - u - v));
+
#if SHOW_DUPLICATES
if (isectData.duplicate == 0)
e.hitInfo.scratchpad<ScratchPadInfo>().payload =
Color(RGB(1.0,1.0,1.0));
Modified: branches/itanium2/Model/Groups/KDTree.h
==============================================================================
--- branches/itanium2/Model/Groups/KDTree.h (original)
+++ branches/itanium2/Model/Groups/KDTree.h Thu Oct 27 17:39:46 2005
@@ -100,6 +100,17 @@
};
///////////////////////////////////////////////////////////////////////////
+ // TRIANGLE NORMALS TRIANGLE NORMALS TRIANGLE NORMALS TRIANGLE
NORMALS
+
///////////////////////////////////////////////////////////////////////////
+ class TriangleNormal {
+ private:
+ Vectorf normal[3];
+ public:
+ Vectorf &operator[] ( int i ) { return normal[i]; };
+ const Vectorf &operator[] ( int i ) const { return normal[i]; };
+ };
+
+
///////////////////////////////////////////////////////////////////////////
// PACKED TRIANGLES PACKED TRIANGLES PACKED TRIANGLES PACKED TRIANGLES
///////////////////////////////////////////////////////////////////////////
class PackedTriangles {
@@ -287,7 +298,7 @@
private:
VArray<int> *triIndices;
VArray<Triangle> *tris;
- Vectorf *normals;
+ TriangleNormal *normals;
VArray<int> *triToGroupMap;
VArray<int> *groupToNameMap;
Modified: branches/itanium2/Model/Groups/KDTreeLoader.cc
==============================================================================
--- branches/itanium2/Model/Groups/KDTreeLoader.cc (original)
+++ branches/itanium2/Model/Groups/KDTreeLoader.cc Thu Oct 27 17:39:46
2005
@@ -62,9 +62,10 @@
int np;
// Target data.
- float *rawData;
+ float *rawVertColData;
+ float *rawNormData;
VArray<Triangle> **tris;
- Vectorf **perVertNormals;
+ TriangleNormal **perVertNormals;
// Region of data this thread is responsible for.
long begin_offset;
@@ -72,13 +73,14 @@
/////////////////////////////////////////////////////////////////////////////
// Constructor.
- V3C1Worker( Barrier &v3c1_barrier_, int np_, float *rawData_,
VArray<Triangle> **tris_, Vectorf **perVertNormals_,
+ V3C1Worker( Barrier &v3c1_barrier_, int np_, float *rawVertColData_,
float *rawNormData_, VArray<Triangle> **tris_, TriangleNormal
**perVertNormals_,
long begin_offset_, long end_offset_, BBox &bounds_ ) :
Runnable( true ),
bounds( bounds_ ),
v3c1_barrier( v3c1_barrier_ ),
np( np_ ),
- rawData( rawData_ ), tris( tris_ ), perVertNormals(
perVertNormals_ ),
+ rawVertColData( rawVertColData_ ), rawNormData( rawNormData_
),
+ tris( tris_ ), perVertNormals( perVertNormals_ ),
begin_offset( begin_offset_ ), end_offset( end_offset_ ) { }
/////////////////////////////////////////////////////////////////////////////
@@ -89,7 +91,7 @@
for (long i=begin_offset;i<end_offset;++i) {
(**tris)[i][0][0] = 0.0;
- (*perVertNormals)[i][0] = 0.0;
+ (*perVertNormals)[i][0][0] = 0.0;
}
// Wait for other threads to initialize.
@@ -101,24 +103,37 @@
for (long i=begin_offset; i<end_offset; i++) {
// Offset into the data.
- float *_rawData = rawData + 12 * i;
+ float *_rawVertColData = rawVertColData + 12 * i;
+ float *_rawNormData = rawNormData + 9 * i;
// Check to see if we are on a big endian system
// Data was generate on Altix (little endian)
if (is_big_endian()) {
for (int j=0; j<3; ++j) {
- (**tris)[i][j] = Pointf( endian_swap(
_rawData[0] ),
-
endian_swap(
_rawData[1] ),
-
endian_swap(
_rawData[2] ));
- _rawData += 3;
+ (**tris)[i][j] = Pointf( endian_swap(
_rawVertColData[0] ),
+
endian_swap(
_rawVertColData[1] ),
+
endian_swap(
_rawVertColData[2] ));
+ _rawVertColData += 3;
+
+ if(rawNormData) {
+ (*perVertNormals)[i][j] =
Vectorf( endian_swap( _rawNormData[0] ),
+ endian_swap( _rawNormData[1] ),
+ endian_swap( _rawNormData[2] ));
+ _rawNormData += 3;
+ }
}
}
// Otherwise don't swap.
else {
for (int j=0; j<3; ++j) {
- (**tris)[i][j] = Pointf( _rawData[0],
_rawData[1], _rawData[2] );
- _rawData += 3;
+ (**tris)[i][j] = Pointf(
_rawVertColData[0], _rawVertColData[1], _rawVertColData[2] );
+ _rawVertColData += 3;
+
+ if(rawNormData) {
+ (*perVertNormals)[i][j] =
Vectorf( _rawNormData[0], _rawNormData[1], _rawNormData[2] );
+ _rawNormData += 3;
+ }
}
}
@@ -126,19 +141,19 @@
// Check for big endian data.
if (is_big_endian()) {
- r = endian_swap(_rawData[0]);
- g = endian_swap(_rawData[1]);
- b = endian_swap(_rawData[2]);
- _rawData += 3;
+ r = endian_swap(_rawVertColData[0]);
+ g = endian_swap(_rawVertColData[1]);
+ b = endian_swap(_rawVertColData[2]);
+ _rawVertColData += 3;
}
else {
- r = _rawData[0];
- g = _rawData[1];
- b = _rawData[2];
+ r = _rawVertColData[0];
+ g = _rawVertColData[1];
+ b = _rawVertColData[2];
// r = int(_rawData[0] * 255 + .5f);
// g = int(_rawData[1] * 255 + .5f);
// b = int(_rawData[2] * 255 + .5f);
- _rawData += 3;
+ _rawVertColData += 3;
}
// Compute edges.
@@ -149,8 +164,13 @@
// (**tris)[i].payload = ((r<<16)+(g<<8)+b);
(**tris)[i].payload = Color(RGB( r, g, b ));
- // Compute the normal.
- (*perVertNormals)[i] = Cross((**tris)[i].edge1,
(**tris)[i].edge2).normal();
+ if(!rawNormData) {
+ // Compute the normal.
+ (*perVertNormals)[i][0] =
Cross((**tris)[i].edge1, (**tris)[i].edge2).normal();
+
+ // Use the same normal for each vertex
+ (*perVertNormals)[i][1] = (*perVertNormals)[i][2] =
(*perVertNormals)[i][0];
+ }
// Update the bounding box of the whole scene
for (long j=0; j<3; j++) {
@@ -178,7 +198,7 @@
// Load a binary v3c1 file containing the triangles.
static int LoadBin_V3C1(const char *filename, VArray<Triangle> **tris,
- /*Vectorf **perVertNormals,*/ Vectorf
**perVertNormals,
+ /*Vectorf **perVertNormals,*/ TriangleNormal
**perVertNormals,
BBox &bounds, int np ) {
FILE *f;
@@ -197,9 +217,9 @@
long nTris = fileSize / (12*sizeof(float));
// Allocate input buffer.
- float *rawData = new float [fileSize / 4];
+ float *rawVertColData = new float [fileSize / 4];
- if (rawData == 0) {
+ if (rawVertColData == 0) {
std::cerr << "Could not allocate " << (fileSize/4) << " bytes." <<
std::endl;
}
@@ -207,8 +227,39 @@
*tris = new VArray<Triangle> (nTris);
(*tris)->setLen(nTris);
+ char fn[512];
+ strcpy(fn, filename);
+ strcat(fn, ".nor"); // the kd-tree file
+
+ FILE *f2;
+ float *rawNormData = 0;
+ long fileSize2;
+ if ((f2=fopen(fn, "r")) == NULL) {
+ fprintf(stderr, "Cannot open file: %s\n", fn);
+ fprintf(stderr, "Using per triangle normals\n");
+ } else {
+
+ // Determine the input file size.
+ fseek(f2, 0, SEEK_END);
+ fileSize2 = ftell(f2);
+ fseek(f2, 0, SEEK_SET);
+
+
/////////////////////////////////////////////////////////////////////////////
+ // Determine the amount of memory needed.
+ if( (fileSize2 / (9*sizeof(float))) != nTris) {
+ fprintf(stderr, "normal file size doesn't match v3c1
file size\n");
+ return 0;
+ }
+
+ // Allocate input buffer.
+ rawNormData = new float [fileSize2 / 4];
+
+ if (rawNormData == 0) {
+ std::cerr << "Could not allocate " << (fileSize2/4) << " bytes." <<
std::endl;
+ }
+ }
// Allocate normals.
- *perVertNormals = new Vectorf [nTris];
+ *perVertNormals = new TriangleNormal[nTris];
/////////////////////////////////////////////////////////////////////////////
// Start up the worker threads and touch the memory to distribute it.
@@ -233,7 +284,7 @@
for (int i=0;i<np-1;++i) {
sprintf( worker_name, "V3C1 Worker %d", i );
- workers[i] = new Thread( new V3C1Worker( v3c1_barrier, np,
rawData, tris, perVertNormals, begin, end, worker_box[i].box ), worker_name
);
+ workers[i] = new Thread( new V3C1Worker( v3c1_barrier, np,
rawVertColData, rawNormData, tris, perVertNormals, begin, end,
worker_box[i].box ), worker_name );
begin = end;
end += work_per_thread;
@@ -241,7 +292,7 @@
// Assign the rest of the work to the last worker.
sprintf( worker_name, "V3C1 Worker %d", np-1 );
- workers[np-1] = new Thread( new V3C1Worker( v3c1_barrier, np,
rawData, tris, perVertNormals, begin, nTris, worker_box[np-1].box ), "V3C1
Worker" );
+ workers[np-1] = new Thread( new V3C1Worker( v3c1_barrier, np,
rawVertColData, rawNormData, tris, perVertNormals, begin, nTris,
worker_box[np-1].box ), "V3C1 Worker" );
// Wait for all of workers to initialize.
std::cerr << "Waiting for workers to initalize." << std::endl;
@@ -251,11 +302,18 @@
// Read in the data.
double time_begin = Time::currentSeconds();
- long trisRead = fread(rawData, 48, fileSize/48, f);
+ long trisRead = fread(rawVertColData, 48, fileSize/48, f);
if (trisRead != nTris) {
fprintf(stderr, "Error reading file: %s Tris read: %ld;
expected: %ld\n", filename, trisRead, nTris);
}
+ if(rawNormData) {
+ trisRead = fread(rawNormData, 36, fileSize2/36, f2);
+ if (trisRead != nTris) {
+ fprintf(stderr, "Error reading file: %s Tris read:
%ld; expected: %ld\n", fn, trisRead, nTris);
+ }
+ }
+
double time_end = Time::currentSeconds();
std::cerr << "Time to input triangles: " << (time_end-time_begin)
<< " seconds. " << std::endl
@@ -297,7 +355,8 @@
time_begin = Time::currentSeconds();
// Delete the intermediate raw data.
- delete [] rawData;
+ delete [] rawVertColData;
+ delete [] rawNormData;
memory_time += (Time::currentSeconds() - time_begin);
Modified: branches/itanium2/Model/Groups/TransparentKDTree.cc
==============================================================================
--- branches/itanium2/Model/Groups/TransparentKDTree.cc (original)
+++ branches/itanium2/Model/Groups/TransparentKDTree.cc Thu Oct 27 17:39:46
2005
@@ -190,8 +190,17 @@
triangle_color = &c;
}
+ // Interpolate the normal.
+ Vectorf &n0 = normals[array[i].triIdx][0];
+ Vectorf &n1 = normals[array[i].triIdx][1];
+ Vectorf &n2 = normals[array[i].triIdx][2];
+
+ float u = array[i].u;
+ float v = array[i].v;
+
+ Vectorf normal = (n1 * u) + (n2 * v) + (n0 * (1.0f - u - v));
- Real scale = Abs(Dot( normals[array[i].triIdx],
direction ));
+ Real scale = Abs(Dot( normal, direction ));
scale *= (Real)0.75;
scale += (Real)0.25;
@@ -399,7 +408,7 @@
// Copy the normal into the scratch
pad.
// Used for shootOneRay type queries.
e.hitInfo.scratchpad<TransparentKDTree::ScratchPadInfo>().normal =
-
normals[isectData.rayHitTriIndex];
+ normals[isectData.rayHitTriIndex][0];
first_leaf = false;
}
Modified: branches/itanium2/Model/Groups/TransparentKDTree.h
==============================================================================
--- branches/itanium2/Model/Groups/TransparentKDTree.h (original)
+++ branches/itanium2/Model/Groups/TransparentKDTree.h Thu Oct 27 17:39:46
2005
@@ -66,7 +66,7 @@
VArray<int> *triIndices;
VArray<Triangle> *tris;
- Vectorf *normals;
+ TriangleNormal *normals;
VArray<int> *triToGroupMap;
VArray<int> *groupToNameMap;
- [MANTA] r673 - branches/itanium2/Model/Groups, rocky, 10/27/2005
Archive powered by MHonArc 2.6.16.