diff --git a/grafika_projekt/shaders/bubble.frag b/grafika_projekt/shaders/bubble.frag index a172b2b..c7f8798 100644 --- a/grafika_projekt/shaders/bubble.frag +++ b/grafika_projekt/shaders/bubble.frag @@ -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,0.5); + + 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); + + } \ No newline at end of file diff --git a/grafika_projekt/shaders/bubble.vert b/grafika_projekt/shaders/bubble.vert index 170563c..d21f3f6 100644 --- a/grafika_projekt/shaders/bubble.vert +++ b/grafika_projekt/shaders/bubble.vert @@ -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); - } \ No newline at end of file diff --git a/grafika_projekt/src/main.cpp b/grafika_projekt/src/main.cpp index 52b1e8d..1e077ef 100644 --- a/grafika_projekt/src/main.cpp +++ b/grafika_projekt/src/main.cpp @@ -7,7 +7,6 @@ #include #include #include - #include "Shader_Loader.h" #include "Render_Utils.h" #include "Texture.h" @@ -15,16 +14,19 @@ #include "SOIL/stb_image_aug.h" GLuint skyboxProgram, skyboxBuffer; -GLuint cubeProgram, cubeBuffer; +GLuint bubbleProgram; GLuint programColor; GLuint programTexture; + GLuint textureSubmarine; GLuint textureBubble; GLuint textureFloor; GLuint textureFish; + unsigned int cubemapTexture, skyboxVAO; unsigned int cubeVAO, cubeVBO; -std::vector array[100]; + +std::vector bubbleArray[300]; float old_x, old_y = -1; glm::vec3 cursorDiff; glm::vec3 lightDir = glm::normalize(glm::vec3(1.0f, -10.f, -1.0f)); @@ -63,16 +65,6 @@ glm::vec3(-15.0f, -8.0f, -6.0f), glm::vec3(-18.0f, -10.0f, -10.0f), }); - - - - - - - - - - std::vector keyRotation; std::vector fish; @@ -89,35 +81,6 @@ std::string skyboxTextures[6] = { float skyboxVerticeParameter = 50.0f; float skyboxBoundary = 48.0f; - -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 genBubbleKeyPoints() { - float random1 = distr(gen); - float random2 = distr(gen); - std::vector bubbleKeyPoints({ - glm::vec3(random1 , -skyboxVerticeParameter, random2), - glm::vec3(random1 , skyboxVerticeParameter, random2) - } - ); - return bubbleKeyPoints; -}; - - -void generateBubbleArray() { - - for (int i = 0; i < 100; i++) { - array[i] = genBubbleKeyPoints(); - } -} - - - - - float cubeVertices[] = { // positions // normals -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, @@ -214,6 +177,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 genBubbleKeyPoints() { + float random1 = distr(gen); + float random2 = distr(gen); + std::vector 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; @@ -250,9 +235,6 @@ void keyboard(unsigned char key, int x, int y) } } - - - void mouse(int x, int y) { if (old_x >= 0) { @@ -348,8 +330,6 @@ glm::mat4 animationMatrix(float time, glm::vec3 change, std::vector 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); @@ -365,7 +345,6 @@ void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuin } - void renderScene() { cameraMatrix = createCameraMatrix(); @@ -387,7 +366,6 @@ void renderScene() 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 floorTransformation = glm::translate(glm::vec3(-50, -40, 50)) * glm::rotate(glm::radians(0.f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(1.f)); @@ -401,15 +379,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) { @@ -420,11 +392,8 @@ void renderScene() time -= 6; } - - } - - + //drawObjectTexture(bubbleContext, submarineInitialTransformation, cubemapTexture, bubbleProgram); drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture); drawObjectTexture(floorContext, floorTransformation, textureFloor, programTexture); glutSwapBuffers(); @@ -531,7 +500,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); @@ -560,7 +529,7 @@ void shutdown() shaderLoader.DeleteProgram(programColor); shaderLoader.DeleteProgram(programTexture); shaderLoader.DeleteProgram(skyboxProgram); - shaderLoader.DeleteProgram(cubeProgram); + shaderLoader.DeleteProgram(bubbleProgram); } @@ -586,11 +555,7 @@ int main(int argc, char** argv) glutIdleFunc(idle); glutSetCursor(GLUT_CURSOR_NONE); - - glutMainLoop(); - shutdown(); - return 0; } \ No newline at end of file