Try to get some simple lighting working
This commit is contained in:
parent
13e6b34243
commit
b4ad64d5c5
|
@ -2,18 +2,27 @@
|
|||
|
||||
in vec3 vs_position;
|
||||
in mat4 vs_transform;
|
||||
in vec3 vs_color;
|
||||
in vec2 vs_texcoord;
|
||||
in vec3 vs_normal;
|
||||
in vec3 vs_to_light;
|
||||
|
||||
uniform bool texture_is_set;
|
||||
uniform sampler2D vs_texture;
|
||||
uniform vec3 light_color;
|
||||
|
||||
out vec4 fs_color;
|
||||
|
||||
void main() {
|
||||
vec3 unit_normal = normalize(vs_normal);
|
||||
vec3 unit_light_vector = normalize(vs_to_light);
|
||||
|
||||
float dot_prod = dot(unit_normal, unit_light_vector);
|
||||
float brightness = max(dot_prod, 0.f);
|
||||
vec3 diffuse = brightness * light_color;
|
||||
|
||||
if (texture_is_set) {
|
||||
fs_color = texture(vs_texture, vs_texcoord) * vec4(vs_color, 1.0);
|
||||
fs_color = vec4(diffuse, 1.f) * texture(vs_texture, vs_texcoord);
|
||||
} else {
|
||||
fs_color = vec4(vs_color, 1.0);
|
||||
fs_color = vec4(diffuse, 1.f);
|
||||
}
|
||||
}
|
|
@ -1,26 +1,32 @@
|
|||
#version 440
|
||||
|
||||
layout (location = 0) in vec3 vertex_position;
|
||||
layout (location = 1) in vec3 vertex_color;
|
||||
layout (location = 2) in vec2 vertex_texcoord;
|
||||
layout (location = 1) in vec2 vertex_texcoord;
|
||||
layout (location = 2) in vec3 vertex_normal;
|
||||
|
||||
out vec3 vs_position;
|
||||
out vec3 vs_color;
|
||||
out vec2 vs_texcoord;
|
||||
out mat4 vs_transform;
|
||||
out vec3 vs_normal;
|
||||
out vec3 vs_to_light;
|
||||
|
||||
uniform mat4 transform_matrix;
|
||||
uniform mat4 view_matrix;
|
||||
uniform mat4 projection_matrix;
|
||||
uniform vec3 light_position;
|
||||
|
||||
void main() {
|
||||
vs_position = vertex_position;
|
||||
vec4 world_pos = (transform_matrix * vec4(vertex_position, 1.f));
|
||||
|
||||
vs_position = world_pos.xyz;
|
||||
vs_transform = transform_matrix;
|
||||
vs_color = vertex_color;
|
||||
vs_texcoord = vertex_texcoord;
|
||||
|
||||
gl_Position = projection_matrix * view_matrix * transform_matrix * vec4(vertex_position, 1.0f);
|
||||
gl_Position = projection_matrix * view_matrix * world_pos;
|
||||
|
||||
vs_normal = (transform_matrix * vec4(vertex_normal, 0.f)).xyz;
|
||||
vs_to_light = light_position - world_pos.xyz;
|
||||
|
||||
/* vs_position = vec4(transform_matrix * vec4(vertex_position, 1.f)).xyz;
|
||||
vs_transform = transform_matrix;
|
||||
vs_color = vertex_color;
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
#include "simpleengine/camera.h"
|
||||
#include "simpleengine/gfx/light.h"
|
||||
#include "simpleengine/gfx/model.h"
|
||||
#include "simpleengine/gfx/texture.h"
|
||||
#include "simpleengine/vector.h"
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#include <glm/fwd.hpp>
|
||||
#include <simpleengine/gfx/shader.h>
|
||||
#include <simpleengine/renderable.h>
|
||||
#include <simpleengine/event/event.h>
|
||||
|
@ -45,11 +47,19 @@ int main(int argc, char *argv[]) {
|
|||
/* simpleengine::gfx::Texture wall_texture("resources/wall.jpg");
|
||||
simpleengine::gfx::Texture crate_texture("resources/container.jpg", true, true); */
|
||||
|
||||
simpleengine::gfx::Texture stall_texture("resources/stallTexture.png");
|
||||
|
||||
auto light = std::make_shared<simpleengine::gfx::Light>(core_shader, glm::vec3(0.f, 0.f, -20.f), glm::vec3(1.f, 1.f, 1.f));
|
||||
game.add_event(light);
|
||||
|
||||
simpleengine::gfx::Texture white_texture("resources/white_texture.jpg");
|
||||
auto dragon = std::make_shared<simpleengine::objects_3d::ObjModel>(game.get_window(), core_shader, white_texture, "resources/dragon.obj");
|
||||
dragon->translate(0.f, -5.f, -25.f);
|
||||
game.add_event(dragon);
|
||||
|
||||
/* simpleengine::gfx::Texture stall_texture("resources/stallTexture.png");
|
||||
auto stall = std::make_shared<simpleengine::objects_3d::ObjModel>(game.get_window(), core_shader, stall_texture, "resources/stall.obj");
|
||||
stall->translate(0.f, -4.f, -18.f);
|
||||
game.add_event(stall);
|
||||
stall->translate(0.f, -4.f, -25.f);
|
||||
game.add_event(stall); */
|
||||
|
||||
/* std::vector<simpleengine::Vertex> square_vertices = {
|
||||
{ simpleengine::Vectorf(0.5f, 0.5f, -1.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) }, // top right
|
||||
|
@ -129,7 +139,7 @@ int main(int argc, char *argv[]) {
|
|||
auto cube = std::make_shared<simpleengine::gfx::Model>(game.get_window(), core_shader, cube_vertices, cube_indicies);
|
||||
game.add_event(cube); */
|
||||
|
||||
auto camera = std::make_shared<simpleengine::Camera>(game.get_window(), core_shader);
|
||||
auto camera = std::make_shared<simpleengine::Camera>(game.get_window(), core_shader, 70, glm::vec3(0, 0, -10));
|
||||
game.add_event(camera);
|
||||
|
||||
return game.run();
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include "shader.h"
|
||||
#include "../renderable.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace simpleengine::gfx {
|
||||
class Light : public simpleengine::Renderable {
|
||||
public:
|
||||
gfx::Shader shader;
|
||||
glm::vec3 position;
|
||||
glm::vec3 color;
|
||||
|
||||
Light(gfx::Shader shader, glm::vec3 position, glm::vec3 color) : shader(shader), position(position), color(color) {
|
||||
|
||||
}
|
||||
|
||||
virtual void update(const float& delta_time) override {
|
||||
shader.use();
|
||||
shader.set_uniform_float_vec3("light_position", position, false);
|
||||
shader.set_uniform_float_vec3("light_color", color, false);
|
||||
shader.unuse();
|
||||
}
|
||||
|
||||
virtual void render(GLFWwindow* target) override {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
|
@ -22,7 +22,15 @@ namespace simpleengine::objects_3d {
|
|||
std::vector<std::string> split_string(std::string str, const char delim);
|
||||
static void process_vertex(const std::vector<std::string>& vertex_data, std::vector<GLuint>& indicies, const std::vector<glm::vec2>& in_textures,
|
||||
const std::vector<glm::vec3>& in_normals, std::vector<glm::vec2>& out_textures, std::vector<glm::vec3>& out_normals);
|
||||
private:
|
||||
/**
|
||||
* @brief This is replaced with `lit_vertices`!!!!
|
||||
*
|
||||
*/
|
||||
using simpleengine::gfx::Model::vertices;
|
||||
public:
|
||||
std::vector<LitVertex> lit_vertices;
|
||||
|
||||
ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::string filename);
|
||||
ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::ifstream file_stream);
|
||||
|
||||
|
|
|
@ -93,7 +93,7 @@ namespace simpleengine {
|
|||
}
|
||||
|
||||
virtual void scale(float scalar) {
|
||||
transform_matrix = scalar * transform_matrix;
|
||||
transform_matrix = glm::scale(transform_matrix, glm::vec3(scalar, scalar, scalar));
|
||||
}
|
||||
};
|
||||
}
|
|
@ -11,6 +11,8 @@ namespace simpleengine {
|
|||
private:
|
||||
glm::vec<3, VectorType, glm::defaultp> inner_vec;
|
||||
public:
|
||||
Vector() = default;
|
||||
|
||||
Vector(VectorType x, VectorType y, VectorType z) : inner_vec(x, y, z) {
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#ifdef __linux__
|
||||
#include <GL/glew.h>
|
||||
#include <GL/gl.h>
|
||||
|
@ -15,13 +16,33 @@
|
|||
namespace simpleengine {
|
||||
class Vertex {
|
||||
public:
|
||||
//glm::vec3 position;
|
||||
simpleengine::Vectorf position;
|
||||
glm::vec3 color;
|
||||
glm::vec2 tex_coord;
|
||||
|
||||
Vertex() = default;
|
||||
|
||||
Vertex(simpleengine::Vectorf position, glm::vec3 color, glm::vec2 tex_coord) : position(position), color(color), tex_coord(tex_coord) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A `Vertex` that can be lit up.
|
||||
*
|
||||
*/
|
||||
class LitVertex {
|
||||
public:
|
||||
simpleengine::Vectorf position;
|
||||
glm::vec3 color;
|
||||
glm::vec2 tex_coord;
|
||||
glm::vec3 normal;
|
||||
|
||||
LitVertex() = default;
|
||||
|
||||
LitVertex(simpleengine::Vectorf position, glm::vec3 color, glm::vec2 tex_coord, glm::vec3 normal) :
|
||||
position(position), color(color), tex_coord(tex_coord), normal(normal) {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
|
@ -89,21 +89,17 @@ namespace simpleengine::objects_3d {
|
|||
file_stream.close();
|
||||
|
||||
for (int i = 0; i < obj_vertices.size(); i++) {
|
||||
vertices.emplace_back(simpleengine::Vectorf(obj_vertices.at(i)), glm::vec3(1.f), textures.at(i));
|
||||
lit_vertices.emplace_back(simpleengine::Vectorf(obj_vertices.at(i)), glm::vec3(1.f), textures.at(i), normals.at(i));
|
||||
}
|
||||
|
||||
vao.bind();
|
||||
vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size());
|
||||
vbo.buffer(lit_vertices.data(), 0, sizeof(LitVertex) * lit_vertices.size());
|
||||
ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint));
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position));
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, tex_coord));
|
||||
glEnableVertexAttribArray(2);
|
||||
vao.enable_attrib(vbo, 0, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, position));
|
||||
//vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, color));
|
||||
vao.enable_attrib(vbo, 1, 2, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, tex_coord));
|
||||
vao.enable_attrib(vbo, 2, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, normal));
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
|
Loading…
Reference in New Issue