Skip to content
Browse files

Minor update to the bounding box calculations in the Picking3D sample…

…. Also fixed an issue with loading the shader files in Visual Studio.
  • Loading branch information...
1 parent ed92147 commit 6dc5e5d39280378ec364cab0593f0413a5f2a76b @paulhoux paulhoux committed Jun 2, 2012
View
11 samples/Picking3D/resources/phong_frag.glsl
@@ -7,22 +7,25 @@ varying vec3 N;
void main()
{
- float shinyness = 50.0;
+ const vec4 ambient = vec4(0.1, 0.1, 0.1, 1);
+ const vec4 diffuse = vec4(0.9, 0.9, 0.9, 1);
+ const vec4 specular = vec4(1, 1, 1, 1);
+ const float shinyness = 50.0;
vec3 L = normalize(gl_LightSource[0].position.xyz - v);
vec3 E = normalize(-v);
vec3 R = normalize(-reflect(L,N));
// ambient term
- vec4 Iamb = gl_FrontLightProduct[0].ambient;
+ vec4 Iamb = ambient;
// diffuse term
- vec4 Idiff = texture2D( tex0, gl_TexCoord[0].st) * gl_FrontLightProduct[0].diffuse;
+ vec4 Idiff = texture2D( tex0, gl_TexCoord[0].st) * diffuse;
Idiff *= max(dot(N,L), 0.0);
Idiff = clamp(Idiff, 0.0, 1.0);
// specular term
- vec4 Ispec = vec4(1, 1, 1, 1); // gl_FrontLightProduct[0].specular;
+ vec4 Ispec = specular;
Ispec *= pow(max(dot(R,E),0.0), shinyness);
Ispec = clamp(Ispec, 0.0, 1.0);
View
43 samples/Picking3D/src/Picking3DApp.cpp
@@ -98,6 +98,9 @@ class Picking3DApp : public AppBasic
// the model of a rubber ducky
TriMesh mMesh;
+ // the object space bounding box of the mesh
+ AxisAlignedBox3f mObjectBounds;
+
// transformations (translate, rotate, scale) of the model
Matrix44f mTransform;
@@ -137,7 +140,10 @@ void Picking3DApp::setup()
// (note: the mesh was created from an OBJ file
// using the ObjLoader class. The data was then saved using the
// TriMesh::write() method. Reading binary files is much quicker.)
- mMesh.read( loadResource( RES_DUCKY_MESH ) );
+ mMesh.read( loadResource( RES_DUCKY_MESH ) );
+
+ // get the object space bounding box of the model, for fast intersection testing
+ mObjectBounds = mMesh.calcBoundingBox();
// set up the camera
CameraPersp cam;
@@ -190,7 +196,7 @@ void Picking3DApp::draw()
drawGrid();
// bind the texture
- mTexture.bind();
+ mTexture.enableAndBind();
// bind the shader and tell it to use our texture
mShader.bind();
@@ -212,7 +218,7 @@ void Picking3DApp::draw()
// perform 3D picking now, so we can draw the intersection as a sphere
Vec3f pickedPoint, pickedNormal;
if( performPicking( &pickedPoint, &pickedNormal ) ) {
- gl::color( ColorAf(0.0f, 1.0f, 0.0f, 0.5f) );
+ gl::color( Color(0, 1, 0) );
// draw an arrow to the picked point along its normal
gl::drawVector( pickedPoint + pickedNormal, pickedPoint );
@@ -254,29 +260,36 @@ bool Picking3DApp::performPicking( Vec3f *pickedPoint, Vec3f *pickedNormal )
// where (0, 0) is in the LOWERleft corner, we have to flip the v-coordinate
Ray ray = cam.generateRay(u , 1.0f - v, cam.getAspectRatio() );
- // get the bounding box of the model, for fast intersection testing
- AxisAlignedBox3f objectBounds = mMesh.calcBoundingBox();
-
- // draw this untransformed box in yellow
+ // draw the object space bounding box in yellow
gl::color( Color(1, 1, 0) );
- gl::drawStrokedCube(objectBounds);
+ gl::drawStrokedCube(mObjectBounds);
// the coordinates of the bounding box are in object space, not world space,
// so if the model was translated, rotated or scaled, the bounding box would not
- // reflect that.
- AxisAlignedBox3f worldBounds = mMesh.calcBoundingBox(mTransform);
+ // reflect that.
+ //
+ // One solution would be to pass the transformation to the calcBoundingBox() function:
+ AxisAlignedBox3f worldBoundsExact = mMesh.calcBoundingBox(mTransform); // slow
+
+ // draw this transformed box in orange
+ gl::color( Color(1, 0.5, 0) );
+ gl::drawStrokedCube(worldBoundsExact);
+
+ // But if you already have an object space bounding box, it's much faster to
+ // approximate the world space bounding box like this:
+ AxisAlignedBox3f worldBoundsApprox = mObjectBounds.transformed(mTransform); // fast
// draw this transformed box in cyan
gl::color( Color(0, 1, 1) );
- gl::drawStrokedCube(worldBounds);
-
- // set initial distance to something far, far away
- float result = 1.0e6f;
+ gl::drawStrokedCube(worldBoundsApprox);
// fast detection first - test against the bounding box itself
- if( ! worldBounds.intersects(ray) )
+ if( ! worldBoundsExact.intersects(ray) )
return false;
+ // set initial distance to something far, far away
+ float result = 1.0e6f;
+
// traverse triangle list and find the picked triangle
size_t polycount = mMesh.getNumTriangles();
float distance = 0.0f;
View
6 samples/Picking3D/vc10/Picking3D.vcxproj
@@ -102,11 +102,11 @@
<ClCompile Include="..\src\Picking3DApp.cpp" />
</ItemGroup>
<ItemGroup>
- <None Include="..\data\phong_frag.glsl" />
- <None Include="..\data\phong_vert.glsl" />
+ <ResourceCompile Include="Resources.rc" />
</ItemGroup>
<ItemGroup>
- <ResourceCompile Include="Resources.rc" />
+ <None Include="..\resources\phong_frag.glsl" />
+ <None Include="..\resources\phong_vert.glsl" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
View
14 samples/Picking3D/vc10/Picking3D.vcxproj.filters
@@ -23,16 +23,16 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
- <None Include="..\data\phong_frag.glsl">
+ <ResourceCompile Include="Resources.rc">
+ <Filter>Resource Files</Filter>
+ </ResourceCompile>
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="..\resources\phong_frag.glsl">
<Filter>Shader Files</Filter>
</None>
- <None Include="..\data\phong_vert.glsl">
+ <None Include="..\resources\phong_vert.glsl">
<Filter>Shader Files</Filter>
</None>
</ItemGroup>
- <ItemGroup>
- <ResourceCompile Include="Resources.rc">
- <Filter>Resource Files</Filter>
- </ResourceCompile>
- </ItemGroup>
</Project>

0 comments on commit 6dc5e5d

Please sign in to comment.
Something went wrong with that request. Please try again.