-
-/* Physics segment */
-
-static int triangle_raycast2d( v3f pA, v3f pB, v3f pC, v3f ray, float *height )
-{
- v2f v0, v1, v2, vp, vp2;
- float d, bca = 0.f, bcb = 0.f, bcc = 0.f;
-
- v0[0] = pB[0] - pA[0];
- v0[1] = pB[2] - pA[2];
- v1[0] = pC[0] - pA[0];
- v1[1] = pC[2] - pA[2];
- v2[0] = pB[0] - pC[0];
- v2[1] = pB[2] - pC[2];
-
- d = 1.f / (v0[0]*v1[1] - v1[0]*v0[1]);
-
-#if 0
- /* Backface culling */
- if( v2_cross( v0, v1 ) > 0.f )
- return;
-#endif
-
- vp[0] = ray[0] - pA[0];
- vp[1] = ray[2] - pA[2];
-
- if( v2_cross( v0, vp ) > 0.f ) return 0;
- if( v2_cross( vp, v1 ) > 0.f ) return 0;
-
- vp2[0] = ray[0] - pB[0];
- vp2[1] = ray[2] - pB[2];
-
- if( v2_cross( vp2, v2 ) > 0.f ) return 0;
-
- bcb = (vp[0]*v1[1] - v1[0]*vp[1]) * d;
- bcc = (v0[0]*vp[1] - vp[0]*v0[1]) * d;
- bca = 1.f - bcb - bcc;
-
- *height = pA[1]*bca + pB[1]*bcb + pC[1]*bcc;
- return 1;
-}
-
-/* Temporary */
-static int sample_scene_height( scene *pscene, v3f pos, v3f norm )
-{
- for( int i=0; i<pscene->indice_count/3; i++ )
- {
- u32 *tri = &pscene->indices[i*3];
-
- float *pA = pscene->verts[tri[0]].co,
- *pB = pscene->verts[tri[1]].co,
- *pC = pscene->verts[tri[2]].co;
-
- float height;
- if( triangle_raycast2d( pA, pB, pC, pos, &height ))
- {
- pos[1] = height;
-
- if( norm )
- {
- v3f v0, v1;
- v3_sub( pA, pB, v0 );
- v3_sub( pC, pB, v1 );
- v3_cross( v1, v0, norm );
- v3_normalize( norm );
- }
-
- return 1;
- }
- }
- return 0;
-}
-
-static void sample_scene_normal( scene *pscene, v3f pos, v3f normal )
-{
- for( int i=0; i<pscene->indice_count/3; i++ )
- {
- u32 *tri = &pscene->indices[i*3];
-
- float height;
- if( triangle_raycast2d(
- pscene->verts[ tri[0] ].co,
- pscene->verts[ tri[1] ].co,
- pscene->verts[ tri[2] ].co, pos, &height ))
- {
- v3f v0, v1;
-
- v3_sub( pscene->verts[ tri[1] ].co,
- pscene->verts[ tri[0] ].co,
- v0 );
-
- v3_sub( pscene->verts[ tri[2] ].co,
- pscene->verts[ tri[0] ].co,
- v1 );
-
- v3_cross( v0, v1, normal );
- v3_normalize( normal );
- return;
- }
- }
-
- normal[0] = 0.0f;
- normal[1] = 1.0f;
- normal[2] = 0.0f;
-}
-
-