ARTICLE AD BOX
I have an issue I can't seem to figure out. I have managed to find multiple implementations of the Catmull-Rom splines but somehow no matter which one I use I cannot seem to get the right positions.
My scene is in a cube from 0 to 1 so that is usually where my positions are concentrated on.
//float3 Camera::CatmullRom(float t,float3 p_mi1, float3 p_0, float3 p_1, float3 p_2) //{ // float3 a4 = p_0; // float3 a3 = (p_1 - p_mi1) / 2.0f; // float3 a1 = (p_2 - p_0) / 2.0f - 2.0f * p_1 + a3 + 2.0f * a4; // float3 a2 = 3.0f * p_1 - (p_2 - p_0) / 2.0f - 2.0f * a3 - 3.0f * a4; // // return a1 * t * t * t + a2 * t * t + a3 * t + a4; //} //float3 Camera::CatmullRom(const float t, float3 p_0, float3 p1, float3 p2, float3 p_3) //{ // return ( // t * ((2 - t) * t - 1) * p_0 // + (t * t * (3 * t - 5) + 2) * p1 // + t * ((4 - 3 * t) * t + 1) * p2 // + (t - 1) * t * t * p_3) / 2; //} float3 Camera::CatmullRom(const float t, float3 p_0, float3 p1, float3 p2, float3 p_3) { const float alpha = 0.5f; const float tension = 0; float t01 = powf(length(p_0, p1), alpha); float t12 = powf(length(p1, p2), alpha); float t23 = powf(length(p2, p_3), alpha); float3 m1 = (1.0f - tension) * (p2 - p1 + t12 * ((p1 - p_0) / t01 - (p2 - p_0) / (t01 + t12))); float3 m2 = (1.0f - tension) * (p2 - p1 + t12 * ((p_3 - p2) / t23 - (p_3 - p1) / (t12 + t23))); Segment segment; float3 point = segment.a * t * t * t + segment.b * t * t + segment.c * t + segment.d; return point; } //float3 Camera::CatmullRom(float t, float3 p0, float3 p1, float3 p2, float3 p3 ) { // // float3 c2 = -.5 * p0 + 0.5 * p2; // float3 c3 = p0 + -2.5 * p1 + 2.0 * p2 + -.5 * p3; // float3 c4 = -.5 * p0 + 1.5 * p1 + -1.5 * p2 + 0.5 * p3; // return(((c4 * t + c3) * t + c2) * t + p1); //} float3 Camera::GetPosAtTime(float t) { const int count = 8; float scaledT = t * 8.0f; int i = int(scaledT); //int i = int(t); float localT = fracf(scaledT); uint4 id = uint4( (i + count - 1) % count, i % count, (i + 1) % count, (i + 2) % count ); float3 p0 = cameraPos[id.x]; float3 p1 = cameraPos[id.y]; float3 p2 = cameraPos[id.z]; float3 p3 = cameraPos[id.w]; return CatmullRom(localT, p0, p1, p2, p3); } bool Camera::HandleInput(const float t) { static float r = 0; r += t * 0.001f; if(r > 1.0f) r -= 1.0f; cameraPos[0] = float3(-0.9f, 0.7f, 1.39f); cameraPos[1] = float3(-0.96f, 0.74f, 1.41f); cameraPos[2] = float3(-0.5f, -0.7f, -0.8f); cameraPos[3] = float3(-0.4f, -0.6f, -0.7f); cameraPos[4] = float3(-0.7f, 0.6f, -0.79f); cameraPos[5] = float3(-0.72f, 0.63f, -0.81f); cameraPos[6] = float3(1.66f, 0.63f, -0.92f); cameraPos[7] = float3(1.7f, 0.6f, -0.9f); camPos = GetPosAtTime(r); //camPos = CatmullRom(r, cameraPos[0], cameraPos[1], cameraPos[2], cameraPos[3]); float3 target = float3(-0.5f, -0.5f, -0.5f); mat4 M = mat4::LookAt(camPos, target); float3 x(M[0], M[4], M[8]), y(M[1], M[5], M[9]), z(M[2], M[6], M[10]); topLeft = camPos + 2.0f * z - aspect * x + y; topRight = camPos + 2.0f * z + aspect * x + y; bottomLeft = camPos + 2.0f * z - aspect * x - y; return true; }These are all implementations I have tried and I can't seem to make them align perfectly with the positions and I receive some strange outcomes. Can someone tell what I did wrong perhaps, or are my positions just picked wrong? Also how do you pick the p0 and p3 for the spline points perfectly so you can get the expected outcome for the aplitude of the middle points? All implementations give me the same outcome so it is probably the positions that I have wrong so could you help me pick the properly?
