void BatchRenderer::DrawCube( const float4x4& worldMatrix, const FColor& color ) { static const XMVECTOR verts[8] = { { -1, -1, -1, 0 }, { 1, -1, -1, 0 }, { 1, -1, 1, 0 }, { -1, -1, 1, 0 }, { -1, 1, -1, 0 }, { 1, 1, -1, 0 }, { 1, 1, 1, 0 }, { -1, 1, 1, 0 } }; const UINT* edgeIndices = AABB::GetEdges(); // copy to vertex buffer //assert( 8 <= MAX_VERTS ); XMFLOAT3 transformedVerts[8]; for( int i=0; i < 8; ++i ) { XMVECTOR v = XMVector3Transform( verts[i], worldMatrix ); XMStoreFloat3( &transformedVerts[i], v ); } for( int iEdge = 0; iEdge < AABB::NUM_EDGES; iEdge++ ) { XMFLOAT3 & start = transformedVerts[ edgeIndices[iEdge*2] ]; XMFLOAT3 & end = transformedVerts[ edgeIndices[iEdge*2 + 1] ]; DrawLine3D( as_vec3(start), as_vec3(end), color, color ); } }
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; } } }
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(); } }
Triangle::Triangle(const Vec4& A, const Vec4& B, const Vec4& C) : A(A), B(B), C(C) { Vec4 AB = B-A; Vec4 AC = C-A; this->normal = cross(as_vec3(AB), as_vec3(AC)).normalize(); }