This commit is contained in:
Damian 2022-01-19 23:17:30 +01:00
commit c5e01c780d
7 changed files with 156 additions and 75 deletions

View File

@ -32,5 +32,5 @@ void main()
float specular = pow(max(0,dot(R,V)),10);
float diffuse = max(0,dot(normal,lightDir));
gl_FragColor = vec4(mix(objectColor.rgb,objectColor.rgb*diffuse+vec3(1)*specular,0.9), 1.0) * (1.0f - depth) + vec4(depth * vec3(5.0f/255.0f, 35.0f/255.0f, 95.0f/255.0f), objectColor.a);
gl_FragColor = vec4(mix(objectColor.rgb,objectColor.rgb*diffuse+vec3(1)*specular,0.9), objectColor.a) * (1.0f - depth) + vec4(depth * vec3(5.0f/255.0f, 35.0f/255.0f, 95.0f/255.0f), objectColor.a);
}

View File

@ -1,38 +1,47 @@
#include "Fish.h"
Fish::Fish(Core::RenderContext _context, GLuint _textureId, std::vector<glm::vec3> _keyPoints, float _speed, float _scale) {
context = _context;
textureId = _textureId;
Fish::Fish(Core::RenderObject _object, std::vector<KeyPointRotation> _keyPoints, float _speed, float _scale) {
object = _object;
speed = _speed;
keyPoints = _keyPoints;
scale = _scale;
glm::vec3 oldDirection = glm::vec3(0, 0, 1);
glm::quat oldRotationCamera = glm::quat(1, 0, 0, 0);
glm::vec3 newDirection;
glm::quat newRotationCamera;
for (int i = 0; i < keyPoints.size() - 1; i++) {
newDirection = keyPoints[i + 1] - keyPoints[i];
newRotationCamera = glm::normalize(glm::rotationCamera(oldDirection, newDirection) * oldRotationCamera);
keyRotation.push_back(newRotationCamera);
}
oldDirection = newDirection;
oldRotationCamera = newRotationCamera;
keyRotation.push_back(glm::quat(1, 0, 0, 0));
keyPoints.push_back(keyPoints[0]);
//glm::vec3 oldDirection = glm::vec3(0, 0, 1);
//glm::quat oldRotationCamera = glm::quat(1, 0, 0, 0);
//glm::vec3 newDirection;
//glm::quat newRotationCamera;
//
//keyPoints.push_back(keyPoints[0]);
//for (int i = 0; i < keyPoints.size() - 1; i++) {
// newDirection = keyPoints[i + 1] - keyPoints[i];
// newRotationCamera = glm::normalize(glm::rotationCamera(oldDirection, newDirection) * oldRotationCamera);
// keyRotation.push_back(newRotationCamera);
// oldDirection = newDirection;
// oldRotationCamera = newRotationCamera;
//}
//keyRotation.push_back(glm::quat(1, 0, 0, 0));
}
Core::RenderContext Fish::getContext() {
return context;
return object.context;
}
GLuint Fish::getTextureId() {
return textureId;
return object.textureId;
}
glm::mat4 Fish::getInitialRotation() {
glm::vec3 tragetVec = glm::normalize(glm::vec3(2) - glm::vec3(0));
glm::quat rotQuat = glm::rotationCamera(glm::vec3(0, 0, 1), tragetVec);
return glm::rotate(glm::radians(90.0f), glm::vec3(1, 0, 0));
}
glm::quat Fish::calcA(int index) {
glm::quat qi = keyRotation[index];
glm::quat qiminus = keyRotation[std::max(0, index - 1)];
glm::quat qiplus = keyRotation[std::min(index + 1, (int)keyRotation.size() - 1)];
glm::quat qi = keyPoints[index].Rotation;
glm::quat qiminus = keyPoints[std::max(0, index - 1)].Rotation;
glm::quat qiplus = keyPoints[std::min(index + 1, (int)keyPoints.size() - 1)].Rotation;
glm::quat qinv = glm::inverse(qi);
return qi * glm::exp(-((glm::log(qinv * qiminus) + glm::log(qinv * qiplus)) / 4.0f));
}
@ -42,8 +51,8 @@ glm::mat4 Fish::animationMatrix(float time) {
std::vector<float> distances;
float timeStep = 0;
for (int i = 0; i < keyPoints.size() - 1; i++) {
timeStep += (keyPoints[i] - keyPoints[i + 1]).length();
distances.push_back((keyPoints[i] - keyPoints[i + 1]).length());
timeStep += (keyPoints[i].Point - keyPoints[i + 1].Point).length();
distances.push_back((keyPoints[i].Point - keyPoints[i + 1].Point).length());
}
time = fmod(time, timeStep);
@ -61,14 +70,13 @@ glm::mat4 Fish::animationMatrix(float time) {
int size = keyPoints.size() - 1;
//replace with catmul rom
//glm::vec3 pos = (keyPoints[std::max(0, index)] * t + keyPoints[std::min(size, index + 1)] * (1 - t));
glm::vec3 pos = glm::catmullRom(keyPoints[std::max(0, index - 1)], keyPoints[std::max(0, index)], keyPoints[std::min(index + 1, size)], keyPoints[std::min(index + 2, size)], t);
glm::vec3 pos = glm::catmullRom(keyPoints[std::max(0, index - 1)].Point, keyPoints[std::max(0, index)].Point, keyPoints[std::min(index + 1, size)].Point, keyPoints[std::min(index + 2, size)].Point, t);
//implement corect animation
auto animationRotation = glm::squad(keyRotation[index], keyRotation[std::min(index + 1, size)], calcA(index), calcA(index + 1), t);
//auto animationRotation = glm::squad(keyRotation[index], keyRotation[std::min(index + 1, size)], calcA(index), calcA(index + 1), t);
auto animationRotation = glm::squad(keyPoints[index].Rotation, keyPoints[std::min(index + 1, size)].Rotation, calcA(index), calcA(index + 1), t);
glm::mat4 result = glm::translate(pos) * glm::mat4_cast(animationRotation);
//return glm::scale(glm::vec3(scale)) * glm::rotate(glm::radians(180.0f), glm::vec3(1, 2, 0));
return result;
glm::mat4 result = glm::translate(pos) * glm::mat4_cast(animationRotation) * object.initialTransformation * glm::scale(glm::vec3(scale));
return result;
}

View File

@ -1,23 +1,26 @@
#pragma once
#include "Render_Utils.h"
#include "glew.h"
#include "freeglut.h"
#include "glm.hpp"
#include "ext.hpp"
#include <vector>
#include "KeyPoints.h"
class Fish {
private:
Core::RenderContext context;
GLuint textureId;
std::vector<glm::vec3> keyPoints;
Core::RenderObject object;
std::vector<KeyPointRotation> keyPoints;
std::vector<glm::quat> keyRotation;
float speed;
float scale;
glm::quat calcA(int index);
glm::mat4 getInitialRotation();
public:
Fish();
Fish(Core::RenderContext _context, GLuint _textureId, std::vector<glm::vec3> _keyPoints, float _speed, float _scale);
Fish(Core::RenderObject object, std::vector<KeyPointRotation> _keyPoints, float _speed, float _scale);
Core::RenderContext getContext();
GLuint getTextureId();
glm::mat4 animationMatrix(float time);

View File

@ -1,17 +1,57 @@
#include "KeyPoints.h"
void Core::initKeyPoints(std::vector<std::vector<glm::vec3>>& keyPoints) {
std::vector<glm::vec3> keyPoints1{
glm::vec3(-0.900827, -0.837407, 0.0714312),
glm::vec3(5.54503, 4.04398, 4.1869),
glm::vec3(-6.34033, 5.34694, 5.65957),
glm::vec3(-2.33467, 2.27294, 5.57716),
glm::vec3(-0.993145, 0.0885422, 1.82899),
glm::vec3(-1.58184, -1.03001, -2.36361),
glm::vec3(-4.5571, -0.317263, -6.44936),
glm::vec3(-8.20225, 1.68523, -8.13876),
glm::vec3(-10.7478, 1.61829, -8.44468),
glm::vec3(-8.79853, 4.69378, -4.0222)
void Core::initKeyPoints(std::vector<std::vector<KeyPointRotation>>& keyPoints) {
std::vector<KeyPointRotation> keyPoints1{
{glm::vec3(1.329308f, -0.329211f, 4.001679f), glm::quat(-0.220721f, 0.603405f, -0.178737f, -0.745143f)},
{glm::vec3(0.000000f, 0.000000f, 5.000000f), glm::quat(0.008705f, 0.069754f, -0.000609f, 0.997526f)},
{glm::vec3(-0.004341f, -0.000000f, 4.975380f), glm::quat(-0.000000f, 0.095846f, 0.000000f, 0.995396f)},
{glm::vec3(-0.065140f, -0.002618f, 4.605472f), glm::quat(-0.017428f, 0.052328f, 0.000913f, 0.998477f)},
{glm::vec3(-0.060358f, -0.022680f, 4.206686f), glm::quat(-0.026141f, -0.052318f, -0.001370f, 0.998287f)},
{glm::vec3(-0.049487f, -0.027914f, 4.107419f), glm::quat(-0.034739f, -0.095787f, -0.003345f, 0.994790f)},
{glm::vec3(0.022853f, -0.039252f, 3.842515f), glm::quat(-0.000000f, -0.147809f, -0.000000f, 0.989016f)},
{glm::vec3(0.146800f, -0.016605f, 3.437103f), glm::quat(0.077597f, -0.147354f, 0.011597f, 0.985967f)},
{glm::vec3(0.268701f, 0.064883f, 3.038382f), glm::quat(0.120531f, -0.146708f, 0.018013f, 0.981644f)},
{glm::vec3(0.389059f, 0.185857f, 2.677159f), glm::quat(0.205060f, -0.161441f, 0.034315f, 0.964733f)},
{glm::vec3(0.504850f, 0.390403f, 2.324083f), glm::quat(0.290770f, -0.099961f, 0.030561f, 0.951066f)},
{glm::vec3(0.511088f, 0.638072f, 1.981824f), glm::quat(0.307594f, 0.091155f, -0.029618f, 0.946678f)},
{glm::vec3(0.357549f, 0.856345f, 1.689975f), glm::quat(0.205503f, 0.396312f, -0.091496f, 0.890131f)},
{glm::vec3(0.038171f, 0.981373f, 1.444655f), glm::quat(0.046632f, 0.453368f, -0.023760f, 0.889785f)},
{glm::vec3(-0.261936f, 0.940139f, 1.153646f), glm::quat(-0.092360f, 0.266008f, 0.025614f, 0.959194f)},
{glm::vec3(-0.416927f, 0.797809f, 0.816381f), glm::quat(-0.255270f, 0.159424f, 0.042717f, 0.952679f)},
{glm::vec3(-0.496174f, 0.573713f, 0.465299f), glm::quat(-0.275543f, 0.025163f, 0.007215f, 0.960932f)},
{glm::vec3(-0.453296f, 0.356376f, 0.104439f), glm::quat(-0.248238f, -0.126369f, -0.032681f, 0.959865f)},
{glm::vec3(-0.323620f, 0.170533f, -0.224422f), glm::quat(-0.210459f, -0.227912f, -0.050527f, 0.949321f)},
{glm::vec3(-0.102625f, 0.033964f, -0.559631f), glm::quat(-0.140171f, -0.313819f, -0.046901f, 0.937907f)},
{glm::vec3(0.190719f, -0.080466f, -0.795414f), glm::quat(-0.085098f, -0.577522f, -0.060700f, 0.809656f)},
{glm::vec3(0.603630f, -0.149074f, -0.847929f), glm::quat(-0.081547f, -0.737605f, -0.090567f, 0.664143f)},
{glm::vec3(0.965145f, -0.276190f, -0.679523f), glm::quat(-0.072011f, -0.896137f, -0.158013f, 0.408394f)},
{glm::vec3(1.223547f, -0.421548f, -0.376012f), glm::quat(-0.052217f, -0.939228f, -0.165611f, 0.296138f)},
{glm::vec3(1.415053f, -0.552588f, -0.050745f), glm::quat(-0.031992f, -0.965572f, -0.144306f, 0.214063f)},
{glm::vec3(1.546427f, -0.679752f, 0.331774f), glm::quat(-0.018151f, -0.979413f, -0.172697f, 0.102941f)},
{glm::vec3(1.558644f, -0.839351f, 0.724004f), glm::quat(0.012171f, -0.978097f, -0.198996f, -0.059823f)},
{glm::vec3(1.469536f, -0.994031f, 1.081175f), glm::quat(0.030077f, -0.969770f, -0.179736f, -0.162283f)},
{glm::vec3(1.316302f, -1.088959f, 1.464742f), glm::quat(0.010601f, -0.982971f, -0.060121f, -0.173324f)},
{glm::vec3(1.250162f, -1.139453f, 1.879798f), glm::quat(0.000457f, -0.998591f, -0.052334f, -0.008714f)},
{glm::vec3(1.260571f, -1.181264f, 2.277251f), glm::quat(-0.001827f, -0.998021f, -0.052304f, 0.034852f)},
{glm::vec3(1.301331f, -1.222212f, 2.698203f), glm::quat(-0.002131f, -0.997527f, -0.034834f, 0.061012f)},
{glm::vec3(1.356522f, -1.237915f, 3.094028f), glm::quat(-0.001369f, -0.996765f, -0.017399f, 0.078448f)},
{glm::vec3(1.457414f, -1.252747f, 3.505888f), glm::quat(-0.003031f, -0.984658f, -0.017187f, 0.173622f)},
{glm::vec3(1.630766f, -1.271067f, 3.893110f), glm::quat(-0.006111f, -0.972036f, -0.025454f, 0.233366f)},
{glm::vec3(1.837539f, -1.279354f, 4.234686f), glm::quat(0.000000f, -0.948323f, 0.000000f, 0.317305f)},
{glm::vec3(2.135239f, -1.264522f, 4.535461f), glm::quat(0.007651f, -0.898657f, 0.015686f, 0.438305f)},
{glm::vec3(2.501256f, -1.249689f, 4.748298f), glm::quat(0.009885f, -0.824000f, 0.014383f, 0.566320f)},
{glm::vec3(2.894553f, -1.235730f, 4.807496f), glm::quat(0.018510f, -0.706864f, 0.018510f, 0.706865f)},
{glm::vec3(3.279297f, -1.209567f, 4.668199f), glm::quat(0.031757f, -0.414440f, 0.014473f, 0.909407f)},
{glm::vec3(3.557473f, -1.181664f, 4.382639f), glm::quat(0.032795f, -0.341812f, 0.011936f, 0.939120f)},
{glm::vec3(3.811645f, -1.135947f, 4.045370f), glm::quat(0.058223f, -0.300145f, 0.018358f, 0.951938f)},
{glm::vec3(4.030757f, -1.075943f, 3.686652f), glm::quat(0.084748f, -0.232557f, 0.020346f, 0.968670f)},
{glm::vec3(4.178676f, -0.987683f, 3.326471f), glm::quat(0.120961f, -0.120961f, 0.014852f, 0.985148f)},
{glm::vec3(4.238292f, -0.878539f, 2.921264f), glm::quat(0.139152f, -0.017282f, 0.002429f, 0.990117f)},
{glm::vec3(4.233769f, -0.752050f, 2.541922f), glm::quat(0.173642f, 0.008594f, -0.001515f, 0.984770f)},
{glm::vec3(4.237119f, -0.620841f, 2.138239f), glm::quat(0.095813f, 0.026057f, -0.002509f, 0.995055f)},
{glm::vec3(4.101572f, -0.574726f, 1.748940f), glm::quat(-0.000000f, 0.398749f, 0.000000f, 0.917060f)},
{glm::vec3(3.771825f, -0.581706f, 1.523900f), glm::quat(-0.007557f, 0.499981f, 0.004363f, 0.865992f)},
{glm::vec3(3.384779f, -0.579089f, 1.351094f), glm::quat(0.014119f, 0.587696f, -0.010258f, 0.808894f)}
};
keyPoints.push_back(keyPoints1);

View File

@ -3,6 +3,11 @@
#include "glm.hpp"
#include "ext.hpp"
struct KeyPointRotation {
glm::vec3 Point;
glm::quat Rotation;
};
namespace Core {
void initKeyPoints(std::vector<std::vector<glm::vec3>>& keyPoints);
void initKeyPoints(std::vector<std::vector<KeyPointRotation>>& keyPoints);
}

View File

@ -10,6 +10,7 @@
namespace Core
{
struct RenderContext
{
GLuint vertexArray;
@ -22,6 +23,12 @@ namespace Core
void initFromAssimpMesh(aiMesh* mesh);
};
struct RenderObject {
RenderContext context;
GLuint textureId;
glm::mat4 initialTransformation;
};
// vertexArray - jednowymiarowa tablica zawierajaca wartosci opisujace pozycje kolejnych wierzcholkow w jednym ciagu (x1, y1, z1, w1, x2, y2, z2, w2, ...)
// numVertices - liczba wierzcholkow do narysowania
// elementSize - liczba wartosci opisujacych pojedynczy wierzcholek (np. 3 gdy wierzcholek opisany jest trojka (x, y, z))

View File

@ -35,11 +35,13 @@ glm::mat4 cameraMatrix, perspectiveMatrix;
glm::vec3 lightDir = glm::vec3(0.0f, 100.0f, 0.0f);
glm::quat rotationX = glm::quat(1, 0, 0, 0);
glm::quat rotationY = glm::quat(1, 0, 0, 0);
glm::quat rotationX = glm::quat(0, 0, 0, 0);
glm::quat rotationY = glm::quat(0, 0, 0, 0);
glm::quat rotationZ = glm::quat(0, 0, 0, 0);
glm::quat rotation;
std::vector<glm::vec3> planetsCoords;
std::vector<std::vector<glm::vec3>> keyPoints;
std::vector<std::vector<KeyPointRotation>> keyPoints;
std::vector<Bubble> bubbles;
std::vector<Fish> fish;
@ -49,6 +51,8 @@ float mouseY = 0;
float prevX = 0;
float prevY = 0;
int prevTime;
GLuint sharkTexture;
GLuint fish1Texture;
@ -57,15 +61,15 @@ void keyboard(unsigned char key, int x, int y)
float angleSpeed = 0.1f;
float moveSpeed = 0.1f;
switch(key)
{
case 'z': cameraAngle -= angleSpeed; break;
case 'x': cameraAngle += angleSpeed; break;
case 'w': cameraPos += cameraDir * moveSpeed; break;
case 's': cameraPos -= cameraDir * moveSpeed; break;
case 'd': cameraPos += cameraSide * moveSpeed; break;
case 'a': cameraPos -= cameraSide * moveSpeed; break;
}
glm::quat rotationxd = glm::inverse(rotation);
if(key == 'z') cameraAngle -= angleSpeed;
if(key == 'x') cameraAngle += angleSpeed;
if(key == 'w') cameraPos += cameraDir * moveSpeed;
if(key == 's') cameraPos -= cameraDir * moveSpeed;
if(key == 'd') cameraPos += cameraSide * moveSpeed;
if(key == 'a') cameraPos -= cameraSide * moveSpeed;
if(key == 'q') printf("{glm::vec3(%ff, %ff, %ff), glm::quat(%ff, %ff, %ff, %ff)},\n", cameraPos.x, cameraPos.y, cameraPos.z, rotationxd.x, rotationxd.y, rotationxd.z, rotationxd.w);
}
void mouse(int x, int y)
@ -87,7 +91,7 @@ glm::mat4 createCameraMatrix()
rotationX = glm::normalize(xRotate * rotationX);
rotationY = glm::normalize(yRotate * rotationY);
rotationZ = glm::normalize(zRotate * rotationZ);
glm::quat rotation = rotationX * rotationY * rotationZ;
rotation = rotationX * rotationY * rotationZ;
cameraDir = glm::inverse(rotation) * glm::vec3(0.0f, 0.0f, -1.0f);
cameraSide = glm::inverse(rotation) * glm::vec3(1.0f, 0.0f, 0.0f);
return Core::createViewMatrixQuat(cameraPos,rotation);
@ -143,7 +147,7 @@ void drawBubbles()
void drawFish(float time)
{
for (Fish& fishElem : fish) {
drawObjectTexture(fishElem.getContext(), fishElem.animationMatrix(time), fishElem.getTextureId());
drawObjectTexture(fishElem.getContext(), fishElem.animationMatrix(time) * glm::rotate(glm::radians(90.0f), glm::vec3(0,0,1)), fishElem.getTextureId());
}
}
@ -154,11 +158,17 @@ void renderScene()
perspectiveMatrix = Core::createPerspectiveMatrix();
float time = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
glm::quat rotationxd = glm::inverse(rotation);
int currTime = (int)(time * 10);
if (currTime != prevTime && currTime % 5 == 0) {
printf("{glm::vec3(%ff, %ff, %ff), glm::quat(%ff, %ff, %ff, %ff)},\n", cameraPos.x, cameraPos.y, cameraPos.z, rotationxd.x, rotationxd.y, rotationxd.z, rotationxd.w);
prevTime = currTime;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glm::mat4 shipInitialTransformation = glm::translate(glm::vec3(0,-1.0f,-1.0f)) * glm::rotate(glm::radians(180.0f), glm::vec3(0,1,0)) * glm::scale(glm::vec3(0.25f));
glm::mat4 shipInitialTransformation = glm::translate(glm::vec3(0, -1.0f, -1.0f)) * glm::rotate(glm::radians(180.0f), glm::vec3(0,1,0)) * glm::scale(glm::vec3(0.25f));
glm::mat4 shipModelMatrix = glm::translate(cameraPos + cameraDir * 0.5f) * glm::mat4_cast(glm::inverse(rotationX * rotationY * rotationZ)) * shipInitialTransformation;
drawObjectTexture(sharkModel, shipModelMatrix, sharkTexture);
@ -190,23 +200,29 @@ void loadModelToContext(std::string path, Core::RenderContext& context)
void initBubbles() {
bubbles.insert(bubbles.end(), {
Bubble(0.7f, 1.0f, 1.0f),
Bubble(0.5f, 3.0f, 2.3f),
Bubble(0.2f, 1.0f, 1.0f),
Bubble(0.2f, 3.0f, 2.3f),
Bubble(0.2f, 5.7f, 1.2f),
Bubble(0.5f, 7.0f, 4.0f),
Bubble(0.4f, 4.7f, 3.7f),
Bubble(0.1f, 1.0f, 2.1f),
Bubble(0.3f, 2.6f, 8.4f),
Bubble(0.4f, 1.3f, 0.3f),
Bubble(0.6f, 5.2f, 2.1f),
Bubble(0.5f, 4.0f, 1.2f)
Bubble(0.2f, 7.0f, 4.0f),
Bubble(0.2f, 4.7f, 3.7f),
Bubble(0.2f, 1.0f, 2.1f),
Bubble(0.2f, 2.6f, 8.4f),
Bubble(0.2f, 1.3f, 0.3f),
Bubble(0.2f, 5.2f, 2.1f),
Bubble(0.2f, 4.0f, 1.2f)
}
);
}
void initFish() {
Core::RenderObject object;
loadModelToContext("models/fish_yellow.obj", object.context);
object.textureId = Core::LoadTexture("textures/fish_yellow.jpg");
object.initialTransformation = glm::rotate(glm::radians(90.0f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.01f));
fish.insert(fish.end(), {
Fish(fish1Model, fish1Texture, keyPoints[0], 1.0f, 1.0f)
Fish(object, keyPoints[0], 7.0f, 1.0f)
}
);
}
@ -223,15 +239,17 @@ void init()
programTexture = shaderLoader.CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag");
loadModelToContext("models/orca.obj", sharkModel);
loadModelToContext("models/sphere.obj", sphereContext);
loadModelToContext("models/fish_blue.obj", fish1Model);
fish1Texture = Core::LoadTexture("textures/fish_blue.jpg");
loadModelToContext("models/fish_yellow.obj", fish1Model);
fish1Texture = Core::LoadTexture("textures/fish_yellow.jpg");
sharkTexture = Core::LoadTexture("textures/Orca_Diffuse.jpg");
initBubbles();
initFish();
for (int i = 0; i < 10; i++) {
/*initRenderables();
initPhysicsScene();*/
/*for (int i = 0; i < 10; i++) {
float r = (float)(rand()) / (float)(RAND_MAX/20.0);
planetsCoords.push_back(glm::ballRand(r));
}
}*/
}
void shutdown()