Skip to content

Commit

Permalink
Miscellaneous changes.
Browse files Browse the repository at this point in the history
* common/map_funcs: Optimise implementation of map functions using fmod() to improve performance on Linux.
* Use git branch and commit hash for title bar.
* Log version info.
* Code cleanup and other minor changes.
  • Loading branch information
adityaruplaha committed Mar 19, 2020
1 parent 8b8b437 commit 9f9be69
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 52 deletions.
10 changes: 2 additions & 8 deletions src/Common/map_funcs/map_funcs.cpp
Expand Up @@ -27,10 +27,7 @@ void PingPongMap::recompute()
double seconds = getSeconds();

val = seconds * speed;
while (val > 2)
{
val -= 2;
}
val = fmod(val, 2);
// val between 0 & 2

// magic function: y = -|x-1|+1
Expand All @@ -50,10 +47,7 @@ void BouncyMap::recompute()
double seconds = getSeconds();

val = seconds * speed * 2;
while (val > 2)
{
val -= 2;
}
val = fmod(val, 2);
// val between 0 & 2

// magic function: y = x(2-x)
Expand Down
10 changes: 7 additions & 3 deletions src/Engine/main.cpp
Expand Up @@ -16,7 +16,11 @@ int main()
Initializer::InitGL();
REGISTER_LOG_FILE(new std::ofstream(PROJECT_ROOT_DIR + "logs/log.log"));

auto *window = new Window(600, 600, "OpenGL Engine 0.0.1", {45.0f, 0.1f, 100.0f});
LOG_INFO("Initialized program.");
auto version = GIT_BRANCH + "@" + GIT_COMMIT;
LOG_INFO("Version: %s.", version.c_str());

auto *window = new Window(600, 600, "OpenGL Engine " + version, {45.0f, 0.1f, 100.0f});
Initializer::GLAD_Init();
auto *handler = new InputHandler(window);

Expand Down Expand Up @@ -66,8 +70,8 @@ int main()
prog->setFloat("ratio", mix_ratio);

instance->orientation.position.z = mix_ratio;
instance->orientation.rotation.x -= 0.5f;
instance->orientation.rotation.y += 0.37f;
instance->orientation.rotation.x -= 0.7f;
instance->orientation.rotation.y += 0.57f;
instance->render();

Camera::flush();
Expand Down
10 changes: 3 additions & 7 deletions src/GLCore/base/base.h
Expand Up @@ -5,12 +5,9 @@

#include <Common/log.h>

class Initializer {
namespace Initializer {

public:

static void InitGL()
{
inline void InitGL() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
Expand All @@ -19,8 +16,7 @@ class Initializer {
glfwWindowHint(GLFW_SAMPLES, 4);
}

static void GLAD_Init()
{
inline void GLAD_Init() {
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
throw "Failed to initialize GLAD!\n";
Expand Down
8 changes: 4 additions & 4 deletions src/GLCore/buffer_objects/ebo.cpp
Expand Up @@ -3,21 +3,21 @@
EBO::EBO(std::vector<GLuint> buffer_data)
{
glGenBuffers(1, &ebo);
LOG_SUCCESS("New EBO created at 0x%p.", &ebo);
LOG_SUCCESS("New EBO created at %p.", &ebo);
bind();
if (!buffer_data.size())
{
LOG_ERROR("0x%p: Empty buffer passed as a argument.", &ebo);
LOG_ERROR("%p: Empty buffer passed as a argument.", &ebo);
}
copyFrom(buffer_data);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data_internal, GL_STATIC_DRAW);
if (glGetError() == GL_NO_ERROR)
{
LOG_SUCCESS("0x%p: Buffer data initialized successfully.", &ebo);
LOG_SUCCESS("%p: Buffer data initialized successfully.", &ebo);
}
else
{
LOG_ERROR("0x%p: Buffer data was NOT initialized successfully.", &ebo);
LOG_ERROR("%p: Buffer data was NOT initialized successfully.", &ebo);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/GLCore/buffer_objects/vao.cpp
Expand Up @@ -3,7 +3,7 @@
VAO::VAO()
{
glGenVertexArrays(1, &vao);
LOG_SUCCESS("New VAO created at 0x%p.", &vao);
LOG_SUCCESS("New VAO created at %p.", &vao);
}


Expand Down
14 changes: 7 additions & 7 deletions src/GLCore/buffer_objects/vbo.cpp
Expand Up @@ -3,21 +3,21 @@
VBO::VBO(std::vector<float> buffer_data, std::vector<int> slice_attribs, unsigned int stride)
{
glGenBuffers(1, &vbo);
LOG_SUCCESS("New VBO created at 0x%p.", &vbo);
LOG_SUCCESS("New VBO created at %p.", &vbo);
bind();
if (!buffer_data.size())
{
LOG_ERROR("0x%p: Empty buffer passed as a argument.", &vbo);
LOG_ERROR("%p: Empty buffer passed as a argument.", &vbo);
}
copyFrom(buffer_data);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data_internal, GL_STATIC_DRAW);
if (glGetError() == GL_NO_ERROR)
{
LOG_SUCCESS("0x%p: Buffer data initialized successfully.", &vbo);
LOG_SUCCESS("%p: Buffer data initialized successfully.", &vbo);
}
else
{
LOG_ERROR("0x%p: Buffer data was NOT initialized successfully.", &vbo);
LOG_ERROR("%p: Buffer data was NOT initialized successfully.", &vbo);
}

if (!stride)
Expand All @@ -26,7 +26,7 @@ VBO::VBO(std::vector<float> buffer_data, std::vector<int> slice_attribs, unsigne
{
stride += size;
}
LOG_INFO("0x%p: Buffer stride determined as %i.", &vbo, stride);
LOG_INFO("%p: Buffer stride determined as %i.", &vbo, stride);
}

slice_using(slice_attribs, stride);
Expand All @@ -46,11 +46,11 @@ void VBO::slice_using(std::vector<int> slice_attribs, unsigned int stride)
glEnableVertexAttribArray(i);
if (glGetError() == GL_NO_ERROR)
{
LOG_SUCCESS("0x%p: Sliced %i values at [offset=0x%i] for attribute (location=%i).", &vbo, size, offset, i);
LOG_SUCCESS("%p: Sliced %i values at [offset=%i] for attribute (location=%i).", &vbo, size, offset, i);
}
else
{
LOG_ERROR("0x%p: Unable to slice for attribute location %i.", &vbo, i);
LOG_ERROR("%p: Unable to slice for attribute location %i.", &vbo, i);
}
i++;
offset += size;
Expand Down
20 changes: 10 additions & 10 deletions src/GLCore/program.cpp
Expand Up @@ -6,13 +6,13 @@ Program* Program::current_program{};
Program::Program() : metadata{"", "", ""}
{
program = glCreateProgram();
LOG_INFO("A new shader program was created at 0x%p.", &program);
LOG_INFO("A new shader program was created at %p.", &program);
}

Program::Program(int shader_count, ...) : metadata{"", "", ""}
{
program = glCreateProgram();
LOG_INFO("A new shader program was created at 0x%p.", &program);
LOG_INFO("A new shader program was created at %p.", &program);

va_list args;
va_start(args, shader_count);
Expand Down Expand Up @@ -40,11 +40,11 @@ void Program::link()
{
if (!shader->is_compiled)
{
LOG_WARNING("0x%p: A shader wasn't pre-compiled. Compiling it now...", &program);
LOG_WARNING("%p: A shader wasn't pre-compiled. Compiling it now...", &program);
shader->compile();
if (!shader->is_compiled)
{
LOG_CRITICAL("0x%p: Shader couldn't be compiled!", &program);
LOG_CRITICAL("%p: Shader couldn't be compiled!", &program);
}
}
glAttachShader(program, shader->get());
Expand All @@ -61,10 +61,10 @@ void Program::link()
if (!success)
{
glGetProgramInfoLog(program, 1024, NULL, infoLog);
LOG_ERROR("Program at 0x%p failed to be linked.\n%s", &program, infoLog);
LOG_ERROR("Program at %p failed to be linked.\n%s", &program, infoLog);
return;
}
LOG_SUCCESS("Program at 0x%p was linked successfully.", &program);
LOG_SUCCESS("Program at %p was linked successfully.", &program);
}

void Program::start()
Expand Down Expand Up @@ -123,7 +123,7 @@ int Program::getUniformLocation(std::string name)
auto result = glGetUniformLocation(program, name.c_str());
if (result == -1)
{
LOG_WARNING("0x%p: Invalid uniform name: '%s'", &program, name.c_str());
LOG_WARNING("%p: Invalid uniform name: '%s'", &program, name.c_str());
}
return result;
}
Expand All @@ -136,7 +136,7 @@ void Program::parseMetadata(Shader*& shader)
else
{
if (!shader->metadata.model_mat_name.empty())
LOG_ERROR("0x%p: Model matrix uniform name mismatch!", &program);
LOG_ERROR("%p: Model matrix uniform name mismatch!", &program);
}

if (metadata.view_mat_name.empty() || metadata.view_mat_name == shader->metadata.view_mat_name) {
Expand All @@ -145,7 +145,7 @@ void Program::parseMetadata(Shader*& shader)
else
{
if (!shader->metadata.view_mat_name.empty())
LOG_ERROR("0x%p: View matrix uniform name mismatch!", &program);
LOG_ERROR("%p: View matrix uniform name mismatch!", &program);
}

if (metadata.proj_mat_name.empty() || metadata.proj_mat_name == shader->metadata.proj_mat_name) {
Expand All @@ -154,6 +154,6 @@ void Program::parseMetadata(Shader*& shader)
else
{
if (!shader->metadata.proj_mat_name.empty())
LOG_ERROR("0x%p: Projection matrix uniform name mismatch!", &program);
LOG_ERROR("%p: Projection matrix uniform name mismatch!", &program);
}
}
10 changes: 5 additions & 5 deletions src/GLCore/shader.cpp
Expand Up @@ -21,10 +21,10 @@ Shader* Shader::create(std::ifstream stream, int type)
Shader::Shader(std::string& src, int type) : src(src), type(type)
{
shader = glCreateShader(this->type);
LOG_INFO("A new %s was created at 0x%p.", getShaderName(this->type).c_str(), &shader);
LOG_INFO("A new %s was created at %p.", getShaderName(this->type).c_str(), &shader);
if (this->src.empty())
{
LOG_ERROR("0x%p: Shader is blank!", &shader);
LOG_ERROR("%p: Shader is blank!", &shader);
}

shader_cache[src] = this;
Expand All @@ -33,7 +33,7 @@ Shader::Shader(std::string& src, int type) : src(src), type(type)
Shader::~Shader()
{
shader = glCreateShader(type);
LOG_INFO("0x%p: Shader deleted.", getShaderName(type), &shader);
LOG_INFO("%p: Shader deleted.", getShaderName(type), &shader);

glDeleteShader(shader);
}
Expand Down Expand Up @@ -92,10 +92,10 @@ void Shader::compile()
if (!success)
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
LOG_ERROR("Shader compilation for %s at 0x%p failed.\n%s", getShaderName(type).c_str(), &shader, infoLog);
LOG_ERROR("Shader compilation for %s at %p failed.\n%s", getShaderName(type).c_str(), &shader, infoLog);
return;
}
LOG_SUCCESS("%s at 0x%p was compiled successfully.", getShaderName(type).c_str(), &shader);
LOG_SUCCESS("%s at %p was compiled successfully.", getShaderName(type).c_str(), &shader);
is_compiled = true;
}
GLuint Shader::get()
Expand Down
8 changes: 4 additions & 4 deletions src/GLCore/textures/surface.cpp
Expand Up @@ -20,11 +20,11 @@ Surface* Surface::create(std::string path)
auto* surface = iterator->second;
if (surface && surface->getData())
{
LOG_INFO("Pre-cached surface found for %s: 0x%p", path.c_str(), surface_cache[path]);
LOG_INFO("Pre-cached surface found for %s: %p", path.c_str(), surface_cache[path]);
return surface_cache[path];
}
// Surface is invalid. Delete it and return a fresh one.
LOG_WARNING("Pre-cached surface found for %s: 0x%p but it is invalid. Regenerating...", path.c_str(), surface_cache[path]);
LOG_WARNING("Pre-cached surface found for %s: %p but it is invalid. Regenerating...", path.c_str(), surface_cache[path]);
delete surface_cache[path];
return Surface::create(path);
}
Expand All @@ -35,10 +35,10 @@ Surface::Surface(std::string path) : path(path)
data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
if (!data)
{
LOG_ERROR("0x%p: Failed to load image %s.", this, path.c_str());
LOG_ERROR("%p: Failed to load image %s.", this, path.c_str());
return;
}
LOG_SUCCESS("0x%p: Loaded image %s.", this, path.c_str());
LOG_SUCCESS("%p: Loaded image %s.", this, path.c_str());

surface_cache[path] = this;
}
Expand Down
3 changes: 2 additions & 1 deletion src/GLCore/textures/surface.h
Expand Up @@ -8,7 +8,6 @@ class Surface
public:
static Surface* create(std::string path);

Surface(std::string path);
~Surface();
int width, height, nrChannels;
unsigned char* getData();
Expand All @@ -19,6 +18,8 @@ class Surface
static void clearCache();

private:
Surface(std::string path);

const std::string path;
unsigned char* data;

Expand Down
2 changes: 1 addition & 1 deletion src/GLCore/textures/texture2D.cpp
Expand Up @@ -12,7 +12,7 @@ Texture2D::Texture2D(std::string path, GLenum image_format, GLint filtering, uns
glActiveTexture(bound_sampler_unit + GL_TEXTURE0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
LOG_INFO("A new 2D texture was created at 0x%p.", &texture);
LOG_INFO("A new 2D texture was created at %p.", &texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Expand Down
2 changes: 1 addition & 1 deletion src/GLCore/textures/textureCube.cpp
Expand Up @@ -13,7 +13,7 @@ TextureCube::TextureCube(std::array<std::string, 6> path, GLenum image_format, G
glActiveTexture(bound_sampler_unit + GL_TEXTURE0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
LOG_INFO("A new cubemap texture was created at 0x%p.", &texture);
LOG_INFO("A new cubemap texture was created at %p.", &texture);

glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Expand Down

0 comments on commit 9f9be69

Please sign in to comment.