Starting the refactor session on day 14. Moved the glad and KHR dirs into src to start.
This commit is contained in:
parent
1b7066757a
commit
5b5e8a9fb0
62 changed files with 13880 additions and 0 deletions
362
14-refactor/src/main.cpp
Normal file
362
14-refactor/src/main.cpp
Normal file
|
|
@ -0,0 +1,362 @@
|
|||
#define _USE_MATH_DEFINES
|
||||
#include <math.h>
|
||||
#include "dbc.hpp"
|
||||
#include <print>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include "shader.hpp"
|
||||
#include <stb_image.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "data.hpp"
|
||||
#include "model.hpp"
|
||||
#include "camera.hpp"
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 600;
|
||||
|
||||
const float AMBIENT_LEVEL = 0.25f;
|
||||
|
||||
struct Config {
|
||||
Shader shader;
|
||||
Shader lightShader;
|
||||
|
||||
Lighting light{
|
||||
.directional={
|
||||
.direction={-0.2f, -1.0f, -0.3f},
|
||||
.ambient={0.2f, 0.2f, 0.2f},
|
||||
.diffuse={0.8f, 0.8f, 0.8f},
|
||||
.specular={1.0f, 1.0f, 1.0f},
|
||||
},
|
||||
.positioned={
|
||||
{
|
||||
.position={1.2f, 1.0f, 2.0f},
|
||||
.direction={-0.2f, -1.0f, -0.3f},
|
||||
.ambient={0.2f, 0.2f, 0.2f},
|
||||
.diffuse={0.8f, 0.8f, 0.8f},
|
||||
.specular={1.0f, 1.0f, 1.0f},
|
||||
.constant=1.0f,
|
||||
.linear=0.09f,
|
||||
.quadratic=0.032f,
|
||||
.cutOff=12.5f,
|
||||
.outerCutOff=17.5f,
|
||||
},
|
||||
{
|
||||
.position={-1.2f, -1.0f, -2.0f},
|
||||
.direction={0.2f, 1.0f, 0.3f},
|
||||
.ambient={0.2f, 0.2f, 0.2f},
|
||||
.diffuse={0.8f, 0.8f, 0.8f},
|
||||
.specular={1.0f, 1.0f, 1.0f},
|
||||
.constant=1.0f,
|
||||
.linear=0.09f,
|
||||
.quadratic=0.032f,
|
||||
.cutOff=12.5f,
|
||||
.outerCutOff=17.5f,
|
||||
},
|
||||
},
|
||||
.spot={
|
||||
.position={2.2f, 1.0f, 2.0f},
|
||||
.direction={0.0f, 0.0f, -1.0f},
|
||||
.ambient={0.2f, 0.2f, 0.2f},
|
||||
.diffuse={0.8f, 0.8f, 0.8f},
|
||||
.specular={1.0f, 1.0f, 1.0f},
|
||||
.constant=1.0f,
|
||||
.linear=0.09f,
|
||||
.quadratic=0.032f,
|
||||
.cutOff=12.5f,
|
||||
.outerCutOff=17.5f,
|
||||
},
|
||||
};
|
||||
|
||||
glm::vec3 cubePos{2.0f, 0.0f, 0.0f};
|
||||
Camera camera{
|
||||
.pos={2.2f, 1.0f, 2.0f},
|
||||
.movementSpeed=2.0f
|
||||
};
|
||||
|
||||
Material cubeMaterial{
|
||||
.ambient = {1.0f, 0.5f, 0.31f},
|
||||
.shininess = 32.0f,
|
||||
.diffuseMap = 0,
|
||||
.specularMap = 0,
|
||||
};
|
||||
|
||||
unsigned int cubeVAO = 0;
|
||||
unsigned int lightCubeVAO = 0;
|
||||
unsigned int VBO = 0;
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
void updateDelta() {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
}
|
||||
};
|
||||
|
||||
void init_glfw() {
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
}
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow *, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
|
||||
Camera* camera = static_cast<Camera*>(glfwGetWindowUserPointer(window));
|
||||
camera->mouse_move(xpos, ypos);
|
||||
}
|
||||
|
||||
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
|
||||
Camera* camera = static_cast<Camera*>(glfwGetWindowUserPointer(window));
|
||||
camera->mouse_scroll(xoffset, yoffset);
|
||||
}
|
||||
|
||||
GLFWwindow* create_window() {
|
||||
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
||||
dbc::check(window != NULL, "failed to open window");
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
auto good = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||
dbc::check(good, "failed to load GLAD");
|
||||
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
|
||||
void processInput(GLFWwindow *window, Config& config) {
|
||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
config.camera.forward();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
config.camera.back();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
config.camera.left();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
config.camera.right();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
|
||||
config.light.directional.adjust(-0.01f);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
|
||||
config.light.directional.adjust(0.01f);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int load_texture(const std::string& image_file) {
|
||||
unsigned int texture_id;
|
||||
glGenTextures(1, &texture_id);
|
||||
glBindTexture(GL_TEXTURE_2D, texture_id);
|
||||
|
||||
// set the texture wrapping parameters
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
|
||||
// set texture filtering parameters
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int nrChannels = 0;
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
|
||||
unsigned char *data = stbi_load(image_file.c_str(), &width, &height, &nrChannels, 0);
|
||||
|
||||
dbc::check(data != nullptr, std::format("Failed to load texture: {}", image_file));
|
||||
|
||||
dbc::check(nrChannels == 3 || nrChannels == 4,
|
||||
std::format("Image {} has invalid channels {} should be 3 or 4.", image_file, nrChannels));
|
||||
|
||||
auto rgb_or_a = nrChannels == 3 ? GL_RGB : GL_RGBA;
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, rgb_or_a, width, height, 0, rgb_or_a, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture_id;
|
||||
}
|
||||
|
||||
Config setup() {
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
Config config{
|
||||
.shader={"shaders/13-vert.glsl", "shaders/13-frag.glsl"},
|
||||
.lightShader={"shaders/13-vert.glsl", "shaders/13-lightsource.frag.glsl"},
|
||||
};
|
||||
|
||||
unsigned int VBO = 0;
|
||||
unsigned int lightCubeVAO = 0;
|
||||
unsigned int cubeVAO = 0;
|
||||
|
||||
// cube vao configure, this is the light target
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(cubeVAO);
|
||||
|
||||
// position attribute
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
// light vao configure, this is the light source
|
||||
glGenVertexArrays(1, &lightCubeVAO);
|
||||
glBindVertexArray(lightCubeVAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
config.lightCubeVAO = lightCubeVAO;
|
||||
config.cubeVAO = cubeVAO;
|
||||
config.VBO = VBO;
|
||||
|
||||
config.cubeMaterial.diffuseMap = load_texture("resources/textures/container2.png");
|
||||
config.cubeMaterial.specularMap = load_texture("resources/textures/container2_specular.png");
|
||||
|
||||
config.shader.use();
|
||||
config.shader.setInt("material.diffuse", 0);
|
||||
config.shader.setInt("material.specular", 1);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
void draw_cube(size_t pos, Config& config, glm::mat4& projection, glm::mat4& view) {
|
||||
config.shader.setMat4("view", view);
|
||||
config.shader.setMat4("projection", projection);
|
||||
config.shader.setVec3("viewPos", config.camera.pos);
|
||||
|
||||
config.shader.applyMaterial(config.cubeMaterial);
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, cube_positions[pos]);
|
||||
|
||||
float angle = glfwGetTime() * (float)pos;
|
||||
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
|
||||
config.shader.setMat4("model", model);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, config.cubeMaterial.diffuseMap);
|
||||
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, config.cubeMaterial.specularMap);
|
||||
|
||||
glBindVertexArray(config.cubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
void draw_light(Config& config, Light& light, glm::mat4& projection, glm::mat4& view) {
|
||||
config.lightShader.use();
|
||||
|
||||
config.lightShader.setVec3("diffuse", light.diffuse);
|
||||
config.lightShader.setMat4("projection", projection);
|
||||
config.lightShader.setMat4("view", view);
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, light.position);
|
||||
model = glm::scale(model, glm::vec3(0.2f));
|
||||
config.lightShader.setMat4("model", model);
|
||||
|
||||
glBindVertexArray(config.lightCubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
void render(GLFWwindow* window, Config& config) {
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
config.shader.use();
|
||||
|
||||
// time is used for fake 3d rotation
|
||||
float time = glfwGetTime();
|
||||
|
||||
config.camera.update(config.deltaTime);
|
||||
|
||||
glm::mat4 view = config.camera.lookAt();
|
||||
glm::mat4 projection = glm::mat4(1.0f);
|
||||
|
||||
// fov, aspect, near plane, far plane
|
||||
projection = glm::perspective(glm::radians(config.camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
|
||||
config.shader.use();
|
||||
config.light.spot.position = config.camera.pos;
|
||||
config.light.spot.direction = config.camera.front;
|
||||
config.shader.applyLighting(config.light);
|
||||
|
||||
for(size_t i = 0; i < CUBE_COUNT; i++) {
|
||||
draw_cube(i, config, projection, view);
|
||||
}
|
||||
|
||||
for(auto& light : config.light.positioned) {
|
||||
draw_light(config, light, projection, view);
|
||||
}
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
void cleanup(Config& config) {
|
||||
glDeleteVertexArrays(1, &config.lightCubeVAO);
|
||||
glDeleteBuffers(1, &config.lightCubeVAO);
|
||||
glDeleteBuffers(1, &config.VBO);
|
||||
config.shader.cleanup();
|
||||
}
|
||||
|
||||
int main() {
|
||||
init_glfw();
|
||||
|
||||
auto window = create_window();
|
||||
auto config = setup();
|
||||
|
||||
glfwSetWindowUserPointer(window, &config.camera);
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
processInput(window, config);
|
||||
render(window, config);
|
||||
config.updateDelta();
|
||||
}
|
||||
|
||||
cleanup(config);
|
||||
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue