Solve fish error

This commit is contained in:
Mateusz Walas 2021-12-30 14:10:58 +01:00
parent ac7f096101
commit 34c4b58a16
2 changed files with 29 additions and 28 deletions

View File

@ -16,6 +16,7 @@
GLuint skyboxProgram, skyboxBuffer; GLuint skyboxProgram, skyboxBuffer;
GLuint programColor; GLuint programColor;
GLuint programTexture; GLuint programTexture;
GLuint textureSubmarine;
GLuint textureFish; GLuint textureFish;
unsigned int cubemapTexture, skyboxVAO; unsigned int cubemapTexture, skyboxVAO;
@ -32,25 +33,15 @@ glm::quat rotation = glm::quat(1, 0, 0, 0);
glm::mat4 cameraMatrix, perspectiveMatrix; glm::mat4 cameraMatrix, perspectiveMatrix;
Core::Shader_Loader shaderLoader; Core::Shader_Loader shaderLoader;
Core::RenderContext submarineContext;
Core::RenderContext fishContext; Core::RenderContext fishContext;
std::vector<glm::vec3> keyPoints({ std::vector<glm::vec3> keyPoints({
glm::vec3(5.0f, 5.0f, 5.0f), glm::vec3(15.0f, 5.0f, 15.0f),
glm::vec3(5.0f, 5.0f, -5.0f), glm::vec3(15.0f, 5.0f, -15.0f),
glm::vec3(-5.0f, 5.0f, -5.0f), glm::vec3(-15.0f, 5.0f, -15.0f),
glm::vec3(-5.0f, 5.0f, 5.0f), glm::vec3(-15.0f, 5.0f, 15.0f),
glm::vec3(5.0f, 5.0f, 5.0f), glm::vec3(15.0f, 5.0f, 15.0f),
glm::vec3(5.0f, 5.0f, -5.0f),
glm::vec3(-5.0f, 5.0f, -5.0f),
glm::vec3(-5.0f, 5.0f, 5.0f),
glm::vec3(5.0f, 5.0f, 5.0f),
glm::vec3(5.0f, 5.0f, -5.0f),
glm::vec3(-5.0f, 5.0f, -5.0f),
glm::vec3(-5.0f, 5.0f, 5.0f),
glm::vec3(5.0f, 5.0f, 5.0f),
glm::vec3(5.0f, 5.0f, -5.0f),
glm::vec3(-5.0f, 5.0f, -5.0f),
glm::vec3(-5.0f, 5.0f, 5.0f),
}); });
std::vector<glm::quat> keyRotation; std::vector<glm::quat> keyRotation;
@ -185,7 +176,7 @@ glm::mat4 createCameraMatrix()
} }
glm::mat4 animationMatrix(float time) { glm::mat4 animationMatrix(float time) {
float speed = 2.; float speed = 1.;
time = time * speed; time = time * speed;
std::vector<float> distances; std::vector<float> distances;
float timeStep = 0; float timeStep = 0;
@ -205,17 +196,19 @@ glm::mat4 animationMatrix(float time) {
float t = time / distances[index]; float t = time / distances[index];
int size = keyPoints.size() - 1; int size = keyPoints.size();
int rotationSize = keyRotation.size() - 1; int rotationSize = keyRotation.size();
glm::vec3 pos = glm::catmullRom(keyPoints[index - 1], keyPoints[index], keyPoints[index + 1], keyPoints[index + 2], t); std::cout << (index - 1) % size << " " << index % size << " " << (index+1)%size << " " << (index+2)%size << std::endl;
glm::vec3 pos = glm::catmullRom(keyPoints[std::max(0, (index-1)%size)], keyPoints[(index) % size], keyPoints[(index + 1) % size], keyPoints[(index + 2) % size], t);
glm::quat divideByFour = glm::quat(0.25f, 0.25f, 0.25f, 0.25f); glm::quat divideByFour = glm::quat(0.25f, 0.25f, 0.25f, 0.25f);
auto a1 = keyRotation[index] * glm::exp(-(glm::log(glm::inverse(keyRotation[index]) * keyRotation[index - 1]) + glm::log(glm::inverse(keyRotation[index]) * keyRotation[index + 1])) * divideByFour); auto a1 = keyRotation[index % rotationSize] * glm::exp(-(glm::log(glm::inverse(keyRotation[index % rotationSize]) * keyRotation[std::max(0, (index - 1)%rotationSize)]) + glm::log(glm::inverse(keyRotation[index % rotationSize]) * keyRotation[(index + 1) % rotationSize])) * divideByFour);
auto a2 = keyRotation[index + 1] * glm::exp(-(glm::log(glm::inverse(keyRotation[index + 1]) * keyRotation[index]) + glm::log(glm::inverse(keyRotation[index + 1]) * keyRotation[index + 2])) * divideByFour); auto a2 = keyRotation[(index + 1) % rotationSize] * glm::exp(-(glm::log(glm::inverse(keyRotation[(index + 1) % rotationSize]) * keyRotation[index % rotationSize]) + glm::log(glm::inverse(keyRotation[(index + 1) % rotationSize]) * keyRotation[(index + 2) % rotationSize])) * divideByFour);
auto animationRotation = glm::squad(keyRotation[index], keyRotation[index + 1], a1, a2, t); auto animationRotation = glm::squad(keyRotation[index % rotationSize], keyRotation[(index + 1) % rotationSize], a1, a2, t);
glm::mat4 result = glm::translate(pos) * glm::mat4_cast(animationRotation); glm::mat4 result = glm::translate(pos) * glm::mat4_cast(animationRotation);
@ -275,10 +268,13 @@ void renderScene()
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0); glBindVertexArray(0);
glm::mat4 fishInitialTransformation = glm::translate(glm::vec3(0, -0.5, -0.4)) * glm::rotate(glm::radians(180.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.25f)); glm::mat4 submarineInitialTransformation = glm::translate(glm::vec3(0, -0.5, -0.4)) * glm::rotate(glm::radians(180.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.25f));
glm::mat4 fishModelMatrix = glm::translate(cameraPos + cameraDir) * glm::mat4_cast(glm::inverse(rotation)) * animationMatrix(time); glm::mat4 submarineModelMatrix = glm::translate(cameraPos + cameraDir) * glm::mat4_cast(glm::inverse(rotation)) * submarineInitialTransformation;
drawObjectTexture(fishContext, fishModelMatrix, textureFish); glm::mat4 fishInitialTransformation = glm::translate(glm::vec3(0, 0, 0)) * glm::rotate(glm::radians(180.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.25f));
drawObjectTexture(fishContext, animationMatrix(time + 15), textureFish);
drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine);
glutSwapBuffers(); glutSwapBuffers();
} }
@ -372,8 +368,13 @@ void init()
programTexture = shaderLoader.CreateProgram((char*) "shaders/shader_tex.vert", (char*) "shaders/shader_tex.frag"); programTexture = shaderLoader.CreateProgram((char*) "shaders/shader_tex.vert", (char*) "shaders/shader_tex.frag");
skyboxProgram = shaderLoader.CreateProgram((char *) "shaders/skybox.vert", (char *) "shaders/skybox.frag"); skyboxProgram = shaderLoader.CreateProgram((char *) "shaders/skybox.vert", (char *) "shaders/skybox.frag");
cubemapTexture = loadCubemap(); cubemapTexture = loadCubemap();
loadModelToContext("models/submarine.obj", fishContext);
textureFish = Core::LoadTexture("textures/submarine.png"); loadModelToContext("models/submarine.obj", submarineContext);
textureSubmarine = Core::LoadTexture("textures/submarine.png");
loadModelToContext("models/fish.obj", fishContext);
textureFish = Core::LoadTexture("textures/fish.png");
initKeyRotation(); initKeyRotation();
initSkybox(); initSkybox();
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB