From 7a2a862a949bada1105e6a2d21ef7c3972a357e3 Mon Sep 17 00:00:00 2001 From: matixezor Date: Mon, 24 Jan 2022 12:06:17 +0100 Subject: [PATCH] feat: refactor --- grafika_projekt/grafika_projekt.vcxproj | 4 -- .../grafika_projekt.vcxproj.filters | 12 ---- grafika_projekt/shaders/terrainShader.frag | 36 ----------- grafika_projekt/shaders/terrainShader.vert | 28 --------- grafika_projekt/src/RawModel.cpp | 14 ----- grafika_projekt/src/RawModel.h | 13 ---- grafika_projekt/src/Terrain.cpp | 49 ++++----------- grafika_projekt/src/Terrain.h | 12 ++-- grafika_projekt/src/TerrainRenderer.cpp | 60 ------------------- grafika_projekt/src/TerrainRenderer.h | 36 ----------- grafika_projekt/src/main.cpp | 37 ++++++------ 11 files changed, 36 insertions(+), 265 deletions(-) delete mode 100644 grafika_projekt/shaders/terrainShader.frag delete mode 100644 grafika_projekt/shaders/terrainShader.vert delete mode 100644 grafika_projekt/src/RawModel.cpp delete mode 100644 grafika_projekt/src/RawModel.h delete mode 100644 grafika_projekt/src/TerrainRenderer.cpp delete mode 100644 grafika_projekt/src/TerrainRenderer.h diff --git a/grafika_projekt/grafika_projekt.vcxproj b/grafika_projekt/grafika_projekt.vcxproj index a9e779e..56083b8 100644 --- a/grafika_projekt/grafika_projekt.vcxproj +++ b/grafika_projekt/grafika_projekt.vcxproj @@ -142,7 +142,6 @@ - @@ -154,10 +153,8 @@ - - @@ -171,7 +168,6 @@ - diff --git a/grafika_projekt/grafika_projekt.vcxproj.filters b/grafika_projekt/grafika_projekt.vcxproj.filters index 36eed1b..7f3efc7 100644 --- a/grafika_projekt/grafika_projekt.vcxproj.filters +++ b/grafika_projekt/grafika_projekt.vcxproj.filters @@ -48,12 +48,6 @@ Source Files - - Source Files - - - Source Files - @@ -95,12 +89,6 @@ Header Files - - Header Files - - - Header Files - diff --git a/grafika_projekt/shaders/terrainShader.frag b/grafika_projekt/shaders/terrainShader.frag deleted file mode 100644 index 4a15458..0000000 --- a/grafika_projekt/shaders/terrainShader.frag +++ /dev/null @@ -1,36 +0,0 @@ -#version 400 core - -in vec2 pass_textureCoordinates; -in vec3 surfaceNormal; -in vec3 toLightVector; -in vec3 toCameraVector; - -out vec4 out_Color; - -uniform sampler2D modelTexture; - -uniform float shineDamper; -uniform float reflectivity; - -void main(void){ - - vec3 unitNormal = normalize(surfaceNormal); - vec3 unitLightVector = normalize(toLightVector); - - float nDotl = dot(unitNormal,unitLightVector); - float brightness = max(nDotl,0.2); - vec3 diffuse = brightness * vec3(1.0, 1.0, 1.0); //Tu była zmiana - - vec3 unitVectorToCamera = normalize(toCameraVector); - vec3 lightDirection = -unitLightVector; - vec3 reflectedLightDirection = reflect(lightDirection,unitNormal); - - float specularFactor = dot(reflectedLightDirection , unitVectorToCamera); - specularFactor = max(specularFactor,0.0); - float dampedFactor = pow(specularFactor,shineDamper); - vec3 finalSpecular = dampedFactor * reflectivity * vec3(1.0, 1.0, 1.0); //Tu była zmiana - - - out_Color = vec4(diffuse,1.0) * texture(modelTexture,pass_textureCoordinates) + vec4(finalSpecular,1.0); - -} \ No newline at end of file diff --git a/grafika_projekt/shaders/terrainShader.vert b/grafika_projekt/shaders/terrainShader.vert deleted file mode 100644 index 247df2e..0000000 --- a/grafika_projekt/shaders/terrainShader.vert +++ /dev/null @@ -1,28 +0,0 @@ -#version 400 core - -layout(location = 3) in vec3 position; -layout(location = 4) in vec2 textureCoordinates; -layout(location = 5) in vec3 normal; - -out vec2 pass_textureCoordinates; -out vec3 surfaceNormal; -out vec3 toLightVector; -out vec3 toCameraVector; - -uniform mat4 transformationMatrix; -uniform mat4 projectionMatrix; -uniform mat4 viewMatrix; -uniform vec3 lightDir; - -void main(void){ - - vec4 worldPosition = transformationMatrix * vec4(position,1.0); - gl_Position = projectionMatrix * viewMatrix * worldPosition; - pass_textureCoordinates = textureCoordinates * 40.0; - - surfaceNormal = (transformationMatrix * vec4(normal,0.0)).xyz; - toLightVector = lightDir - worldPosition.xyz; - toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz; - - -} \ No newline at end of file diff --git a/grafika_projekt/src/RawModel.cpp b/grafika_projekt/src/RawModel.cpp deleted file mode 100644 index 5eefdba..0000000 --- a/grafika_projekt/src/RawModel.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "RawModel.h" - -RawModel::RawModel(int vaoID, int vertexCount) { - this->vaoID = vaoID; - this->vertexCount = vertexCount; -} - -int RawModel::getVertexCount() { - return vertexCount; -} - -int RawModel::getVaoID() { - return vaoID; -} \ No newline at end of file diff --git a/grafika_projekt/src/RawModel.h b/grafika_projekt/src/RawModel.h deleted file mode 100644 index 8a13b0d..0000000 --- a/grafika_projekt/src/RawModel.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -class RawModel -{ -public: - RawModel() = default; - RawModel(int vaoID, int vertexCount); - int getVertexCount(); - int getVaoID(); -private: - int vaoID; - int vertexCount; -}; - diff --git a/grafika_projekt/src/Terrain.cpp b/grafika_projekt/src/Terrain.cpp index d1c2684..1d2b503 100644 --- a/grafika_projekt/src/Terrain.cpp +++ b/grafika_projekt/src/Terrain.cpp @@ -1,22 +1,18 @@ #include "Terrain.h" -#include "glew.h" -#include "freeglut.h" -#include "glm.hpp" + const float Terrain::SIZE = 100.f; -const int Terrain::VERTEX_COUNT = 64; +const int Terrain::VERTEX_COUNT = 32; const int Terrain::COUNT = Terrain::VERTEX_COUNT * Terrain::VERTEX_COUNT; -Terrain::Terrain(int gridX, int gridZ, GLuint textureID, HeightGenerator heightGenerator) { +Terrain::Terrain(int gridX, int gridZ, HeightGenerator heightGenerator) { x = gridX * SIZE; z = gridZ * SIZE; - texture = textureID; - model = generateTerrain(); this->heightGenerator = heightGenerator; } -RawModel Terrain::generateTerrain() { +obj::Model Terrain::generateTerrain() { float vertices[COUNT * 3]; float normals[COUNT * 3]; float textureCoords[COUNT * 2]; @@ -31,8 +27,8 @@ RawModel Terrain::generateTerrain() { normals[vertexPointer * 3] = normal.x; normals[vertexPointer * 3 + 1] = normal.y; normals[vertexPointer * 3 + 2] = normal.z; - textureCoords[vertexPointer * 2] = float(j) / float(VERTEX_COUNT - 1);// *SIZE; - textureCoords[vertexPointer * 2 + 1] = float(i) / float(VERTEX_COUNT - 1);// *SIZE; + textureCoords[vertexPointer * 2] = float(j) / float(VERTEX_COUNT - 1); + textureCoords[vertexPointer * 2 + 1] = float(i) / float(VERTEX_COUNT - 1); vertexPointer++; } } @@ -51,37 +47,16 @@ RawModel Terrain::generateTerrain() { indices[pointer++] = bottomRight; } } - unsigned int vaoID; - glGenVertexArrays(1, &vaoID); - glBindVertexArray(vaoID); - unsigned int vboID; - glGenBuffers(1, &vboID); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices, GL_STATIC_DRAW); - storeDataInAttributeList(0, 3, vertices); - storeDataInAttributeList(1, 2, textureCoords); - storeDataInAttributeList(2, 3, normals); - glBindVertexArray(0); - return RawModel(vaoID, sizeof indices / sizeof indices[0]); -} + std::vector vVertices(std::begin(vertices), std::end(vertices)); + std::vector vTextures(std::begin(textureCoords), std::end(textureCoords)); + std::vector vNormals(std::begin(normals), std::end(normals)); + std::map> faces; + faces[std::string("default")] = std::vector(std::begin(indices), std::end(indices)); -void Terrain::storeDataInAttributeList(int attributeNumber, int coordinateSize, float data[]) { - unsigned int vboID; - glGenBuffers(1, &vboID); - glBindBuffer(GL_ARRAY_BUFFER, vboID); - glBufferData(GL_ARRAY_BUFFER, sizeof(data), &data, GL_STATIC_DRAW); - glVertexAttribPointer(attributeNumber, coordinateSize, GL_FLOAT, false, 0, 0); - glBindBuffer(GL_ARRAY_BUFFER, 0); -} - -RawModel Terrain::getModel() { + obj::Model model = { vVertices, vTextures, vNormals, faces }; return model; } -GLuint Terrain::getTexture() { - return texture; -} - glm::vec3 Terrain::calculateNormal(int x, int z) { float heightL = getHeight(x - 1, z); float heightR = getHeight(x + 1, z); diff --git a/grafika_projekt/src/Terrain.h b/grafika_projekt/src/Terrain.h index ad30090..060fe63 100644 --- a/grafika_projekt/src/Terrain.h +++ b/grafika_projekt/src/Terrain.h @@ -1,17 +1,17 @@ #pragma once -#include "RawModel.h" #include "glew.h" #include "freeglut.h" #include "glm.hpp" #include "HeightGenerator.h" +#include "objload.h" +#include class Terrain { public: Terrain() = default; - Terrain(int gridX, int gridZ, GLuint textureID, HeightGenerator heightGenerator); - RawModel getModel(); - GLuint getTexture(); + Terrain(int gridX, int gridZ, HeightGenerator heightGenerator); + obj::Model generateTerrain(); int getX(); int getZ(); @@ -22,10 +22,6 @@ private: float x; float z; HeightGenerator heightGenerator; - GLuint texture; - RawModel model; - RawModel generateTerrain(); - void storeDataInAttributeList(int attributeNumber, int coordinateSize, float data[]); glm::vec3 calculateNormal(int x, int z); float getHeight(int x, int z); }; diff --git a/grafika_projekt/src/TerrainRenderer.cpp b/grafika_projekt/src/TerrainRenderer.cpp deleted file mode 100644 index 8e6cdc8..0000000 --- a/grafika_projekt/src/TerrainRenderer.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "TerrainRenderer.h" -#include "glew.h" -#include "freeglut.h" -#include "glm.hpp" -#include "iostream" -#include "Texture.h" - -TerrainRenderer::TerrainRenderer(GLuint terrainProgram, - glm::mat4 projectionMatrix, - Terrain terrain, - glm::mat4 transformationMatrix, - glm::mat4 viewMatrix, - glm::vec3 lightDir, -// glm::vec3 lightColour, - float shineDamper, - float reflectivity) { - - this->terrainProgram = terrainProgram; - this->projectionMatrix = projectionMatrix; - this->terrain = terrain; - this->transformationMatrix = transformationMatrix; - this->viewMatrix = viewMatrix; - this->lightDir = lightDir; -// this->lightColour = lightColour; - this->shineDamper = shineDamper; - this->reflectivity = reflectivity; -} - -void TerrainRenderer::render() { - initShader(); - prepareTerrain(); - glDrawElements(GL_TRIANGLES, terrain.getModel().getVertexCount(), GL_UNSIGNED_INT, 0); - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glBindVertexArray(0); -} - -void TerrainRenderer::initShader() { - glBindAttribLocation(terrainProgram, 0, "position"); - glBindAttribLocation(terrainProgram, 1, "textureCoordinates"); - glBindAttribLocation(terrainProgram, 2, "normal"); - glUniformMatrix4fv(glGetUniformLocation(terrainProgram, "transformationMatrix"), 1, GL_FALSE, (float*) &transformationMatrix); - glUniformMatrix4fv(glGetUniformLocation(terrainProgram, "projectionMatrix"), 2, GL_FALSE, (float*) &projectionMatrix); - glUniformMatrix4fv(glGetUniformLocation(terrainProgram, "viewMatrix"), 3, GL_FALSE, (float*) &viewMatrix); - glUniform3f(glGetUniformLocation(terrainProgram, "lightDir"), lightDir.x, lightDir.y, lightDir.z); -// glUniform3f(glGetUniformLocation(terrainProgram, "lightColour"), lightColour.x, lightColour.y, lightColour.z); - glUniform1f(glGetUniformLocation(terrainProgram, "shineDamper"), shineDamper); - glUniform1f(glGetUniformLocation(terrainProgram, "reflectivity"), reflectivity); -} - -void TerrainRenderer::prepareTerrain() { - RawModel rawModel = terrain.getModel(); - glBindVertexArray(rawModel.getVaoID()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - GLuint texture = terrain.getTexture(); - Core::SetActiveTexture(texture, "modelTexture", terrainProgram, 0); -} \ No newline at end of file diff --git a/grafika_projekt/src/TerrainRenderer.h b/grafika_projekt/src/TerrainRenderer.h deleted file mode 100644 index e2b03f3..0000000 --- a/grafika_projekt/src/TerrainRenderer.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once -#include "Terrain.h" -#include "glm.hpp" -#include "glew.h" - -class TerrainRenderer -{ -public: - TerrainRenderer() = default; - TerrainRenderer(GLuint terrainProgram, - glm::mat4 projectionMatrix, - Terrain terrain, - glm::mat4 transformationMatrix, - glm::mat4 viewMatrix, - glm::vec3 lightDir, -// glm::vec3 lightColour, - float shineDamper, - float reflectivity); - - void render(); - -private: - void prepareTerrain(); - void initShader(); - - GLuint terrainProgram; - glm::mat4 projectionMatrix; - Terrain terrain; - glm::mat4 transformationMatrix; - glm::mat4 viewMatrix; - glm::vec3 lightDir; -// glm::vec3 lightColour; - float shineDamper; - float reflectivity; -}; - diff --git a/grafika_projekt/src/main.cpp b/grafika_projekt/src/main.cpp index f6f41ba..4257231 100644 --- a/grafika_projekt/src/main.cpp +++ b/grafika_projekt/src/main.cpp @@ -14,7 +14,6 @@ #include "SOIL/stb_image_aug.h" #include "HeightGenerator.h" #include "Terrain.h" -#include "TerrainRenderer.h" GLuint skyboxProgram, skyboxBuffer; GLuint bubbleProgram; @@ -51,8 +50,9 @@ Core::Shader_Loader shaderLoader; Core::RenderContext submarineContext; Core::RenderContext fishContext; Core::RenderContext bubbleContext; +Core::RenderContext terrainContext; -GLuint textureID; +GLuint textureTerrain; HeightGenerator heightGenerator; std::vector fishKeyPoints({ @@ -371,18 +371,18 @@ void renderScene() glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); - glUseProgram(terrainProgram); - glm::mat4 terrainTransformation = glm::translate(glm::vec3(0, 0, 0)) * glm::rotate(glm::radians(180.f), glm::vec3(5, 5, 5)); - TerrainRenderer terrainRenderer(terrainProgram, perspectiveMatrix, terrain, terrainTransformation, cameraMatrix, lightDir, 0.5f, 0.5f); - terrainRenderer.render(); - glUseProgram(0); - glm::mat4 submarineInitialTransformation = glm::translate(glm::vec3(0, -0.5, -0.4)) * glm::rotate(glm::radians(180.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.25f)); glm::mat4 submarineModelMatrix = glm::translate(cameraPos + cameraDir) * glm::mat4_cast(glm::inverse(rotation)) * submarineInitialTransformation; + + /// + + drawObjectTexture(terrainContext, glm::translate(glm::vec3(1, 1, 1)), textureTerrain, programTexture); + + /// + + 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); @@ -393,7 +393,7 @@ void renderScene() glm::vec3 change0 = glm::vec3(0, 0, 0); for (int j = 0; j < 100; j++) { - drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.04f), 0.2f), cubemapTexture, bubbleProgram); + drawObjectTexture(bubbleContext, animationMatrix(time + j, change0, bubbleArray[j], glm::vec3(0.04f), 0.2f), textureBubble, bubbleProgram); } for (int i = 0; i < 5; i++) { @@ -512,7 +512,6 @@ void init() 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"); - terrainProgram = shaderLoader.CreateProgram((char*)"shaders/terrainShader.vert", (char*)"shaders/terrainShader.frag"); cubemapTexture = loadCubemap(); @@ -522,6 +521,15 @@ void init() loadModelToContext("models/fish.obj", fishContext); textureFish = Core::LoadTexture("textures/fish.png"); + + + textureTerrain = Core::LoadTexture("textures/terrain.png"); + terrain = Terrain(0, 0, heightGenerator); + obj::Model model = terrain.generateTerrain(); + terrainContext.initFromOBJ(model); + + + initKeyRotation(); loadModelToContext("models/submarine.obj", submarineContext); textureSubmarine = Core::LoadTexture("textures/submarine.png"); @@ -530,11 +538,6 @@ void init() generateBubbleArray(); initCube(); initSkybox(); - - textureID = Core::LoadTexture("textures/terrain.png"); - - terrain = Terrain(0, 0, textureID, heightGenerator); - } void shutdown()