diff options
-rw-r--r-- | geometry.py | 10 | ||||
-rw-r--r-- | solid.py | 2 | ||||
-rw-r--r-- | src/intersect.h | 16 | ||||
-rw-r--r-- | src/kernel.cu | 2 | ||||
-rwxr-xr-x | view.py | 2 |
5 files changed, 23 insertions, 9 deletions
diff --git a/geometry.py b/geometry.py index fdc361c..5acbbc4 100644 --- a/geometry.py +++ b/geometry.py @@ -104,6 +104,8 @@ class Geometry(object): for i, surface in enumerate(surface2[reorder]): self.surface2_index[i] = self.surfaces.index(surface) + self.colors = np.concatenate([solid.color for solid in self.solids]) + self.solid_id = np.concatenate([np.tile(solid.id, len(solid.mesh)) \ for solid in self.solids])[reorder] @@ -179,7 +181,7 @@ class Geometry(object): if unique_bit_shifted_zvalues.size == 1: break - def load(self, module): + def load(self, module, color=False): """ Load the bounding volume hierarchy onto the GPU module, bind it to the appropriate textures, and return a list @@ -196,6 +198,12 @@ class Geometry(object): triangles['y'] = self.mesh.triangles[:,1] triangles['z'] = self.mesh.triangles[:,2] + if color: + triangles['w'] = self.colors + else: + triangles['w'] = (self.material1 << 24) | \ + (self.material2 << 16) | (self.surface1 << 8) | self.surface2 + lower_bounds = np.empty(self.lower_bounds.shape[0], dtype=gpuarray.vec.float4) lower_bounds['x'] = self.lower_bounds[:,0] lower_bounds['y'] = self.lower_bounds[:,1] @@ -3,7 +3,7 @@ import numpy as np class Solid(object): def __init__(self, id, mesh, rotation=np.identity(3), displacement=(0,0,0), material1=None, material2=None, surface1=None, surface2=None, - color=None): + color=0xffffffff): self.id = id self.mesh = mesh diff --git a/src/intersect.h b/src/intersect.h index a968f09..92bcf0c 100644 --- a/src/intersect.h +++ b/src/intersect.h @@ -54,16 +54,22 @@ __device__ bool intersect_triangle(const float3 &origin, const float3 &direction angle between the ray and the plane normal to determine the brightness. `direction` must be normalized. */ -__device__ int get_color(const float3 &direction, const float3 &v0, const float3& v1, const float3 &v2) +__device__ int get_color(const float3 &direction, const float3 &v0, const float3& v1, const float3 &v2, const int base_color=0xFFFFFFFF) { - float scale = 255*dot(normalize(cross(v1-v0,v2-v0)),-direction); + float scale = dot(normalize(cross(v1-v0,v2-v0)),-direction); + + unsigned int r = 0xFF & (base_color >> 16); + unsigned int g = 0xFF & (base_color >> 8); + unsigned int b = 0xFF & base_color; if (scale < 0.0f) - scale = 255*dot(-normalize(cross(v1-v0,v2-v0)),-direction); + scale = dot(-normalize(cross(v1-v0,v2-v0)),-direction); - int rgb = floorf(scale); + r = floorf(r*scale); + g = floorf(g*scale); + b = floorf(b*scale); - return rgb << 16 | rgb << 8 | rgb; + return r << 16 | g << 8 | b; } /* Test the intersection between a ray starting from `origin` traveling in the diff --git a/src/kernel.cu b/src/kernel.cu index a020180..ead08da 100644 --- a/src/kernel.cu +++ b/src/kernel.cu @@ -190,7 +190,7 @@ __global__ void ray_trace(int nthreads, float3 *origins, float3 *directions, int float3 v1 = make_float3(tex1Dfetch(vertices, triangle_data.y)); float3 v2 = make_float3(tex1Dfetch(vertices, triangle_data.z)); - *(pixels+idx) = get_color(direction, v0, v1, v2); + *(pixels+idx) = get_color(direction, v0, v1, v2, triangle_data.w); } } // ray_trace @@ -40,7 +40,7 @@ def view(geometry, name=''): module = SourceModule(src.kernel, options=['-I' + src.dir], no_extern_c=True, cache_dir=False) - texrefs = geometry.load(module) + texrefs = geometry.load(module, color=True) cuda_raytrace = module.get_function('ray_trace') cuda_rotate = module.get_function('rotate') cuda_translate = module.get_function('translate') |