refactoring
This commit is contained in:
65
src/main.cpp
65
src/main.cpp
@@ -1,28 +1,19 @@
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <optional>
|
||||
|
||||
#include "util.hpp"
|
||||
#include "shaders.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
static GLFWwindow* window;
|
||||
|
||||
template<class T>
|
||||
struct SimpleArray {
|
||||
T* data;
|
||||
size_t size;
|
||||
};
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void processInput(GLFWwindow* window)
|
||||
{
|
||||
void processInput(GLFWwindow* window) {
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
@@ -32,7 +23,7 @@ bool glfw_window_setup() {
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
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);
|
||||
if (window == NULL) {
|
||||
std::cout << "Failed to create GLFW windoww" << std::endl;
|
||||
@@ -49,7 +40,7 @@ enum class ParserState {
|
||||
FACE,
|
||||
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;
|
||||
|
||||
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);
|
||||
size_t indices_data_size = 0;
|
||||
|
||||
ParserState state = ParserState::PREFIX;
|
||||
ParserState state = ParserState::PREFIX;
|
||||
|
||||
char ln[128];
|
||||
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] == ' ') {
|
||||
state = ParserState::VERTEX;
|
||||
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;
|
||||
start = len + 1;
|
||||
|
||||
}
|
||||
break;
|
||||
case ParserState::VERTEX:
|
||||
@@ -110,27 +99,26 @@ optional<pair<SimpleArray<float>, SimpleArray<unsigned int>>> load_icosphere() {
|
||||
len++;
|
||||
}
|
||||
|
||||
SimpleArray<float> verts = { vert_data, vert_data_size };
|
||||
SimpleArray<unsigned int> indices = { indices_data, indices_data_size };
|
||||
array<float> verts = { vert_data, vert_data_size };
|
||||
array<unsigned int> indices = { indices_data, indices_data_size };
|
||||
|
||||
return make_pair(verts, indices);
|
||||
return std::make_pair(verts, indices);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
if (!glfw_window_setup()) return -1;
|
||||
int main() {
|
||||
if (!glfw_window_setup())
|
||||
return -1;
|
||||
|
||||
auto data = load_icosphere();
|
||||
if (!data) {
|
||||
std::cout << "ERROR loading the icosphere obj file failed" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
auto verts = data.value().first;
|
||||
auto indices = data.value().second;
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
@@ -150,27 +138,32 @@ int main()
|
||||
unsigned int VBO;
|
||||
glGenBuffers(1, &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;
|
||||
glGenBuffers(1, &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;
|
||||
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);
|
||||
|
||||
// Tell opengl which attribute our data is for (i.e. location)
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)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);
|
||||
glDrawElements(GL_TRIANGLES, indices.size, GL_UNSIGNED_INT, 0);
|
||||
glDrawElements(GL_TRIANGLES, indices.len, GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
processInput(window);
|
||||
|
||||
Reference in New Issue
Block a user