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

Reviewed-on: #11
This commit is contained in:
Konrad Jasiński 2022-01-12 13:29:25 +01:00
commit 13298e75f6
3 changed files with 58 additions and 91 deletions

View File

@ -3,28 +3,35 @@
in vec3 fragPos;
in vec3 interpNormal;
in float v_fresnel;
uniform vec3 cameraPos;
in vec3 incident;
uniform samplerCube bubble;
uniform vec3 lightPos;
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 Reflect = 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 refractionColor = texture(tex_map, Refract);
vec4 reflectionColor = texture(tex_map, Reflect);
//vec4 color = vec4(mix(flipColor,reflectionColor,0).rgb,1.0);
gl_FragColor = vec4(mix( flipColor,refractionColor,0).rgb,1.0);
vec4 envColor = vec4(mix( flipColor,refractionColor,0).rgb,0.5);
vec3 lightDir = normalize(lightPos-fragPos);
vec3 V = normalize(cameraPos-fragPos);
vec3 normal = normalize(interpNormal);
float diffuse = max(0,dot(normal,normalize(lightDir)));
vec3 R = reflect(-normalize(lightDir),normal);
float ambient = 0.5;
float specular = pow(max(0,dot(R,V)),10);
gl_FragColor = (envColor*(ambient + (1-ambient)*diffuse)+vec4(1)*specular*0.2);
}

View File

@ -7,33 +7,19 @@ 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

@ -7,7 +7,6 @@
#include <cmath>
#include <vector>
#include <random>
#include "Shader_Loader.h"
#include "Render_Utils.h"
#include "Texture.h"
@ -15,15 +14,18 @@
#include "SOIL/stb_image_aug.h"
GLuint skyboxProgram, skyboxBuffer;
GLuint cubeProgram, cubeBuffer;
GLuint bubbleProgram;
GLuint programColor;
GLuint programTexture;
GLuint textureSubmarine;
GLuint textureBubble;
GLuint textureFish;
unsigned int cubemapTexture, skyboxVAO;
unsigned int cubeVAO, cubeVBO;
std::vector<glm::vec3> array[100];
std::vector<glm::vec3> bubbleArray[300];
float old_x, old_y = -1;
glm::vec3 cursorDiff;
glm::vec3 lightDir = glm::normalize(glm::vec3(1.0f, -10.f, -1.0f));
@ -61,39 +63,6 @@ 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;
@ -206,6 +175,28 @@ bool isInBoundaries(glm::vec3 nextPosition) {
}
std::random_device rd; // obtain a random number from hardware
std::mt19937 gen(rd()); // seed the generator
std::uniform_int_distribution<> distr(-skyboxVerticeParameter, skyboxVerticeParameter); // define the range
std::vector<glm::vec3> genBubbleKeyPoints() {
float random1 = distr(gen);
float random2 = distr(gen);
std::vector<glm::vec3> bubbleKeyPoints({
glm::vec3(random1 , -skyboxVerticeParameter, random2),
glm::vec3(random1 , skyboxVerticeParameter, random2)
}
);
return bubbleKeyPoints;
};
void generateBubbleArray() {
for (int i = 0; i < 300; i++) {
bubbleArray[i] = genBubbleKeyPoints();
}
}
void keyboard(unsigned char key, int x, int y)
{
float angleSpeed = 10.f;
@ -242,9 +233,6 @@ void keyboard(unsigned char key, int x, int y)
}
}
void mouse(int x, int y)
{
if (old_x >= 0) {
@ -340,8 +328,6 @@ glm::mat4 animationMatrix(float time, glm::vec3 change, std::vector<glm::vec3> k
void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuint textureId, GLuint program)
{
glUseProgram(program);
glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z);
@ -357,7 +343,6 @@ void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuin
}
void renderScene()
{
cameraMatrix = createCameraMatrix();
@ -376,6 +361,8 @@ void renderScene()
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
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;
@ -389,15 +376,9 @@ void renderScene()
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);
for (int j = 0; j < 100; j++) {
drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.04f), 0.2f), cubemapTexture, bubbleProgram);
}
for (int i = 0; i < 5; i++) {
if (time > -10) {
@ -408,11 +389,8 @@ void renderScene()
time -= 6;
}
}
//drawObjectTexture(bubbleContext, submarineInitialTransformation, cubemapTexture, bubbleProgram);
drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture);
glutSwapBuffers();
}
@ -518,7 +496,7 @@ void init()
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");
bubbleProgram = shaderLoader.CreateProgram((char*)"shaders/bubble.vert", (char*)"shaders/bubble.frag");
cubemapTexture = loadCubemap();
loadModelToContext("models/submarine.obj", submarineContext);
@ -532,7 +510,7 @@ void init()
textureSubmarine = Core::LoadTexture("textures/submarine.png");
loadModelToContext("models/sphere.obj", bubbleContext);
textureBubble = Core::LoadTexture("textures/bubble.png");
generateArray();
generateBubbleArray();
initCube();
initSkybox();
@ -543,7 +521,7 @@ void shutdown()
shaderLoader.DeleteProgram(programColor);
shaderLoader.DeleteProgram(programTexture);
shaderLoader.DeleteProgram(skyboxProgram);
shaderLoader.DeleteProgram(cubeProgram);
shaderLoader.DeleteProgram(bubbleProgram);
}
@ -569,11 +547,7 @@ int main(int argc, char** argv)
glutIdleFunc(idle);
glutSetCursor(GLUT_CURSOR_NONE);
glutMainLoop();
shutdown();
return 0;
}