main.cpp all cleaned out and scene.cpp ready to refactor.
This commit is contained in:
parent
a7beb5a92a
commit
74cfaf4514
5 changed files with 206 additions and 211 deletions
|
|
@ -79,6 +79,7 @@ sources = [
|
|||
'src/physics.cpp',
|
||||
'src/mesh.cpp',
|
||||
'src/model.cpp',
|
||||
'src/scene.cpp',
|
||||
'src/camera.cpp',
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -2,17 +2,3 @@
|
|||
#include <glm/glm.hpp>
|
||||
|
||||
#define CUBE_COUNT 9
|
||||
|
||||
// positions all containers
|
||||
glm::vec3 popcorn_positions[] = {
|
||||
{0.0f, 0.0f, 0.0f},
|
||||
{2.0f, 5.0f, -15.0f},
|
||||
{-1.5f, -2.2f, -2.5f},
|
||||
{-3.8f, -2.0f, -12.3f},
|
||||
{ 2.4f, -0.4f, -3.5f},
|
||||
{-1.7f, 3.0f, -7.5f},
|
||||
{ 1.3f, -2.0f, -2.5f},
|
||||
{ 1.5f, 2.0f, -2.5f},
|
||||
{ 1.5f, 0.2f, -1.5f},
|
||||
{-1.3f, 1.0f, -1.5f}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,101 +1,9 @@
|
|||
#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"
|
||||
#include "scene.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;
|
||||
std::vector<Model> models;
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
void updateDelta() {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
}
|
||||
};
|
||||
|
||||
void init_glfw() {
|
||||
glfwInit();
|
||||
|
|
@ -137,75 +45,40 @@ GLFWwindow* create_window() {
|
|||
}
|
||||
|
||||
|
||||
void processInput(GLFWwindow *window, Config& config) {
|
||||
void process_input(GLFWwindow *window, Scene& scene) {
|
||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
config.camera.forward();
|
||||
scene.camera.forward();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
config.camera.back();
|
||||
scene.camera.back();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
config.camera.left();
|
||||
scene.camera.left();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
config.camera.right();
|
||||
scene.camera.right();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
|
||||
config.light.directional.adjust(-0.01f);
|
||||
scene.light.directional.adjust(-0.01f);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
|
||||
config.light.directional.adjust(0.01f);
|
||||
scene.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() {
|
||||
Scene setup() {
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
Config config{
|
||||
Scene scene{
|
||||
.shader={"shaders/14-vert.glsl", "shaders/14-frag.glsl"},
|
||||
.lightShader={"shaders/14-vert.glsl", "shaders/14-lightsource.frag.glsl"},
|
||||
.models={
|
||||
|
|
@ -213,84 +86,39 @@ Config setup() {
|
|||
},
|
||||
};
|
||||
|
||||
// REFACTOR: really this should be done somewhere else
|
||||
for(auto& model : config.models) {
|
||||
model.connect_shader(config.shader);
|
||||
}
|
||||
scene.configure();
|
||||
|
||||
return config;
|
||||
return scene;
|
||||
}
|
||||
|
||||
void draw_models(Config& config, size_t model_i, size_t model_pos, glm::mat4& projection, glm::mat4& view)
|
||||
{
|
||||
glm::vec3 objectColor = {1.0, 0.94, 0.78};
|
||||
config.shader.apply_material(config.cubeMaterial);
|
||||
|
||||
float time = glfwGetTime();
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, popcorn_positions[model_pos]);
|
||||
model = glm::rotate(model, glm::radians(time * 10.0f), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
|
||||
config.shader.setMat4("model", model);
|
||||
|
||||
config.models[model_i].draw(config.shader);
|
||||
void update(GLFWwindow* window, Scene& scene) {
|
||||
process_input(window, scene);
|
||||
scene.updateDelta();
|
||||
}
|
||||
|
||||
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.look_at();
|
||||
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.shader.setMat4("view", view);
|
||||
config.shader.setMat4("projection", projection);
|
||||
config.shader.setVec3("viewPos", config.camera.pos);
|
||||
|
||||
config.light.spot.position = config.camera.pos;
|
||||
config.light.spot.direction = config.camera.front;
|
||||
config.shader.apply_lighting(config.light);
|
||||
|
||||
for(size_t i = 0; i < CUBE_COUNT; i++) {
|
||||
draw_models(config, 0, i, projection, view);
|
||||
}
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
void render(GLFWwindow* window, Scene& scene) {
|
||||
scene.render(window);
|
||||
}
|
||||
|
||||
void cleanup(Config& config) {
|
||||
config.shader.cleanup();
|
||||
void quit(GLFWwindow* window, Scene& scene) {
|
||||
scene.cleanup();
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
int main() {
|
||||
init_glfw();
|
||||
|
||||
auto window = create_window();
|
||||
auto config = setup();
|
||||
auto scene = setup();
|
||||
|
||||
glfwSetWindowUserPointer(window, &config.camera);
|
||||
glfwSetWindowUserPointer(window, &scene.camera);
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
processInput(window, config);
|
||||
render(window, config);
|
||||
config.updateDelta();
|
||||
update(window, scene);
|
||||
render(window, scene);
|
||||
}
|
||||
|
||||
cleanup(config);
|
||||
|
||||
glfwTerminate();
|
||||
quit(window, scene);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
72
14-refactor/src/scene.cpp
Normal file
72
14-refactor/src/scene.cpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include "scene.hpp"
|
||||
#include "dbc.hpp"
|
||||
#include <math.h>
|
||||
|
||||
void Scene::updateDelta() {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
}
|
||||
|
||||
|
||||
void Scene::configure() {
|
||||
// REFACTOR: really this should be done somewhere else
|
||||
for(auto& model : models) {
|
||||
model.connect_shader(shader);
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::draw_model(Model& scene_model, glm::mat4& projection, glm::mat4& view, glm::vec3& position)
|
||||
{
|
||||
glm::vec3 objectColor = {1.0, 0.94, 0.78};
|
||||
shader.apply_material(cubeMaterial);
|
||||
|
||||
float time = glfwGetTime();
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, position);
|
||||
model = glm::rotate(model, glm::radians(time * 10.0f), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
|
||||
shader.setMat4("model", model);
|
||||
|
||||
scene_model.draw(shader);
|
||||
}
|
||||
|
||||
void Scene::render(GLFWwindow* window) {
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
shader.use();
|
||||
|
||||
// time is used for fake 3d rotation
|
||||
float time = glfwGetTime();
|
||||
|
||||
camera.update(deltaTime);
|
||||
|
||||
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);
|
||||
shader.setVec3("viewPos", camera.pos);
|
||||
|
||||
light.spot.position = camera.pos;
|
||||
light.spot.direction = camera.front;
|
||||
shader.apply_lighting(light);
|
||||
|
||||
// BUG: no connection between models and positions
|
||||
for(auto& position : positions) {
|
||||
draw_model(models[0], projection, view, position);
|
||||
}
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
|
||||
void Scene::cleanup() {
|
||||
shader.cleanup();
|
||||
}
|
||||
108
14-refactor/src/scene.hpp
Normal file
108
14-refactor/src/scene.hpp
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "shader.hpp"
|
||||
#include "model.hpp"
|
||||
#include "shader.hpp"
|
||||
#include "model.hpp"
|
||||
#include "camera.hpp"
|
||||
#include "scene.hpp"
|
||||
#include <vector>
|
||||
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 600;
|
||||
|
||||
const float AMBIENT_LEVEL = 0.25f;
|
||||
|
||||
struct Scene {
|
||||
Shader shader;
|
||||
Shader lightShader;
|
||||
std::vector<Model> models;
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
|
||||
// positions all containers
|
||||
std::vector<glm::vec3> positions{
|
||||
{0.0f, 0.0f, 0.0f},
|
||||
{2.0f, 5.0f, -15.0f},
|
||||
{-1.5f, -2.2f, -2.5f},
|
||||
{-3.8f, -2.0f, -12.3f},
|
||||
{ 2.4f, -0.4f, -3.5f},
|
||||
{-1.7f, 3.0f, -7.5f},
|
||||
{ 1.3f, -2.0f, -2.5f},
|
||||
{ 1.5f, 2.0f, -2.5f},
|
||||
{ 1.5f, 0.2f, -1.5f},
|
||||
{-1.3f, 1.0f, -1.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,
|
||||
};
|
||||
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
void updateDelta();
|
||||
void configure();
|
||||
void draw_model(Model& scene_model, glm::mat4& projection, glm::mat4& view, glm::vec3& position);
|
||||
void render(GLFWwindow* window);
|
||||
void cleanup();
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue