void Transformation(const Real O[restrict], const Real scale[restrict], const Real angle[restrict], const Real offset[restrict], Polyhedron *poly) { const RealVec Sin = {sin(angle[X]), sin(angle[Y]), sin(angle[Z])}; const RealVec Cos = {cos(angle[X]), cos(angle[Y]), cos(angle[Z])}; const Real rotate[DIMS][DIMS] = { /* point rotation matrix */ {Cos[Y]*Cos[Z], -Cos[X]*Sin[Z]+Sin[X]*Sin[Y]*Cos[Z], Sin[X]*Sin[Z]+Cos[X]*Sin[Y]*Cos[Z]}, {Cos[Y]*Sin[Z], Cos[X]*Cos[Z]+Sin[X]*Sin[Y]*Sin[Z], -Sin[X]*Cos[Z]+Cos[X]*Sin[Y]*Sin[Z]}, {-Sin[Y], Sin[X]*Cos[Y], Cos[X]*Cos[Y]}}; const Real invrot[DIMS][DIMS] = { /* inverse rotation matrix */ {rotate[0][0], rotate[1][0], rotate[2][0]}, {rotate[0][1], rotate[1][1], rotate[2][1]}, {rotate[0][2], rotate[1][2], rotate[2][2]}}; const Real num = 1.0 / sqrt(2.0); const Real axe[6][DIMS] = { /* direction vector of axis xx, yy, zz, xy, yz, zx */ {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {num, num, 0.0}, {0.0, num, num}, {num, 0.0, num}}; RealVec axis = {0.0}; /* direction vector of axis in rotated frame */ Real I[6] = {0.0}; /* inertia tensor after rotation */ /* transforming vertex and build the new bounding box */ for (int s = 0; s < DIMS; ++s) { poly->box[s][MIN] = FLT_MAX; poly->box[s][MAX] = FLT_MIN; } TransformVertex(O, scale, rotate, offset, poly->box, poly->vertN, poly->v); /* transforming normal assuming pure rotation and translation */ TransformNormal(rotate, poly->faceN, poly->Nf); TransformNormal(rotate, poly->edgeN, poly->Ne); TransformNormal(rotate, poly->vertN, poly->Nv); /* transform inertial tensor */ for (int n = 0; n < 6; ++n) { axis[X] = Dot(invrot[X], axe[n]); axis[Y] = Dot(invrot[Y], axe[n]); axis[Z] = Dot(invrot[Z], axe[n]); I[n] = TransformInertia(axis, poly->I); } poly->I[X][X] = I[0]; poly->I[X][Y] = -I[3]; poly->I[X][Z] = -I[5]; poly->I[Y][X] = -I[3]; poly->I[Y][Y] = I[1]; poly->I[Y][Z] = -I[4]; poly->I[Z][X] = -I[5]; poly->I[Z][Y] = -I[4]; poly->I[Z][Z] = I[2]; /* centroid should be transformed at last */ Real Oc[1][DIMS] = {{poly->O[X], poly->O[Y], poly->O[Z]}}; TransformVertex(O, scale, rotate, offset, poly->box, 1, Oc); poly->O[X] = Oc[0][X]; poly->O[Y] = Oc[0][Y]; poly->O[Z] = Oc[0][Z]; return; }
// Recalculate vertex positions for the color space triangles and then update the vertex buffer. // The scene's command list must be in the recording state when this method is called. void D3D12HDR::UpdateVertexBuffer() { const XMFLOAT2 primaries709[] = { { 0.64f, 0.33f }, { 0.30f, 0.60f }, { 0.15f, 0.06f }, { 0.3127f, 0.3290f } }; const XMFLOAT2 primaries2020[] = { { 0.708f, 0.292f }, { 0.170f, 0.797f }, { 0.131f, 0.046f }, { 0.3127f, 0.3290f } }; const XMFLOAT2 offset1 = { 0.2f, 0.0f }; const XMFLOAT2 offset2 = { 0.2f, -1.0f }; const XMFLOAT3 triangle709[] = { TransformVertex(primaries709[0], offset1), // R TransformVertex(primaries709[1], offset1), // G TransformVertex(primaries709[2], offset1), // B TransformVertex(primaries709[3], offset1) // White }; const XMFLOAT3 triangle2020[] = { TransformVertex(primaries2020[0], offset2), // R TransformVertex(primaries2020[1], offset2), // G TransformVertex(primaries2020[2], offset2), // B TransformVertex(primaries2020[3], offset2) // White }; TrianglesVertex triangleVertices[] = { // Upper triangles. Rec 709 primaries. { triangle709[2], primaries709[2] }, { triangle709[1], primaries709[1] }, { triangle709[3], primaries709[3] }, { triangle709[1], primaries709[1] }, { triangle709[0], primaries709[0] }, { triangle709[3], primaries709[3] }, { triangle709[0], primaries709[0] }, { triangle709[2], primaries709[2] }, { triangle709[3], primaries709[3] }, // Lower triangles. Rec 2020 primaries. { triangle2020[2], primaries2020[2] }, { triangle2020[1], primaries2020[1] }, { triangle2020[3], primaries2020[3] }, { triangle2020[1], primaries2020[1] }, { triangle2020[0], primaries2020[0] }, { triangle2020[3], primaries2020[3] }, { triangle2020[0], primaries2020[0] }, { triangle2020[2], primaries2020[2] }, { triangle2020[3], primaries2020[3] }, }; // Copy data to the intermediate upload heap and then schedule a copy // from the upload heap to the vertex buffer. UINT8* mappedUploadHeap = nullptr; ThrowIfFailed(m_vertexBufferUpload->Map(0, &CD3DX12_RANGE(0, 0), reinterpret_cast<void**>(&mappedUploadHeap))); mappedUploadHeap += m_gradientVertexBufferView.SizeInBytes; memcpy(mappedUploadHeap, triangleVertices, sizeof(triangleVertices)); m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_vertexBuffer.Get(), D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER, D3D12_RESOURCE_STATE_COPY_DEST)); m_commandList->CopyBufferRegion(m_vertexBuffer.Get(), 0, m_vertexBufferUpload.Get(), 0, m_vertexBuffer->GetDesc().Width); m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_vertexBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER)); }
void TransformVertices(TIterator vrtsBegin, TIterator vrtsEnd, matrix33& m, TAAPos& aaPos) { for(TIterator iter = vrtsBegin; iter != vrtsEnd; ++iter) TransformVertex(*iter, m, aaPos); }