Skip to content

Commit

Permalink
changed most normal arrays to std::array
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacey committed May 30, 2023
1 parent cdb2ced commit 879f780
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
command: 'apt-get update && apt-get install -y sudo && rm -rf /var/lib/apt/lists/*'
- run :
name : Install tools
command: 'apt-get update && sudo apt-get install -y cmake libglfw3-dev qtbase5-dev libpython2.7-dev libxinerama-dev libxcursor-dev xorg-dev libglu1-mesa-dev curl zip unzip tar'
command: 'apt-get update && sudo apt-get install -y cmake libglfw3-dev qtbase5-dev libxinerama-dev libxcursor-dev xorg-dev libglu1-mesa-dev curl zip unzip tar'

- run :
name : Install vcpkg
Expand Down
55 changes: 29 additions & 26 deletions src/ShaderProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#include "ShaderProgram.h"
#include "Mat3.h"
#include "Mat4.h"
#include "fmt/format.h"
#include <fmt/format.h>
#include <fmt/ranges.h>
#include <iostream>
#include <memory>
namespace ngl
Expand Down Expand Up @@ -194,11 +195,11 @@ void ShaderProgram::printActiveAttributes() const noexcept
GLint size;
GLenum type;
GLsizei l;
char name[256];
std::array<char,256> name;
for(GLint i = 0; i < nAttribs; ++i)
{
std::string Type;
glGetActiveAttrib(m_programID, static_cast< GLuint >(i), 256, &l, &size, &type, name);
glGetActiveAttrib(m_programID, static_cast< GLuint >(i), 256, &l, &size, &type, &name[0]);
switch(type)
{
case GL_FLOAT:
Expand Down Expand Up @@ -301,7 +302,7 @@ bool ShaderProgram::setRegisteredUniform2f(std::string_view _varname, float _v0,
bool ShaderProgram::getRegisteredUniform2f(std::string_view _varname, float &o_v0, float &o_v1) const noexcept
{
auto uniform = m_registeredUniforms.find(_varname.data());
float data[2];
std::array<float,2> data;
// make sure we have a valid shader
if(uniform != m_registeredUniforms.end())
{
Expand Down Expand Up @@ -337,7 +338,7 @@ bool ShaderProgram::getRegisteredUniform2f(std::string_view _varname, float &o_v
bool ShaderProgram::getRegisteredUniform3f(std::string_view _varname, float &o_v0, float &o_v1, float &o_v2) const noexcept
{
auto uniform = m_registeredUniforms.find(_varname.data());
float data[3];
std::array<float,3> data;
// make sure we have a valid shader
if(uniform != m_registeredUniforms.end())
{
Expand Down Expand Up @@ -374,7 +375,7 @@ bool ShaderProgram::setRegisteredUniform4f(std::string_view _varname, float _v0,
bool ShaderProgram::getRegisteredUniform4f(std::string_view _varname, float &o_v0, float &o_v1, float &o_v2, float &o_v3) const noexcept
{
auto uniform = m_registeredUniforms.find(_varname.data());
float data[4];
std::array<float,4> data;
// make sure we have a valid shader
if(uniform != m_registeredUniforms.end())
{
Expand Down Expand Up @@ -452,7 +453,7 @@ bool ShaderProgram::getRegisteredUniform2i(std::string_view _varname, GLint &o_v
// make sure we have a valid shader
if(uniform != m_registeredUniforms.end())
{
GLint ret[2];
std::array<GLint,2> ret;
glGetUniformiv(m_programID, uniform->second.loc, &ret[0]);
o_v0=ret[0];
o_v1=ret[1];
Expand Down Expand Up @@ -490,7 +491,7 @@ bool ShaderProgram::getRegisteredUniform3i(std::string_view _varname, GLint &o_v
// make sure we have a valid shader
if(uniform != m_registeredUniforms.end())
{
GLint ret[3];
std::array<GLint,3> ret;
glGetUniformiv(m_programID, uniform->second.loc, &ret[0]);
o_v0=ret[0];
o_v1=ret[1];
Expand Down Expand Up @@ -531,7 +532,7 @@ bool ShaderProgram::getRegisteredUniform4i(std::string_view _varname, GLint &o_v
// make sure we have a valid shader
if(uniform != m_registeredUniforms.end())
{
GLint ret[4];
std::array<GLint,4> ret;
glGetUniformiv(m_programID, uniform->second.loc, &ret[0]);
o_v0=ret[0];
o_v1=ret[1];
Expand Down Expand Up @@ -696,16 +697,16 @@ void ShaderProgram::autoRegisterUniformBlocks() noexcept
glGetProgramiv(m_programID, GL_ACTIVE_UNIFORM_BLOCKS, &nUniforms);
if(m_debugState == true)
NGLMessage::addMessage(fmt::format("FOUND UNIFORM BLOCKS {0}", nUniforms), Colours::WHITE, TimeFormat::NONE);
char name[256];
std::array<char,256> name;
uniformBlockData data;
for(GLint i = 0; i < nUniforms; ++i)
{
GLsizei nameLen = 0;
glGetActiveUniformBlockName(m_programID, i, sizeof(name) - 1, &nameLen, name);
data.name = name;
data.loc = glGetUniformBlockIndex(m_programID, name);
glGetActiveUniformBlockName(m_programID, i, sizeof(name) - 1, &nameLen, &name[0]);
data.name = std::string(name.data());
data.loc = glGetUniformBlockIndex(m_programID, &name[0]);
glGenBuffers(1, &data.buffer);
m_registeredUniformBlocks[name] = data;
m_registeredUniformBlocks[std::string(name.data())] = data;
NGLMessage::addMessage(fmt::format("Uniform Block {0} {1} {2}", name, data.loc, data.buffer), Colours::WHITE, TimeFormat::NONE);
}
}
Expand Down Expand Up @@ -737,26 +738,26 @@ void ShaderProgram::autoRegisterUniforms() noexcept
glGetProgramiv(m_programID, GL_ACTIVE_UNIFORMS, &nUniforms);
// could use this with better OpenGL version
// glGetProgramInterfaceiv(i, GL_UNIFORM, GL_ACTIVE_RESOURCES, &numUniforms);
char name[256];
std::array<char,256> name;
uniformData data;
for(GLint i = 0; i < nUniforms; ++i)
{
GLenum type = GL_ZERO;
GLsizei nameLen = 0;
GLint num = 0;
glGetActiveUniform(m_programID, i, sizeof(name) - 1, &nameLen, &num, &type, name);
glGetActiveUniform(m_programID, i, sizeof(name) - 1, &nameLen, &num, &type, &name[0]);
// two options we either have an array or single value
// if not array
if(num == 1)
{
data.name = name;
data.loc = glGetUniformLocation(m_programID, name);
data.name = std::string(name.data());
data.loc = glGetUniformLocation(m_programID, name.data());
data.type = type;
m_registeredUniforms[name] = data;
m_registeredUniforms[std::string(name.data())] = data;
}
else
{
std::string uniform(name);
std::string uniform(std::string(name.data()));
std::string baseName = uniform.substr(0, uniform.find("["));
// nvidia returns uniform[0], ATI uniform, best way is to split on [
for(int b = 0; b < num; ++b)
Expand All @@ -783,33 +784,34 @@ std::string ShaderProgram::getValueFromShader(const uniformData &_d) const noexc

else if(_d.type == GL_FLOAT_VEC2)
{
float v[2];
std::array<float,2> v;
getUniformfv(_d.name.c_str(), &v[0]);
value = fmt::format("[{:.4f},{:.4f}]", v[0], v[1]);
}
else if(_d.type == GL_FLOAT_VEC3)
{
float v[3];
std::array<float,3> v;
getUniformfv(_d.name.c_str(), &v[0]);
value = fmt::format("[{:.4f},{:.4f},{:.4f}]", v[0], v[1], v[2]);
}
else if(_d.type == GL_FLOAT_VEC4)
{
float v[4];
std::array<float,4> v;
getUniformfv(_d.name.c_str(), &v[0]);
value = fmt::format("[{:.4f},{:.4f},{:.4f},{:.4f}]", v[0], v[1], v[2], v[3]);
}
else if(_d.type == GL_FLOAT_MAT2)
{
float v[4];
std::array<float,4> v;
getUniformfv(_d.name.c_str(), &v[0]);
value = fmt::format("\n[{:.4f},{:.4f}]\n"
"[{:.4f},{:.4f}]",
v[0], v[1], v[2], v[3]);
}
else if(_d.type == GL_FLOAT_MAT3)
{
float v[9];
std::array<float,9> v;

getUniformfv(_d.name.c_str(), &v[0]);
value = fmt::format("\n[{:.4f},{:.4f},{:.4f}]\n"
"[{:.4f},{:.4f},{:.4f}]\n"
Expand All @@ -818,7 +820,8 @@ std::string ShaderProgram::getValueFromShader(const uniformData &_d) const noexc
}
else if(_d.type == GL_FLOAT_MAT4)
{
float v[16];
std::array<float,16> v;

getUniformfv(_d.name.c_str(), &v[0]);
value = fmt::format("\n[{:.4f},{:.4f},{:.4f},{:.4f}]\n"
"[{:.4f},{:.4f},{:.4f},{:.4f}]\n"
Expand Down

0 comments on commit 879f780

Please sign in to comment.