summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2022-11-26 16:46:01 -0500
committerrealtradam <[email protected]>2022-11-26 16:46:01 -0500
commit368269070f851ff78a1bce35c1d993d5f02cc5f9 (patch)
tree83292448b320f561148f16efdbb456c85ab00f87
parent68486053b032fd3313c887ea7d73064e59dce570 (diff)
downloadOgle-368269070f851ff78a1bce35c1d993d5f02cc5f9.tar.gz
Ogle-368269070f851ff78a1bce35c1d993d5f02cc5f9.zip
namespace it all
-rw-r--r--src/batch.cpp372
-rw-r--r--src/batch.hpp72
-rw-r--r--src/input.cpp62
-rw-r--r--src/input.hpp12
-rw-r--r--src/main.cpp4
-rw-r--r--src/random.cpp9
-rw-r--r--src/random.hpp7
-rw-r--r--src/shader.cpp205
-rw-r--r--src/shader.hpp43
-rw-r--r--src/texture.cpp39
-rw-r--r--src/texture.hpp13
-rw-r--r--src/window.cpp84
-rw-r--r--src/window.hpp17
13 files changed, 487 insertions, 452 deletions
diff --git a/src/batch.cpp b/src/batch.cpp
index 36c8a19..ee6bd4f 100644
--- a/src/batch.cpp
+++ b/src/batch.cpp
@@ -10,190 +10,192 @@
#include <vector>
#include <iostream>
-
-Batch::Batch(unsigned int batch_limit)
-{
- this->batch_limit = batch_limit;
-
- glGenBuffers(1, &VBO); // generate the buffer
- glGenBuffers(1, &EBO);
- glGenVertexArrays(1, &VAO); // generate array
-
- glBindVertexArray(VAO); // bind array to apply "settings" to it
- glBindBuffer(GL_ARRAY_BUFFER, VBO); // bind(activate) the buffer to the ARRAY_BUFFER
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
-
- // position attribute
- GLsizei stride = 10 * sizeof(float);
- glVertexAttribPointer(
- 0, // index
- 3, // size(number of elements)
- GL_FLOAT,
- GL_FALSE,
- stride, // stride(distance to reach next one)
- (void*)0); // pointer(offset from start)
- glEnableVertexAttribArray(0);
-
- // color attribute
- glVertexAttribPointer(
- 1,
- 4,
- GL_FLOAT,
- GL_FALSE,
- stride,
- (void*)(3 * sizeof(float))); // configure how the vertex array looks
- glEnableVertexAttribArray(1);
-
- //texture coordinates
- glVertexAttribPointer(
- 2,
- 2,
- GL_FLOAT,
- GL_FALSE,
- stride,
- (void*)(7 * sizeof(float))); // configure how the vertex array looks
- glEnableVertexAttribArray(2);
-
- //texture id
- glVertexAttribPointer(
- 3,
- 1,
- GL_FLOAT,
- GL_FALSE,
- stride,
- (void*)(9 * sizeof(float))); // configure how the vertex array looks
- glEnableVertexAttribArray(3);
-}
-
-int Batch::drawTexture(
- Texture texture,
- float x,
- float y,
- float width,
- float height,
- glm::vec4 color
- )
-{
- float h_height = height / 2.0;
- float h_width = width / 2.0;
- return this->primativeDrawQuad(
- texture.id,
- glm::vec3(x+h_height,y+h_width, -1.0f), // top right
- glm::vec3(x+h_height, y-h_width, -1.0f), // bottom right
- glm::vec3(x-h_height, y-h_width, -1.0f), // bottom left
- glm::vec3(x-h_height, y+h_height, -1.0f), // top left
- color
- );
-}
-
-
-int Batch::drawRectangle(
- float x,
- float y,
- float width,
- float height,
- glm::vec4 color
- )
-{
- float h_height = height / 2.0;
- float h_width = width / 2.0;
- return this->primativeDrawQuad(
- 0,
- glm::vec3(x+h_height,y+h_width, -1.0f), // top right
- glm::vec3(x+h_height, y-h_width, -1.0f), // bottom right
- glm::vec3(x-h_height, y-h_width, -1.0f), // bottom left
- glm::vec3(x-h_height, y+h_height, -1.0f), // top left
- color
- );
-}
-
-int Batch::primativeDrawQuad(
- unsigned int texture_id,
- glm::vec3 a,
- glm::vec3 b,
- glm::vec3 c,
- glm::vec3 d,
- glm::vec4 color
- )
-{
- // if no assigned texture, attempt to assign it
- if(texture_id == 0)
- last_texture = texture_id;
- // if surpassing batch limit
- // or if texture is different
- if((size >= batch_limit) || ((last_texture != texture_id) && (texture_id != 0)))
- this->flush(); last_texture = texture_id;
-
-
- float verts[] = {
- // top right
- a.x, a.y, a.z, // position
- color.r, color.g, color.b, color.a, // colors
- 1.0f, 1.0f, // tex coords
- (float)texture_id, // tex id(0 means none)
-
- // bottom right
- b.x, b.y, b.z,
- color.r, color.g, color.b, color.a,
- 1.0f, 0.0f,
- (float)texture_id,
-
- // bottom left
- c.x, c.y, c.z,
- color.r, color.g, color.b, color.a,
- 0.0f, 0.0f,
- (float)texture_id,
-
- // top left
- d.x, d.y, d.z,
- color.r, color.g, color.b, color.a,
- 0.0f, 1.0f,
- (float)texture_id
- };
- unsigned int inds[] = {
- (size * 4) + 0, (size * 4) + 1, (size * 4) + 3, // first triangle
- (size * 4) + 1, (size * 4) + 2, (size * 4) + 3, // second triangle
- };
- vertices.reserve(sizeof(verts));
- indices.reserve(sizeof(inds));
- vertices.insert(vertices.end(), std::cbegin(verts), std::cend(verts));
- indices.insert(indices.end(), std::cbegin(inds), std::cend(inds));
- size += 1;
-
- return 0;
-}
-
-void Batch::flush()
+namespace Ogle
{
- glBindVertexArray(VAO);
-
- // we already binded this in the VAO during initialization
- // so no need to do it again
- //glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData( // uploading vertices
- GL_ARRAY_BUFFER,
- vertices.size() * sizeof(float),
- vertices.data(),
- GL_DYNAMIC_DRAW
- );
-
- // we already binded this in the VAO during initialization
- // so no need to do it again
- //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData( // uploading indices
- GL_ELEMENT_ARRAY_BUFFER,
- indices.size() * sizeof(unsigned int),
- indices.data(),
- GL_DYNAMIC_DRAW
- );
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, last_texture);
- glDrawElements(GL_TRIANGLES, 6 * size, GL_UNSIGNED_INT, 0);
-
- glBindVertexArray(0); // unbind the VAO
-
- last_texture = 0;
- size = 0;
- vertices.clear();
- indices.clear();
+ Batch::Batch(unsigned int batch_limit)
+ {
+ this->batch_limit = batch_limit;
+
+ glGenBuffers(1, &VBO); // generate the buffer
+ glGenBuffers(1, &EBO);
+ glGenVertexArrays(1, &VAO); // generate array
+
+ glBindVertexArray(VAO); // bind array to apply "settings" to it
+ glBindBuffer(GL_ARRAY_BUFFER, VBO); // bind(activate) the buffer to the ARRAY_BUFFER
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
+
+ // position attribute
+ GLsizei stride = 10 * sizeof(float);
+ glVertexAttribPointer(
+ 0, // index
+ 3, // size(number of elements)
+ GL_FLOAT,
+ GL_FALSE,
+ stride, // stride(distance to reach next one)
+ (void*)0); // pointer(offset from start)
+ glEnableVertexAttribArray(0);
+
+ // color attribute
+ glVertexAttribPointer(
+ 1,
+ 4,
+ GL_FLOAT,
+ GL_FALSE,
+ stride,
+ (void*)(3 * sizeof(float))); // configure how the vertex array looks
+ glEnableVertexAttribArray(1);
+
+ //texture coordinates
+ glVertexAttribPointer(
+ 2,
+ 2,
+ GL_FLOAT,
+ GL_FALSE,
+ stride,
+ (void*)(7 * sizeof(float))); // configure how the vertex array looks
+ glEnableVertexAttribArray(2);
+
+ //texture id
+ glVertexAttribPointer(
+ 3,
+ 1,
+ GL_FLOAT,
+ GL_FALSE,
+ stride,
+ (void*)(9 * sizeof(float))); // configure how the vertex array looks
+ glEnableVertexAttribArray(3);
+ }
+
+ int Batch::drawTexture(
+ Texture texture,
+ float x,
+ float y,
+ float width,
+ float height,
+ glm::vec4 color
+ )
+ {
+ float h_height = height / 2.0;
+ float h_width = width / 2.0;
+ return this->primativeDrawQuad(
+ texture.id,
+ glm::vec3(x+h_height,y+h_width, -1.0f), // top right
+ glm::vec3(x+h_height, y-h_width, -1.0f), // bottom right
+ glm::vec3(x-h_height, y-h_width, -1.0f), // bottom left
+ glm::vec3(x-h_height, y+h_height, -1.0f), // top left
+ color
+ );
+ }
+
+
+ int Batch::drawRectangle(
+ float x,
+ float y,
+ float width,
+ float height,
+ glm::vec4 color
+ )
+ {
+ float h_height = height / 2.0;
+ float h_width = width / 2.0;
+ return this->primativeDrawQuad(
+ 0,
+ glm::vec3(x+h_height,y+h_width, -1.0f), // top right
+ glm::vec3(x+h_height, y-h_width, -1.0f), // bottom right
+ glm::vec3(x-h_height, y-h_width, -1.0f), // bottom left
+ glm::vec3(x-h_height, y+h_height, -1.0f), // top left
+ color
+ );
+ }
+
+ int Batch::primativeDrawQuad(
+ unsigned int texture_id,
+ glm::vec3 a,
+ glm::vec3 b,
+ glm::vec3 c,
+ glm::vec3 d,
+ glm::vec4 color
+ )
+ {
+ // if no assigned texture, attempt to assign it
+ if(texture_id == 0)
+ last_texture = texture_id;
+ // if surpassing batch limit
+ // or if texture is different
+ if((size >= batch_limit) || ((last_texture != texture_id) && (texture_id != 0)))
+ this->flush(); last_texture = texture_id;
+
+
+ float verts[] = {
+ // top right
+ a.x, a.y, a.z, // position
+ color.r, color.g, color.b, color.a, // colors
+ 1.0f, 1.0f, // tex coords
+ (float)texture_id, // tex id(0 means none)
+
+ // bottom right
+ b.x, b.y, b.z,
+ color.r, color.g, color.b, color.a,
+ 1.0f, 0.0f,
+ (float)texture_id,
+
+ // bottom left
+ c.x, c.y, c.z,
+ color.r, color.g, color.b, color.a,
+ 0.0f, 0.0f,
+ (float)texture_id,
+
+ // top left
+ d.x, d.y, d.z,
+ color.r, color.g, color.b, color.a,
+ 0.0f, 1.0f,
+ (float)texture_id
+ };
+ unsigned int inds[] = {
+ (size * 4) + 0, (size * 4) + 1, (size * 4) + 3, // first triangle
+ (size * 4) + 1, (size * 4) + 2, (size * 4) + 3, // second triangle
+ };
+ vertices.reserve(sizeof(verts));
+ indices.reserve(sizeof(inds));
+ vertices.insert(vertices.end(), std::cbegin(verts), std::cend(verts));
+ indices.insert(indices.end(), std::cbegin(inds), std::cend(inds));
+ size += 1;
+
+ return 0;
+ }
+
+ void Batch::flush()
+ {
+ glBindVertexArray(VAO);
+
+ // we already binded this in the VAO during initialization
+ // so no need to do it again
+ //glBindBuffer(GL_ARRAY_BUFFER, VBO);
+ glBufferData( // uploading vertices
+ GL_ARRAY_BUFFER,
+ vertices.size() * sizeof(float),
+ vertices.data(),
+ GL_DYNAMIC_DRAW
+ );
+
+ // we already binded this in the VAO during initialization
+ // so no need to do it again
+ //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
+ glBufferData( // uploading indices
+ GL_ELEMENT_ARRAY_BUFFER,
+ indices.size() * sizeof(unsigned int),
+ indices.data(),
+ GL_DYNAMIC_DRAW
+ );
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, last_texture);
+ glDrawElements(GL_TRIANGLES, 6 * size, GL_UNSIGNED_INT, 0);
+
+ glBindVertexArray(0); // unbind the VAO
+
+ last_texture = 0;
+ size = 0;
+ vertices.clear();
+ indices.clear();
+ }
}
diff --git a/src/batch.hpp b/src/batch.hpp
index 39a2a25..958898e 100644
--- a/src/batch.hpp
+++ b/src/batch.hpp
@@ -10,39 +10,43 @@
// std libs
#include <vector>
-class Batch
+
+namespace Ogle
{
- public:
- unsigned int VAO, VBO, EBO;
- std::vector<float> vertices = {};
- std::vector<unsigned int> indices = {};
- unsigned int size, batch_limit;
- unsigned int last_texture = 0;
+ class Batch
+ {
+ public:
+ unsigned int VAO, VBO, EBO;
+ std::vector<float> vertices = {};
+ std::vector<unsigned int> indices = {};
+ unsigned int size, batch_limit;
+ unsigned int last_texture = 0;
- Batch(unsigned int batch_limit = 8192);
- int drawTexture(
- Texture texture,
- float x,
- float y,
- float width,
- float height,
- glm::vec4 color
- );
- int drawRectangle(
- float x,
- float y,
- float width,
- float height,
- glm::vec4 color
- );
- void flush();
- private:
- int primativeDrawQuad(
- unsigned int texture_id,
- glm::vec3 a,
- glm::vec3 b,
- glm::vec3 c,
- glm::vec3 d,
- glm::vec4 color
- );
-};
+ Batch(unsigned int batch_limit = 8192);
+ int drawTexture(
+ Texture texture,
+ float x,
+ float y,
+ float width,
+ float height,
+ glm::vec4 color
+ );
+ int drawRectangle(
+ float x,
+ float y,
+ float width,
+ float height,
+ glm::vec4 color
+ );
+ void flush();
+ private:
+ int primativeDrawQuad(
+ unsigned int texture_id,
+ glm::vec3 a,
+ glm::vec3 b,
+ glm::vec3 c,
+ glm::vec3 d,
+ glm::vec4 color
+ );
+ };
+}
diff --git a/src/input.cpp b/src/input.cpp
index 674b274..ce9bac2 100644
--- a/src/input.cpp
+++ b/src/input.cpp
@@ -5,41 +5,43 @@
// project headers
#include "input.hpp"
-
-namespace Input
+namespace Ogle
{
- namespace {
- int mouse_x;
- int mouse_y;
- bool mouse_click;
- }
- void process(GLFWwindow *window)
+ namespace Input
{
- if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
- glfwSetWindowShouldClose(window, true);
- else
+ namespace {
+ int mouse_x;
+ int mouse_y;
+ bool mouse_click;
+ }
+ void process(GLFWwindow *window)
{
- double _mouse_x, _mouse_y;
- glfwGetCursorPos(window, &_mouse_x, &_mouse_y);
- mouse_x = (int)_mouse_x;
- mouse_y = (int)_mouse_y;
- if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT))
- mouse_click = true;
+ if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
+ glfwSetWindowShouldClose(window, true);
else
- mouse_click = false;
+ {
+ double _mouse_x, _mouse_y;
+ glfwGetCursorPos(window, &_mouse_x, &_mouse_y);
+ mouse_x = (int)_mouse_x;
+ mouse_y = (int)_mouse_y;
+ if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT))
+ mouse_click = true;
+ else
+ mouse_click = false;
+ }
}
- }
- int get_mouse_x()
- {
- return mouse_x;
- }
- int get_mouse_y()
- {
- return mouse_y;
- }
- bool get_mouse_click()
- {
- return mouse_click;
+ int get_mouse_x()
+ {
+ return mouse_x;
+ }
+ int get_mouse_y()
+ {
+ return mouse_y;
+ }
+ bool get_mouse_click()
+ {
+ return mouse_click;
+ }
}
}
diff --git a/src/input.hpp b/src/input.hpp
index abaa6fc..a42ac9c 100644
--- a/src/input.hpp
+++ b/src/input.hpp
@@ -3,9 +3,11 @@
// external libs
#include "GLFW/glfw3.h"
-namespace Input {
- void process(GLFWwindow *window);
- int get_mouse_x();
- int get_mouse_y();
- bool get_mouse_click();
+namespace Ogle {
+ namespace Input {
+ void process(GLFWwindow *window);
+ int get_mouse_x();
+ int get_mouse_y();
+ bool get_mouse_click();
+ }
}
diff --git a/src/main.cpp b/src/main.cpp
index 81ee360..06bee3e 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -18,10 +18,12 @@
#include <cmath>
#include <chrono>
+using namespace Ogle;
+
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 450;
-const int MAX_BUNNIES = 15000;
+const int MAX_BUNNIES = 50000;
typedef struct Bunny {
glm::vec2 position;
glm::vec2 speed;
diff --git a/src/random.cpp b/src/random.cpp
index 8ff600f..5d5801b 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -5,10 +5,13 @@
// std libs
#include <cstdlib>
-namespace Random
+namespace Ogle
{
- int get_value(int min, int max)
+ namespace Random
{
- return (std::rand()%(abs(max - min) + 1) + min);
+ int get_value(int min, int max)
+ {
+ return (std::rand()%(abs(max - min) + 1) + min);
+ }
}
}
diff --git a/src/random.hpp b/src/random.hpp
index 326cc3e..d159650 100644
--- a/src/random.hpp
+++ b/src/random.hpp
@@ -1,4 +1,7 @@
-namespace Random {
- int get_value(int min, int max);
+namespace Ogle
+{
+ namespace Random {
+ int get_value(int min, int max);
+ }
}
diff --git a/src/shader.cpp b/src/shader.cpp
index 5491d1f..56efa90 100644
--- a/src/shader.cpp
+++ b/src/shader.cpp
@@ -14,117 +14,120 @@
#include <sstream>
#include <iostream>
-Shader::Shader(const char* vertexPath, const char* fragmentPath)
+namespace Ogle
{
- // -- read shaders from file --
- std::string vertexCode;
- std::string fragmentCode;
- std::ifstream vShaderFile;
- std::ifstream fShaderFile;
- // ensure ifstream objects can throw exceptions:
- vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
- fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
- try
+ Shader::Shader(const char* vertexPath, const char* fragmentPath)
{
- // open files
- vShaderFile.open(vertexPath);
- fShaderFile.open(fragmentPath);
- std::stringstream vShaderStream, fShaderStream;
- // read file's buffer contents into streams
- vShaderStream << vShaderFile.rdbuf();
- fShaderStream << fShaderFile.rdbuf();
- // close file handlers
- vShaderFile.close();
- fShaderFile.close();
- // convert stream into string
- vertexCode = vShaderStream.str();
- fragmentCode = fShaderStream.str();
+ // -- read shaders from file --
+ std::string vertexCode;
+ std::string fragmentCode;
+ std::ifstream vShaderFile;
+ std::ifstream fShaderFile;
+ // ensure ifstream objects can throw exceptions:
+ vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
+ fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
+ try
+ {
+ // open files
+ vShaderFile.open(vertexPath);
+ fShaderFile.open(fragmentPath);
+ std::stringstream vShaderStream, fShaderStream;
+ // read file's buffer contents into streams
+ vShaderStream << vShaderFile.rdbuf();
+ fShaderStream << fShaderFile.rdbuf();
+ // close file handlers
+ vShaderFile.close();
+ fShaderFile.close();
+ // convert stream into string
+ vertexCode = vShaderStream.str();
+ fragmentCode = fShaderStream.str();
+ }
+ catch(std::ifstream::failure e)
+ {
+ std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
+ }
+ const char* vShaderCode = vertexCode.c_str();
+ const char* fShaderCode = fragmentCode.c_str();
+
+ // -- compile shaders --
+ unsigned int vertex, fragment;
+ int success;
+ char infoLog[512];
+
+ // vertex Shader
+ vertex = glCreateShader(GL_VERTEX_SHADER);
+ glShaderSource(vertex, 1, &vShaderCode, NULL);
+ glCompileShader(vertex);
+ glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
+ if(!success)
+ {
+ glGetShaderInfoLog(vertex, 512, NULL, infoLog);
+ std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
+ };
+
+ // fragment Shader
+ fragment = glCreateShader(GL_FRAGMENT_SHADER);
+ glShaderSource(fragment, 1, &fShaderCode, NULL);
+ glCompileShader(fragment);
+ glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
+ if(!success)
+ {
+ glGetShaderInfoLog(fragment, 512, NULL, infoLog);
+ std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
+ };
+
+ // linking shaders
+ this->ID = glCreateProgram();
+ glAttachShader(ID, vertex);
+ glAttachShader(ID, fragment);
+ glLinkProgram(ID);
+ glGetProgramiv(ID, GL_LINK_STATUS, &success);
+ if(!success)
+ {
+ glGetProgramInfoLog(ID, 512, NULL, infoLog);
+ std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
+ }
+
+ // cleanup uneeded pre-linked shaders
+ glDeleteShader(vertex);
+ glDeleteShader(fragment);
}
- catch(std::ifstream::failure e)
- {
- std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
- }
- const char* vShaderCode = vertexCode.c_str();
- const char* fShaderCode = fragmentCode.c_str();
-
- // -- compile shaders --
- unsigned int vertex, fragment;
- int success;
- char infoLog[512];
-
- // vertex Shader
- vertex = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertex, 1, &vShaderCode, NULL);
- glCompileShader(vertex);
- glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
- if(!success)
- {
- glGetShaderInfoLog(vertex, 512, NULL, infoLog);
- std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
- };
-
- // fragment Shader
- fragment = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragment, 1, &fShaderCode, NULL);
- glCompileShader(fragment);
- glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
- if(!success)
- {
- glGetShaderInfoLog(fragment, 512, NULL, infoLog);
- std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
- };
-
- // linking shaders
- this->ID = glCreateProgram();
- glAttachShader(ID, vertex);
- glAttachShader(ID, fragment);
- glLinkProgram(ID);
- glGetProgramiv(ID, GL_LINK_STATUS, &success);
- if(!success)
+
+ void Shader::use()
{
- glGetProgramInfoLog(ID, 512, NULL, infoLog);
- std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
+ glUseProgram(ID);
}
- // cleanup uneeded pre-linked shaders
- glDeleteShader(vertex);
- glDeleteShader(fragment);
-}
-void Shader::use()
-{
- glUseProgram(ID);
-}
+ // -- various setters for shader uniforms --
+ void Shader::setBool(const std::string &name, bool value) const
+ {
+ glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
+ }
-// -- various setters for shader uniforms --
-
-void Shader::setBool(const std::string &name, bool value) const
-{
- glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
-}
-
-void Shader::setInt(const std::string &name, int value) const
-{
- glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
-}
+ void Shader::setInt(const std::string &name, int value) const
+ {
+ glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
+ }
-void Shader::set1f(const std::string &name, float value) const
-{
- glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
-}
+ void Shader::set1f(const std::string &name, float value) const
+ {
+ glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
+ }
-void Shader::set4f(const std::string &name, float value0, float value1, float value2, float value3) const
-{
- glUniform4f(glGetUniformLocation(ID, name.c_str()), value0, value1, value2, value3);
-}
+ void Shader::set4f(const std::string &name, float value0, float value1, float value2, float value3) const
+ {
+ glUniform4f(glGetUniformLocation(ID, name.c_str()), value0, value1, value2, value3);
+ }
-void Shader::setMatrix4fv(const std::string &name, glm::mat4 matrix)
-{
- glUniformMatrix4fv(
- glGetUniformLocation(ID, name.c_str()),
- 1,
- GL_FALSE,
- glm::value_ptr(matrix)
- );
+ void Shader::setMatrix4fv(const std::string &name, glm::mat4 matrix)
+ {
+ glUniformMatrix4fv(
+ glGetUniformLocation(ID, name.c_str()),
+ 1,
+ GL_FALSE,
+ glm::value_ptr(matrix)
+ );
+ }
}
diff --git a/src/shader.hpp b/src/shader.hpp
index 88ac64a..057cd5f 100644
--- a/src/shader.hpp
+++ b/src/shader.hpp
@@ -9,27 +9,30 @@
// std libs
#include <string>
-class Shader
+namespace Ogle
{
- public:
- // id of the shader program
- unsigned int ID;
+ class Shader
+ {
+ public:
+ // id of the shader program
+ unsigned int ID;
- Shader(const char* vertexPath, const char* fragmentPath);
+ Shader(const char* vertexPath, const char* fragmentPath);
- // activate the shader
- void use();
+ // activate the shader
+ void use();
- // utility setter functions for uniforms
- void setBool(const std::string &name, bool value) const;
- void setInt(const std::string &name, int value) const;
- void set1f(const std::string &name, float value) const;
- void set4f(
- const std::string &name,
- float value0,
- float value1,
- float value2,
- float value3
- ) const;
- void setMatrix4fv(const std::string &name, glm::mat4 matrix);
-};
+ // utility setter functions for uniforms
+ void setBool(const std::string &name, bool value) const;
+ void setInt(const std::string &name, int value) const;
+ void set1f(const std::string &name, float value) const;
+ void set4f(
+ const std::string &name,
+ float value0,
+ float value1,
+ float value2,
+ float value3
+ ) const;
+ void setMatrix4fv(const std::string &name, glm::mat4 matrix);
+ };
+}
diff --git a/src/texture.cpp b/src/texture.cpp
index 6322ce7..9360369 100644
--- a/src/texture.cpp
+++ b/src/texture.cpp
@@ -10,25 +10,28 @@
// std libs
#include <iostream>
-Texture::Texture(const char* texture_path)
+namespace Ogle
{
- glGenTextures(1, &id);
- glBindTexture(GL_TEXTURE_2D, id);
- // set the texture wrapping/filtering options (on the currently bound texture object)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- int width, height, nrChannels;
- unsigned char *data = stbi_load(texture_path, &width, &height, &nrChannels, 0);
- if (data)
+ Texture::Texture(const char* texture_path)
{
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
- glGenerateMipmap(GL_TEXTURE_2D);
+ glGenTextures(1, &id);
+ glBindTexture(GL_TEXTURE_2D, id);
+ // set the texture wrapping/filtering options (on the currently bound texture object)
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ int width, height, nrChannels;
+ unsigned char *data = stbi_load(texture_path, &width, &height, &nrChannels, 0);
+ if (data)
+ {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
+ glGenerateMipmap(GL_TEXTURE_2D);
+ }
+ else
+ {
+ std::cout << "Failed to load texture" << std::endl;
+ }
+ stbi_image_free(data);
}
- else
- {
- std::cout << "Failed to load texture" << std::endl;
- }
- stbi_image_free(data);
}
diff --git a/src/texture.hpp b/src/texture.hpp
index 5309bd0..6ed89ae 100644
--- a/src/texture.hpp
+++ b/src/texture.hpp
@@ -3,9 +3,12 @@
// external libs
#include "glad/glad.h"
-class Texture
+namespace Ogle
{
- public:
- unsigned int id;
- Texture(const char* texture_path);
-};
+ class Texture
+ {
+ public:
+ unsigned int id;
+ Texture(const char* texture_path);
+ };
+}
diff --git a/src/window.cpp b/src/window.cpp
index f1a6c46..c091b42 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -9,57 +9,59 @@
// std libs
#include <iostream>
-namespace Window {
- namespace { // private
- GLFWwindow* window;
+namespace Ogle {
+ namespace Window {
+ namespace { // private
+ GLFWwindow* window;
- 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);
+ }
}
- }
-
- GLFWwindow* get()
- {
- return window;
- }
-
- int init(
- unsigned int width,
- unsigned int height,
- const char* title
- )
- {
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // 4.6 is highest, but lets use 3.3 for compatability
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
-
- glfwSwapInterval(1);
- window = glfwCreateWindow(width, height, title, NULL, NULL);
- if (window == NULL)
+ GLFWwindow* get()
{
- std::cout << "Failed to create GLFW window" << std::endl;
- glfwTerminate();
- return -1;
+ return window;
}
- glfwMakeContextCurrent(window);
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
+ int init(
+ unsigned int width,
+ unsigned int height,
+ const char* title
+ )
{
- std::cout << "Failed to init GLAD" << std::endl;
- return -1;
- }
+ glfwInit();
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // 4.6 is highest, but lets use 3.3 for compatability
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
+ glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+ glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
+
+ glfwSwapInterval(1);
+
+ window = glfwCreateWindow(width, height, title, NULL, NULL);
+ if (window == NULL)
+ {
+ std::cout << "Failed to create GLFW window" << std::endl;
+ glfwTerminate();
+ return -1;
+ }
+ glfwMakeContextCurrent(window);
- glViewport(0, 0, width, height);
+ if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
+ {
+ std::cout << "Failed to init GLAD" << std::endl;
+ return -1;
+ }
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
+ glViewport(0, 0, width, height);
+
+ glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- return 0;
+ return 0;
+ }
}
}
diff --git a/src/window.hpp b/src/window.hpp
index 568fd2a..5771df3 100644
--- a/src/window.hpp
+++ b/src/window.hpp
@@ -3,11 +3,14 @@
#include "glad/glad.h"
#include "GLFW/glfw3.h"
-namespace Window {
- GLFWwindow* get();
- int init(
- unsigned int width,
- unsigned int height,
- const char* title
- );
+namespace Ogle
+{
+ namespace Window {
+ GLFWwindow* get();
+ int init(
+ unsigned int width,
+ unsigned int height,
+ const char* title
+ );
+ }
}