Integrate Marcin's parallax with project

This commit is contained in:
xkamikoo 2021-02-24 23:50:12 +01:00
parent 36fe4ce569
commit dcc9542440
10 changed files with 194 additions and 20 deletions

View File

@ -19,6 +19,8 @@
<None Include="shaders\shader_blur.vert" />
<None Include="shaders\shader_normal.frag" />
<None Include="shaders\shader_normal.vert" />
<None Include="shaders\shader_parallax.frag" />
<None Include="shaders\shader_parallax.vert" />
<None Include="shaders\shader_particle.frag" />
<None Include="shaders\shader_particle.vert" />
<None Include="shaders\shader_skybox.frag" />
@ -40,10 +42,8 @@
<ClCompile Include="src\Texture.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Object.h" />
<ClInclude Include="Skybox.h" />
<ClInclude Include="src\Camera.h" />
<ClInclude Include="src\main.h" />
<ClInclude Include="src\mesh.h" />
<ClInclude Include="src\model.h" />
<ClInclude Include="src\Object.h" />

View File

@ -66,6 +66,12 @@
<None Include="shaders\shader_asteroid.vert">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_parallax.frag">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_parallax.vert">
<Filter>Shader Files</Filter>
</None>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Box.cpp">
@ -130,14 +136,8 @@
<ClInclude Include="src\Physics.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="Object.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Object.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\main.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -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

View File

@ -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

View File

@ -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);
}

View File

@ -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; i<LightsCount; i++)
LightPosTS[i] = TBN * pointLights[i].position;
CameraPosTS = TBN * cameraPos;
FragPosTS = TBN * fragPos;
fragPos = (modelMatrix*vec4(vertexPosition,1)).xyz;
vTexCoord = vertexTexCoord;
}

View File

@ -10,10 +10,15 @@ void Object::Draw(glm::mat4 perspectiveMatrix, glm::mat4 cameraMatrix)
glUniformMatrix4fv(glGetUniformLocation(shaderID, "modelMatrix"), 1, GL_FALSE, (float*)&modelM);
glUniformMatrix4fv(glGetUniformLocation(shaderID, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix3fv(glGetUniformLocation(shaderID, "normalMatrix"), 1, GL_FALSE, (float*)&invModelM);
glUniform3f(glGetUniformLocation(shaderID, "objectColor"), color.r, color.g, color.b);
if (textureID != -1)
Core::SetActiveTexture(textureID, "diffuseTexture", shaderID, 0);
if (textureDiffuseID != -1)
Core::SetActiveTexture(textureDiffuseID, "diffuseTexture", shaderID, 0);
if (textureNormalID != -1)
Core::SetActiveTexture(textureNormalID, "normalTexture", shaderID, 1);
if (textureDepthID != -1)
Core::SetActiveTexture(textureDepthID, "depthTexture", shaderID, 2);
modelParent->Draw(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;

View File

@ -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<Model> 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;

View File

@ -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);

View File