fixing stuff
This commit is contained in:
174
src/main.cpp
174
src/main.cpp
@@ -1,39 +1,33 @@
|
||||
#include <bitset>
|
||||
#include <array>
|
||||
#include <span>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
|
||||
#include "glad/glad.h"
|
||||
#include "lib/glad/glad.h"
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include "loaders/stb_image.h"
|
||||
#include "lib/loaders/stb_image.h"
|
||||
|
||||
#include "gfx/geometry.h"
|
||||
#include "gfx/Texture.h"
|
||||
#include "gfx/Mesh.h"
|
||||
#include "gfx/Shader.h"
|
||||
#include "gfx/Color.h"
|
||||
#include "VoxelSpace.h"
|
||||
#include "SomaSolve.h"
|
||||
#include "lib/djstdlib/core.cpp"
|
||||
|
||||
struct Entity;
|
||||
struct Polycube;
|
||||
struct SceneGraphNode;
|
||||
auto new_entity() -> int;
|
||||
auto get_entity(int id) -> Entity*;
|
||||
auto get_scene_graph_node(int id) -> SceneGraphNode*;
|
||||
auto new_graph_node() -> int;
|
||||
int new_entity();
|
||||
Entity *get_entity(int id);
|
||||
SceneGraphNode *get_scene_graph_node(int id);
|
||||
int new_graph_node();
|
||||
|
||||
auto print_mat(glm::mat4* matrix) -> void {
|
||||
void print_mat(glm::mat4* matrix) {
|
||||
auto mat = *matrix;
|
||||
std::cout << mat[0][0] << mat[0][1] << mat[0][2] << mat[0][3] << std::endl;
|
||||
std::cout << mat[1][0] << mat[1][1] << mat[1][2] << mat[1][3] << std::endl;
|
||||
@@ -49,19 +43,19 @@ struct Camera {
|
||||
glm::vec3 up;
|
||||
glm::vec3 target;
|
||||
|
||||
auto init(float aspect_ratio = 800.0f / 600.0f) -> void {
|
||||
void init(float aspect_ratio = 800.0f / 600.0f) {
|
||||
view = glm::mat4();
|
||||
proj = glm::perspective(glm::radians(45.0f), aspect_ratio, 0.1f, 100.0f);
|
||||
pos = glm::vec3(0.0f);
|
||||
up = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||
}
|
||||
|
||||
auto look_at(float x, float y, float z) -> void {
|
||||
void look_at(float x, float y, float z) {
|
||||
target = glm::vec3(x, y, z);
|
||||
view = glm::lookAt(pos, target, up);
|
||||
}
|
||||
|
||||
auto set_up(float up_x, float up_y, float up_z) -> void {
|
||||
void set_up(float up_x, float up_y, float up_z) {
|
||||
up = glm::vec3(up_x, up_y, up_z);
|
||||
}
|
||||
};
|
||||
@@ -69,7 +63,7 @@ struct Camera {
|
||||
struct GlobalAppState {
|
||||
int current_polycube;
|
||||
int last_polycube_visible;
|
||||
Shader* active_shader;
|
||||
Shader *active_shader;
|
||||
std::vector<Polycube> polycubes;
|
||||
};
|
||||
GlobalAppState app_state;
|
||||
@@ -80,8 +74,8 @@ struct WindowDims {
|
||||
};
|
||||
|
||||
struct Entity {
|
||||
Mesh* mesh;
|
||||
Texture* tex;
|
||||
Mesh *mesh;
|
||||
Texture *tex;
|
||||
bool visible;
|
||||
int scene_graph_node;
|
||||
};
|
||||
@@ -95,19 +89,19 @@ struct SceneGraphNode {
|
||||
std::vector<int> children;
|
||||
std::optional<int> entity;
|
||||
|
||||
auto reset() -> void {
|
||||
void reset() {
|
||||
scale = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
translation = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
rotation = glm::quat(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
auto init() -> void {
|
||||
void init() {
|
||||
reset();
|
||||
local = glm::mat4(1.0f);
|
||||
world = local;
|
||||
}
|
||||
|
||||
auto update_local() -> void {
|
||||
void update_local() {
|
||||
local = glm::scale(
|
||||
glm::translate(
|
||||
glm::mat4(1.0f),
|
||||
@@ -122,8 +116,8 @@ struct Polycube {
|
||||
int graph_node;
|
||||
glm::vec3 color;
|
||||
|
||||
auto show() -> void {
|
||||
auto node = get_scene_graph_node(graph_node);
|
||||
void show() {
|
||||
SceneGraphNode *node = get_scene_graph_node(graph_node);
|
||||
for (auto &child : node->children) {
|
||||
auto node = get_scene_graph_node(child);
|
||||
if (node->entity) {
|
||||
@@ -132,19 +126,19 @@ struct Polycube {
|
||||
}
|
||||
}
|
||||
|
||||
auto hide() -> void {
|
||||
auto node = get_scene_graph_node(graph_node);
|
||||
for (auto &child : node->children) {
|
||||
auto node = get_scene_graph_node(child);
|
||||
void hide() {
|
||||
SceneGraphNode *node = get_scene_graph_node(graph_node);
|
||||
for (int &child : node->children) {
|
||||
SceneGraphNode *node = get_scene_graph_node(child);
|
||||
if (node->entity) {
|
||||
get_entity(*node->entity)->visible = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto get_centre() -> glm::vec3 {
|
||||
auto centre = glm::vec3(0.0f);
|
||||
for (auto &child : get_scene_graph_node(graph_node)->children) {
|
||||
glm::vec3 get_centre() {
|
||||
glm::vec3 centre = glm::vec3(0.0f);
|
||||
for (int &child : get_scene_graph_node(graph_node)->children) {
|
||||
centre += get_scene_graph_node(child)->translation;
|
||||
}
|
||||
centre /= get_scene_graph_node(graph_node)->children.size();
|
||||
@@ -159,17 +153,17 @@ struct Frame {
|
||||
int y;
|
||||
Camera* cam;
|
||||
|
||||
auto init(Camera* camera) -> void {
|
||||
void init(Camera* camera) {
|
||||
camera->init((float)width / (float)height);
|
||||
cam = camera;
|
||||
}
|
||||
};
|
||||
|
||||
auto framebuffer_size_callback(GLFWwindow* window, int width, int height) -> void {
|
||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
auto init_window_and_gl(WindowDims* window_dims) -> GLFWwindow* {
|
||||
GLFWwindow *init_window_and_gl(WindowDims *window_dims) {
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
|
||||
@@ -193,19 +187,19 @@ auto init_window_and_gl(WindowDims* window_dims) -> GLFWwindow* {
|
||||
return window;
|
||||
}
|
||||
|
||||
auto gl_update_viewport(WindowDims* window_dims, Frame* frame) -> void {
|
||||
void gl_update_viewport(WindowDims* window_dims, Frame* frame) {
|
||||
glViewport(frame->x, window_dims->height - frame->y - frame->height, frame->width, frame->height);
|
||||
}
|
||||
|
||||
auto cube_mesh = Mesh{};
|
||||
auto wall_tex = Texture{};
|
||||
auto entities = std::vector<Entity>();
|
||||
auto scene_graph_nodes = std::vector<SceneGraphNode>();
|
||||
Mesh cube_mesh = {0};
|
||||
Texture wall_tex = {0};
|
||||
std::vector<Entity> entities = std::vector<Entity>();
|
||||
std::vector<SceneGraphNode> scene_graph_nodes = std::vector<SceneGraphNode>();
|
||||
|
||||
auto process_input(GLFWwindow *window) -> void {
|
||||
static auto wireframe = false;
|
||||
static auto last_frame_state_press_enter = false;
|
||||
static auto last_frame_state_press = false;
|
||||
void process_input(GLFWwindow *window) {
|
||||
static bool wireframe = false;
|
||||
static bool last_frame_state_press_enter = false;
|
||||
static bool last_frame_state_press = false;
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
@@ -232,7 +226,7 @@ auto process_input(GLFWwindow *window) -> void {
|
||||
}
|
||||
|
||||
|
||||
auto new_entity() -> int {
|
||||
int new_entity() {
|
||||
entities.emplace_back();
|
||||
scene_graph_nodes.emplace_back();
|
||||
entities.back().scene_graph_node = scene_graph_nodes.size();
|
||||
@@ -240,21 +234,21 @@ auto new_entity() -> int {
|
||||
return entities.size();
|
||||
}
|
||||
|
||||
auto get_entity(int id) -> Entity* {
|
||||
Entity *get_entity(int id) {
|
||||
return &entities[id - 1];
|
||||
}
|
||||
|
||||
auto get_scene_graph_node(int id) -> SceneGraphNode* {
|
||||
SceneGraphNode *get_scene_graph_node(int id) {
|
||||
return &scene_graph_nodes[id - 1];
|
||||
}
|
||||
|
||||
auto new_graph_node() -> int {
|
||||
int new_graph_node() {
|
||||
scene_graph_nodes.emplace_back();
|
||||
return scene_graph_nodes.size();
|
||||
}
|
||||
|
||||
auto draw_entity(Entity* entity) -> void {
|
||||
auto modelUniformLoc = glGetUniformLocation(app_state.active_shader->prog_id, "model");
|
||||
void draw_entity(Entity *entity) {
|
||||
GLint modelUniformLoc = glGetUniformLocation(app_state.active_shader->prog_id, "model");
|
||||
glUniformMatrix4fv(modelUniformLoc, 1, GL_FALSE, glm::value_ptr(get_scene_graph_node(entity->scene_graph_node)->world));
|
||||
glBindTexture(GL_TEXTURE_2D, entity->tex->tex_id);
|
||||
glBindVertexArray(entity->mesh->vao);
|
||||
@@ -262,17 +256,17 @@ auto draw_entity(Entity* entity) -> void {
|
||||
//glDrawElements(GL_TRIANGLES, entity->mesh->num_indices, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
auto create_polycube_from_repr(Voxel::Space* repr) -> Polycube {
|
||||
auto polycube_id = new_graph_node();
|
||||
Polycube create_polycube_from_repr(Space *repr) {
|
||||
int polycube_id = new_graph_node();
|
||||
get_scene_graph_node(polycube_id)->init();
|
||||
for (int x = 0; x < repr->dim_x; x++) {
|
||||
for (int y = 0; y < repr->dim_y; y++) {
|
||||
for (int z = 0; z < repr->dim_z; z++) {
|
||||
if (Voxel::filledAt(repr, x, y, z)) {
|
||||
auto polycube_segment = get_entity(new_entity());
|
||||
if (filledAt(repr, x, y, z)) {
|
||||
Entity *polycube_segment = get_entity(new_entity());
|
||||
polycube_segment->mesh=&cube_mesh,
|
||||
polycube_segment->tex=&wall_tex;
|
||||
auto graph_node = get_scene_graph_node(polycube_segment->scene_graph_node);
|
||||
SceneGraphNode *graph_node = get_scene_graph_node(polycube_segment->scene_graph_node);
|
||||
graph_node->init();
|
||||
graph_node->translation = glm::vec3(
|
||||
-((repr->dim_z - 1)/2.0f) + z,
|
||||
@@ -285,67 +279,67 @@ auto create_polycube_from_repr(Voxel::Space* repr) -> Polycube {
|
||||
}
|
||||
}
|
||||
}
|
||||
auto result = Polycube{
|
||||
Polycube result = {
|
||||
.graph_node=polycube_id,
|
||||
.color=glm::vec3(1.0f),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
auto recalculate_scene_graph(SceneGraphNode* top) -> void {
|
||||
void recalculate_scene_graph(SceneGraphNode *top) {
|
||||
if (top->children.size() == 0) {
|
||||
return;
|
||||
}
|
||||
for (auto &node_id : top->children) {
|
||||
auto graph_node = get_scene_graph_node(node_id);
|
||||
for (int &node_id : top->children) {
|
||||
SceneGraphNode *graph_node = get_scene_graph_node(node_id);
|
||||
graph_node->update_local();
|
||||
graph_node->world = top->world * graph_node->local;
|
||||
recalculate_scene_graph(graph_node);
|
||||
}
|
||||
}
|
||||
|
||||
auto main_cmd() -> int {
|
||||
SomaSolve::interactive_cmd_line_solve_soma();
|
||||
int main_cmd() {
|
||||
interactive_cmd_line_solve_soma();
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto main_gfx() -> int {
|
||||
auto window_dims = WindowDims{ 800, 600 };
|
||||
auto window = init_window_and_gl(&window_dims);
|
||||
if (window == nullptr) {
|
||||
int main_gfx() {
|
||||
WindowDims window_dims = { 800, 600 };
|
||||
GLFWwindow *window = init_window_and_gl(&window_dims);
|
||||
if (!window) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
app_state = GlobalAppState{
|
||||
app_state = {
|
||||
.current_polycube=0,
|
||||
.last_polycube_visible=6,
|
||||
.active_shader=nullptr,
|
||||
.active_shader=0,
|
||||
.polycubes={},
|
||||
};
|
||||
|
||||
auto phong_shader = Shader{};
|
||||
Shader phong_shader = {0};
|
||||
phong_shader.init("../assets/shaders/phong-solid.vertex.glsl", "../assets/shaders/phong-solid.fragment.glsl");
|
||||
app_state.active_shader = &phong_shader;
|
||||
|
||||
cube_mesh.init("../assets/models/c000000.obj");
|
||||
wall_tex.init("../assets/textures/brick-wall.jpg");
|
||||
|
||||
auto little_frame = Frame{ .width=80, .height=60, .x=20, .y=20 };
|
||||
auto big_frame = Frame{ .width=800, .height=600, .x=0, .y=0 };
|
||||
auto main_cam = Camera{};
|
||||
auto other_cam = Camera{};
|
||||
Frame little_frame = { .width=80, .height=60, .x=20, .y=20 };
|
||||
Frame big_frame = { .width=800, .height=600, .x=0, .y=0 };
|
||||
Camera main_cam = {};
|
||||
Camera other_cam = {};
|
||||
little_frame.init(&other_cam);
|
||||
big_frame.init(&main_cam);
|
||||
auto frames = std::vector{ &big_frame, &little_frame };
|
||||
std::vector<Frame> frames = { &big_frame, &little_frame };
|
||||
|
||||
auto root_node = SceneGraphNode{};
|
||||
SceneGraphNode root_node = {};
|
||||
root_node.init();
|
||||
|
||||
for (int i = 0; i < SomaSolve::STD_SOMA.size(); i++) {
|
||||
auto voxel_space = Voxel::Space{ SomaSolve::STD_SOMA[i], 3, 3, 3 };
|
||||
Voxel::cullEmptySpace(&voxel_space);
|
||||
auto polycube = create_polycube_from_repr(&voxel_space);
|
||||
polycube.color = Color::color_from_index(i);
|
||||
for (int i = 0; i < STD_SOMA.size(); i++) {
|
||||
auto voxel_space = Space{ STD_SOMA[i], 3, 3, 3 };
|
||||
cullEmptySpace(&voxel_space);
|
||||
Polycube polycube = create_polycube_from_repr(&voxel_space);
|
||||
polycube.color = color_from_index(i);
|
||||
app_state.polycubes.push_back(polycube);
|
||||
root_node.children.push_back(app_state.polycubes.back().graph_node);
|
||||
}
|
||||
@@ -353,18 +347,18 @@ auto main_gfx() -> int {
|
||||
main_cam.pos = glm::vec3(4.0f, 4.0f, 4.0f);
|
||||
main_cam.look_at(0.0f, 0.0f, 0.0f);
|
||||
|
||||
auto light_pos = glm::vec3(6.0f);
|
||||
glm::vec3 light_pos = glm::vec3(6.0f);
|
||||
|
||||
glUseProgram(app_state.active_shader->prog_id);
|
||||
auto view_loc = glGetUniformLocation(app_state.active_shader->prog_id, "view");
|
||||
auto proj_loc = glGetUniformLocation(app_state.active_shader->prog_id, "projection");
|
||||
auto light_pos_loc = glGetUniformLocation(app_state.active_shader->prog_id, "light_pos");
|
||||
GLint view_loc = glGetUniformLocation(app_state.active_shader->prog_id, "view");
|
||||
GLint proj_loc = glGetUniformLocation(app_state.active_shader->prog_id, "projection");
|
||||
GLint light_pos_loc = glGetUniformLocation(app_state.active_shader->prog_id, "light_pos");
|
||||
glUniform3fv(light_pos_loc, 1, glm::value_ptr(light_pos));
|
||||
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, glm::value_ptr(main_cam.proj));
|
||||
glUniformMatrix4fv(view_loc, 1, GL_FALSE, glm::value_ptr(main_cam.view));
|
||||
|
||||
auto last_frame = glfwGetTime();
|
||||
auto time_delta = 1.0f/60.0f;
|
||||
real32 last_frame = glfwGetTime();
|
||||
real32 time_delta = 1.0f/60.0f;
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
time_delta = glfwGetTime() - last_frame;
|
||||
process_input(window);
|
||||
@@ -379,16 +373,16 @@ auto main_gfx() -> int {
|
||||
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||||
|
||||
gl_update_viewport(&window_dims, &big_frame);
|
||||
auto current_polycube = &app_state.polycubes[app_state.current_polycube];
|
||||
Polycube *current_polycube = &app_state.polycubes[app_state.current_polycube];
|
||||
get_scene_graph_node(current_polycube->graph_node)->rotation = glm::quat(glm::vec3(0, glfwGetTime() / 2, 0));
|
||||
|
||||
glBindVertexArray(cube_mesh.vao);
|
||||
//glBindTexture(GL_TEXTURE_2D, entity.tex->tex_id);
|
||||
recalculate_scene_graph(&root_node);
|
||||
auto model_uniform_loc = glGetUniformLocation(app_state.active_shader->prog_id, "model");
|
||||
auto solid_color_loc = glGetUniformLocation(app_state.active_shader->prog_id, "solid_color");
|
||||
GLint model_uniform_loc = glGetUniformLocation(app_state.active_shader->prog_id, "model");
|
||||
GLint solid_color_loc = glGetUniformLocation(app_state.active_shader->prog_id, "solid_color");
|
||||
glUniform3fv(solid_color_loc, 1, glm::value_ptr(current_polycube->color));
|
||||
for (auto &entity : entities) {
|
||||
for (Entity &entity : entities) {
|
||||
if (entity.visible) {
|
||||
glUniformMatrix4fv(model_uniform_loc, 1, GL_FALSE, glm::value_ptr(get_scene_graph_node(entity.scene_graph_node)->world));
|
||||
glDrawArrays(GL_TRIANGLES, 0, entity.mesh->num_indices);
|
||||
@@ -404,7 +398,7 @@ auto main_gfx() -> int {
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto main() -> int {
|
||||
int main() {
|
||||
return main_cmd();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user