bool Triangle::intersect(const Ray& r, LocalGeometry& lgeo) { Mat4 TInv = this->transform.inverse(); Vec3 dir = as_vec3(TInv*as_vec4(r.direction)); Vec4 origin = TInv*r.origin; if (cross(dir, normal).is_zero()) return false; double t = dot(as_vec3(origin-A), normal)/dot(dir, normal); if (t < 0.0) return false; else { Vec4 point = origin + (as_vec4(dir) * t); Vec3 bar_coords = barycentric_coordinates(A, B, C, point); double alpha = bar_coords.el(0); double beta = bar_coords.el(1); double gamma = bar_coords.el(2); if (alpha < 0.0 || beta < 0.0 || gamma < 0.0) return false; else { lgeo.normal = submatrix(transform).inverse().transpose() * normal; lgeo.point = transform * point; lgeo.geo = this; return true; } } }
void BatchRenderer::DrawGrid( const Vec3D& XAxis, const Vec3D& YAxis, const Vec3D& Origin, int iXDivisions, int iYDivisions, const FColor& color ) { // HRESULT hr; iXDivisions = Max( 1, iXDivisions ); iYDivisions = Max( 1, iYDivisions ); // build grid geometry // INT iLineCount = iXDivisions + iYDivisions + 2; //assert( (2*iLineCount) <= MAX_VERTS ); XMVECTOR vX = XMLoadFloat3( &as_float3(XAxis) ); XMVECTOR vY = XMLoadFloat3( &as_float3(YAxis) ); XMVECTOR vOrigin = XMLoadFloat3( &as_float3(Origin) ); for( INT i = 0; i <= iXDivisions; i++ ) { FLOAT fPercent = ( FLOAT )i / ( FLOAT )iXDivisions; fPercent = ( fPercent * 2.0f ) - 1.0f; XMVECTOR vScale = XMVectorScale( vX, fPercent ); vScale = XMVectorAdd( vScale, vOrigin ); XMVECTOR vA, vB; vA = XMVectorSubtract( vScale, vY ); vB = XMVectorAdd( vScale, vY ); DrawLine3D( as_vec4(vA).ToVec3(), as_vec4(vB).ToVec3(), color, color ); } // INT iStartIndex = ( iXDivisions + 1 ) * 2; for( INT i = 0; i <= iYDivisions; i++ ) { FLOAT fPercent = ( FLOAT )i / ( FLOAT )iYDivisions; fPercent = ( fPercent * 2.0f ) - 1.0f; XMVECTOR vScale = XMVectorScale( vY, fPercent ); vScale = XMVectorAdd( vScale, vOrigin ); XMVECTOR vA, vB; vA = XMVectorSubtract( vScale, vX ); vB = XMVectorAdd( vScale, vX ); DrawLine3D( as_vec4(vA).ToVec3(), as_vec4(vB).ToVec3(), color, color ); } }
Color ConfigEntry::as_color() const { if(type != ENTRY_DATA) { printf("[ConfigEntry] Trying to read a non-string entry as color\n"); abort(); } size_t len = split(entry_string, ",", false).size(); if(len == 3) { return Color(as_vec3()); } else if(len == 4) { return Color(as_vec4()); } else { printf("A color must have 3 or 4 components: %s\n", entry_string.c_str()); abort(); return Color(); } }
void Local_Light_Editor::Draw_Editor_Stuff( const EdSceneViewport& viewport, const rxSceneContext& sceneContext ) { BatchRenderer & renderer = gRenderer.GetDrawHelper(); const rxLocalLight& rLight = this->GetLight(); renderer.SetTexture( m_parent.m_lightBulbTextureSRV ); { const FLOAT spriteSize = 0.1f; const FLOAT spriteSizeX = spriteSize; const FLOAT spriteSizeY = spriteSize; const FColor spriteColor(as_vec4(rLight.m_diffuseColor)); renderer.DrawSprite( as_matrix4(sceneContext.invViewMatrix), rLight.GetOrigin(), spriteSizeX, spriteSizeY, spriteColor ); } renderer.SetTexture( nil ); }
void Local_Light_Editor::Placeable_Draw_Editor_Stuff( const EdDrawContext& drawContext ) { const rxSceneContext & sceneContext = drawContext.sceneContext; BatchRenderer & renderer = drawContext.renderer; renderer.SetTransform(sceneContext.viewProjectionMatrix); const rxLocalLight& rLight = this->GetLight(); const ELightType eLightType = rLight.m_lightType; const FColor lightColor(as_vec4(rLight.m_diffuseColor)); const Vec3D center = rLight.GetOrigin(); const FLOAT radius = rLight.GetRadius(); if( eLightType == Light_Point ) { const UINT numCircleSegments = 32; const Vec3D axisX( 1.0f, 0.0f, 0.0f ); const Vec3D axisY( 0.0f, 1.0f, 0.0f ); const Vec3D axisZ( 0.0f, 0.0f, 1.0f ); renderer.DrawCircle( center, axisY, axisZ, lightColor, radius, numCircleSegments ); renderer.DrawCircle( center, axisX, axisZ, lightColor, radius, numCircleSegments ); renderer.DrawCircle( center, axisX, axisY, lightColor, radius, numCircleSegments ); } else if( eLightType == Light_Spot ) { // Draw the axis arrow cone // Calculate the vertices for the base of the cone const UINT nAXIS_ARROW_SEGMENTS = 8; const FLOAT fAXIS_ARROW_SEGMENTS = (FLOAT)nAXIS_ARROW_SEGMENTS; Vec3D arrowVertices[ nAXIS_ARROW_SEGMENTS + 1 ]; const FLOAT bottomRadius = rLight.CalcBottomRadius(); const Vec3D bottomCenter = center + rLight.GetDirection() * radius; Matrix4 coneTransform( Matrix4::CreateTranslation( Vec3D( 0.0f, 0.0f, radius ) ) * Matrix3::CreateRotation( Vec3D::vec3_unit_z, rLight.GetDirection() ).ToMat4() * Matrix4::CreateTranslation( Vec3D( center.x, center.y, center.z ) ) ); for( UINT iSegment = 0 ; iSegment <= nAXIS_ARROW_SEGMENTS ; iSegment++ ) { const FLOAT theta = iSegment * (MX_TWO_PI / fAXIS_ARROW_SEGMENTS); // in radians FLOAT s, c; mxSinCos( theta, s, c ); Vec3D arrowVertex( bottomRadius * s, bottomRadius * c, 0.0f ); arrowVertices[ iSegment ] = coneTransform.TransformVector( arrowVertex ); } for( UINT iSegment = 0 ; iSegment < nAXIS_ARROW_SEGMENTS ; iSegment++ ) { const Vec3D& p0 = arrowVertices[ iSegment ]; const Vec3D& p1 = arrowVertices[ iSegment + 1 ]; renderer.DrawLine3D( p0, p1, lightColor, lightColor ); renderer.DrawLine3D( p0, center, lightColor, lightColor ); renderer.DrawLine3D( p1, center, lightColor, lightColor ); } } else { mxDBG_UNREACHABLE; } }