From dcc9542440a279e07591a5f7a91f42976ff3efc5 Mon Sep 17 00:00:00 2001 From: xkamikoo <58092037+xkamikoo@users.noreply.github.com> Date: Wed, 24 Feb 2021 23:50:12 +0100 Subject: [PATCH] Integrate Marcin's parallax with project --- grk-project.vcxproj | 4 +- grk-project.vcxproj.filters | 12 ++-- models/space_humster.mtl | 1 + shaders/shader_normal.vert | 3 +- shaders/shader_parallax.frag | 106 +++++++++++++++++++++++++++++++++++ shaders/shader_parallax.vert | 53 ++++++++++++++++++ src/Object.cpp | 13 +++-- src/Object.h | 7 ++- src/main.cpp | 15 +++-- src/main.h | 0 10 files changed, 194 insertions(+), 20 deletions(-) create mode 100644 shaders/shader_parallax.frag create mode 100644 shaders/shader_parallax.vert delete mode 100644 src/main.h diff --git a/grk-project.vcxproj b/grk-project.vcxproj index 793bdd8..54e0199 100644 --- a/grk-project.vcxproj +++ b/grk-project.vcxproj @@ -19,6 +19,8 @@ + + @@ -40,10 +42,8 @@ - - diff --git a/grk-project.vcxproj.filters b/grk-project.vcxproj.filters index de22e09..b2f13ea 100644 --- a/grk-project.vcxproj.filters +++ b/grk-project.vcxproj.filters @@ -66,6 +66,12 @@ Shader Files + + Shader Files + + + Shader Files + @@ -130,14 +136,8 @@ Source Files - - Header Files - Source Files - - Header Files - \ No newline at end of file diff --git a/models/space_humster.mtl b/models/space_humster.mtl index 703715d..896419b 100644 --- a/models/space_humster.mtl +++ b/models/space_humster.mtl @@ -22,4 +22,5 @@ d 1.000000 illum 2 map_Kd textures\\humster_BaseColor.png map_Ns textures\\humster_Roughness.png +bump textures\\humster_Height.png refl textures\\humster_Metallic.png diff --git a/shaders/shader_normal.vert b/shaders/shader_normal.vert index fabd687..5255b7e 100644 --- a/shaders/shader_normal.vert +++ b/shaders/shader_normal.vert @@ -19,6 +19,7 @@ uniform mat4 modelMatrix; uniform vec3 cameraPos; uniform PointLight pointLights[MAX_POINT_LIGHTS]; uniform int LightsCount; +uniform mat3 normalMatrix; out vec3 fragPos; @@ -31,8 +32,6 @@ void main() { gl_Position = transformation * vec4(vertexPosition, 1.0); - mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); - vec3 T = normalize(normalMatrix * aTangent); vec3 N = normalize(normalMatrix * vertexNormal); // re-orthogonalize T with respect to N diff --git a/shaders/shader_parallax.frag b/shaders/shader_parallax.frag new file mode 100644 index 0000000..64840f9 --- /dev/null +++ b/shaders/shader_parallax.frag @@ -0,0 +1,106 @@ +#version 430 core + +layout (location = 0) out vec4 FragColor; +layout (location = 1) out vec4 BrightColor; + +struct PointLight { + vec3 position; + vec3 color; + float intensity; +}; + +#define MAX_POINT_LIGHTS 16 + +uniform vec3 cameraPos; +uniform sampler2D diffuseTexture; +uniform sampler2D normalTexture; +uniform sampler2D depthTexture; + +uniform PointLight pointLights[MAX_POINT_LIGHTS]; +uniform int LightsCount; + +in vec3 fragPos; +in vec2 vTexCoord; +in vec3 LightPosTS[MAX_POINT_LIGHTS]; +in vec3 CameraPosTS; +in vec3 FragPosTS; + +vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir) +{ + // number of depth layers + const float minLayers = 8; + const float maxLayers = 32; + float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0.0, 0.0, 1.0), viewDir))); + // calculate the size of each layer + float layerDepth = 1.0 / numLayers; + // depth of current layer + float currentLayerDepth = 0.0; + // the amount to shift the texture coordinates per layer (from vector P) + vec2 P = viewDir.xy / viewDir.z * 0.1; + vec2 deltaTexCoords = P / numLayers; + + // get initial values + vec2 currentTexCoords = texCoords; + float currentdepthTextureValue = texture(depthTexture, currentTexCoords).r; + + while(currentLayerDepth < currentdepthTextureValue) + { + // shift texture coordinates along direction of P + currentTexCoords -= deltaTexCoords; + // get depthTexture value at current texture coordinates + currentdepthTextureValue = texture(depthTexture, currentTexCoords).r; + // get depth of next layer + currentLayerDepth += layerDepth; + } + + // get texture coordinates before collision (reverse operations) + vec2 prevTexCoords = currentTexCoords + deltaTexCoords; + + // get depth after and before collision for linear interpolation + float afterDepth = currentdepthTextureValue - currentLayerDepth; + float beforeDepth = texture(depthTexture, prevTexCoords).r - currentLayerDepth + layerDepth; + + // interpolation of texture coordinates + float weight = afterDepth / (afterDepth - beforeDepth); + vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight); + + return finalTexCoords; +} + +void main() +{ + vec3 fragColor = vec3(0,0,0); + vec3 V = normalize(CameraPosTS-FragPosTS); + vec2 texCoords = ParallaxMapping(vTexCoord, V); + if(texCoords.x > 1.0 || texCoords.y > 1.0 || texCoords.x < 0.0 || texCoords.y < 0.0) + discard; + + vec3 texture = texture2D(diffuseTexture, texCoords).rgb; + //vec3 texture = vec3(textureColor.x, textureColor.y, textureColor.z); + vec3 ambient = vec3(0.1, 0.1, 0.1) * texture; + + vec3 normal = texture2D(normalTexture, vTexCoord).rgb; + normal = normalize(normal * 2.0 - 1.0); + for(int i = 0; i < LightsCount; i++) + { + vec3 lightDir = normalize(LightPosTS[i] - FragPosTS); + + + vec3 R = reflect(-lightDir,normal); + + float dist = distance(fragPos, pointLights[i].position); + float distance = (1/dist) * (1/dist); + + float spec = pow(max(0,dot(R,V)),2); + float diff = max(0,dot(normal,normalize(lightDir))); + + vec3 diffuse = pointLights[i].color * diff * distance * pointLights[i].intensity; + vec3 specular = spec * pointLights[i].color * (pointLights[i].intensity/dist); + + + fragColor += texture*(diffuse+specular); + } + + BrightColor = vec4(0.0, 0.0, 0.0, 1.0); + FragColor = vec4(fragColor+ambient,1.0); +} diff --git a/shaders/shader_parallax.vert b/shaders/shader_parallax.vert new file mode 100644 index 0000000..5255b7e --- /dev/null +++ b/shaders/shader_parallax.vert @@ -0,0 +1,53 @@ +#version 430 core + +layout(location = 0) in vec3 vertexPosition; +layout(location = 1) in vec2 vertexTexCoord; +layout(location = 2) in vec3 vertexNormal; +layout (location = 3) in vec3 aTangent; +layout (location = 4) in vec3 aBitangent; + +struct PointLight { + vec3 position; + vec3 color; + float intensity; +}; + +#define MAX_POINT_LIGHTS 16 + +uniform mat4 transformation; +uniform mat4 modelMatrix; +uniform vec3 cameraPos; +uniform PointLight pointLights[MAX_POINT_LIGHTS]; +uniform int LightsCount; +uniform mat3 normalMatrix; + + +out vec3 fragPos; +out vec2 vTexCoord; +out vec3 LightPosTS[MAX_POINT_LIGHTS]; +out vec3 CameraPosTS; +out vec3 FragPosTS; + +void main() +{ + gl_Position = transformation * vec4(vertexPosition, 1.0); + + vec3 T = normalize(normalMatrix * aTangent); + vec3 N = normalize(normalMatrix * vertexNormal); + // re-orthogonalize T with respect to N + T = normalize(T - dot(T, N) * N); + // then retrieve perpendicular vector B with the cross product of T and N + vec3 B = cross(N, T); + + mat3 TBN = mat3(T, B, N); + + for(int i=0; iDraw(shaderID); glUseProgram(0); @@ -90,7 +95,7 @@ Object::Object( std::string name, this->name = name; SetMatrix(position, scale, rotation, angle); this->modelParent = modelParent; - this->textureID = textureID; + this->textureDiffuseID = textureID; this->shaderID = shaderID; this->color = color; this->dynamic = dynamic; @@ -110,7 +115,7 @@ Object::Object( std::string name, { this->name = name; SetMatrix(position, scale, rotation, angle); - this->textureID = -1; + this->textureDiffuseID = -1; this->modelParent = modelParent; this->shaderID = shaderID; this->color = color; diff --git a/src/Object.h b/src/Object.h index 8b83133..1e30427 100644 --- a/src/Object.h +++ b/src/Object.h @@ -56,6 +56,10 @@ public: glm::vec3 rotation, glm::vec3 scale, float angle, bool dynamic, bool kinematic); bool exists = true; + GLuint textureDiffuseID = -1; + GLuint textureNormalID = -1; + GLuint textureDepthID = -1; + glm::vec3 getScaleFromMatrix(glm::mat4 modelMatrix); glm::vec3 getPositionFromMatrix(glm::mat4 modelMatrix); glm::vec3 findOrbit(float time, glm::vec3 center, glm::vec3 orbit, glm::vec3 radius); @@ -66,9 +70,8 @@ private: std::shared_ptr modelParent; std::string name; glm::mat4 modelM; - glm::mat4 invModelM; + glm::mat3 invModelM; glm::mat4 rotationM; - GLuint textureID; GLuint shaderID; glm::vec3 color; glm::vec3 position; diff --git a/src/main.cpp b/src/main.cpp index 51e8d2f..032d5a2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -84,6 +84,7 @@ GLuint programBloom; GLuint programNormal; GLuint programParticle; GLuint programAsteroid; +GLuint programParallax; //bloompart unsigned int pingpongFBO[2]; @@ -643,6 +644,7 @@ void updatePhysics() void updateLights(GLuint program) { + glUniform1i(glGetUniformLocation(program, "LightsCount"), lights.size()); for (int i = 0; i < lights.size(); i++) { std::string col = "pointLights[" + std::to_string(i) + "].color"; @@ -677,15 +679,18 @@ void renderScene() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(programTex); - glUniform1i(glGetUniformLocation(programTex,"LightsCount"), lights.size()); + updateLights(programTex); glUniform3f(glGetUniformLocation(programTex, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z); glUseProgram(programNormal); - glUniform1i(glGetUniformLocation(programNormal, "LightsCount"), lights.size()); updateLights(programNormal); glUniform3f(glGetUniformLocation(programNormal, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z); + glUseProgram(programParallax); + updateLights(programParallax); + glUniform3f(glGetUniformLocation(programParallax, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z); + glUseProgram(programSun); glUniform3f(glGetUniformLocation(programSun, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z); @@ -1038,12 +1043,12 @@ void initObjects() glm::vec3(0, 2, 2), glm::vec3(1.5f, 1.0f, 1.0f), glm::vec3(0.3f), 0, false, true); objects.push_back(moon); - Object crewmateObj = Object("Space Humster", crewmate, programNormal, glm::vec3(1.0f), + Object crewmateObj = Object("Space Humster", crewmate, programTex, glm::vec3(1.0f), glm::vec3(-5, 0, 0), glm::vec3(1, 0, 1), glm::vec3(0.1), 0, true, false); objects.push_back(crewmateObj); //glm::mat4 shipModelMatrix = glm::translate(cameraPos + cameraDir * 0.7f + glm::vec3(0, -0.25f, 0)) * glm::rotate(-cameraAngle + glm::radians(90.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.0001f));; - Object ship = Object("Corvette", corvette, programNormal, glm::vec3(1.0f), + Object ship = Object("Corvette", corvette, programParallax, glm::vec3(1.0f), cameraPos+glm::vec3(75,-0.3,50), glm::vec3(0, 1, 0), glm::vec3(0.0001f), 60, true, false); objects.push_back(ship); } @@ -1059,6 +1064,8 @@ void init() programBloom = shaderLoader.CreateProgram("shaders/shader_bloom.vert", "shaders/shader_bloom.frag"); programParticle = shaderLoader.CreateProgram("shaders/shader_particle.vert", "shaders/shader_particle.frag"); programAsteroid = shaderLoader.CreateProgram("shaders/shader_asteroid.vert", "shaders/shader_asteroid.frag"); + programParallax = shaderLoader.CreateProgram("shaders/shader_parallax.vert", "shaders/shader_parallax.frag"); + glUseProgram(programBlur); glUniform1i(glGetUniformLocation(programBlur, "image"), 0); diff --git a/src/main.h b/src/main.h deleted file mode 100644 index e69de29..0000000