Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef clFreeStep_h__
- #define clFreeStep_h__
- #include "clVector3.h"
- class clFreeStep3
- {
- public:
- clFreeStep3(const clVec3 &pos, const clVec3 &dir)
- {
- m_pos = v3I(clFloor(pos));
- v3 invDir = v3(1.0) / clAbs(dir);
- const i32 quality = 32;
- invDir = clMin(invDir, v3(f32(clMaxValue<i32>() / quality)));
- invDir *= (clMaxValue<i32>() / quality) / clMaxComponent(invDir);
- v3 invDirSqrRounded = v3(clRound(invDir));
- m_waveLengths = v3I(invDirSqrRounded);
- clVec3I m_iSign = v3I(dir.x >= 0, dir.y >= 0, dir.z >= 0);
- m_sign = m_iSign * 2 - 1;
- v3 crossDist = (v3(m_pos + m_iSign) - pos);
- m_wavePhase = v3I(clAbs(crossDist) * m_waveLengths);
- auto d((v3(1.0) / m_waveLengths).Normalized()[0] * m_waveLengths[0]);
- m_pipsPerMeter = i32(d);
- }
- clVec3I Step()
- {
- ui8 minIndex = clMinComponentIndex(m_wavePhase);
- i32 sub = m_wavePhase[minIndex];
- m_wavePhase -= sub;
- m_wavePhase[minIndex] = m_waveLengths[minIndex];
- m_pos[minIndex] += m_sign[minIndex];
- return m_pos;
- }
- void DoubleRes()
- {
- v3I residual = m_waveLengths - m_wavePhase; // Convert to residual
- v3I half = residual / (m_waveLengths / 2);
- residual = v3I(residual.x % (m_waveLengths.x >> 1), residual.y % (residual.y >> 1), residual.z % (m_waveLengths.z >> 1));
- m_wavePhase = m_waveLengths - residual; // Convert residual back to wave length
- m_pos = m_pos * 2 + half * m_sign;
- }
- void Jump(i32 distance)
- {
- v3I64 pipDistance = v3I64(i64(m_pipsPerMeter) * distance);
- v3I64 wholeSteps = pipDistance / m_waveLengths;
- pipDistance -= wholeSteps * m_waveLengths;
- v3I64 additionalWholeSteps = (pipDistance + m_waveLengths - m_wavePhase) / m_waveLengths;
- wholeSteps += additionalWholeSteps;
- v3I64 newPips = m_wavePhase - pipDistance + m_waveLengths;
- for (int i = 0; i < 3; i++) m_wavePhase[i] = i32(newPips[i] % m_waveLengths[i]);
- m_pos += v3I(wholeSteps * m_sign);
- }
- public:
- clVec3I m_pos;
- clVec3I m_sign;
- v3I m_wavePhase;
- v3I m_waveLengths;
- i32 m_pipsPerMeter;
- };
- #endif // clFreeStep_h__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement