void Plane::ToLineList(VertexBuffer &vb, float uWidth, float vHeight, const vec ¢erPoint, int numLinesU, int numLinesV) const { vec topLeft = Point(-uWidth*0.5f, -vHeight *0.5f, centerPoint); vec uEdge = (Point(uWidth*0.5f, -vHeight *0.5f, centerPoint) - topLeft) / (float)numLinesU; vec vEdge = (Point(-uWidth*0.5f, vHeight *0.5f, centerPoint) - topLeft) / (float)numLinesV; int i = vb.AppendVertices((numLinesU + numLinesV) * 2); for(int y = 0; y < numLinesV; ++y) { float4 start = POINT_TO_FLOAT4(topLeft + vEdge * (float)y); float4 end = POINT_TO_FLOAT4(topLeft + uWidth * uEdge + vEdge * (float)y); vb.Set(i, VDPosition, start); vb.Set(i+1, VDPosition, end); i += 2; } for(int x = 0; x < numLinesU; ++x) { float4 start = POINT_TO_FLOAT4(topLeft + uEdge * (float)x); float4 end = POINT_TO_FLOAT4(topLeft + vHeight * vEdge + uEdge * (float)x); vb.Set(i, VDPosition, start); vb.Set(i+1, VDPosition, end); i += 2; } }
void Plane::Triangulate(VertexBuffer &vb, float uWidth, float vHeight, const vec ¢erPoint, int numFacesU, int numFacesV, bool ccwIsFrontFacing) const { vec topLeft = Point(-uWidth*0.5f, -vHeight *0.5f, centerPoint); vec uEdge = (Point(uWidth*0.5f, -vHeight *0.5f, centerPoint) - topLeft) / (float)numFacesU; vec vEdge = (Point(-uWidth*0.5f, vHeight *0.5f, centerPoint) - topLeft) / (float)numFacesV; int i = vb.AppendVertices(numFacesU * numFacesV * 6); for(int y = 0; y < numFacesV; ++y) for(int x = 0; x < numFacesU; ++x) { float4 tl = POINT_TO_FLOAT4(topLeft + uEdge * (float)x + vEdge * (float)y); float4 tr = POINT_TO_FLOAT4(topLeft + uEdge * (float)(x+1) + vEdge * (float)y); float4 bl = POINT_TO_FLOAT4(topLeft + uEdge * (float)x + vEdge * (float)(y+1)); float4 br = POINT_TO_FLOAT4(topLeft + uEdge * (float)(x+1) + vEdge * (float)(y+1)); int i0 = ccwIsFrontFacing ? i : i+5; int i1 = ccwIsFrontFacing ? i+5 : i; vb.Set(i0, VDPosition, tl); vb.Set(i+1, VDPosition, tr); vb.Set(i+2, VDPosition, bl); vb.Set(i+3, VDPosition, bl); vb.Set(i+4, VDPosition, tr); vb.Set(i1, VDPosition, br); if (vb.Declaration()->HasType(VDUV)) { float4 uvTL((float)x/numFacesU, (float)y/numFacesV, 0.f, 1.f); float4 uvTR((float)(x+1)/numFacesU, (float)y/numFacesV, 0.f, 1.f); float4 uvBL((float)x/numFacesU, (float)(y+1)/numFacesV, 0.f, 1.f); float4 uvBR((float)(x+1)/numFacesU, (float)(y+1)/numFacesV, 0.f, 1.f); vb.Set(i0, VDUV, uvTL); vb.Set(i+1, VDUV, uvTR); vb.Set(i+2, VDUV, uvBL); vb.Set(i+3, VDUV, uvBL); vb.Set(i+4, VDUV, uvTR); vb.Set(i1, VDUV, uvBR); } if (vb.Declaration()->HasType(VDNormal)) { for(int k = 0; k < 6; ++k) vb.Set(i+k, VDNormal, DIR_TO_FLOAT4(normal)); } i += 6; } }
void AABB::ToLineList(VertexBuffer &vb) const { Array<vec> pos; pos.Resize_pod(NumVerticesInEdgeList()); ToEdgeList(&pos[0]); int startIndex = vb.AppendVertices((int)pos.size()); for(int i = 0; i < (int)pos.size(); ++i) vb.Set(startIndex+i, VDPosition, POINT_TO_FLOAT4(pos[i])); }
void Sphere::Triangulate(VertexBuffer &vb, int numVertices, bool ccwIsFrontFacing) const { Array<vec> position; Array<vec> normal; Array<float2> uv; position.Resize_unspecified(numVertices); normal.Resize_unspecified(numVertices); uv.Resize_unspecified(numVertices); Triangulate(position.ptr(), normal.ptr(), uv.ptr(), numVertices, ccwIsFrontFacing); int startIndex = vb.AppendVertices(numVertices); for(int i = 0; i < (int)position.size(); ++i) { vb.Set(startIndex+i, VDPosition, POINT_TO_FLOAT4(position[i])); if (vb.Declaration()->TypeOffset(VDNormal) >= 0) vb.Set(startIndex+i, VDNormal, DIR_TO_FLOAT4(normal[i])); if (vb.Declaration()->TypeOffset(VDUV) >= 0) vb.SetFloat2(startIndex+i, VDUV, 0, uv[i]); } }
void AABB::Triangulate(VertexBuffer &vb, int numFacesX, int numFacesY, int numFacesZ, bool ccwIsFrontFacing) const { Array<vec> pos; Array<vec> normal; Array<float2> uv; int numVertices = (numFacesX*numFacesY + numFacesY*numFacesZ + numFacesX*numFacesZ)*2*6; pos.Resize_pod(numVertices); normal.Resize_pod(numVertices); uv.Resize_pod(numVertices); Triangulate(numFacesX, numFacesY, numFacesZ, &pos[0], &normal[0], &uv[0], ccwIsFrontFacing); int startIndex = vb.AppendVertices(numVertices); for(int i = 0; i < (int)pos.size(); ++i) { vb.Set(startIndex+i, VDPosition, POINT_TO_FLOAT4(pos[i])); if (vb.Declaration()->TypeOffset(VDNormal) >= 0) vb.Set(startIndex+i, VDNormal, DIR_TO_FLOAT4(normal[i])); if (vb.Declaration()->TypeOffset(VDUV) >= 0) vb.SetFloat2(startIndex+i, VDUV, 0, uv[i]); } }
vec Frustum::Project(const vec &point) const { float4 projectedPoint = ViewProjMatrix().Mul(POINT_TO_FLOAT4(point)); projectedPoint /= projectedPoint.w; // Post-projective perspective divide. return FLOAT4_TO_POINT(projectedPoint); }