Merge pull request 'bubbles' (#10) from bubbles into master

Reviewed-on: #10
This commit is contained in:
Konrad Jasiński 2022-01-08 19:56:25 +01:00
commit c14b1a6f34
8 changed files with 3198 additions and 55 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,30 @@
#version 430 compatibility
in vec3 fragPos;
in vec3 interpNormal;
in float v_fresnel;
uniform vec3 cameraPos;
in vec3 incident;
uniform samplerCube bubble;
layout (binding = 0) uniform samplerCube tex_map;
void main()
{
float ratio = 1.33 / 1.00;
vec3 I = normalize(fragPos - cameraPos);
vec3 Refract = refract(I, normalize(interpNormal), ratio);
vec3 R = reflect(I, normalize(interpNormal));
vec3 flip = reflect(normalize(-fragPos), normalize(interpNormal));
vec4 flipColor = texture( tex_map, flip );
vec4 refractionColor = texture( tex_map, Refract );
vec4 reflectionColor = texture( tex_map, R );
//vec4 color = vec4(mix(flipColor,reflectionColor,0).rgb,1.0);
gl_FragColor = vec4(mix( flipColor,refractionColor,0).rgb,1.0);
}

View File

@ -0,0 +1,39 @@
#version 430 compatibility
layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexNormal;
out vec3 interpNormal;
out vec3 fragPos;
out vec3 TexCoords;
out vec3 incident;
out float v_fresnel;
uniform vec3 viewPos;
// Indices of refraction
const float Air = 1.0;
const float Glass = 4; //1.51714;//4
// Air to glass ratio of the indices of refraction (Eta)
const float Eta = Air / Glass;
// see http://en.wikipedia.org/wiki/Refractive_index Reflectivity
const float R0 = ((Air - Glass) * (Air - Glass)) / ((Air + Glass) * (Air + Glass));
uniform mat4 modelMatrix;
uniform mat4 modelViewProjectionMatrix;
layout (binding = 0) uniform samplerCube tex_map;
void main()
{
vec4 vertex = modelMatrix * vec4( vertexPosition, 1.0 );
vec4 camera = vec4( viewPos, 1.0 );
incident = normalize( vec3( vertex - camera ) );
v_fresnel = R0 + (1.0 - R0) * pow( (1.0 - dot( -incident, vertexNormal ) ), 5.0);
interpNormal = mat3(transpose(inverse(modelMatrix))) * vertexNormal;
fragPos = (modelMatrix * vec4(vertexPosition, 1.0)).xyz;
gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
}

View File

@ -10,8 +10,24 @@ in vec3 fragPos;
in vec3 interpNormal;
in vec2 interpTexCoord;
float near = 0.02f;
float far = 100.0f;
float linearizeDepth(float depth)
{
return (2.0 * near * far) / (far + near - (depth * 2.0 - 1.0) * (far - near));
}
float logisticDepth(float depth, float steepness, float offset)
{
float zVal = linearizeDepth(depth);
return (1 / (1 + exp(-steepness * (zVal - offset))));
}
void main()
{
float depth = logisticDepth(gl_FragCoord.z, 0.1f, 3.0f);
vec3 lightDir = normalize(lightPos-fragPos);
vec3 V = normalize(cameraPos-fragPos);
vec2 modifiedTexCoord = vec2(interpTexCoord.x, 1.0 - interpTexCoord.y); // Poprawka dla tekstur Ziemi, ktore bez tego wyswietlaja sie 'do gory nogami'
@ -21,6 +37,6 @@ void main()
vec3 R = reflect(-normalize(lightDir),normal);
float ambient = 0.2;
float specular = pow(max(0,dot(R,V)),1000);
gl_FragColor = vec4(color*(ambient + (1-ambient)*diffuse)+vec3(1)*specular*0.2, 1.0);
gl_FragColor = vec4(color*(ambient + (1-ambient)*diffuse)+vec3(1)*specular*0.2, 1.0) * (1.0f - depth) + vec4(depth * vec3(0.0f, 0.109f, 0.447f), 1.0f);;
}

View File

@ -5,7 +5,22 @@ in vec3 TexCoords;
uniform samplerCube skybox;
float near = 0.02f;
float far = 100.0f;
float linearizeDepth(float depth)
{
return (2.0 * near * far) / (far + near - (depth * 2.0 - 1.0) * (far - near));
}
float logisticDepth(float depth, float steepness, float offset)
{
float zVal = linearizeDepth(depth);
return (1 / (1 + exp(-steepness * (zVal - offset))));
}
void main()
{
FragColor = texture(skybox, TexCoords);
float depth = logisticDepth(gl_FragCoord.z, 0.1f, 3.0f);
FragColor = texture(skybox, TexCoords)* (1.0f - depth) + vec4(depth * vec3(0.0f, 0.109f, 0.447f), 1.0f);
}

View File

@ -15,16 +15,21 @@
#include "SOIL/stb_image_aug.h"
GLuint skyboxProgram, skyboxBuffer;
GLuint cubeProgram, cubeBuffer;
GLuint programColor;
GLuint programTexture;
GLuint textureSubmarine;
GLuint textureBubble;
GLuint textureFish;
unsigned int cubemapTexture, skyboxVAO;
unsigned int cubeVAO, cubeVBO;
std::vector<glm::vec3> array[100];
float old_x, old_y = -1;
glm::vec3 cursorDiff;
glm::vec3 lightDir = glm::normalize(glm::vec3(1.0f, -10.f, -1.0f));
glm::vec3 cameraPos = glm::vec3(0, 0, 5);
glm::vec3 cameraPos = glm::vec3(0, 0, 0);
glm::vec3 oldCameraPos = glm::vec3(0, 0, 5);
glm::vec3 cameraDir; // Wektor "do przodu" kamery
glm::vec3 cameraSide; // Wektor "w bok" kamery
float cameraAngle = 0;
@ -36,8 +41,9 @@ glm::mat4 cameraMatrix, perspectiveMatrix;
Core::Shader_Loader shaderLoader;
Core::RenderContext submarineContext;
Core::RenderContext fishContext;
Core::RenderContext bubbleContext;
std::vector<glm::vec3> keyPoints({
std::vector<glm::vec3> fishKeyPoints({
glm::vec3(-18.0f, -10.0f, -10.0f),
glm::vec3(-10.0f, -5.0f, -12.0f),
glm::vec3(8.0f, -3.0f, -3.0f),
@ -52,10 +58,42 @@ glm::vec3(-1.0f, 4.0f, 8.0f),
glm::vec3(-8.0f, 0.0f, 3.0f),
glm::vec3(-12.0f, -6.0f, -3.0f),
glm::vec3(-15.0f, -8.0f, -6.0f),
glm::vec3(-18.0f, -10.0f, -10.0f),
});
std::random_device rd; // obtain a random number from hardware
std::mt19937 gen(rd()); // seed the generator
std::uniform_int_distribution<> distr(-50, 50); // define the range
std::vector<glm::vec3> genBubbleKeyPoints(){
float random1 = distr(gen);
float random2 = distr(gen);
std::vector<glm::vec3> bubbleKeyPoints({
glm::vec3(random1 , -50.0f, random2),
glm::vec3(random1 , 50.0f, random2)
}
);
return bubbleKeyPoints;
};
void generateArray() {
for (int i = 0; i < 100; i++) {
array[i] = genBubbleKeyPoints();
}
}
std::vector<glm::quat> keyRotation;
std::vector<Core::Node> fish;
@ -72,6 +110,51 @@ std::string skyboxTextures[6] = {
float skyboxVerticeParameter = 50.0f;
float skyboxBoundary = 48.0f;
float cubeVertices[] = {
// positions // normals
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
};
float skyboxVertices[] = {
-skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter,
-skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter,
@ -118,8 +201,8 @@ float skyboxVertices[] = {
bool isInBoundaries(glm::vec3 nextPosition) {
return nextPosition.z > -skyboxBoundary && nextPosition.z < skyboxBoundary && nextPosition.y > -skyboxBoundary &&
nextPosition.y < skyboxBoundary && nextPosition.x < skyboxBoundary && nextPosition.x > -skyboxBoundary;
return nextPosition.z > -skyboxBoundary && nextPosition.z < skyboxBoundary&& nextPosition.y > -skyboxBoundary &&
nextPosition.y < skyboxBoundary&& nextPosition.x < skyboxBoundary&& nextPosition.x > -skyboxBoundary;
}
@ -132,25 +215,25 @@ void keyboard(unsigned char key, int x, int y)
{
case 'z': cursorDiff.z -= angleSpeed; break;
case 'x': cursorDiff.z += angleSpeed; break;
case 'w':
case 'w':
nextPosition = cameraPos + (cameraDir * moveSpeed);
if (isInBoundaries(nextPosition)) {
cameraPos = nextPosition;
}
break;
case 's':
case 's':
nextPosition = cameraPos - (cameraDir * moveSpeed);
if (isInBoundaries(nextPosition)) {
cameraPos = nextPosition;
}
break;
case 'd':
case 'd':
nextPosition = cameraPos + (cameraSide * moveSpeed);
if (isInBoundaries(nextPosition)) {
cameraPos = nextPosition;
}
break;
case 'a':
case 'a':
nextPosition = cameraPos - (cameraSide * moveSpeed);
if (isInBoundaries(nextPosition)) {
cameraPos = nextPosition;
@ -159,6 +242,9 @@ void keyboard(unsigned char key, int x, int y)
}
}
void mouse(int x, int y)
{
if (old_x >= 0) {
@ -209,12 +295,12 @@ std::vector<glm::vec3> changeKeyPoints(std::vector<glm::vec3> keyPoints, glm::ve
change.z = keyPoints[i].z + toChange.z;
result.push_back(change);
}
return result;
}
glm::mat4 animationMatrix(float time, glm::vec3 change) {
float speed = 1.;
glm::mat4 animationMatrix(float time, glm::vec3 change, std::vector<glm::vec3> keyPoints, glm::vec3 scaleValue, float speed) {
time = time * speed;
std::vector<float> distances;
std::vector<glm::vec3> newKeyPoints = changeKeyPoints(keyPoints, change);
@ -238,41 +324,23 @@ glm::mat4 animationMatrix(float time, glm::vec3 change) {
int size = keyPoints.size();
int rotationSize = keyRotation.size();
glm::vec3 pos = glm::catmullRom(newKeyPoints[std::max(0, (index-1)%size)], newKeyPoints[(index) % size], newKeyPoints[(index + 1) % size], newKeyPoints[(index + 2) % size], t);
glm::vec3 pos = glm::catmullRom(newKeyPoints[std::max(0, (index - 1) % size)], newKeyPoints[(index) % size], newKeyPoints[(index + 1) % size], newKeyPoints[(index + 2) % size], t);
glm::quat divideByFour = glm::quat(0.25f, 0.25f, 0.25f, 0.25f);
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 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) % 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 % rotationSize], keyRotation[(index + 1) % rotationSize], a1, a2, t);
glm::mat4 result = glm::translate(pos) * glm::scale(glm::vec3(0.5f)) * glm::mat4_cast(animationRotation);
glm::mat4 result = glm::translate(pos) * glm::scale(glm::vec3(scaleValue)) * glm::mat4_cast(animationRotation);
return result;
}
void drawObjectColor(Core::RenderContext context, glm::mat4 modelMatrix, glm::vec3 color)
void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuint textureId, GLuint program)
{
GLuint program = programColor;
glUseProgram(program);
glUniform3f(glGetUniformLocation(program, "objectColor"), color.x, color.y, color.z);
glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z);
glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "modelViewProjectionMatrix"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
Core::DrawContext(context);
glUseProgram(0);
}
void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuint textureId)
{
GLuint program = programTexture;
glUseProgram(program);
@ -288,12 +356,16 @@ void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuin
glUseProgram(0);
}
void renderScene()
{
cameraMatrix = createCameraMatrix();
perspectiveMatrix = Core::createPerspectiveMatrix();
glClearColor(0.219f, 0.407f, 0.658f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float time = glutGet(GLUT_ELAPSED_TIME) / 1000.f;
glUseProgram(skyboxProgram);
glUniform1i(glGetUniformLocation(skyboxProgram, "skybox"), 0);
@ -303,26 +375,45 @@ void renderScene()
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
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 submarineModelMatrix = glm::translate(cameraPos + cameraDir) * glm::mat4_cast(glm::inverse(rotation)) * submarineInitialTransformation;
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));
glm::mat4 bubbleInitialTransformation = 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.5f));
glm::vec3 change1 = glm::vec3(0, 3, 0);
glm::vec3 change2 = glm::vec3(0, 0, 0);
glm::vec3 change3 = glm::vec3(3, 0, 0);
glm::vec3 change4 = glm::vec3(0, 2, 1);
for (int i = 0; i < 4; i++) {
if (time > -10) {
drawObjectTexture(fishContext, animationMatrix(time + 15, change1), textureFish);
drawObjectTexture(fishContext, animationMatrix(time + 15, change2), textureFish);
drawObjectTexture(fishContext, animationMatrix(time + 15, change3), textureFish);
drawObjectTexture(fishContext, animationMatrix(time + 15, change4), textureFish);
time -= 8;
}
glm::vec3 change0 = glm::vec3(0, 0, 0);
for (int j = 0; j < 50; j++) {
drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, array[j], glm::vec3(0.1f), 0.99f), cubemapTexture, cubeProgram);
}
drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine);
for (int i = 0; i < 5; i++) {
if (time > -10) {
drawObjectTexture(fishContext, animationMatrix(time + 15, change1, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture);
drawObjectTexture(fishContext, animationMatrix(time + 15, change2, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture);
drawObjectTexture(fishContext, animationMatrix(time + 15, change3, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture);
drawObjectTexture(fishContext, animationMatrix(time + 15, change4, fishKeyPoints, glm::vec3(0.25f), 1.f), textureFish, programTexture);
time -= 6;
}
}
drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture);
glutSwapBuffers();
}
@ -394,9 +485,9 @@ void initKeyRotation() {
glm::quat oldRotationCamera = glm::quat(1, 0, 0, 0);
glm::vec3 direction;
glm::quat rotation;
for (int i = 0; i < keyPoints.size() - 1; i++) {
for (int i = 0; i < fishKeyPoints.size() - 1; i++) {
//3.1
direction = glm::normalize(keyPoints[i + 1] - keyPoints[i]);
direction = glm::normalize(fishKeyPoints[i + 1] - fishKeyPoints[i]);
//3.2
rotation = glm::normalize(glm::rotationCamera(oldDirection, direction) * oldRotationCamera);
//3.3
@ -408,14 +499,28 @@ void initKeyRotation() {
keyRotation.push_back(glm::quat(1, 0, 0, 0));
}
void initCube()
{
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &cubeVBO);
glBindVertexArray(cubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
}
void init()
{
glEnable(GL_DEPTH_TEST);
programColor = shaderLoader.CreateProgram((char*) "shaders/shader_color.vert", (char*) "shaders/shader_color.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");
programColor = shaderLoader.CreateProgram((char*)"shaders/shader_color.vert", (char*)"shaders/shader_color.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");
cubeProgram = shaderLoader.CreateProgram((char*)"shaders/bubble.vert", (char*)"shaders/bubble.frag");
cubemapTexture = loadCubemap();
loadModelToContext("models/submarine.obj", submarineContext);
textureSubmarine = Core::LoadTexture("textures/submarine.png");
@ -423,7 +528,14 @@ void init()
textureFish = Core::LoadTexture("textures/fish.png");
initKeyRotation();
loadModelToContext("models/submarine.obj", submarineContext);
textureSubmarine = Core::LoadTexture("textures/submarine.png");
loadModelToContext("models/sphere.obj", bubbleContext);
textureBubble = Core::LoadTexture("textures/bubble.png");
generateArray();
initCube();
initSkybox();
}
void shutdown()
@ -431,6 +543,8 @@ void shutdown()
shaderLoader.DeleteProgram(programColor);
shaderLoader.DeleteProgram(programTexture);
shaderLoader.DeleteProgram(skyboxProgram);
shaderLoader.DeleteProgram(cubeProgram);
}
void idle()

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 126 KiB