refactoring
This commit is contained in:
@@ -136,7 +136,7 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="glad.c" />
|
<ClCompile Include="src\glad.c" />
|
||||||
<ClCompile Include="src\main.cpp" />
|
<ClCompile Include="src\main.cpp" />
|
||||||
<ClCompile Include="src\shaders.cpp" />
|
<ClCompile Include="src\shaders.cpp" />
|
||||||
<ClCompile Include="src\util.cpp" />
|
<ClCompile Include="src\util.cpp" />
|
||||||
|
|||||||
@@ -15,9 +15,6 @@
|
|||||||
</Filter>
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="glad.c">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="src\main.cpp">
|
<ClCompile Include="src\main.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@@ -27,5 +24,8 @@
|
|||||||
<ClCompile Include="src\util.cpp">
|
<ClCompile Include="src\util.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\glad.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,13 +1,25 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"directory": "C:/Users/seth/Documents/repos/LivePlotter/",
|
"directory": "C:/Users/sethh/Documents/repos/LivePlotter/",
|
||||||
"command": "\"C:/Users/seth/AppData/Roaming/ClangPowerTools/LLVM_Lite/Bin/clang++.exe\" -x c \"C:/Users/seth/Documents/repos/LivePlotter/glad.c\" -Wall -fms-compatibility-version=19.10 -Wmicrosoft -Wno-invalid-token-paste -Wno-unknown-pragmas -Wno-unused-value -fsyntax-only \"-DUNICODE\" \"-D_UNICODE\" \"-D_MT\" \"-D_DLL\" \"-D_DEBUG\" \"-D_CONSOLE\" \"-D_DEBUG_FUNCTIONAL_MACHINERY\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/include\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/atlmfc/include\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Auxiliary/VS/include\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/ucrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/um\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/shared\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/winrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/cppwinrt\" -isystem\"C:/Users/seth/Documents/repos/LivePlotter/ext/glfw/include\" -isystem\"C:/Users/seth/Documents/repos/LivePlotter/inc\"",
|
"command": "\"C:/Users/sethh/AppData/Roaming/ClangPowerTools/LLVM_Lite/Bin/clang++.exe\" -x c++ \"C:/Users/sethh/Documents/repos/LivePlotter/src/util.cpp\" -std=c++17 -Wall -fms-compatibility-version=19.10 -Wmicrosoft -Wno-invalid-token-paste -Wno-unknown-pragmas -Wno-unused-value -fsyntax-only \"-DUNICODE\" \"-D_UNICODE\" \"-D_MT\" \"-D_DLL\" \"-D_DEBUG\" \"-D_CONSOLE\" \"-D_DEBUG_FUNCTIONAL_MACHINERY\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/include\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/atlmfc/include\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/VS/include\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/um\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/shared\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/winrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/cppwinrt\" -isystem\"C:/Users/sethh/Documents/repos/LivePlotter/ext/glfw/include\" -isystem\"C:/Users/sethh/Documents/repos/LivePlotter/inc\"",
|
||||||
"file": "C:/Users/seth/Documents/repos/LivePlotter/glad.c"
|
"file": "C:/Users/sethh/Documents/repos/LivePlotter/src/util.cpp"
|
||||||
}
|
}
|
||||||
,
|
,
|
||||||
{
|
{
|
||||||
"directory": "C:/Users/seth/Documents/repos/LivePlotter/",
|
"directory": "C:/Users/sethh/Documents/repos/LivePlotter/",
|
||||||
"command": "\"C:/Users/seth/AppData/Roaming/ClangPowerTools/LLVM_Lite/Bin/clang++.exe\" -x c++ \"C:/Users/seth/Documents/repos/LivePlotter/src/main.cpp\" -std=c++17 -Wall -fms-compatibility-version=19.10 -Wmicrosoft -Wno-invalid-token-paste -Wno-unknown-pragmas -Wno-unused-value -fsyntax-only \"-DUNICODE\" \"-D_UNICODE\" \"-D_MT\" \"-D_DLL\" \"-D_DEBUG\" \"-D_CONSOLE\" \"-D_DEBUG_FUNCTIONAL_MACHINERY\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/include\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/atlmfc/include\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Auxiliary/VS/include\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/ucrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/um\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/shared\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/winrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/cppwinrt\" -isystem\"C:/Users/seth/Documents/repos/LivePlotter/ext/glfw/include\" -isystem\"C:/Users/seth/Documents/repos/LivePlotter/inc\"",
|
"command": "\"C:/Users/sethh/AppData/Roaming/ClangPowerTools/LLVM_Lite/Bin/clang++.exe\" -x c \"C:/Users/sethh/Documents/repos/LivePlotter/src/glad.c\" -Wall -fms-compatibility-version=19.10 -Wmicrosoft -Wno-invalid-token-paste -Wno-unknown-pragmas -Wno-unused-value -fsyntax-only \"-DUNICODE\" \"-D_UNICODE\" \"-D_MT\" \"-D_DLL\" \"-D_DEBUG\" \"-D_CONSOLE\" \"-D_DEBUG_FUNCTIONAL_MACHINERY\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/include\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/atlmfc/include\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/VS/include\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/um\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/shared\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/winrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/cppwinrt\" -isystem\"C:/Users/sethh/Documents/repos/LivePlotter/ext/glfw/include\" -isystem\"C:/Users/sethh/Documents/repos/LivePlotter/inc\"",
|
||||||
"file": "C:/Users/seth/Documents/repos/LivePlotter/src/main.cpp"
|
"file": "C:/Users/sethh/Documents/repos/LivePlotter/src/glad.c"
|
||||||
|
}
|
||||||
|
,
|
||||||
|
{
|
||||||
|
"directory": "C:/Users/sethh/Documents/repos/LivePlotter/",
|
||||||
|
"command": "\"C:/Users/sethh/AppData/Roaming/ClangPowerTools/LLVM_Lite/Bin/clang++.exe\" -x c++ \"C:/Users/sethh/Documents/repos/LivePlotter/src/shaders.cpp\" -std=c++17 -Wall -fms-compatibility-version=19.10 -Wmicrosoft -Wno-invalid-token-paste -Wno-unknown-pragmas -Wno-unused-value -fsyntax-only \"-DUNICODE\" \"-D_UNICODE\" \"-D_MT\" \"-D_DLL\" \"-D_DEBUG\" \"-D_CONSOLE\" \"-D_DEBUG_FUNCTIONAL_MACHINERY\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/include\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/atlmfc/include\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/VS/include\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/um\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/shared\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/winrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/cppwinrt\" -isystem\"C:/Users/sethh/Documents/repos/LivePlotter/ext/glfw/include\" -isystem\"C:/Users/sethh/Documents/repos/LivePlotter/inc\"",
|
||||||
|
"file": "C:/Users/sethh/Documents/repos/LivePlotter/src/shaders.cpp"
|
||||||
|
}
|
||||||
|
,
|
||||||
|
{
|
||||||
|
"directory": "C:/Users/sethh/Documents/repos/LivePlotter/",
|
||||||
|
"command": "\"C:/Users/sethh/AppData/Roaming/ClangPowerTools/LLVM_Lite/Bin/clang++.exe\" -x c++ \"C:/Users/sethh/Documents/repos/LivePlotter/src/main.cpp\" -std=c++17 -Wall -fms-compatibility-version=19.10 -Wmicrosoft -Wno-invalid-token-paste -Wno-unknown-pragmas -Wno-unused-value -fsyntax-only \"-DUNICODE\" \"-D_UNICODE\" \"-D_MT\" \"-D_DLL\" \"-D_DEBUG\" \"-D_CONSOLE\" \"-D_DEBUG_FUNCTIONAL_MACHINERY\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/include\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/atlmfc/include\" -isystem\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/VS/include\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/um\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/shared\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/winrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/cppwinrt\" -isystem\"C:/Users/sethh/Documents/repos/LivePlotter/ext/glfw/include\" -isystem\"C:/Users/sethh/Documents/repos/LivePlotter/inc\"",
|
||||||
|
"file": "C:/Users/sethh/Documents/repos/LivePlotter/src/main.cpp"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
3
inc/globj.hpp
Normal file
3
inc/globj.hpp
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#include "util.hpp"
|
||||||
|
|
||||||
|
|
||||||
@@ -7,13 +7,13 @@
|
|||||||
namespace shader {
|
namespace shader {
|
||||||
|
|
||||||
// Reads shader source files, compiles, and links
|
// Reads shader source files, compiles, and links
|
||||||
bool load(uint* out_id, const char *vertex_filepath, const char *fragment_filepath);
|
bool load(uint* out_id, const char* vertex_filepath, const char* fragment_filepath);
|
||||||
|
|
||||||
// Sets shader as active on the gpu
|
// Sets shader as active on the gpu
|
||||||
void use(uint id);
|
void use(uint id);
|
||||||
|
|
||||||
// Set uniform value
|
// Set uniform value
|
||||||
typedef std::variant<int, float, bool> uniform_variant;
|
typedef std::variant<int, float, bool, vec4, vec3, vec2, vec4i, vec3i, vec2i> uniform_variant;
|
||||||
void set_uniform(uint id, const char *name, uniform_variant value);
|
void set_uniform(uint id, const char* name, uniform_variant value);
|
||||||
|
|
||||||
} // namespace shaders
|
} // namespace shaders
|
||||||
|
|||||||
43
inc/util.hpp
43
inc/util.hpp
@@ -2,13 +2,48 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
template<class T> struct Array {
|
template<class T> struct array {
|
||||||
T* _data;
|
T* data;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
T& operator[](int i);
|
T& operator[](int i);
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef unsigned int uint;
|
typedef unsigned int uint;
|
||||||
|
|
||||||
bool read_file(Array<char>* out, const char* filepath);
|
bool read_file(array<char>* out, const char* filepath);
|
||||||
|
|
||||||
|
struct vec4 {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
float w;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct vec3 {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct vec2 {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct vec4i {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int z;
|
||||||
|
int w;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct vec3i {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int z;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct vec2i {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
};
|
||||||
|
|||||||
65
src/main.cpp
65
src/main.cpp
@@ -1,28 +1,19 @@
|
|||||||
|
#include <array>
|
||||||
|
#include <cmath>
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
|
#include "util.hpp"
|
||||||
#include "shaders.hpp"
|
#include "shaders.hpp"
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
static GLFWwindow* window;
|
static GLFWwindow* window;
|
||||||
|
|
||||||
template<class T>
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
|
||||||
struct SimpleArray {
|
|
||||||
T* data;
|
|
||||||
size_t size;
|
|
||||||
};
|
|
||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
void processInput(GLFWwindow* window) {
|
||||||
{
|
|
||||||
glViewport(0, 0, width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
void processInput(GLFWwindow* window)
|
|
||||||
{
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
}
|
}
|
||||||
@@ -32,7 +23,7 @@ bool glfw_window_setup() {
|
|||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
window = glfwCreateWindow(800, 800, "LearnOpenGL", NULL, NULL);
|
window = glfwCreateWindow(800, 800, "LearnOpenGL", NULL, NULL);
|
||||||
if (window == NULL) {
|
if (window == NULL) {
|
||||||
std::cout << "Failed to create GLFW windoww" << std::endl;
|
std::cout << "Failed to create GLFW windoww" << std::endl;
|
||||||
@@ -49,7 +40,7 @@ enum class ParserState {
|
|||||||
FACE,
|
FACE,
|
||||||
FACE_SKIP,
|
FACE_SKIP,
|
||||||
};
|
};
|
||||||
optional<pair<SimpleArray<float>, SimpleArray<unsigned int>>> load_icosphere() {
|
std::optional<std::pair<array<float>, array<unsigned int>>> load_icosphere() {
|
||||||
std::FILE* f = NULL;
|
std::FILE* f = NULL;
|
||||||
|
|
||||||
if (fopen_s(&f, "Icosphere.obj", "r")) {
|
if (fopen_s(&f, "Icosphere.obj", "r")) {
|
||||||
@@ -64,7 +55,7 @@ optional<pair<SimpleArray<float>, SimpleArray<unsigned int>>> load_icosphere() {
|
|||||||
unsigned int* indices_data = (unsigned int*)malloc(sizeof(int) * 256);
|
unsigned int* indices_data = (unsigned int*)malloc(sizeof(int) * 256);
|
||||||
size_t indices_data_size = 0;
|
size_t indices_data_size = 0;
|
||||||
|
|
||||||
ParserState state = ParserState::PREFIX;
|
ParserState state = ParserState::PREFIX;
|
||||||
|
|
||||||
char ln[128];
|
char ln[128];
|
||||||
int len = 0;
|
int len = 0;
|
||||||
@@ -75,11 +66,9 @@ optional<pair<SimpleArray<float>, SimpleArray<unsigned int>>> load_icosphere() {
|
|||||||
if (len > 0 && ln[len - 1] == 'v' && ln[len] == ' ') {
|
if (len > 0 && ln[len - 1] == 'v' && ln[len] == ' ') {
|
||||||
state = ParserState::VERTEX;
|
state = ParserState::VERTEX;
|
||||||
start = len + 1;
|
start = len + 1;
|
||||||
}
|
} else if (len > 0 && ln[len - 1] == 'f' && ln[len] == ' ') {
|
||||||
else if (len > 0 && ln[len - 1] == 'f' && ln[len] == ' ') {
|
|
||||||
state = ParserState::FACE;
|
state = ParserState::FACE;
|
||||||
start = len + 1;
|
start = len + 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ParserState::VERTEX:
|
case ParserState::VERTEX:
|
||||||
@@ -110,27 +99,26 @@ optional<pair<SimpleArray<float>, SimpleArray<unsigned int>>> load_icosphere() {
|
|||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleArray<float> verts = { vert_data, vert_data_size };
|
array<float> verts = { vert_data, vert_data_size };
|
||||||
SimpleArray<unsigned int> indices = { indices_data, indices_data_size };
|
array<unsigned int> indices = { indices_data, indices_data_size };
|
||||||
|
|
||||||
return make_pair(verts, indices);
|
return std::make_pair(verts, indices);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
if (!glfw_window_setup())
|
||||||
if (!glfw_window_setup()) return -1;
|
return -1;
|
||||||
|
|
||||||
auto data = load_icosphere();
|
auto data = load_icosphere();
|
||||||
if (!data) {
|
if (!data) {
|
||||||
std::cout << "ERROR loading the icosphere obj file failed" << std::endl;
|
std::cout << "ERROR loading the icosphere obj file failed" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto verts = data.value().first;
|
auto verts = data.value().first;
|
||||||
auto indices = data.value().second;
|
auto indices = data.value().second;
|
||||||
|
|
||||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||||
{
|
|
||||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -150,27 +138,32 @@ int main()
|
|||||||
unsigned int VBO;
|
unsigned int VBO;
|
||||||
glGenBuffers(1, &VBO);
|
glGenBuffers(1, &VBO);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
glBufferData(GL_ARRAY_BUFFER, verts.size * sizeof(float), verts.data, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, verts.len * sizeof(float), verts.data, GL_STATIC_DRAW);
|
||||||
|
|
||||||
unsigned int EBO;
|
unsigned int EBO;
|
||||||
glGenBuffers(1, &EBO);
|
glGenBuffers(1, &EBO);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size * sizeof(unsigned int), indices.data, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.len * sizeof(unsigned int), indices.data, GL_STATIC_DRAW);
|
||||||
|
|
||||||
uint program_id;
|
uint program_id;
|
||||||
if (!shader::load(&program_id, "src/shaders/vertex.vs", "src/shaders/fragment.fs")) return -1;
|
const char* vertex_filepath = "src/shaders/vertex.glsl";
|
||||||
|
const char* fragment_filepath = "src/shaders/fragment.glsl";
|
||||||
|
if (!shader::load(&program_id, vertex_filepath, fragment_filepath))
|
||||||
|
return -1;
|
||||||
shader::use(program_id);
|
shader::use(program_id);
|
||||||
|
|
||||||
// Tell opengl which attribute our data is for (i.e. location)
|
// Tell opengl which attribute our data is for (i.e. location)
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window)) {
|
||||||
{
|
float time = glfwGetTime();
|
||||||
|
shader::set_uniform(program_id, "color", vec4 { 1, sin(time), 1, 1.0 });
|
||||||
|
shader::use(program_id);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glDrawElements(GL_TRIANGLES, indices.size, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, indices.len, GL_UNSIGNED_INT, 0);
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
processInput(window);
|
processInput(window);
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
#define MAX_ERR_MSG_LEN 256
|
#define MAX_ERR_MSG_LEN 256
|
||||||
|
|
||||||
|
|
||||||
// Visit/Variant overload pattern
|
// Visit/Variant overload pattern
|
||||||
// https://www.modernescpp.com/index.php/visiting-a-std-variant-with-the-overload-pattern/
|
// https://www.modernescpp.com/index.php/visiting-a-std-variant-with-the-overload-pattern/
|
||||||
template<typename ... Ts>
|
template<typename ... Ts>
|
||||||
@@ -57,6 +56,12 @@ void set_uniform(uint id, const char* name, uniform_variant value) {
|
|||||||
[id, name](int i) { glUniform1i(glGetUniformLocation(id, name), i); },
|
[id, name](int i) { glUniform1i(glGetUniformLocation(id, name), i); },
|
||||||
[id, name](float f) { glUniform1f(glGetUniformLocation(id, name), f); },
|
[id, name](float f) { glUniform1f(glGetUniformLocation(id, name), f); },
|
||||||
[id, name](bool b) { glUniform1i(glGetUniformLocation(id, name), b); },
|
[id, name](bool b) { glUniform1i(glGetUniformLocation(id, name), b); },
|
||||||
|
[id, name](vec4 v) { glUniform4f(glGetUniformLocation(id, name), v.x, v.y, v.z, v.w); },
|
||||||
|
[id, name](vec3 v) { glUniform3f(glGetUniformLocation(id, name), v.x, v.y, v.z); },
|
||||||
|
[id, name](vec2 v) { glUniform2f(glGetUniformLocation(id, name), v.x, v.y); },
|
||||||
|
[id, name](vec4i v) { glUniform4i(glGetUniformLocation(id, name), v.x, v.y, v.z, v.w); },
|
||||||
|
[id, name](vec3i v) { glUniform3i(glGetUniformLocation(id, name), v.x, v.y, v.z); },
|
||||||
|
[id, name](vec2i v) { glUniform2i(glGetUniformLocation(id, name), v.x, v.y); },
|
||||||
};
|
};
|
||||||
visit(visitor, value);
|
visit(visitor, value);
|
||||||
}
|
}
|
||||||
@@ -64,12 +69,12 @@ void set_uniform(uint id, const char* name, uniform_variant value) {
|
|||||||
// Privates
|
// Privates
|
||||||
|
|
||||||
bool _compile_shader(uint *out_id, const char* filepath, int shader_type) {
|
bool _compile_shader(uint *out_id, const char* filepath, int shader_type) {
|
||||||
Array<char> source;
|
array<char> source;
|
||||||
if (!read_file(&source, filepath))
|
if (!read_file(&source, filepath))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
uint id = glCreateShader(shader_type);
|
uint id = glCreateShader(shader_type);
|
||||||
glShaderSource(id, 1, &source._data, (int*)&source.len);
|
glShaderSource(id, 1, &source.data, (int*)&source.len);
|
||||||
glCompileShader(id);
|
glCompileShader(id);
|
||||||
|
|
||||||
int status;
|
int status;
|
||||||
@@ -78,7 +83,7 @@ bool _compile_shader(uint *out_id, const char* filepath, int shader_type) {
|
|||||||
char err_msg[MAX_ERR_MSG_LEN];
|
char err_msg[MAX_ERR_MSG_LEN];
|
||||||
glGetShaderInfoLog(id, MAX_ERR_MSG_LEN, NULL, err_msg);
|
glGetShaderInfoLog(id, MAX_ERR_MSG_LEN, NULL, err_msg);
|
||||||
printf("Error compiling shader %s\n", filepath);
|
printf("Error compiling shader %s\n", filepath);
|
||||||
printf("%.*s\n", (int)source.len, source._data);
|
printf("%.*s\n", (int)source.len, source.data);
|
||||||
printf("Error msg: %s\n", err_msg);
|
printf("Error msg: %s\n", err_msg);
|
||||||
}
|
}
|
||||||
*out_id = id;
|
*out_id = id;
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
|
uniform vec4 color;
|
||||||
|
|
||||||
out vec4 frag_color;
|
out vec4 frag_color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
frag_color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
frag_color = color;
|
||||||
}
|
}
|
||||||
8
src/shaders/vertex.glsl
Normal file
8
src/shaders/vertex.glsl
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 pos;
|
||||||
|
|
||||||
|
uniform vec4 color;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = vec4(pos.x, pos.y, pos.z, 1.0);
|
||||||
|
}
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#version 330 core
|
|
||||||
layout (location = 0) in vec3 aPos;
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
|
||||||
}
|
|
||||||
@@ -3,21 +3,21 @@
|
|||||||
|
|
||||||
#include "util.hpp"
|
#include "util.hpp"
|
||||||
|
|
||||||
bool read_file(Array<char>* out, const char* filepath) {
|
bool read_file(array<char>* out, const char* filepath) {
|
||||||
FILE* fp = NULL;
|
FILE* fp = NULL;
|
||||||
if (fopen_s(&fp, filepath, "r") != 0) {
|
if (fopen_s(&fp, filepath, "rb") != 0) {
|
||||||
printf("ERROR Failed to open file %s\n", filepath);
|
printf("ERROR Failed to open file %s\n", filepath);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fseek(fp, 0L, SEEK_END);
|
fseek(fp, 0L, SEEK_END);
|
||||||
size_t sz = ftell(fp);
|
size_t sz = ftell(fp);
|
||||||
fseek(fp, 0L, 0L);
|
fseek(fp, 0, SEEK_SET);
|
||||||
|
|
||||||
char *data = (char*)malloc(sizeof(char)*sz);
|
char *data = (char*)malloc(sizeof(char)*sz);
|
||||||
fread(data, sizeof(char), sz, fp);
|
fread(data, sizeof(char), sz, fp);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
out->_data = data;
|
out->data = data;
|
||||||
out->len = sz;
|
out->len = sz;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user