From ce3acacbcf63f6a4d5e1f18e0eed52ea6e2bc6bd Mon Sep 17 00:00:00 2001 From: Thyme1 Date: Sat, 8 Jan 2022 22:34:09 +0100 Subject: [PATCH 1/6] add transparency for bubbles --- grafika_projekt/shaders/bubble.frag | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grafika_projekt/shaders/bubble.frag b/grafika_projekt/shaders/bubble.frag index f3dbd26..a172b2b 100644 --- a/grafika_projekt/shaders/bubble.frag +++ b/grafika_projekt/shaders/bubble.frag @@ -26,5 +26,5 @@ void main() //vec4 color = vec4(mix(flipColor,reflectionColor,0).rgb,1.0); - gl_FragColor = vec4(mix( flipColor,refractionColor,0).rgb,1.0); + gl_FragColor = vec4(mix( flipColor,refractionColor,0).rgb,0.5); } \ No newline at end of file From c11c78114ff636c950db31e5b1a3fd56f44e1b8c Mon Sep 17 00:00:00 2001 From: Thyme1 Date: Sat, 8 Jan 2022 22:35:24 +0100 Subject: [PATCH 2/6] add transparency for bubbles --- grafika_projekt/src/main.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/grafika_projekt/src/main.cpp b/grafika_projekt/src/main.cpp index 1879ed7..935a0d9 100644 --- a/grafika_projekt/src/main.cpp +++ b/grafika_projekt/src/main.cpp @@ -376,6 +376,9 @@ 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; @@ -390,7 +393,7 @@ 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); @@ -412,7 +415,7 @@ void renderScene() } - + drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture); glutSwapBuffers(); } From acd223d652254d1b315fe08348661670dd511760 Mon Sep 17 00:00:00 2001 From: Thyme1 Date: Mon, 10 Jan 2022 17:31:56 +0100 Subject: [PATCH 3/6] clean main --- .../7fed72ea74378557bed202e4042e1d/main.cpp | 553 ++++++++++++++++++ grafika_projekt/src/main.cpp | 93 +-- 2 files changed, 585 insertions(+), 61 deletions(-) create mode 100644 enc_temp_folder/7fed72ea74378557bed202e4042e1d/main.cpp diff --git a/enc_temp_folder/7fed72ea74378557bed202e4042e1d/main.cpp b/enc_temp_folder/7fed72ea74378557bed202e4042e1d/main.cpp new file mode 100644 index 0000000..34c8fe9 --- /dev/null +++ b/enc_temp_folder/7fed72ea74378557bed202e4042e1d/main.cpp @@ -0,0 +1,553 @@ +#define STB_IMAGE_IMPLEMENTATION +#include "glew.h" +#include "freeglut.h" +#include "glm.hpp" +#include "ext.hpp" +#include +#include +#include +#include +#include "Shader_Loader.h" +#include "Render_Utils.h" +#include "Texture.h" +#include "Camera.h" +#include "SOIL/stb_image_aug.h" + +GLuint skyboxProgram, skyboxBuffer; +GLuint bubbleProgram; +GLuint programColor; +GLuint programTexture; + +GLuint textureSubmarine; +GLuint textureBubble; +GLuint textureFish; + +unsigned int cubemapTexture, skyboxVAO; +unsigned int cubeVAO, cubeVBO; + +std::vector bubbleArray[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, 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; + +glm::quat rotation = glm::quat(1, 0, 0, 0); + +glm::mat4 cameraMatrix, perspectiveMatrix; + +Core::Shader_Loader shaderLoader; +Core::RenderContext submarineContext; +Core::RenderContext fishContext; +Core::RenderContext bubbleContext; + +std::vector 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), +glm::vec3(5.0f, 0.0f, 3.0f), +glm::vec3(3.0f, 2.0f, 4.0f), +glm::vec3(8.0f, 5.0f, 9.0f), +glm::vec3(14.0f, 6.0f, 15.0f), +glm::vec3(15.0f, 12.0f, 12.0f), +glm::vec3(10.0f, 17.0f, 15.0f), +glm::vec3(5.0f, 10.0f, 7.0f), +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::vector keyRotation; + +std::vector fish; + +std::string skyboxTextures[6] = { + "models/skybox/right.jpg", + "models/skybox/left.jpg", + "models/skybox/top.jpg", + "models/skybox/bottom.jpg", + "models/skybox/front.jpg", + "models/skybox/back.jpg" +}; + +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, + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + + -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + + -skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter +}; + + +bool isInBoundaries(glm::vec3 nextPosition) { + return nextPosition.z > -skyboxBoundary && nextPosition.z < skyboxBoundary&& nextPosition.y > -skyboxBoundary && + nextPosition.y < skyboxBoundary&& nextPosition.x < skyboxBoundary&& nextPosition.x > -skyboxBoundary; +} + + +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++) { + bubbleArray[i] = genBubbleKeyPoints(); + } +} + +void keyboard(unsigned char key, int x, int y) +{ + float angleSpeed = 10.f; + float moveSpeed = 1.0f; + glm::vec3 nextPosition; + switch (key) + { + case 'z': cursorDiff.z -= angleSpeed; break; + case 'x': cursorDiff.z += angleSpeed; break; + case 'w': + nextPosition = cameraPos + (cameraDir * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; + case 's': + nextPosition = cameraPos - (cameraDir * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; + case 'd': + nextPosition = cameraPos + (cameraSide * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; + case 'a': + nextPosition = cameraPos - (cameraSide * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; + } +} + +void mouse(int x, int y) +{ + if (old_x >= 0) { + cursorDiff.x = x - old_x; + cursorDiff.y = y - old_y; + } + old_x = x; + old_y = y; + + if (x < 100 || x > 800 - 100) { //you can use values other than 100 for the screen edges if you like, kind of seems to depend on your mouse sensitivity for what ends up working best + old_x = 800 / 2; //centers the last known position, this way there isn't an odd jump with your cam as it resets + old_y = 800 / 2; + glutWarpPointer(800 / 2, 800 / 2); //centers the cursor + } + else if (y < 100 || y > 800 - 100) { + old_x = 800 / 2; + old_y = 800 / 2; + glutWarpPointer(800 / 2, 800 / 2); + } +} + +glm::mat4 createCameraMatrix() +{ + glm::quat rotation_x = glm::angleAxis(cursorDiff.y * 0.03f, glm::vec3(1, 0, 0)); + cursorDiff.y = 0; + glm::quat rotation_y = glm::angleAxis(cursorDiff.x * 0.03f, glm::vec3(0, 1, 0)); + cursorDiff.x = 0; + glm::quat rotation_z = glm::angleAxis(cursorDiff.z * 0.03f, glm::vec3(0, 0, 1)); + cursorDiff.z = 0; + + glm::quat rotationChange = rotation_x * rotation_y * rotation_z; + rotation = glm::normalize(rotationChange * rotation); + + cameraDir = glm::inverse(rotation) * glm::vec3(0, 0, -1); + cameraSide = glm::inverse(rotation) * glm::vec3(1, 0, 0); + + return Core::createViewMatrixQuat(cameraPos, rotation); +} + +std::vector changeKeyPoints(std::vector keyPoints, glm::vec3 toChange) { + std::vector result; + int size = keyPoints.size(); + glm::vec3 change; + + for (int i = 0; i < size; i++) { + change.x = keyPoints[i].x + toChange.x; + change.y = keyPoints[i].y + toChange.y; + change.z = keyPoints[i].z + toChange.z; + result.push_back(change); + } + + return result; +} + +glm::mat4 animationMatrix(float time, glm::vec3 change, std::vector keyPoints, glm::vec3 scaleValue, float speed) { + + time = time * speed; + std::vector distances; + std::vector newKeyPoints = changeKeyPoints(keyPoints, change); + 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()); + } + time = fmod(time, timeStep); + + //index of first keyPoint + int index = 0; + + while (distances[index] <= time) { + time = time - distances[index]; + index += 1; + } + + float t = time / distances[index]; + + 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::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 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(scaleValue)) * glm::mat4_cast(animationRotation); + + return result; +} + +void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuint textureId, GLuint program) +{ + glUseProgram(program); + + glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z); + Core::SetActiveTexture(textureId, "textureSampler", program, 0); + + 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 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); + glm::mat4 transformation = perspectiveMatrix * cameraMatrix; + glUniformMatrix4fv(glGetUniformLocation(skyboxProgram, "projectionViewMatrix"), 1, GL_FALSE, (float*)&transformation); + glBindVertexArray(skyboxVAO); + glActiveTexture(GL_TEXTURE0); + 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; + + 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); + + glm::vec3 change0 = glm::vec3(0, 0, 0); + + for (int j = 0; j < 50; j++) { + drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.1f), 0.09f), cubemapTexture, bubbleProgram); + } + + 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(bubbleContext, submarineInitialTransformation, cubemapTexture, bubbleProgram); + drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture); + glutSwapBuffers(); +} + +void loadModelToContext(std::string path, Core::RenderContext& context) +{ + Assimp::Importer import; + const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace); + + if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) + { + std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl; + return; + } + context.initFromAssimpMesh(scene->mMeshes[0]); +} + +unsigned int loadCubemap() +{ + unsigned int textureID; + glGenTextures(1, &textureID); + glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); + + int width, height, nrChannels; + for (unsigned int i = 0; i < 6; i++) + { + unsigned char* data = stbi_load(skyboxTextures[i].c_str(), &width, &height, &nrChannels, STBI_rgb_alpha); + if (data) + { + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, + 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data + ); + stbi_image_free(data); + } + else + { + std::cout << stbi_failure_reason() << std::endl; + std::cout << "Cubemap tex failed to load at path: " << skyboxTextures[i] << std::endl; + stbi_image_free(data); + } + } + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + + return textureID; +} + +void initSkybox() +{ + glGenVertexArrays(1, &skyboxVAO); + glBindVertexArray(skyboxVAO); + + glGenBuffers(1, &skyboxBuffer); + glBindBuffer(GL_ARRAY_BUFFER, skyboxBuffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW); + + GLuint vPosition = glGetAttribLocation(skyboxProgram, "aPos"); + glEnableVertexAttribArray(vPosition); + + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(skyboxVertices), skyboxVertices); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); +} + +void initKeyRotation() { + glm::vec3 oldDirection = glm::vec3(0, 0, 1); + glm::quat oldRotationCamera = glm::quat(1, 0, 0, 0); + glm::vec3 direction; + glm::quat rotation; + for (int i = 0; i < fishKeyPoints.size() - 1; i++) { + //3.1 + direction = glm::normalize(fishKeyPoints[i + 1] - fishKeyPoints[i]); + //3.2 + rotation = glm::normalize(glm::rotationCamera(oldDirection, direction) * oldRotationCamera); + //3.3 + keyRotation.push_back(rotation); + //3.4 + oldDirection = direction; + oldRotationCamera = rotation; + } + 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"); + bubbleProgram = shaderLoader.CreateProgram((char*)"shaders/bubble.vert", (char*)"shaders/bubble.frag"); + cubemapTexture = loadCubemap(); + + loadModelToContext("models/submarine.obj", submarineContext); + textureSubmarine = Core::LoadTexture("textures/submarine.png"); + + loadModelToContext("models/fish.obj", fishContext); + 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"); + generateBubbleArray(); + initCube(); + initSkybox(); + +} + +void shutdown() +{ + shaderLoader.DeleteProgram(programColor); + shaderLoader.DeleteProgram(programTexture); + shaderLoader.DeleteProgram(skyboxProgram); + shaderLoader.DeleteProgram(bubbleProgram); + +} + +void idle() +{ + glutPostRedisplay(); +} + + +int main(int argc, char** argv) +{ + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); + glutInitWindowPosition(200, 200); + glutInitWindowSize(800, 800); + glutCreateWindow("Water and shit"); + glewInit(); + + init(); + glutKeyboardFunc(keyboard); + glutPassiveMotionFunc(mouse); + glutDisplayFunc(renderScene); + glutIdleFunc(idle); + + glutSetCursor(GLUT_CURSOR_NONE); + glutMainLoop(); + shutdown(); + return 0; +} \ No newline at end of file diff --git a/grafika_projekt/src/main.cpp b/grafika_projekt/src/main.cpp index 935a0d9..34c8fe9 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,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 array[100]; + +std::vector bubbleArray[100]; 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 genBubbleKeyPoints(){ - float random1 = distr(gen); - float random2 = distr(gen); - std::vector 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 keyRotation; std::vector 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 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++) { + 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 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(); @@ -379,7 +364,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 submarineModelMatrix = glm::translate(cameraPos + cameraDir) * glm::mat4_cast(glm::inverse(rotation)) * submarineInitialTransformation; @@ -392,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); + drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.1f), 0.09f), cubemapTexture, bubbleProgram); } - - - for (int i = 0; i < 5; i++) { if (time > -10) { @@ -411,11 +389,8 @@ void renderScene() time -= 6; } - - } - - + drawObjectTexture(bubbleContext, submarineInitialTransformation, cubemapTexture, bubbleProgram); drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture); glutSwapBuffers(); } @@ -521,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); @@ -535,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(); @@ -546,7 +521,7 @@ void shutdown() shaderLoader.DeleteProgram(programColor); shaderLoader.DeleteProgram(programTexture); shaderLoader.DeleteProgram(skyboxProgram); - shaderLoader.DeleteProgram(cubeProgram); + shaderLoader.DeleteProgram(bubbleProgram); } @@ -572,11 +547,7 @@ int main(int argc, char** argv) glutIdleFunc(idle); glutSetCursor(GLUT_CURSOR_NONE); - - glutMainLoop(); - shutdown(); - return 0; } \ No newline at end of file From 0c12d3adf3aacdcfb0dd83e3a74fc3dd61e2cd13 Mon Sep 17 00:00:00 2001 From: Thyme1 Date: Mon, 10 Jan 2022 21:14:14 +0100 Subject: [PATCH 4/6] clean bubble shaders --- .../7fed72ea74378557bed202e4042e1d/main.cpp | 553 ------------------ grafika_projekt/shaders/bubble.frag | 18 +- grafika_projekt/shaders/bubble.vert | 20 +- 3 files changed, 9 insertions(+), 582 deletions(-) delete mode 100644 enc_temp_folder/7fed72ea74378557bed202e4042e1d/main.cpp diff --git a/enc_temp_folder/7fed72ea74378557bed202e4042e1d/main.cpp b/enc_temp_folder/7fed72ea74378557bed202e4042e1d/main.cpp deleted file mode 100644 index 34c8fe9..0000000 --- a/enc_temp_folder/7fed72ea74378557bed202e4042e1d/main.cpp +++ /dev/null @@ -1,553 +0,0 @@ -#define STB_IMAGE_IMPLEMENTATION -#include "glew.h" -#include "freeglut.h" -#include "glm.hpp" -#include "ext.hpp" -#include -#include -#include -#include -#include "Shader_Loader.h" -#include "Render_Utils.h" -#include "Texture.h" -#include "Camera.h" -#include "SOIL/stb_image_aug.h" - -GLuint skyboxProgram, skyboxBuffer; -GLuint bubbleProgram; -GLuint programColor; -GLuint programTexture; - -GLuint textureSubmarine; -GLuint textureBubble; -GLuint textureFish; - -unsigned int cubemapTexture, skyboxVAO; -unsigned int cubeVAO, cubeVBO; - -std::vector bubbleArray[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, 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; - -glm::quat rotation = glm::quat(1, 0, 0, 0); - -glm::mat4 cameraMatrix, perspectiveMatrix; - -Core::Shader_Loader shaderLoader; -Core::RenderContext submarineContext; -Core::RenderContext fishContext; -Core::RenderContext bubbleContext; - -std::vector 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), -glm::vec3(5.0f, 0.0f, 3.0f), -glm::vec3(3.0f, 2.0f, 4.0f), -glm::vec3(8.0f, 5.0f, 9.0f), -glm::vec3(14.0f, 6.0f, 15.0f), -glm::vec3(15.0f, 12.0f, 12.0f), -glm::vec3(10.0f, 17.0f, 15.0f), -glm::vec3(5.0f, 10.0f, 7.0f), -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::vector keyRotation; - -std::vector fish; - -std::string skyboxTextures[6] = { - "models/skybox/right.jpg", - "models/skybox/left.jpg", - "models/skybox/top.jpg", - "models/skybox/bottom.jpg", - "models/skybox/front.jpg", - "models/skybox/back.jpg" -}; - -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, - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - - -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - - -skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter -}; - - -bool isInBoundaries(glm::vec3 nextPosition) { - return nextPosition.z > -skyboxBoundary && nextPosition.z < skyboxBoundary&& nextPosition.y > -skyboxBoundary && - nextPosition.y < skyboxBoundary&& nextPosition.x < skyboxBoundary&& nextPosition.x > -skyboxBoundary; -} - - -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++) { - bubbleArray[i] = genBubbleKeyPoints(); - } -} - -void keyboard(unsigned char key, int x, int y) -{ - float angleSpeed = 10.f; - float moveSpeed = 1.0f; - glm::vec3 nextPosition; - switch (key) - { - case 'z': cursorDiff.z -= angleSpeed; break; - case 'x': cursorDiff.z += angleSpeed; break; - case 'w': - nextPosition = cameraPos + (cameraDir * moveSpeed); - if (isInBoundaries(nextPosition)) { - cameraPos = nextPosition; - } - break; - case 's': - nextPosition = cameraPos - (cameraDir * moveSpeed); - if (isInBoundaries(nextPosition)) { - cameraPos = nextPosition; - } - break; - case 'd': - nextPosition = cameraPos + (cameraSide * moveSpeed); - if (isInBoundaries(nextPosition)) { - cameraPos = nextPosition; - } - break; - case 'a': - nextPosition = cameraPos - (cameraSide * moveSpeed); - if (isInBoundaries(nextPosition)) { - cameraPos = nextPosition; - } - break; - } -} - -void mouse(int x, int y) -{ - if (old_x >= 0) { - cursorDiff.x = x - old_x; - cursorDiff.y = y - old_y; - } - old_x = x; - old_y = y; - - if (x < 100 || x > 800 - 100) { //you can use values other than 100 for the screen edges if you like, kind of seems to depend on your mouse sensitivity for what ends up working best - old_x = 800 / 2; //centers the last known position, this way there isn't an odd jump with your cam as it resets - old_y = 800 / 2; - glutWarpPointer(800 / 2, 800 / 2); //centers the cursor - } - else if (y < 100 || y > 800 - 100) { - old_x = 800 / 2; - old_y = 800 / 2; - glutWarpPointer(800 / 2, 800 / 2); - } -} - -glm::mat4 createCameraMatrix() -{ - glm::quat rotation_x = glm::angleAxis(cursorDiff.y * 0.03f, glm::vec3(1, 0, 0)); - cursorDiff.y = 0; - glm::quat rotation_y = glm::angleAxis(cursorDiff.x * 0.03f, glm::vec3(0, 1, 0)); - cursorDiff.x = 0; - glm::quat rotation_z = glm::angleAxis(cursorDiff.z * 0.03f, glm::vec3(0, 0, 1)); - cursorDiff.z = 0; - - glm::quat rotationChange = rotation_x * rotation_y * rotation_z; - rotation = glm::normalize(rotationChange * rotation); - - cameraDir = glm::inverse(rotation) * glm::vec3(0, 0, -1); - cameraSide = glm::inverse(rotation) * glm::vec3(1, 0, 0); - - return Core::createViewMatrixQuat(cameraPos, rotation); -} - -std::vector changeKeyPoints(std::vector keyPoints, glm::vec3 toChange) { - std::vector result; - int size = keyPoints.size(); - glm::vec3 change; - - for (int i = 0; i < size; i++) { - change.x = keyPoints[i].x + toChange.x; - change.y = keyPoints[i].y + toChange.y; - change.z = keyPoints[i].z + toChange.z; - result.push_back(change); - } - - return result; -} - -glm::mat4 animationMatrix(float time, glm::vec3 change, std::vector keyPoints, glm::vec3 scaleValue, float speed) { - - time = time * speed; - std::vector distances; - std::vector newKeyPoints = changeKeyPoints(keyPoints, change); - 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()); - } - time = fmod(time, timeStep); - - //index of first keyPoint - int index = 0; - - while (distances[index] <= time) { - time = time - distances[index]; - index += 1; - } - - float t = time / distances[index]; - - 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::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 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(scaleValue)) * glm::mat4_cast(animationRotation); - - return result; -} - -void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuint textureId, GLuint program) -{ - glUseProgram(program); - - glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z); - Core::SetActiveTexture(textureId, "textureSampler", program, 0); - - 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 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); - glm::mat4 transformation = perspectiveMatrix * cameraMatrix; - glUniformMatrix4fv(glGetUniformLocation(skyboxProgram, "projectionViewMatrix"), 1, GL_FALSE, (float*)&transformation); - glBindVertexArray(skyboxVAO); - glActiveTexture(GL_TEXTURE0); - 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; - - 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); - - glm::vec3 change0 = glm::vec3(0, 0, 0); - - for (int j = 0; j < 50; j++) { - drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.1f), 0.09f), cubemapTexture, bubbleProgram); - } - - 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(bubbleContext, submarineInitialTransformation, cubemapTexture, bubbleProgram); - drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture); - glutSwapBuffers(); -} - -void loadModelToContext(std::string path, Core::RenderContext& context) -{ - Assimp::Importer import; - const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace); - - if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) - { - std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl; - return; - } - context.initFromAssimpMesh(scene->mMeshes[0]); -} - -unsigned int loadCubemap() -{ - unsigned int textureID; - glGenTextures(1, &textureID); - glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); - - int width, height, nrChannels; - for (unsigned int i = 0; i < 6; i++) - { - unsigned char* data = stbi_load(skyboxTextures[i].c_str(), &width, &height, &nrChannels, STBI_rgb_alpha); - if (data) - { - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, - 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data - ); - stbi_image_free(data); - } - else - { - std::cout << stbi_failure_reason() << std::endl; - std::cout << "Cubemap tex failed to load at path: " << skyboxTextures[i] << std::endl; - stbi_image_free(data); - } - } - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - - return textureID; -} - -void initSkybox() -{ - glGenVertexArrays(1, &skyboxVAO); - glBindVertexArray(skyboxVAO); - - glGenBuffers(1, &skyboxBuffer); - glBindBuffer(GL_ARRAY_BUFFER, skyboxBuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW); - - GLuint vPosition = glGetAttribLocation(skyboxProgram, "aPos"); - glEnableVertexAttribArray(vPosition); - - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(skyboxVertices), skyboxVertices); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); -} - -void initKeyRotation() { - glm::vec3 oldDirection = glm::vec3(0, 0, 1); - glm::quat oldRotationCamera = glm::quat(1, 0, 0, 0); - glm::vec3 direction; - glm::quat rotation; - for (int i = 0; i < fishKeyPoints.size() - 1; i++) { - //3.1 - direction = glm::normalize(fishKeyPoints[i + 1] - fishKeyPoints[i]); - //3.2 - rotation = glm::normalize(glm::rotationCamera(oldDirection, direction) * oldRotationCamera); - //3.3 - keyRotation.push_back(rotation); - //3.4 - oldDirection = direction; - oldRotationCamera = rotation; - } - 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"); - bubbleProgram = shaderLoader.CreateProgram((char*)"shaders/bubble.vert", (char*)"shaders/bubble.frag"); - cubemapTexture = loadCubemap(); - - loadModelToContext("models/submarine.obj", submarineContext); - textureSubmarine = Core::LoadTexture("textures/submarine.png"); - - loadModelToContext("models/fish.obj", fishContext); - 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"); - generateBubbleArray(); - initCube(); - initSkybox(); - -} - -void shutdown() -{ - shaderLoader.DeleteProgram(programColor); - shaderLoader.DeleteProgram(programTexture); - shaderLoader.DeleteProgram(skyboxProgram); - shaderLoader.DeleteProgram(bubbleProgram); - -} - -void idle() -{ - glutPostRedisplay(); -} - - -int main(int argc, char** argv) -{ - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); - glutInitWindowPosition(200, 200); - glutInitWindowSize(800, 800); - glutCreateWindow("Water and shit"); - glewInit(); - - init(); - glutKeyboardFunc(keyboard); - glutPassiveMotionFunc(mouse); - glutDisplayFunc(renderScene); - glutIdleFunc(idle); - - glutSetCursor(GLUT_CURSOR_NONE); - glutMainLoop(); - shutdown(); - return 0; -} \ No newline at end of file diff --git a/grafika_projekt/shaders/bubble.frag b/grafika_projekt/shaders/bubble.frag index a172b2b..c9e8948 100644 --- a/grafika_projekt/shaders/bubble.frag +++ b/grafika_projekt/shaders/bubble.frag @@ -3,28 +3,22 @@ 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 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); } \ 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 From 9ee4d45a9b5b6e9557ce447b045abd7d13de668b Mon Sep 17 00:00:00 2001 From: Thyme1 Date: Mon, 10 Jan 2022 21:43:19 +0100 Subject: [PATCH 5/6] smaller bubbles --- .../1b31f5b984722d905427b82b748d629/main.cpp | 553 ++++++++++++++++++ grafika_projekt/src/main.cpp | 4 +- 2 files changed, 555 insertions(+), 2 deletions(-) create mode 100644 enc_temp_folder/1b31f5b984722d905427b82b748d629/main.cpp diff --git a/enc_temp_folder/1b31f5b984722d905427b82b748d629/main.cpp b/enc_temp_folder/1b31f5b984722d905427b82b748d629/main.cpp new file mode 100644 index 0000000..15db157 --- /dev/null +++ b/enc_temp_folder/1b31f5b984722d905427b82b748d629/main.cpp @@ -0,0 +1,553 @@ +#define STB_IMAGE_IMPLEMENTATION +#include "glew.h" +#include "freeglut.h" +#include "glm.hpp" +#include "ext.hpp" +#include +#include +#include +#include +#include "Shader_Loader.h" +#include "Render_Utils.h" +#include "Texture.h" +#include "Camera.h" +#include "SOIL/stb_image_aug.h" + +GLuint skyboxProgram, skyboxBuffer; +GLuint bubbleProgram; +GLuint programColor; +GLuint programTexture; + +GLuint textureSubmarine; +GLuint textureBubble; +GLuint textureFish; + +unsigned int cubemapTexture, skyboxVAO; +unsigned int cubeVAO, cubeVBO; + +std::vector bubbleArray[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, 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; + +glm::quat rotation = glm::quat(1, 0, 0, 0); + +glm::mat4 cameraMatrix, perspectiveMatrix; + +Core::Shader_Loader shaderLoader; +Core::RenderContext submarineContext; +Core::RenderContext fishContext; +Core::RenderContext bubbleContext; + +std::vector 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), +glm::vec3(5.0f, 0.0f, 3.0f), +glm::vec3(3.0f, 2.0f, 4.0f), +glm::vec3(8.0f, 5.0f, 9.0f), +glm::vec3(14.0f, 6.0f, 15.0f), +glm::vec3(15.0f, 12.0f, 12.0f), +glm::vec3(10.0f, 17.0f, 15.0f), +glm::vec3(5.0f, 10.0f, 7.0f), +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::vector keyRotation; + +std::vector fish; + +std::string skyboxTextures[6] = { + "models/skybox/right.jpg", + "models/skybox/left.jpg", + "models/skybox/top.jpg", + "models/skybox/bottom.jpg", + "models/skybox/front.jpg", + "models/skybox/back.jpg" +}; + +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, + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + + -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, + -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, + + -skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, + -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, + skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter +}; + + +bool isInBoundaries(glm::vec3 nextPosition) { + return nextPosition.z > -skyboxBoundary && nextPosition.z < skyboxBoundary&& nextPosition.y > -skyboxBoundary && + nextPosition.y < skyboxBoundary&& nextPosition.x < skyboxBoundary&& nextPosition.x > -skyboxBoundary; +} + + +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++) { + bubbleArray[i] = genBubbleKeyPoints(); + } +} + +void keyboard(unsigned char key, int x, int y) +{ + float angleSpeed = 10.f; + float moveSpeed = 1.0f; + glm::vec3 nextPosition; + switch (key) + { + case 'z': cursorDiff.z -= angleSpeed; break; + case 'x': cursorDiff.z += angleSpeed; break; + case 'w': + nextPosition = cameraPos + (cameraDir * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; + case 's': + nextPosition = cameraPos - (cameraDir * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; + case 'd': + nextPosition = cameraPos + (cameraSide * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; + case 'a': + nextPosition = cameraPos - (cameraSide * moveSpeed); + if (isInBoundaries(nextPosition)) { + cameraPos = nextPosition; + } + break; + } +} + +void mouse(int x, int y) +{ + if (old_x >= 0) { + cursorDiff.x = x - old_x; + cursorDiff.y = y - old_y; + } + old_x = x; + old_y = y; + + if (x < 100 || x > 800 - 100) { //you can use values other than 100 for the screen edges if you like, kind of seems to depend on your mouse sensitivity for what ends up working best + old_x = 800 / 2; //centers the last known position, this way there isn't an odd jump with your cam as it resets + old_y = 800 / 2; + glutWarpPointer(800 / 2, 800 / 2); //centers the cursor + } + else if (y < 100 || y > 800 - 100) { + old_x = 800 / 2; + old_y = 800 / 2; + glutWarpPointer(800 / 2, 800 / 2); + } +} + +glm::mat4 createCameraMatrix() +{ + glm::quat rotation_x = glm::angleAxis(cursorDiff.y * 0.03f, glm::vec3(1, 0, 0)); + cursorDiff.y = 0; + glm::quat rotation_y = glm::angleAxis(cursorDiff.x * 0.03f, glm::vec3(0, 1, 0)); + cursorDiff.x = 0; + glm::quat rotation_z = glm::angleAxis(cursorDiff.z * 0.03f, glm::vec3(0, 0, 1)); + cursorDiff.z = 0; + + glm::quat rotationChange = rotation_x * rotation_y * rotation_z; + rotation = glm::normalize(rotationChange * rotation); + + cameraDir = glm::inverse(rotation) * glm::vec3(0, 0, -1); + cameraSide = glm::inverse(rotation) * glm::vec3(1, 0, 0); + + return Core::createViewMatrixQuat(cameraPos, rotation); +} + +std::vector changeKeyPoints(std::vector keyPoints, glm::vec3 toChange) { + std::vector result; + int size = keyPoints.size(); + glm::vec3 change; + + for (int i = 0; i < size; i++) { + change.x = keyPoints[i].x + toChange.x; + change.y = keyPoints[i].y + toChange.y; + change.z = keyPoints[i].z + toChange.z; + result.push_back(change); + } + + return result; +} + +glm::mat4 animationMatrix(float time, glm::vec3 change, std::vector keyPoints, glm::vec3 scaleValue, float speed) { + + time = time * speed; + std::vector distances; + std::vector newKeyPoints = changeKeyPoints(keyPoints, change); + 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()); + } + time = fmod(time, timeStep); + + //index of first keyPoint + int index = 0; + + while (distances[index] <= time) { + time = time - distances[index]; + index += 1; + } + + float t = time / distances[index]; + + 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::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 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(scaleValue)) * glm::mat4_cast(animationRotation); + + return result; +} + +void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuint textureId, GLuint program) +{ + glUseProgram(program); + + glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z); + Core::SetActiveTexture(textureId, "textureSampler", program, 0); + + 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 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); + glm::mat4 transformation = perspectiveMatrix * cameraMatrix; + glUniformMatrix4fv(glGetUniformLocation(skyboxProgram, "projectionViewMatrix"), 1, GL_FALSE, (float*)&transformation); + glBindVertexArray(skyboxVAO); + glActiveTexture(GL_TEXTURE0); + 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; + + 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); + + glm::vec3 change0 = glm::vec3(0, 0, 0); + + for (int j = 0; j < 50; j++) { + drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.03f), 0.2f), cubemapTexture, bubbleProgram); + } + + 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(bubbleContext, submarineInitialTransformation, cubemapTexture, bubbleProgram); + drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture); + glutSwapBuffers(); +} + +void loadModelToContext(std::string path, Core::RenderContext& context) +{ + Assimp::Importer import; + const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace); + + if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) + { + std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl; + return; + } + context.initFromAssimpMesh(scene->mMeshes[0]); +} + +unsigned int loadCubemap() +{ + unsigned int textureID; + glGenTextures(1, &textureID); + glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); + + int width, height, nrChannels; + for (unsigned int i = 0; i < 6; i++) + { + unsigned char* data = stbi_load(skyboxTextures[i].c_str(), &width, &height, &nrChannels, STBI_rgb_alpha); + if (data) + { + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, + 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data + ); + stbi_image_free(data); + } + else + { + std::cout << stbi_failure_reason() << std::endl; + std::cout << "Cubemap tex failed to load at path: " << skyboxTextures[i] << std::endl; + stbi_image_free(data); + } + } + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + + return textureID; +} + +void initSkybox() +{ + glGenVertexArrays(1, &skyboxVAO); + glBindVertexArray(skyboxVAO); + + glGenBuffers(1, &skyboxBuffer); + glBindBuffer(GL_ARRAY_BUFFER, skyboxBuffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW); + + GLuint vPosition = glGetAttribLocation(skyboxProgram, "aPos"); + glEnableVertexAttribArray(vPosition); + + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(skyboxVertices), skyboxVertices); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); +} + +void initKeyRotation() { + glm::vec3 oldDirection = glm::vec3(0, 0, 1); + glm::quat oldRotationCamera = glm::quat(1, 0, 0, 0); + glm::vec3 direction; + glm::quat rotation; + for (int i = 0; i < fishKeyPoints.size() - 1; i++) { + //3.1 + direction = glm::normalize(fishKeyPoints[i + 1] - fishKeyPoints[i]); + //3.2 + rotation = glm::normalize(glm::rotationCamera(oldDirection, direction) * oldRotationCamera); + //3.3 + keyRotation.push_back(rotation); + //3.4 + oldDirection = direction; + oldRotationCamera = rotation; + } + 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"); + bubbleProgram = shaderLoader.CreateProgram((char*)"shaders/bubble.vert", (char*)"shaders/bubble.frag"); + cubemapTexture = loadCubemap(); + + loadModelToContext("models/submarine.obj", submarineContext); + textureSubmarine = Core::LoadTexture("textures/submarine.png"); + + loadModelToContext("models/fish.obj", fishContext); + 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"); + generateBubbleArray(); + initCube(); + initSkybox(); + +} + +void shutdown() +{ + shaderLoader.DeleteProgram(programColor); + shaderLoader.DeleteProgram(programTexture); + shaderLoader.DeleteProgram(skyboxProgram); + shaderLoader.DeleteProgram(bubbleProgram); + +} + +void idle() +{ + glutPostRedisplay(); +} + + +int main(int argc, char** argv) +{ + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); + glutInitWindowPosition(200, 200); + glutInitWindowSize(800, 800); + glutCreateWindow("Water and shit"); + glewInit(); + + init(); + glutKeyboardFunc(keyboard); + glutPassiveMotionFunc(mouse); + glutDisplayFunc(renderScene); + glutIdleFunc(idle); + + glutSetCursor(GLUT_CURSOR_NONE); + glutMainLoop(); + shutdown(); + return 0; +} \ No newline at end of file diff --git a/grafika_projekt/src/main.cpp b/grafika_projekt/src/main.cpp index 34c8fe9..9d1dbc1 100644 --- a/grafika_projekt/src/main.cpp +++ b/grafika_projekt/src/main.cpp @@ -377,7 +377,7 @@ void renderScene() glm::vec3 change0 = glm::vec3(0, 0, 0); for (int j = 0; j < 50; j++) { - drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.1f), 0.09f), cubemapTexture, bubbleProgram); + drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.03f), 0.2f), cubemapTexture, bubbleProgram); } for (int i = 0; i < 5; i++) { @@ -390,7 +390,7 @@ void renderScene() time -= 6; } } - drawObjectTexture(bubbleContext, submarineInitialTransformation, cubemapTexture, bubbleProgram); + //drawObjectTexture(bubbleContext, submarineInitialTransformation, cubemapTexture, bubbleProgram); drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture); glutSwapBuffers(); } From 551dc2e3e051ce39ceb4b73614cd5a913331bdfc Mon Sep 17 00:00:00 2001 From: Thyme1 Date: Wed, 12 Jan 2022 13:28:22 +0100 Subject: [PATCH 6/6] add phong for bubbles --- .../1b31f5b984722d905427b82b748d629/main.cpp | 553 ------------------ grafika_projekt/shaders/bubble.frag | 15 +- grafika_projekt/src/main.cpp | 8 +- 3 files changed, 18 insertions(+), 558 deletions(-) delete mode 100644 enc_temp_folder/1b31f5b984722d905427b82b748d629/main.cpp diff --git a/enc_temp_folder/1b31f5b984722d905427b82b748d629/main.cpp b/enc_temp_folder/1b31f5b984722d905427b82b748d629/main.cpp deleted file mode 100644 index 15db157..0000000 --- a/enc_temp_folder/1b31f5b984722d905427b82b748d629/main.cpp +++ /dev/null @@ -1,553 +0,0 @@ -#define STB_IMAGE_IMPLEMENTATION -#include "glew.h" -#include "freeglut.h" -#include "glm.hpp" -#include "ext.hpp" -#include -#include -#include -#include -#include "Shader_Loader.h" -#include "Render_Utils.h" -#include "Texture.h" -#include "Camera.h" -#include "SOIL/stb_image_aug.h" - -GLuint skyboxProgram, skyboxBuffer; -GLuint bubbleProgram; -GLuint programColor; -GLuint programTexture; - -GLuint textureSubmarine; -GLuint textureBubble; -GLuint textureFish; - -unsigned int cubemapTexture, skyboxVAO; -unsigned int cubeVAO, cubeVBO; - -std::vector bubbleArray[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, 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; - -glm::quat rotation = glm::quat(1, 0, 0, 0); - -glm::mat4 cameraMatrix, perspectiveMatrix; - -Core::Shader_Loader shaderLoader; -Core::RenderContext submarineContext; -Core::RenderContext fishContext; -Core::RenderContext bubbleContext; - -std::vector 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), -glm::vec3(5.0f, 0.0f, 3.0f), -glm::vec3(3.0f, 2.0f, 4.0f), -glm::vec3(8.0f, 5.0f, 9.0f), -glm::vec3(14.0f, 6.0f, 15.0f), -glm::vec3(15.0f, 12.0f, 12.0f), -glm::vec3(10.0f, 17.0f, 15.0f), -glm::vec3(5.0f, 10.0f, 7.0f), -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::vector keyRotation; - -std::vector fish; - -std::string skyboxTextures[6] = { - "models/skybox/right.jpg", - "models/skybox/left.jpg", - "models/skybox/top.jpg", - "models/skybox/bottom.jpg", - "models/skybox/front.jpg", - "models/skybox/back.jpg" -}; - -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, - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - - -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, skyboxVerticeParameter, - -skyboxVerticeParameter, skyboxVerticeParameter, -skyboxVerticeParameter, - - -skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, -skyboxVerticeParameter, - -skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter, - skyboxVerticeParameter, -skyboxVerticeParameter, skyboxVerticeParameter -}; - - -bool isInBoundaries(glm::vec3 nextPosition) { - return nextPosition.z > -skyboxBoundary && nextPosition.z < skyboxBoundary&& nextPosition.y > -skyboxBoundary && - nextPosition.y < skyboxBoundary&& nextPosition.x < skyboxBoundary&& nextPosition.x > -skyboxBoundary; -} - - -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++) { - bubbleArray[i] = genBubbleKeyPoints(); - } -} - -void keyboard(unsigned char key, int x, int y) -{ - float angleSpeed = 10.f; - float moveSpeed = 1.0f; - glm::vec3 nextPosition; - switch (key) - { - case 'z': cursorDiff.z -= angleSpeed; break; - case 'x': cursorDiff.z += angleSpeed; break; - case 'w': - nextPosition = cameraPos + (cameraDir * moveSpeed); - if (isInBoundaries(nextPosition)) { - cameraPos = nextPosition; - } - break; - case 's': - nextPosition = cameraPos - (cameraDir * moveSpeed); - if (isInBoundaries(nextPosition)) { - cameraPos = nextPosition; - } - break; - case 'd': - nextPosition = cameraPos + (cameraSide * moveSpeed); - if (isInBoundaries(nextPosition)) { - cameraPos = nextPosition; - } - break; - case 'a': - nextPosition = cameraPos - (cameraSide * moveSpeed); - if (isInBoundaries(nextPosition)) { - cameraPos = nextPosition; - } - break; - } -} - -void mouse(int x, int y) -{ - if (old_x >= 0) { - cursorDiff.x = x - old_x; - cursorDiff.y = y - old_y; - } - old_x = x; - old_y = y; - - if (x < 100 || x > 800 - 100) { //you can use values other than 100 for the screen edges if you like, kind of seems to depend on your mouse sensitivity for what ends up working best - old_x = 800 / 2; //centers the last known position, this way there isn't an odd jump with your cam as it resets - old_y = 800 / 2; - glutWarpPointer(800 / 2, 800 / 2); //centers the cursor - } - else if (y < 100 || y > 800 - 100) { - old_x = 800 / 2; - old_y = 800 / 2; - glutWarpPointer(800 / 2, 800 / 2); - } -} - -glm::mat4 createCameraMatrix() -{ - glm::quat rotation_x = glm::angleAxis(cursorDiff.y * 0.03f, glm::vec3(1, 0, 0)); - cursorDiff.y = 0; - glm::quat rotation_y = glm::angleAxis(cursorDiff.x * 0.03f, glm::vec3(0, 1, 0)); - cursorDiff.x = 0; - glm::quat rotation_z = glm::angleAxis(cursorDiff.z * 0.03f, glm::vec3(0, 0, 1)); - cursorDiff.z = 0; - - glm::quat rotationChange = rotation_x * rotation_y * rotation_z; - rotation = glm::normalize(rotationChange * rotation); - - cameraDir = glm::inverse(rotation) * glm::vec3(0, 0, -1); - cameraSide = glm::inverse(rotation) * glm::vec3(1, 0, 0); - - return Core::createViewMatrixQuat(cameraPos, rotation); -} - -std::vector changeKeyPoints(std::vector keyPoints, glm::vec3 toChange) { - std::vector result; - int size = keyPoints.size(); - glm::vec3 change; - - for (int i = 0; i < size; i++) { - change.x = keyPoints[i].x + toChange.x; - change.y = keyPoints[i].y + toChange.y; - change.z = keyPoints[i].z + toChange.z; - result.push_back(change); - } - - return result; -} - -glm::mat4 animationMatrix(float time, glm::vec3 change, std::vector keyPoints, glm::vec3 scaleValue, float speed) { - - time = time * speed; - std::vector distances; - std::vector newKeyPoints = changeKeyPoints(keyPoints, change); - 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()); - } - time = fmod(time, timeStep); - - //index of first keyPoint - int index = 0; - - while (distances[index] <= time) { - time = time - distances[index]; - index += 1; - } - - float t = time / distances[index]; - - 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::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 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(scaleValue)) * glm::mat4_cast(animationRotation); - - return result; -} - -void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuint textureId, GLuint program) -{ - glUseProgram(program); - - glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z); - Core::SetActiveTexture(textureId, "textureSampler", program, 0); - - 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 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); - glm::mat4 transformation = perspectiveMatrix * cameraMatrix; - glUniformMatrix4fv(glGetUniformLocation(skyboxProgram, "projectionViewMatrix"), 1, GL_FALSE, (float*)&transformation); - glBindVertexArray(skyboxVAO); - glActiveTexture(GL_TEXTURE0); - 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; - - 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); - - glm::vec3 change0 = glm::vec3(0, 0, 0); - - for (int j = 0; j < 50; j++) { - drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.03f), 0.2f), cubemapTexture, bubbleProgram); - } - - 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(bubbleContext, submarineInitialTransformation, cubemapTexture, bubbleProgram); - drawObjectTexture(submarineContext, submarineModelMatrix, textureSubmarine, programTexture); - glutSwapBuffers(); -} - -void loadModelToContext(std::string path, Core::RenderContext& context) -{ - Assimp::Importer import; - const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace); - - if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) - { - std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl; - return; - } - context.initFromAssimpMesh(scene->mMeshes[0]); -} - -unsigned int loadCubemap() -{ - unsigned int textureID; - glGenTextures(1, &textureID); - glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); - - int width, height, nrChannels; - for (unsigned int i = 0; i < 6; i++) - { - unsigned char* data = stbi_load(skyboxTextures[i].c_str(), &width, &height, &nrChannels, STBI_rgb_alpha); - if (data) - { - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, - 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data - ); - stbi_image_free(data); - } - else - { - std::cout << stbi_failure_reason() << std::endl; - std::cout << "Cubemap tex failed to load at path: " << skyboxTextures[i] << std::endl; - stbi_image_free(data); - } - } - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - - return textureID; -} - -void initSkybox() -{ - glGenVertexArrays(1, &skyboxVAO); - glBindVertexArray(skyboxVAO); - - glGenBuffers(1, &skyboxBuffer); - glBindBuffer(GL_ARRAY_BUFFER, skyboxBuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW); - - GLuint vPosition = glGetAttribLocation(skyboxProgram, "aPos"); - glEnableVertexAttribArray(vPosition); - - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(skyboxVertices), skyboxVertices); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); -} - -void initKeyRotation() { - glm::vec3 oldDirection = glm::vec3(0, 0, 1); - glm::quat oldRotationCamera = glm::quat(1, 0, 0, 0); - glm::vec3 direction; - glm::quat rotation; - for (int i = 0; i < fishKeyPoints.size() - 1; i++) { - //3.1 - direction = glm::normalize(fishKeyPoints[i + 1] - fishKeyPoints[i]); - //3.2 - rotation = glm::normalize(glm::rotationCamera(oldDirection, direction) * oldRotationCamera); - //3.3 - keyRotation.push_back(rotation); - //3.4 - oldDirection = direction; - oldRotationCamera = rotation; - } - 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"); - bubbleProgram = shaderLoader.CreateProgram((char*)"shaders/bubble.vert", (char*)"shaders/bubble.frag"); - cubemapTexture = loadCubemap(); - - loadModelToContext("models/submarine.obj", submarineContext); - textureSubmarine = Core::LoadTexture("textures/submarine.png"); - - loadModelToContext("models/fish.obj", fishContext); - 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"); - generateBubbleArray(); - initCube(); - initSkybox(); - -} - -void shutdown() -{ - shaderLoader.DeleteProgram(programColor); - shaderLoader.DeleteProgram(programTexture); - shaderLoader.DeleteProgram(skyboxProgram); - shaderLoader.DeleteProgram(bubbleProgram); - -} - -void idle() -{ - glutPostRedisplay(); -} - - -int main(int argc, char** argv) -{ - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); - glutInitWindowPosition(200, 200); - glutInitWindowSize(800, 800); - glutCreateWindow("Water and shit"); - glewInit(); - - init(); - glutKeyboardFunc(keyboard); - glutPassiveMotionFunc(mouse); - glutDisplayFunc(renderScene); - glutIdleFunc(idle); - - glutSetCursor(GLUT_CURSOR_NONE); - glutMainLoop(); - shutdown(); - return 0; -} \ No newline at end of file diff --git a/grafika_projekt/shaders/bubble.frag b/grafika_projekt/shaders/bubble.frag index c9e8948..c7f8798 100644 --- a/grafika_projekt/shaders/bubble.frag +++ b/grafika_projekt/shaders/bubble.frag @@ -6,6 +6,7 @@ in vec3 interpNormal; uniform vec3 cameraPos; uniform samplerCube bubble; +uniform vec3 lightPos; layout (binding = 0) uniform samplerCube tex_map; @@ -20,5 +21,17 @@ void main() 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/src/main.cpp b/grafika_projekt/src/main.cpp index 9d1dbc1..cef3f2f 100644 --- a/grafika_projekt/src/main.cpp +++ b/grafika_projekt/src/main.cpp @@ -25,7 +25,7 @@ GLuint textureFish; unsigned int cubemapTexture, skyboxVAO; unsigned int cubeVAO, cubeVBO; -std::vector bubbleArray[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)); @@ -192,7 +192,7 @@ std::vector genBubbleKeyPoints() { void generateBubbleArray() { - for (int i = 0; i < 100; i++) { + for (int i = 0; i < 300; i++) { bubbleArray[i] = genBubbleKeyPoints(); } } @@ -376,8 +376,8 @@ void renderScene() glm::vec3 change0 = glm::vec3(0, 0, 0); - for (int j = 0; j < 50; j++) { - drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.03f), 0.2f), cubemapTexture, bubbleProgram); + 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++) {