Prepared better shaders.

This commit is contained in:
Nicolás A. Ortega Froysa 2018-12-17 15:38:39 +01:00
parent d225234640
commit 5405063f28
5 changed files with 43 additions and 16 deletions

View File

@ -1,7 +1,15 @@
#version 330 core #version 330 core
uniform vec3 light_dir;
in vec3 frag_col; in vec3 frag_col;
in vec3 frag_pos;
in vec3 normal;
out vec3 col; out vec3 col;
void main() { void main() {
col = frag_col; vec3 norm = normalize(normal);
vec3 lightDir = normalize(-light_dir);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * frag_col;
col = diffuse;
} }

View File

@ -1,9 +1,20 @@
#version 330 core #version 330 core
layout(location = 0) in vec3 vert_pos; layout(location = 0) in vec3 vert_pos;
layout(location = 1) in vec3 vert_norm;
uniform vec3 col; uniform vec3 col;
uniform mat4 mvp; uniform vec3 light_col;
uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;
out vec3 frag_pos;
out vec3 frag_col; out vec3 frag_col;
out vec3 normal;
void main() { void main() {
gl_Position = mvp * vec4(vert_pos, 1); gl_Position = model * view * proj * vec4(vert_pos, 1);
frag_col = col; frag_pos = vec3(model * vec4(vert_pos, 1.0));
frag_col = col * light_col;
normal = mat3(transpose(inverse(model))) * vert_norm;
} }

View File

@ -21,6 +21,7 @@
#include <string> #include <string>
#include <GL/glew.h> #include <GL/glew.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <glm/glm.hpp>
class Shader { class Shader {
public: public:
@ -48,6 +49,18 @@ public:
glUniform1f(glGetUniformLocation(id, name.c_str()), glUniform1f(glGetUniformLocation(id, name.c_str()),
value); value);
} }
inline void setVec3(const std::string &name,
const glm::vec3 &v)
{
glUniform3f(glGetUniformLocation(id, name.c_str()),
v.x, v.y, v.z);
}
inline void setMat4(const std::string &name,
const glm::mat4 &m)
{
glUniformMatrix4fv(glGetUniformLocation(id, name.c_str()),
1, GL_FALSE, &m[0][0]);
}
inline unsigned int getId() const { inline unsigned int getId() const {
return id; return id;
} }

View File

@ -65,15 +65,9 @@ System::System() : running(true) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
/*shader = std::make_unique<Shader>(
"../assets/shaders/vertex_shader.glsl",
"../assets/shaders/fragment_shader.glsl");*/
shaders.insert({"default", Shader( shaders.insert({"default", Shader(
"../assets/shaders/vertex_shader.glsl", "../assets/shaders/vertex_shader.glsl",
"../assets/shaders/fragment_shader.glsl")}); "../assets/shaders/fragment_shader.glsl")});
matrix_id = glGetUniformLocation(
shaders.at("default").getId(),
"mvp");
asset_mngr.loadModel( asset_mngr.loadModel(
"../assets/models/board.obj", "../assets/models/board.obj",
"board"); "board");
@ -101,7 +95,10 @@ void System::run() {
glClear(GL_COLOR_BUFFER_BIT bitor GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT bitor GL_DEPTH_BUFFER_BIT);
shaders.at("default").use(); shaders.at("default").use();
glm::mat4 mvp; shaders.at("default").setVec3("light_col",
glm::vec3(1.0f, 1.0f, 0.8f));
shaders.at("default").setVec3("light_dir",
glm::vec3(-0.3f, -1, 0));
{ {
glm::mat4 proj = glm::perspective(glm::radians(45.0f), glm::mat4 proj = glm::perspective(glm::radians(45.0f),
static_cast<float>(WINDOW_WIDTH) / static_cast<float>(WINDOW_HEIGHT), static_cast<float>(WINDOW_WIDTH) / static_cast<float>(WINDOW_HEIGHT),
@ -113,11 +110,10 @@ void System::run() {
glm::vec3(0,1,0) // which way is vertically up glm::vec3(0,1,0) // which way is vertically up
); );
glm::mat4 mod = glm::mat4(1.0f); // identity matrix, object is at origin glm::mat4 mod = glm::mat4(1.0f); // identity matrix, object is at origin
mvp = proj * view * mod; shaders.at("default").setMat4("model", mod);
shaders.at("default").setMat4("proj", proj);
shaders.at("default").setMat4("view", view);
} }
glUniformMatrix4fv(matrix_id, 1, GL_FALSE, &mvp[0][0]);
asset_mngr.getModel("board")->setColor(1,0,0); asset_mngr.getModel("board")->setColor(1,0,0);
asset_mngr.getModel("board")->draw(shaders.at("default")); asset_mngr.getModel("board")->draw(shaders.at("default"));

View File

@ -43,5 +43,4 @@ private:
std::map<std::string, Shader> shaders; std::map<std::string, Shader> shaders;
Camera cam; Camera cam;
bool running; bool running;
GLuint matrix_id;
}; };