feat: reorganize source code + cleanup
This commit is contained in:
@ -25,13 +25,13 @@ endif()
|
|||||||
# set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb")
|
# set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb")
|
||||||
|
|
||||||
add_executable(CodingGame
|
add_executable(CodingGame
|
||||||
src/prelude.cpp
|
src/IO/file_manager.cpp
|
||||||
src/file_manager.cpp
|
|
||||||
src/shader.cpp
|
src/renderer/debug.cpp
|
||||||
src/block.cpp
|
src/renderer/shader.cpp
|
||||||
src/vertex.cpp
|
src/renderer/texture.cpp
|
||||||
src/texture.cpp
|
src/renderer/wavefront.cpp
|
||||||
src/model.cpp
|
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
#ifndef BLOCK_H_
|
|
||||||
#define BLOCK_H_
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
|
|
||||||
class Block {
|
|
||||||
private:
|
|
||||||
glm::vec3 m_position;
|
|
||||||
glm::vec4 m_color;
|
|
||||||
public:
|
|
||||||
Block(glm::vec3 position, glm::vec4 color);
|
|
||||||
public:
|
|
||||||
inline glm::vec3 Position() const { return m_position; }
|
|
||||||
inline glm::vec4 Color() const { return m_color; }
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
141
include/model.h
141
include/model.h
@ -1,141 +0,0 @@
|
|||||||
#ifndef MODEL_H_
|
|
||||||
#define MODEL_H_
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
#include <filesystem>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "shader.h"
|
|
||||||
#include "texture.h"
|
|
||||||
|
|
||||||
enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
|
|
||||||
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
|
|
||||||
|
|
||||||
class Vertex {
|
|
||||||
private:
|
|
||||||
glm::vec3 m_position;
|
|
||||||
glm::vec3 m_normal;
|
|
||||||
glm::vec2 m_texCoord;
|
|
||||||
public:
|
|
||||||
Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texCoord)
|
|
||||||
: m_position(position), m_normal(normal), m_texCoord(texCoord) {}
|
|
||||||
public:
|
|
||||||
static void DefineAttrib();
|
|
||||||
};
|
|
||||||
|
|
||||||
class FaceItem {
|
|
||||||
private:
|
|
||||||
unsigned int m_vIndex;
|
|
||||||
unsigned int m_nIndex;
|
|
||||||
unsigned int m_tIndex;
|
|
||||||
public:
|
|
||||||
FaceItem()
|
|
||||||
: m_vIndex(0), m_nIndex(0), m_tIndex(0) {}
|
|
||||||
FaceItem(unsigned int vI, unsigned int nI, unsigned int tI)
|
|
||||||
: m_vIndex(vI), m_nIndex(nI), m_tIndex(tI) {}
|
|
||||||
public:
|
|
||||||
inline const unsigned int GetVertex() const { return m_vIndex; }
|
|
||||||
inline const unsigned int GetNormal() const { return m_nIndex; }
|
|
||||||
inline const unsigned int GetTex() const { return m_tIndex; }
|
|
||||||
public:
|
|
||||||
inline void SetVertex(unsigned int vIndex) { m_vIndex = vIndex; }
|
|
||||||
inline void SetNorm(unsigned int nIndex) { m_nIndex = nIndex; }
|
|
||||||
inline void SetTex(unsigned int tIndex) { m_tIndex = tIndex; }
|
|
||||||
};
|
|
||||||
|
|
||||||
class Face {
|
|
||||||
private:
|
|
||||||
std::vector<FaceItem> m_items;
|
|
||||||
public:
|
|
||||||
Face()
|
|
||||||
: m_items(std::vector<FaceItem>()) {}
|
|
||||||
public:
|
|
||||||
void PushItem(const FaceItem& item);
|
|
||||||
public:
|
|
||||||
inline const unsigned int GetSize() const { return m_items.size(); }
|
|
||||||
inline const FaceItem& GetItem(unsigned int index) const { return m_items[index]; }
|
|
||||||
};
|
|
||||||
|
|
||||||
class Material {
|
|
||||||
private:
|
|
||||||
glm::vec3 m_ambient { 0.2f, 0.2f, 0.2f };
|
|
||||||
glm::vec3 m_diffuse { 0.8f, 0.8f, 0.8f };
|
|
||||||
glm::vec3 m_specular { 1.0f, 1.0f, 1.0f };
|
|
||||||
float m_shininess { 32.0f };
|
|
||||||
float m_opacity { 1.0f };
|
|
||||||
int m_illum { 2 };
|
|
||||||
|
|
||||||
std::unique_ptr<Texture> m_diffuse_tex { nullptr };
|
|
||||||
public:
|
|
||||||
Material() = default;
|
|
||||||
Material(const Material& other) = default; // copy constructor
|
|
||||||
Material& operator=(const Material& other) = default;
|
|
||||||
public:
|
|
||||||
inline const glm::vec3 GetAmbientColor() const { return m_ambient; }
|
|
||||||
inline const glm::vec3 GetDiffuseColor() const { return m_diffuse; }
|
|
||||||
inline const glm::vec3 GetSpecularColor() const { return m_specular; }
|
|
||||||
inline const float GetSpecularWeight() const { return m_shininess; }
|
|
||||||
inline const bool HasDiffuseTexture() const { return m_diffuse_tex != nullptr; }
|
|
||||||
inline const Texture* GetDiffuseTexture() const { return m_diffuse_tex.get(); }
|
|
||||||
inline const float GetOpacity() const { return m_opacity; }
|
|
||||||
inline const int GetIllumination() const { return m_illum; }
|
|
||||||
public:
|
|
||||||
inline void SetAmbientColor(glm::vec3 ambient) { m_ambient = ambient; }
|
|
||||||
inline void SetDiffuseColor(glm::vec3 diffuse) { m_diffuse = diffuse; }
|
|
||||||
inline void SetSpecularColor(glm::vec3 specular) { m_specular = specular; }
|
|
||||||
inline void SetSpecularWeight(float weight) { m_shininess = weight; }
|
|
||||||
inline void SetDiffuseTexture(std::unique_ptr<Texture>&& texture) { m_diffuse_tex = std::move(texture); }
|
|
||||||
inline void SetOpacity(float opacity) { m_opacity = opacity; }
|
|
||||||
inline void SetIllumination(float illum) { m_illum = illum; }
|
|
||||||
};
|
|
||||||
|
|
||||||
class Mesh {
|
|
||||||
public: // TODO: abstract away
|
|
||||||
unsigned int m_vao, m_vbo, m_ebo;
|
|
||||||
std::vector<Vertex> m_vertexBuffer;
|
|
||||||
std::vector<unsigned int> m_indexBuffer;
|
|
||||||
public: // TODO: abstract away
|
|
||||||
void Bind() { glBindVertexArray(m_vao); }
|
|
||||||
void Unbind() { glBindVertexArray(0); }
|
|
||||||
void Upload();
|
|
||||||
public:
|
|
||||||
std::string materialName;
|
|
||||||
public:
|
|
||||||
Mesh();
|
|
||||||
public:
|
|
||||||
void Render();
|
|
||||||
};
|
|
||||||
|
|
||||||
class Object {
|
|
||||||
private:
|
|
||||||
std::string m_name;
|
|
||||||
std::vector<glm::vec3> m_vertices;
|
|
||||||
std::vector<glm::vec3> m_normals;
|
|
||||||
std::vector<glm::vec2> m_texCoords;
|
|
||||||
|
|
||||||
std::vector<Mesh> m_meshes;
|
|
||||||
|
|
||||||
std::unordered_map<std::string, std::shared_ptr<Material>> m_materials;
|
|
||||||
private:
|
|
||||||
static inline int NormalizeIndex(const std::string &s, int baseCount);
|
|
||||||
|
|
||||||
private:
|
|
||||||
Object();
|
|
||||||
public:
|
|
||||||
static Object LoadFile(const std::string& filename);
|
|
||||||
|
|
||||||
private:
|
|
||||||
void LoadMaterials(const std::filesystem::path& filename);
|
|
||||||
private:
|
|
||||||
void AddMaterial(std::string name, std::shared_ptr<Material> material);
|
|
||||||
std::shared_ptr<Material> GetMaterial(std::string name);
|
|
||||||
private:
|
|
||||||
Mesh& GetLastMesh();
|
|
||||||
void CreateNewMesh(const std::string& materialName);
|
|
||||||
public:
|
|
||||||
void Render(Shader& shader);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // MODEL_H_
|
|
@ -1,43 +0,0 @@
|
|||||||
#ifndef PRELUDE_H_
|
|
||||||
#define PRELUDE_H_
|
|
||||||
|
|
||||||
#ifndef WIN32
|
|
||||||
#define GLEW_STATIC
|
|
||||||
#endif
|
|
||||||
#include <GL/glew.h>
|
|
||||||
|
|
||||||
#include "SDL3/SDL.h"
|
|
||||||
|
|
||||||
struct RenderContext {
|
|
||||||
Uint64 time;
|
|
||||||
Uint64 prev_time;
|
|
||||||
bool program_failed = false;
|
|
||||||
GLuint program = 0;
|
|
||||||
GLint resolution_location = 0;
|
|
||||||
GLint time_location = 0;
|
|
||||||
bool pause = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader);
|
|
||||||
|
|
||||||
bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader);
|
|
||||||
|
|
||||||
bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program);
|
|
||||||
|
|
||||||
void reload_shaders(RenderContext*context);
|
|
||||||
|
|
||||||
// void key_callback(SDL_Window* window, int key, int scancode, int action, int mods);
|
|
||||||
|
|
||||||
void window_size_callback(SDL_Window* window, int width, int height);
|
|
||||||
|
|
||||||
void MessageCallback(GLenum source,
|
|
||||||
GLenum type,
|
|
||||||
GLuint id,
|
|
||||||
GLenum severity,
|
|
||||||
GLsizei length,
|
|
||||||
const GLchar* message,
|
|
||||||
const void* userParam);
|
|
||||||
|
|
||||||
void process_prelude(RenderContext *context);
|
|
||||||
|
|
||||||
#endif // PRELUDE_H_
|
|
18
include/renderer/basics.h
Normal file
18
include/renderer/basics.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef RENDERER_BASICS_H
|
||||||
|
#define RENDERER_BASICS_H
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
class Vertex {
|
||||||
|
private:
|
||||||
|
glm::vec3 m_position;
|
||||||
|
glm::vec3 m_normal;
|
||||||
|
glm::vec2 m_texCoord;
|
||||||
|
public:
|
||||||
|
Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texCoord)
|
||||||
|
: m_position(position), m_normal(normal), m_texCoord(texCoord) {}
|
||||||
|
public:
|
||||||
|
static void DefineAttrib();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // RENDERER_BASICS_H
|
16
include/renderer/debug.h
Normal file
16
include/renderer/debug.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#ifndef RENDERER_DEBUG_
|
||||||
|
#define RENDERER_DEBUG_
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
void MessageCallback(GLenum source,
|
||||||
|
GLenum type,
|
||||||
|
GLuint id,
|
||||||
|
GLenum severity,
|
||||||
|
GLsizei length,
|
||||||
|
const GLchar* message,
|
||||||
|
const void* userParam);
|
||||||
|
|
||||||
|
#endif // RENDERER_DEBUG_
|
42
include/renderer/material.h
Normal file
42
include/renderer/material.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#ifndef MATERIAL_H_
|
||||||
|
#define MATERIAL_H_
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "texture.h"
|
||||||
|
|
||||||
|
class Material {
|
||||||
|
private:
|
||||||
|
glm::vec3 m_ambient { 0.2f, 0.2f, 0.2f };
|
||||||
|
glm::vec3 m_diffuse { 0.8f, 0.8f, 0.8f };
|
||||||
|
glm::vec3 m_specular { 1.0f, 1.0f, 1.0f };
|
||||||
|
float m_shininess { 32.0f };
|
||||||
|
float m_opacity { 1.0f };
|
||||||
|
int m_illum { 2 };
|
||||||
|
|
||||||
|
std::unique_ptr<Texture> m_diffuse_tex { nullptr };
|
||||||
|
public:
|
||||||
|
Material() = default;
|
||||||
|
Material(const Material& other) = default; // copy constructor
|
||||||
|
Material& operator=(const Material& other) = default;
|
||||||
|
public:
|
||||||
|
inline const glm::vec3 GetAmbientColor() const { return m_ambient; }
|
||||||
|
inline const glm::vec3 GetDiffuseColor() const { return m_diffuse; }
|
||||||
|
inline const glm::vec3 GetSpecularColor() const { return m_specular; }
|
||||||
|
inline const float GetSpecularWeight() const { return m_shininess; }
|
||||||
|
inline const bool HasDiffuseTexture() const { return m_diffuse_tex != nullptr; }
|
||||||
|
inline const Texture* GetDiffuseTexture() const { return m_diffuse_tex.get(); }
|
||||||
|
inline const float GetOpacity() const { return m_opacity; }
|
||||||
|
inline const int GetIllumination() const { return m_illum; }
|
||||||
|
public:
|
||||||
|
inline void SetAmbientColor(glm::vec3 ambient) { m_ambient = ambient; }
|
||||||
|
inline void SetDiffuseColor(glm::vec3 diffuse) { m_diffuse = diffuse; }
|
||||||
|
inline void SetSpecularColor(glm::vec3 specular) { m_specular = specular; }
|
||||||
|
inline void SetSpecularWeight(float weight) { m_shininess = weight; }
|
||||||
|
inline void SetDiffuseTexture(std::unique_ptr<Texture>&& texture) { m_diffuse_tex = std::move(texture); }
|
||||||
|
inline void SetOpacity(float opacity) { m_opacity = opacity; }
|
||||||
|
inline void SetIllumination(float illum) { m_illum = illum; }
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // MATERIAL_H_
|
65
include/renderer/wavefront.h
Normal file
65
include/renderer/wavefront.h
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#ifndef MODEL_H_
|
||||||
|
#define MODEL_H_
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "shader.h"
|
||||||
|
#include "texture.h"
|
||||||
|
#include "renderer/material.h"
|
||||||
|
#include "renderer/basics.h"
|
||||||
|
|
||||||
|
enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
|
||||||
|
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
|
||||||
|
|
||||||
|
class Mesh {
|
||||||
|
public: // TODO: abstract away
|
||||||
|
unsigned int m_vao, m_vbo, m_ebo;
|
||||||
|
std::vector<Vertex> m_vertexBuffer;
|
||||||
|
std::vector<unsigned int> m_indexBuffer;
|
||||||
|
public: // TODO: abstract away
|
||||||
|
void Bind() { glBindVertexArray(m_vao); }
|
||||||
|
void Unbind() { glBindVertexArray(0); }
|
||||||
|
void Upload();
|
||||||
|
public:
|
||||||
|
std::string materialName;
|
||||||
|
public:
|
||||||
|
Mesh();
|
||||||
|
public:
|
||||||
|
void Render();
|
||||||
|
};
|
||||||
|
|
||||||
|
class Object {
|
||||||
|
private:
|
||||||
|
std::string m_name;
|
||||||
|
std::vector<glm::vec3> m_vertices;
|
||||||
|
std::vector<glm::vec3> m_normals;
|
||||||
|
std::vector<glm::vec2> m_texCoords;
|
||||||
|
|
||||||
|
std::vector<Mesh> m_meshes;
|
||||||
|
|
||||||
|
std::unordered_map<std::string, std::shared_ptr<Material>> m_materials;
|
||||||
|
private:
|
||||||
|
static inline int NormalizeIndex(const std::string &s, int baseCount);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Object();
|
||||||
|
public:
|
||||||
|
static Object LoadFile(const std::string& filename);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void LoadMaterials(const std::filesystem::path& filename);
|
||||||
|
private:
|
||||||
|
void AddMaterial(std::string name, std::shared_ptr<Material> material);
|
||||||
|
std::shared_ptr<Material> GetMaterial(std::string name);
|
||||||
|
private:
|
||||||
|
Mesh& GetLastMesh();
|
||||||
|
void CreateNewMesh(const std::string& materialName);
|
||||||
|
public:
|
||||||
|
void Render(Shader& shader);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // MODEL_H_
|
@ -1,37 +0,0 @@
|
|||||||
#ifndef VERTEX_H_
|
|
||||||
#define VERTEX_H_
|
|
||||||
#include <vector>
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
|
|
||||||
#include "block.h"
|
|
||||||
|
|
||||||
class Point {
|
|
||||||
private:
|
|
||||||
glm::vec3 m_position;
|
|
||||||
glm::vec3 m_normal;
|
|
||||||
glm::vec4 m_color;
|
|
||||||
public:
|
|
||||||
Point(glm::vec3 position, glm::vec3 normal, glm::vec4 color);
|
|
||||||
};
|
|
||||||
|
|
||||||
class Vertices {
|
|
||||||
private:
|
|
||||||
std::vector<Point> m_items;
|
|
||||||
std::vector<unsigned int> m_indices;
|
|
||||||
|
|
||||||
unsigned int m_vao;
|
|
||||||
unsigned int m_vbo;
|
|
||||||
unsigned int m_ebo;
|
|
||||||
public:
|
|
||||||
Vertices();
|
|
||||||
public: // GPU
|
|
||||||
void Bind();
|
|
||||||
void Unbind();
|
|
||||||
void Draw();
|
|
||||||
void Upload();
|
|
||||||
public:
|
|
||||||
void PushVertex(const Point& point);
|
|
||||||
void PushIndex(unsigned int index);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // VERTEX_H_
|
|
@ -1,4 +1,4 @@
|
|||||||
#include "file_manager.h"
|
#include "IO/file_manager.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
@ -1,6 +0,0 @@
|
|||||||
#include "block.h"
|
|
||||||
|
|
||||||
Block::Block(glm::vec3 position, glm::vec4 color) {
|
|
||||||
this->m_position = position;
|
|
||||||
this->m_color = m_color;
|
|
||||||
}
|
|
14
src/main.cpp
14
src/main.cpp
@ -5,6 +5,10 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
// #endif
|
// #endif
|
||||||
|
|
||||||
|
#ifndef WIN32
|
||||||
|
#define GLEW_STATIC
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/ext/quaternion_geometric.hpp>
|
#include <glm/ext/quaternion_geometric.hpp>
|
||||||
@ -13,12 +17,10 @@
|
|||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
#include "shader.h"
|
#include "renderer/shader.h"
|
||||||
#include "file_manager.h"
|
#include "IO/file_manager.h"
|
||||||
#include "prelude.h"
|
#include "renderer/debug.h"
|
||||||
#include "block.h"
|
#include "renderer/wavefront.h"
|
||||||
#include "vertex.h"
|
|
||||||
#include "model.h"
|
|
||||||
|
|
||||||
#define WIDTH 1024
|
#define WIDTH 1024
|
||||||
#define HEIGHT 768
|
#define HEIGHT 768
|
||||||
|
192
src/prelude.cpp
192
src/prelude.cpp
@ -1,192 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include "prelude.h"
|
|
||||||
|
|
||||||
#ifndef WIN32
|
|
||||||
#define GLEW_STATIC
|
|
||||||
#endif
|
|
||||||
#include <GL/glew.h>
|
|
||||||
|
|
||||||
#define SCREEN_WIDTH 1024
|
|
||||||
#define SCREEN_HEIGHT 768
|
|
||||||
|
|
||||||
void panic_errno(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "ERROR: ");
|
|
||||||
|
|
||||||
va_list args;
|
|
||||||
va_start(args, fmt);
|
|
||||||
vfprintf(stderr, fmt, args);
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
fprintf(stderr, ": %s\n", strerror(errno));
|
|
||||||
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *slurp_file(const char *file_path)
|
|
||||||
{
|
|
||||||
#define SLURP_FILE_PANIC panic_errno("Could not read file `%s`", file_path)
|
|
||||||
FILE *f = fopen(file_path, "r");
|
|
||||||
if (f == NULL) SLURP_FILE_PANIC;
|
|
||||||
if (fseek(f, 0, SEEK_END) < 0) SLURP_FILE_PANIC;
|
|
||||||
|
|
||||||
long size = ftell(f);
|
|
||||||
if (size < 0) SLURP_FILE_PANIC;
|
|
||||||
|
|
||||||
char *buffer = (char*)malloc(size + 1);
|
|
||||||
if (buffer == NULL) SLURP_FILE_PANIC;
|
|
||||||
|
|
||||||
if (fseek(f, 0, SEEK_SET) < 0) SLURP_FILE_PANIC;
|
|
||||||
|
|
||||||
fread(buffer, 1, size, f);
|
|
||||||
if (ferror(f) < 0) SLURP_FILE_PANIC;
|
|
||||||
|
|
||||||
buffer[size] = '\0';
|
|
||||||
|
|
||||||
if (fclose(f) < 0) SLURP_FILE_PANIC;
|
|
||||||
|
|
||||||
return buffer;
|
|
||||||
#undef SLURP_FILE_PANIC
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader)
|
|
||||||
{
|
|
||||||
*shader = glCreateShader(shader_type);
|
|
||||||
glShaderSource(*shader, 1, &source, NULL);
|
|
||||||
glCompileShader(*shader);
|
|
||||||
|
|
||||||
GLint compiled = 0;
|
|
||||||
glGetShaderiv(*shader, GL_COMPILE_STATUS, &compiled);
|
|
||||||
|
|
||||||
if (!compiled) {
|
|
||||||
GLchar message[1024];
|
|
||||||
GLsizei message_size = 0;
|
|
||||||
glGetShaderInfoLog(*shader, sizeof(message), &message_size, message);
|
|
||||||
fprintf(stderr, "%.*s\n", message_size, message);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader)
|
|
||||||
{
|
|
||||||
char *source = slurp_file(file_path);
|
|
||||||
bool err = compile_shader_source(source, shader_type, shader);
|
|
||||||
free(source);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program)
|
|
||||||
{
|
|
||||||
*program = glCreateProgram();
|
|
||||||
|
|
||||||
glAttachShader(*program, vert_shader);
|
|
||||||
glAttachShader(*program, frag_shader);
|
|
||||||
glLinkProgram(*program);
|
|
||||||
|
|
||||||
GLint linked = 0;
|
|
||||||
glGetProgramiv(*program, GL_LINK_STATUS, &linked);
|
|
||||||
if (!linked) {
|
|
||||||
GLsizei message_size = 0;
|
|
||||||
GLchar message[1024];
|
|
||||||
|
|
||||||
glGetProgramInfoLog(*program, sizeof(message), &message_size, message);
|
|
||||||
fprintf(stderr, "Program Linking: %.*s\n", message_size, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
glDeleteShader(vert_shader);
|
|
||||||
glDeleteShader(frag_shader);
|
|
||||||
|
|
||||||
return program;
|
|
||||||
}
|
|
||||||
|
|
||||||
void reload_shaders(RenderContext* context)
|
|
||||||
{
|
|
||||||
glDeleteProgram(context->program);
|
|
||||||
|
|
||||||
context->program_failed = false;
|
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
|
||||||
|
|
||||||
GLuint vert = 0;
|
|
||||||
if (!compile_shader_file("./main.vert", GL_VERTEX_SHADER, &vert)) {
|
|
||||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
|
||||||
context->program_failed = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
GLuint frag = 0;
|
|
||||||
if (!compile_shader_file("./main.frag", GL_FRAGMENT_SHADER, &frag)) {
|
|
||||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
|
||||||
context->program_failed = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!link_program(vert, frag, &context->program)) {
|
|
||||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
|
||||||
context->program_failed = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
glUseProgram(context->program);
|
|
||||||
|
|
||||||
context->resolution_location = glGetUniformLocation(context->program, "resolution");
|
|
||||||
context->time_location = glGetUniformLocation(context->program, "time");
|
|
||||||
|
|
||||||
printf("Successfully Reload the Shaders\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void window_size_callback(SDL_Window* window, int width, int height)
|
|
||||||
{
|
|
||||||
(void) window;
|
|
||||||
glViewport(
|
|
||||||
width / 2 - SCREEN_WIDTH / 2,
|
|
||||||
height / 2 - SCREEN_HEIGHT / 2,
|
|
||||||
SCREEN_WIDTH,
|
|
||||||
SCREEN_HEIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MessageCallback(GLenum source,
|
|
||||||
GLenum type,
|
|
||||||
GLuint id,
|
|
||||||
GLenum severity,
|
|
||||||
GLsizei length,
|
|
||||||
const GLchar* message,
|
|
||||||
const void* userParam)
|
|
||||||
{
|
|
||||||
(void) source;
|
|
||||||
(void) id;
|
|
||||||
(void) length;
|
|
||||||
(void) userParam;
|
|
||||||
fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
|
|
||||||
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
|
|
||||||
type, severity, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
void process_prelude(RenderContext *context)
|
|
||||||
{
|
|
||||||
// glfwSetKeyCallback(window, key_callback);
|
|
||||||
// glfwSetFramebufferSizeCallback(window, window_size_callback);
|
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
|
||||||
|
|
||||||
if (!context->program_failed) {
|
|
||||||
glUniform2f(context->resolution_location,
|
|
||||||
SCREEN_WIDTH,
|
|
||||||
SCREEN_HEIGHT);
|
|
||||||
glUniform1f(context->time_location, context->time);
|
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t cur_time = SDL_GetTicks();
|
|
||||||
if (!context->pause) {
|
|
||||||
context->time += cur_time - context->prev_time;
|
|
||||||
}
|
|
||||||
context->prev_time = cur_time;
|
|
||||||
}
|
|
22
src/renderer/debug.cpp
Normal file
22
src/renderer/debug.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include "renderer/debug.h"
|
||||||
|
|
||||||
|
void MessageCallback(GLenum source,
|
||||||
|
GLenum type,
|
||||||
|
GLuint id,
|
||||||
|
GLenum severity,
|
||||||
|
GLsizei length,
|
||||||
|
const GLchar* message,
|
||||||
|
const void* userParam)
|
||||||
|
{
|
||||||
|
(void) source;
|
||||||
|
(void) id;
|
||||||
|
(void) length;
|
||||||
|
(void) userParam;
|
||||||
|
std::cerr << "GL CALLBACK: " << (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "")
|
||||||
|
<< " type = 0x" << type
|
||||||
|
<< ", severity = 0x" << severity
|
||||||
|
<< ", message = " << message << std::endl;
|
||||||
|
// std::cerr << "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
|
||||||
|
// (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
|
||||||
|
// type, severity, message);
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
#include "shader.h"
|
#include "renderer/shader.h"
|
||||||
|
|
||||||
Shader::Shader()
|
Shader::Shader()
|
||||||
{
|
{
|
@ -1,7 +1,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include "texture.h"
|
#include "renderer/texture.h"
|
||||||
|
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
@ -5,7 +5,7 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
|
||||||
#include "model.h"
|
#include "renderer/wavefront.h"
|
||||||
|
|
||||||
ObjElement toElement(const std::string &s) {
|
ObjElement toElement(const std::string &s) {
|
||||||
if (s == "#") return ObjElement::OHASH;
|
if (s == "#") return ObjElement::OHASH;
|
||||||
@ -47,11 +47,6 @@ void Vertex::DefineAttrib()
|
|||||||
glEnableVertexAttribArray(2);
|
glEnableVertexAttribArray(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Face::PushItem(const FaceItem& item)
|
|
||||||
{
|
|
||||||
m_items.push_back(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline int Object::NormalizeIndex(const std::string &s, int baseCount) {
|
inline int Object::NormalizeIndex(const std::string &s, int baseCount) {
|
||||||
if (s.empty()) return -1;
|
if (s.empty()) return -1;
|
||||||
int idx = std::stoi(s);
|
int idx = std::stoi(s);
|
||||||
@ -284,8 +279,6 @@ Object Object::LoadFile(const std::string& filename) {
|
|||||||
{
|
{
|
||||||
auto& mesh = obj.GetLastMesh();
|
auto& mesh = obj.GetLastMesh();
|
||||||
std::string token;
|
std::string token;
|
||||||
|
|
||||||
Face fv;
|
|
||||||
|
|
||||||
while (iss >> token) {
|
while (iss >> token) {
|
||||||
std::string a, b, c;
|
std::string a, b, c;
|
151
src/vertex.cpp
151
src/vertex.cpp
@ -1,151 +0,0 @@
|
|||||||
#include <GL/glew.h>
|
|
||||||
|
|
||||||
#include "vertex.h"
|
|
||||||
|
|
||||||
#define BLOCK_SIZE 0.5f
|
|
||||||
|
|
||||||
Point::Point(glm::vec3 position, glm::vec3 normal, glm::vec4 color)
|
|
||||||
{
|
|
||||||
m_position = position;
|
|
||||||
m_normal = normal;
|
|
||||||
m_color = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vertices::Vertices()
|
|
||||||
{
|
|
||||||
m_items = std::vector<Point>();
|
|
||||||
|
|
||||||
glGenVertexArrays(1, &m_vao);
|
|
||||||
glGenBuffers(1, &m_vbo);
|
|
||||||
glGenBuffers(1, &m_ebo);
|
|
||||||
|
|
||||||
glBindVertexArray(m_vao);
|
|
||||||
|
|
||||||
// VBO (vertex buffer)
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
|
|
||||||
|
|
||||||
// EBO (index buffer)
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
|
|
||||||
|
|
||||||
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
|
||||||
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangleIndices), triangleIndices, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)0);
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)(3 * sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
|
|
||||||
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)(6 * sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(2);
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
||||||
glBindVertexArray(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call this after you finish adding vertices (or call it each time after PushBlock)
|
|
||||||
void Vertices::Upload()
|
|
||||||
{
|
|
||||||
glBindVertexArray(m_vao);
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, m_items.size() * sizeof(Point), m_items.data(), GL_DYNAMIC_DRAW);
|
|
||||||
|
|
||||||
// Upload indices
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(unsigned int), m_indices.data(), GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
glBindVertexArray(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// void Vertices::PushBlock(const Block& block)
|
|
||||||
// {
|
|
||||||
// // 1 face
|
|
||||||
// m_items.emplace_back(block.Position(), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
|
||||||
|
|
||||||
// // 2 face
|
|
||||||
// m_items.emplace_back(block.Position(), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
|
||||||
|
|
||||||
// // 3 face
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
|
||||||
|
|
||||||
// // 4 face
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
|
||||||
|
|
||||||
// // 5 face
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position(), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color());
|
|
||||||
|
|
||||||
// // 6 face
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color());
|
|
||||||
|
|
||||||
// // 7 face
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
|
||||||
|
|
||||||
// // 8 face
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
|
||||||
|
|
||||||
// // 9 face
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position(), block.Color());
|
|
||||||
|
|
||||||
// // 10 face
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
|
||||||
|
|
||||||
// // 11 face
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, 0.f), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, 0.f), block.Color());
|
|
||||||
|
|
||||||
// // 12 face
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
|
||||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, 0.f), block.Color());
|
|
||||||
// }
|
|
||||||
|
|
||||||
void Vertices::PushVertex(const Point& point)
|
|
||||||
{
|
|
||||||
m_items.push_back(point);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Vertices::PushIndex(unsigned int index)
|
|
||||||
{
|
|
||||||
m_indices.push_back(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Vertices::Bind()
|
|
||||||
{
|
|
||||||
glBindVertexArray(m_vao);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Vertices::Unbind()
|
|
||||||
{
|
|
||||||
glBindVertexArray(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Vertices::Draw()
|
|
||||||
{
|
|
||||||
Bind();
|
|
||||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indices.size()), GL_UNSIGNED_INT, 0);
|
|
||||||
Unbind();
|
|
||||||
}
|
|
Reference in New Issue
Block a user