Compare commits

..

No commits in common. "c32e7bd00dbea0248d3914263809853a5f11f5e6" and "11b7f4a3edde791496d58d7dca6dd67f3ebf6338" have entirely different histories.

213 changed files with 35 additions and 15362 deletions

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 89 KiB

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,71 +0,0 @@
#include "scene.hpp"
#include "dbc.hpp"
#include <math.h>
void Scene::update() {
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// for now just detect the camera moved and do spotlight update
if(camera.dirty) {
light.camera.position = camera.position;
light.camera.direction = camera.front;
// just update the camera's light
shader.apply_spot_light(light.camera, 0);
camera.dirty = false;
}
camera.update(deltaTime);
}
void Scene::draw_thing(Shader& with_shader, Thing& thing)
{
with_shader.apply_material(thing.material);
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, thing.position);
model = glm::rotate(model, glm::radians(thing.rotation.angle), thing.rotation.axes);
model = glm::scale(model, glm::vec3(thing.scale));
with_shader.setMat4("model", model);
thing.model.draw(with_shader);
}
void Scene::render(GLFWwindow* window) {
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 view = camera.look_at();
glm::mat4 projection = glm::mat4(1.0f);
// fov, aspect, near plane, far plane
projection = glm::perspective(glm::radians(camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
shader.use();
shader.setMat4("view", view);
shader.setMat4("projection", projection);
for(auto& thing : things) {
draw_thing(shader, thing);
}
}
void Scene::cleanup() {
shader.cleanup();
}
void Scene::spawn(const std::string& name, components::Position& position, components::Rotation& rotation) {
if(models.contains(name)) {
things.emplace_back(
name,
models.at(name),
materials.at("default"),
position,
rotation,
1.0f);
} else {
dbc::log($F("No model named: {}", name));
}
}

View file

@ -1,205 +0,0 @@
#include "shader.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <print>
#include <filesystem>
#include "dbc.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
namespace fs = std::filesystem;
inline std::string read_file(const std::string& filename) {
// load the file
std::ifstream in_file{filename, std::ios::binary};
// get the size of the file
std::stringstream in_str;
in_str << in_file.rdbuf();
return in_str.str();
}
void check_error(const std::string& what, unsigned int thing, GLenum check_type) {
int success = 0;
char infoLog[512] = {0};
if(check_type == GL_LINK_STATUS) {
glGetProgramiv(thing, check_type, &success);
if(!success) {
glGetProgramInfoLog(thing, 512, NULL, infoLog);
dbc::sentinel(std::format("ERROR: Program {} compile failed: {}", what, infoLog));
}
} else {
glGetShaderiv(thing, check_type, &success);
if(!success) {
glGetShaderInfoLog(thing, 512, NULL, infoLog);
dbc::sentinel(std::format("ERROR: Shader {} compile failed: {}", what, infoLog));
}
}
}
unsigned int Shader::load_shader(const std::string& filename, GLenum shader_type) {
dbc::check(fs::exists(filename),
std::format("shader file {} does not exist", filename));
// create the shader
std::string shader_code = read_file(filename);
const char* shader_code_ptr = shader_code.c_str();
int shader_id = glCreateShader(shader_type);
glShaderSource(shader_id, 1, &shader_code_ptr, NULL);
glCompileShader(shader_id);
check_error(filename, shader_id, GL_COMPILE_STATUS);
// check compile error
return shader_id;
}
Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath) {
unsigned int vertex = load_shader(vertexPath, GL_VERTEX_SHADER);
unsigned int fragment = load_shader(fragmentPath, GL_FRAGMENT_SHADER);
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
check_error("link", ID, GL_LINK_STATUS);
glDeleteShader(vertex);
glDeleteShader(fragment);
}
void Shader::use() const {
glUseProgram(ID);
}
void Shader::cleanup() {
glDeleteProgram(ID);
}
void Shader::setBool(const std::string &name, bool value) const {
auto uniform = glGetUniformLocation(ID, name.c_str());
glUniform1i(uniform, (int)value);
}
void Shader::setInt(const std::string &name, int value) const {
auto uniform = glGetUniformLocation(ID, name.c_str());
glUniform1i(uniform, value);
}
void Shader::setFloat(const std::string &name, float value) const {
auto uniform = glGetUniformLocation(ID, name.c_str());
glUniform1f(uniform, value);
}
void Shader::setVec4(const std::string &name, float v1, float v2, float v3, float v4) const {
auto uniform = glGetUniformLocation(ID, name.c_str());
glUniform4f(uniform, v1, v2, v3, v4);
}
void Shader::setVec4(const std::string &name, const glm::vec4& value) const
{
auto uniform = glGetUniformLocation(ID, name.c_str());
glUniform4fv(uniform, 1, &value[0]);
}
void Shader::setVec3(const std::string &name, const glm::vec3& value) const
{
auto uniform = glGetUniformLocation(ID, name.c_str());
glUniform3fv(uniform, 1, &value[0]);
}
void Shader::setVec3(const std::string &name, float v1, float v2, float v3) const {
auto uniform = glGetUniformLocation(ID, name.c_str());
glUniform3f(uniform, v1, v2, v3);
}
void Shader::setMat4(const std::string &name, const glm::mat4& mat) const {
unsigned int loc = glGetUniformLocation(ID, name.c_str());
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(mat));
}
void Shader::apply_material(const Material& material) {
setVec3("material.ambient", material.ambient);
setFloat("material.shininess", material.shininess);
}
void Shader::apply_lighting(const Lighting& lighting) {
setInt("pointLightCount", lighting.positioned.size());
setInt("dirLightCount", lighting.directional.size());
for(size_t i = 0; i < lighting.directional.size(); i++) {
apply_dir_light(lighting.directional[i], i);
}
for(size_t i = 0; i < lighting.positioned.size(); i++) {
apply_point_light(lighting.positioned[i], i);
}
// set the first spotlight to the camera light if given
size_t i = 0;
// camera is optional, so only add if used
if(lighting.camera.on) {
apply_spot_light(lighting.camera, 0);
i++;
}
// gross but that's fine for now
for(; i < lighting.spot.size(); i++) {
// need to be off by one because the camera is a spot light
if(lighting.spot[i].on) {
apply_spot_light(lighting.spot[i], i+1);
}
}
// need +1 for the camera spot light
setInt("spotLightCount", (int)i);
}
void Shader::apply_dir_light(const Light& light, size_t index) {
dbc::check(index < MAX_LIGHTS, "too many directional lights");
// nasty, this needs to go
setVec3(std::format("dirLights[{}].direction", index), light.direction);
setVec3(std::format("dirLights[{}].ambient", index), light.ambient);
setVec3(std::format("dirLights[{}].diffuse", index), light.diffuse);
setVec3(std::format("dirLights[{}].specular", index), light.specular);
}
void Shader::apply_point_light(const Light& light, size_t index) {
dbc::check(index < MAX_LIGHTS, "too many positioned lights");
// disgusting, crafting a string on every render?
setVec3(std::format("pointLights[{}].position", index), light.position);
setVec3(std::format("pointLights[{}].ambient", index), light.ambient);
setVec3(std::format("pointLights[{}].diffuse", index), light.diffuse);
setVec3(std::format("pointLights[{}].specular", index), light.specular);
setFloat(std::format("pointLights[{}].constant", index), light.constant);
setFloat(std::format("pointLights[{}].linear", index), light.linear);
setFloat(std::format("pointLights[{}].quadratic", index), light.quadratic);
}
void Shader::apply_spot_light(const Light& light, size_t index) {
dbc::check(index < MAX_LIGHTS, "too many spot lights");
// find a way to not make strings all the time
setVec3(std::format("spotLights[{}].position", index), light.position);
setVec3(std::format("spotLights[{}].direction", index), light.direction);
setVec3(std::format("spotLights[{}].ambient", index), light.ambient);
setVec3(std::format("spotLights[{}].diffuse", index), light.diffuse);
setVec3(std::format("spotLights[{}].specular", index), light.specular);
setFloat(std::format("spotLights[{}].constant", index), light.constant);
setFloat(std::format("spotLights[{}].linear", index), light.linear);
setFloat(std::format("spotLights[{}].quadratic", index), light.quadratic);
setFloat(std::format("spotLights[{}].cut_off", index), glm::cos(glm::radians(light.cut_off)));
setFloat(std::format("spotLights[{}].outer_cut_off", index), glm::cos(glm::radians(light.outer_cut_off)));
}

View file

@ -1,78 +0,0 @@
{
"material_test": {
"ambient": [0.1, 0.1, 0.1],
"shininess": 32.0,
"diffuseMap": 0,
"specularMap": 0
},
"lighting_test": {
"directional": [
{
"position": [0, 0, 0.0],
"direction":[-0.2, -1.0, -0.3],
"ambient":[0.2, 0.2, 0.2],
"diffuse":[0.8, 0.8, 0.8],
"specular":[1.0, 1.0, 1.0],
"constant": 0.0,
"linear": 0.0,
"quadratic": 0.0,
"cut_off": 0.0,
"outer_cut_off": 0.0,
"on": true
}
],
"positioned": [
{
"position": [1.2, 1.0, 2.0],
"direction":[-0.2, -1.0, -0.3],
"ambient":[0.2, 0.2, 0.2],
"diffuse":[0.8, 0.8, 0.8],
"specular":[1.0, 1.0, 1.0],
"constant": 1.0,
"linear": 0.09,
"quadratic": 0.032,
"cut_off": 12.5,
"outer_cut_off": 17.5,
"on": true
}
],
"spot": [
],
"camera": {
"position":[2.2, 1.0, 2.0],
"direction":[0.0, 0.0, -1.0],
"ambient":[0.2, 0.2, 0.2],
"diffuse":[0.8, 0.8, 0.8],
"specular":[1.0, 1.0, 1.0],
"constant": 1.0,
"linear": 0.09,
"quadratic": 0.032,
"cut_off": 12.5,
"outer_cut_off": 17.5,
"on": true
}
},
"camera_test": {
"position": [0.0, 0.0, 3.0],
"front": [0.0, 0.0, -1.0],
"up": [0.0, 1.0, 0.0],
"direction": [0.0, 0.0, 0.0],
"movement_speed": 20.0
},
"model_test": {
"directory": "assets",
"model_path": "create.obj"
},
"thing_test":
{"model": "grass", "position": [2.5, 0.0, 0.0],
"rotation": {"angle": 90.0, "axes": [0.0,1.0,0.0,0.0]},
"scale": 1.0,
"material": "default"
},
"shader_test": {
"vertex_path": "shaders/16-vert.glsl",
"frag_path": "shaders/16-frag.glsl"
}
}

View file

Before

Width:  |  Height:  |  Size: 457 KiB

After

Width:  |  Height:  |  Size: 457 KiB

Before After
Before After

File diff suppressed because one or more lines are too long

View file

Before

Width:  |  Height:  |  Size: 195 KiB

After

Width:  |  Height:  |  Size: 195 KiB

Before After
Before After

Binary file not shown.

After

Width:  |  Height:  |  Size: 508 KiB

File diff suppressed because one or more lines are too long

Binary file not shown.

View file

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

Before After
Before After

Binary file not shown.

View file

@ -16,7 +16,7 @@
"specularMap": 0 "specularMap": 0
}, },
"shiny": { "shiny": {
"ambient": [0.5, 0.5, 0.5], "ambient": [0.5, 0.25, 0.31],
"shininess": 10000.0, "shininess": 10000.0,
"diffuseMap": 0, "diffuseMap": 0,
"specularMap": 0 "specularMap": 0
@ -34,10 +34,6 @@
"grass": { "grass": {
"directory": "assets", "directory": "assets",
"model_path": "grass.glb" "model_path": "grass.glb"
},
"window": {
"directory": "assets",
"model_path": "window.glb"
} }
}, },
"things": [ "things": [
@ -45,9 +41,9 @@
"rotation": {"angle": 0.0, "axes": [1.0, 1.0, 1.0]}, "rotation": {"angle": 0.0, "axes": [1.0, 1.0, 1.0]},
"scale": 1.0, "scale": 1.0,
"material": "default"}, "material": "default"},
{"model": "marble_cube", "position": [1.5, 0.0, 1.0], {"model": "marble_cube", "position": [2.0, 0.0, 0.0],
"rotation": {"angle": 0.0, "axes": [1.0, 1.0, 1.0]}, "rotation": {"angle": 0.0, "axes": [1.0, 1.0, 1.0]},
"scale": 2.0, "scale": 1.0,
"material": "default"}, "material": "default"},
{"model": "metal_floor", "position": [0.0, 0.0, 0.0], {"model": "metal_floor", "position": [0.0, 0.0, 0.0],
"rotation": {"angle": 0.0, "axes": [1.0, 1.0, 1.0]}, "rotation": {"angle": 0.0, "axes": [1.0, 1.0, 1.0]},
@ -56,15 +52,7 @@
{"model": "grass", "position": [0.5, 0.13, 0.0], {"model": "grass", "position": [0.5, 0.13, 0.0],
"rotation": {"angle": 90.0, "axes": [1.0,0.0,0.0]}, "rotation": {"angle": 90.0, "axes": [1.0,0.0,0.0]},
"scale": 1.0, "scale": 1.0,
"material": "default"}, "material": "default"}
{"model": "window", "position": [1.75, 0.25, 0.25],
"rotation": {"angle": 90.0, "axes": [1.0,0.0,0.0]},
"scale": 1.0,
"material": "shiny"},
{"model": "window", "position": [1.5, 0.25, 0.0],
"rotation": {"angle": 90.0, "axes": [1.0,0.0,0.0]},
"scale": 1.0,
"material": "shiny"}
], ],
"camera": { "camera": {
"position": [0.0, 0.1, 3.0], "position": [0.0, 0.1, 3.0],
@ -85,8 +73,7 @@
"linear": 0.0, "linear": 0.0,
"quadratic": 0.0, "quadratic": 0.0,
"cut_off": 0.0, "cut_off": 0.0,
"outer_cut_off": 0.0, "outer_cut_off": 0.0
"on": true
} }
], ],
"positioned": [ "positioned": [
@ -100,8 +87,7 @@
"linear": 0.09, "linear": 0.09,
"quadratic": 0.032, "quadratic": 0.032,
"cut_off": 12.5, "cut_off": 12.5,
"outer_cut_off": 17.5, "outer_cut_off": 17.5
"on": true
} }
], ],
"spot": [ "spot": [
@ -116,8 +102,7 @@
"linear": 0.09, "linear": 0.09,
"quadratic": 0.032, "quadratic": 0.032,
"cut_off": 12.5, "cut_off": 12.5,
"outer_cut_off": 17.5, "outer_cut_off": 17.5
"on": true
} }
} }
} }

View file

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 58 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 181 KiB

After

Width:  |  Height:  |  Size: 181 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 457 KiB

After

Width:  |  Height:  |  Size: 457 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 141 KiB

After

Width:  |  Height:  |  Size: 141 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

Before After
Before After

View file

@ -2,7 +2,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <vector> #include <vector>
#include <unordered_map> #include <map>
#include "json.hpp" #include "json.hpp"
const unsigned int SCR_WIDTH = 800; const unsigned int SCR_WIDTH = 800;
@ -34,7 +34,6 @@ namespace components {
float quadratic=0.0f; float quadratic=0.0f;
float cut_off=0.0f; float cut_off=0.0f;
float outer_cut_off=0.0f; float outer_cut_off=0.0f;
bool on=true;
void adjust(float amount) { void adjust(float amount) {
ambient += amount; ambient += amount;
@ -47,7 +46,7 @@ namespace components {
position, direction, position, direction,
ambient, diffuse, specular, ambient, diffuse, specular,
constant, linear, quadratic, constant, linear, quadratic,
cut_off, outer_cut_off, on); cut_off, outer_cut_off);
struct Lighting { struct Lighting {
std::vector<Light> directional; std::vector<Light> directional;
@ -103,8 +102,8 @@ namespace components {
struct Scene { struct Scene {
components::Shader shader; components::Shader shader;
components::Shader light_shader; components::Shader light_shader;
std::unordered_map<std::string, Material> materials; std::map<std::string, Material> materials;
std::unordered_map<std::string, Model> models; std::map<std::string, Model> models;
std::vector<Thing> things; std::vector<Thing> things;
Camera camera; Camera camera;
Lighting light; Lighting light;

View file

@ -2,7 +2,6 @@
#include <stb_image.h> #include <stb_image.h>
#include "scene.hpp" #include "scene.hpp"
#include "utils.hpp" #include "utils.hpp"
void framebuffer_size_callback(GLFWwindow *window, int width, int height); void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void init_glfw() { void init_glfw() {
@ -72,12 +71,6 @@ void process_input(GLFWwindow *window, Scene& scene) {
} }
if(glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) { if(glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
scene.light.camera.on = !scene.light.camera.on;
scene.shader.apply_lighting(scene.light);
}
if(glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS) {
for(auto& light : scene.light.directional) { for(auto& light : scene.light.directional) {
light.adjust(-0.01f); light.adjust(-0.01f);
} }

View file

@ -7,15 +7,6 @@ void Scene::update() {
deltaTime = currentFrame - lastFrame; deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame; lastFrame = currentFrame;
// for now just detect the camera moved and do spotlight update
if(camera.dirty) {
light.camera.position = camera.position;
light.camera.direction = camera.front;
// just update the camera's light
shader.apply_spot_light(light.camera, 0);
camera.dirty = false;
}
camera.update(deltaTime); camera.update(deltaTime);
} }
@ -47,6 +38,15 @@ void Scene::render(GLFWwindow* window) {
shader.setMat4("view", view); shader.setMat4("view", view);
shader.setMat4("projection", projection); shader.setMat4("projection", projection);
// for now just detect the camera moved and do spotlight update
if(camera.dirty) {
light.camera.position = camera.position;
light.camera.direction = camera.front;
// just update the camera's light
shader.apply_spot_light(light.camera, 0);
camera.dirty = false;
}
for(auto& thing : things) { for(auto& thing : things) {
draw_thing(shader, thing); draw_thing(shader, thing);
} }

View file

@ -12,7 +12,6 @@
#include "camera.hpp" #include "camera.hpp"
#include "components.hpp" #include "components.hpp"
#include <vector> #include <vector>
#include <unordered_map>
struct Thing { struct Thing {
std::string name; std::string name;
@ -21,17 +20,16 @@ struct Thing {
components::Position position; components::Position position;
components::Rotation rotation; components::Rotation rotation;
float scale; float scale;
float distance = 0;
}; };
struct Scene { struct Scene {
Shader shader; Shader shader;
Shader light_shader; Shader light_shader;
std::unordered_map<std::string, components::Material> materials; std::map<std::string, components::Material> materials;
Camera camera; Camera camera;
components::Lighting light; components::Lighting light;
std::unordered_map<std::string, Model> models; std::map<std::string, Model> models;
std::vector<Thing> things; std::vector<Thing> things;
float deltaTime = 0.0f; float deltaTime = 0.0f;

View file

@ -134,6 +134,9 @@ void Shader::apply_lighting(const Lighting& lighting) {
setInt("pointLightCount", lighting.positioned.size()); setInt("pointLightCount", lighting.positioned.size());
setInt("dirLightCount", lighting.directional.size()); setInt("dirLightCount", lighting.directional.size());
// need +1 for the camera spot light
setInt("spotLightCount", lighting.spot.size() + 1);
for(size_t i = 0; i < lighting.directional.size(); i++) { for(size_t i = 0; i < lighting.directional.size(); i++) {
apply_dir_light(lighting.directional[i], i); apply_dir_light(lighting.directional[i], i);
} }
@ -142,25 +145,13 @@ void Shader::apply_lighting(const Lighting& lighting) {
apply_point_light(lighting.positioned[i], i); apply_point_light(lighting.positioned[i], i);
} }
// set the first spotlight to the camera light if given // set the first spotlight to the camera light
size_t i = 0; apply_spot_light(lighting.camera, 0);
// camera is optional, so only add if used for(size_t i = 0; i < lighting.spot.size(); i++) {
if(lighting.camera.on) {
apply_spot_light(lighting.camera, 0);
i++;
}
// gross but that's fine for now
for(; i < lighting.spot.size(); i++) {
// need to be off by one because the camera is a spot light // need to be off by one because the camera is a spot light
if(lighting.spot[i].on) { apply_spot_light(lighting.spot[i], i+1);
apply_spot_light(lighting.spot[i], i+1);
}
} }
// need +1 for the camera spot light
setInt("spotLightCount", (int)i);
} }

Some files were not shown because too many files have changed in this diff Show more