Added the camera position and the models position to pbr shader

This commit is contained in:
Warwick 2022-07-18 14:17:08 +01:00
parent b4fda4612c
commit a0819a4e98
8 changed files with 38 additions and 4 deletions

View file

@ -4,9 +4,12 @@ out vec4 FragColor;
in vec2 ourTexCoord;
in vec3 ourNormCoord;
in vec3 FragPos;
// TODO: make temporary hard coded world/camera pos dynamic
vec3 WorldPos = vec3(0.0f, 0.0f, 0.0f);
vec3 CameraPos = vec3(0.0f, 0.0f, -1.0f);
uniform vec3 WorldPos ;
uniform vec3 CameraPos;
//vec3 WorldPos = vec3(0.0f, 0.0f, 0.0f);
//vec3 CameraPos = vec3(0.0f, 0.0f, -1.0f);
//TODO: make these values rely on associated textures.
vec3 albedo = vec3(0.8f, 0.8f, 0.8f);
float metallic = 0.3f;
@ -112,6 +115,6 @@ void main()
color = color / (color + vec3(1.0));
color = pow(color, vec3(1.0/2.2));
//FragColor = vec4(color, 1.0);
//FragColor = vec4(CameraPos, 1.0);
FragColor = texture(texture_diffuse1, ourTexCoord) * vec4(color, 0.0);
}

View file

@ -0,0 +1,21 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoord;
uniform mat4 MVP;
uniform mat4 Model;
out vec2 ourTexCoord;
out vec3 ourNormCoord;
out vec3 FragPos;
void main()
{
gl_Position = MVP * Model * vec4(aPos, 1.0);
ourNormCoord = aNormal;
ourTexCoord = aTexCoord;
// Calculate position of fragment
FragPos = vec3(Model * vec4(aPos, 1.0));
};

View file

@ -7,6 +7,7 @@ Model::Model(std::vector<Mesh> meshes) { this->meshes = meshes; }
void Model::draw(ShaderLoader &shader) {
for (unsigned int i = 0; i < this->meshes.size(); i++) {
shader.setMat4("Model", this->model);
shader.setVec3("WorldPos", this->position);
this->meshes[i].draw(shader);
}
}

View file

@ -57,3 +57,4 @@ void PlayerCamera::tick() {
}
glm::mat4 PlayerCamera::getMVP() { return MVP; }
glm::vec3 PlayerCamera::getCameraPosition() { return cameraPosition; }

View file

@ -37,4 +37,5 @@ public:
void tick();
glm::mat4 getMVP();
glm::vec3 getCameraPosition();
};

View file

@ -92,3 +92,7 @@ void ShaderLoader::setMat4(const std::string &name, glm::mat4 value) const {
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE,
&value[0][0]);
}
void ShaderLoader::setVec3(const std::string &name, glm::vec3 value) const {
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1,
glm::value_ptr(value));
}

View file

@ -6,6 +6,7 @@
#include "Error.h"
#include <fstream>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <sstream>
#include <string>
@ -27,4 +28,5 @@ public:
void setInt(const std::string &name, int value) const;
void setFloat(const std::string &name, float value) const;
void setMat4(const std::string &name, glm::mat4 value) const;
void setVec3(const std::string &name, glm::vec3 value) const;
};

View file

@ -66,7 +66,7 @@ int main(int argc, char **argv) {
// Create event handling struct
SDL_Event input;
ShaderLoader shader(ROOT_DIR "data/shaders/vertex.glsl",
ShaderLoader shader(ROOT_DIR "data/shaders/pbrVertex.glsl",
ROOT_DIR "data/shaders/pbrFragment.glsl");
Model backpack(std::string(ROOT_DIR) +
@ -117,6 +117,7 @@ int main(int argc, char **argv) {
// Send our glsl shader our mvp
shader.setMat4("MVP", camera.getMVP());
shader.setVec3("CameraPos", camera.getCameraPosition());
// Draw Meshes
cube.draw(shader);