From 97a33e5517e96d319ec3e88a8b2108094166347b Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 16 Feb 2022 18:34:49 +0100 Subject: [PATCH] BIG REFACTOR Part 2 --- include/render/loader/GLLoader.h | 5 +- include/render/loader/TextureLoader.h | 4 +- include/render/shaders/ShaderProgram.h | 28 +++--- src/misc/Time.cpp | 4 +- src/network/IPAddress.cpp | 8 +- src/render/Renderer.cpp | 12 +-- src/render/loader/GLLoader.cpp | 6 +- src/render/loader/TextureLoader.cpp | 4 +- src/render/shaders/EntityShader.cpp | 10 +-- src/render/shaders/ShaderProgram.cpp | 115 +++++++++++-------------- src/render/shaders/WorldShader.cpp | 8 +- 11 files changed, 98 insertions(+), 106 deletions(-) diff --git a/include/render/loader/GLLoader.h b/include/render/loader/GLLoader.h index 519b863..cd89fe2 100644 --- a/include/render/loader/GLLoader.h +++ b/include/render/loader/GLLoader.h @@ -17,8 +17,9 @@ namespace GL { struct VertexAttribPointer { - unsigned int m_Index, m_Size; - int m_Offset; + unsigned int m_Index; + unsigned int m_Size; + unsigned int m_Offset; }; class VertexBuffer { diff --git a/include/render/loader/TextureLoader.h b/include/render/loader/TextureLoader.h index fdb59ca..f72e9d0 100644 --- a/include/render/loader/TextureLoader.h +++ b/include/render/loader/TextureLoader.h @@ -9,7 +9,9 @@ #define RENDER_LOADER_TEXTURELOADER_H_ namespace TextureLoader { -const unsigned int loadGLTexture(const char* fileName); + +unsigned int loadGLTexture(const char* fileName); + } diff --git a/include/render/shaders/ShaderProgram.h b/include/render/shaders/ShaderProgram.h index dda7a4f..5f05f71 100755 --- a/include/render/shaders/ShaderProgram.h +++ b/include/render/shaders/ShaderProgram.h @@ -23,22 +23,24 @@ public: protected: virtual void getAllUniformLocation() = 0; - int getUniformLocation(const std::string& uniformName)const; - void loadFloat(const int location, const float value)const; - void loadInt(const int& location, const int& value)const; - void loadVector(const int& location, const glm::vec2& vector)const; - void loadVector(const int& location, const glm::vec3& vector)const; - void loadVector(const int& location, const glm::vec4& vector)const; - void loadBoolean(const int& location, const bool& value)const; - void loadMatrix(const int& location, const glm::mat4& matrix); + int getUniformLocation(const std::string& uniformName) const; + + void loadFloat(unsigned int location, float value) const; + void loadInt(unsigned int location, int value) const; + void loadVector(unsigned int location, const glm::vec2& vector) const; + void loadVector(unsigned int location, const glm::vec3& vector) const; + void loadVector(unsigned int location, const glm::vec4& vector) const; + void loadBoolean(unsigned int location, bool value) const; + void loadMatrix(unsigned int location, const glm::mat4& matrix) const; void cleanUp() const; private: - unsigned int programID; - unsigned int vertexShaderID; - unsigned int fragmentShaderID; - int loadShaderFromFile(const std::string& file, GLenum type); - int loadShader(const std::string& source, GLenum type); + unsigned int m_ProgramID; + unsigned int m_VertexShaderID; + unsigned int m_FragmentShaderID; + + unsigned int loadShaderFromFile(const std::string& file, GLenum type); + unsigned int loadShader(const std::string& source, GLenum type); }; #endif /* RENDER_SHADERS_SHADERPROGRAM_H_ */ diff --git a/src/misc/Time.cpp b/src/misc/Time.cpp index 899b8de..a64559c 100644 --- a/src/misc/Time.cpp +++ b/src/misc/Time.cpp @@ -46,7 +46,7 @@ void Timer::reset() { bool CooldownTimer::update(std::uint64_t delta) { if (m_Cooldown > 0) { - m_Cooldown = std::max(static_cast(0), static_cast(m_Cooldown - delta)); + m_Cooldown = static_cast(std::max(static_cast(0), static_cast(m_Cooldown - delta))); } return m_Cooldown == 0; } @@ -60,7 +60,7 @@ void CooldownTimer::applyCooldown() { } std::uint64_t getTime() { - return std::chrono::duration_cast(std::chrono::system_clock().now().time_since_epoch()).count(); + return static_cast(std::chrono::duration_cast(std::chrono::system_clock().now().time_since_epoch()).count()); } } // namespace utils diff --git a/src/network/IPAddress.cpp b/src/network/IPAddress.cpp index 63a4d4f..81b3ad5 100644 --- a/src/network/IPAddress.cpp +++ b/src/network/IPAddress.cpp @@ -36,7 +36,7 @@ IPAddress::IPAddress(const std::string& ip) std::uint8_t octet3 = std::stoul(std::string(match[3])); std::uint8_t octet4 = std::stoul(std::string(match[4])); - m_Address = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4; + m_Address = static_cast((octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4); m_Valid = true; } @@ -55,14 +55,14 @@ IPAddress::IPAddress(const std::wstring& ip) std::uint8_t octet3 = std::stoul(match[3]); std::uint8_t octet4 = std::stoul(match[4]); - m_Address = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4; + m_Address = static_cast((octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4); m_Valid = true; } /* Initialize by octets */ IPAddress::IPAddress(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4) noexcept : m_Valid(true) { - m_Address = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4; + m_Address = static_cast((octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4); } /* Get the specific octet */ @@ -82,7 +82,7 @@ void IPAddress::SetOctet(uint8_t num, uint8_t value) { octets[num - 1] = value; - m_Address = (octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3]; + m_Address = static_cast((octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3]); } IPAddress IPAddress::LocalAddress() { diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp index db3af9d..14408a2 100644 --- a/src/render/Renderer.cpp +++ b/src/render/Renderer.cpp @@ -56,7 +56,7 @@ bool Renderer::init() { void Renderer::renderVAO(const GL::VertexArray& vao) { m_WorldShader->start(); vao.bind(); - glDrawArrays(GL_TRIANGLES, 0, vao.getVertexCount()); + glDrawArrays(GL_TRIANGLES, 0, static_cast(vao.getVertexCount())); vao.unbind(); } @@ -64,14 +64,14 @@ void Renderer::renderModel(const Model& model) { m_EntityShader->start(); m_EntityShader->setModelPos(model.positon); model.vao->bind(); - glDrawArrays(GL_TRIANGLES, 0, model.vao->getVertexCount()); + glDrawArrays(GL_TRIANGLES, 0, static_cast(model.vao->getVertexCount())); model.vao->unbind(); } void Renderer::updateIsometricFade() { static std::uint64_t lastTime = utils::getTime(); - if (m_IsometricShade != (float)m_IsometricView) { - float step = (float)(utils::getTime() - lastTime) / 1000.0f * m_AnimationSpeed; + if (m_IsometricShade != static_cast(m_IsometricView)) { + float step = static_cast(utils::getTime() - lastTime) / 1000.0f * m_AnimationSpeed; if (m_IsometricShade < m_IsometricView) { m_IsometricShade += step; } else { @@ -92,9 +92,9 @@ void Renderer::prepare() { void Renderer::resize(int width, int height) { m_WorldShader->start(); - m_WorldShader->setAspectRatio((float)width / height); + m_WorldShader->setAspectRatio(static_cast(width) / height); m_EntityShader->start(); - m_EntityShader->setAspectRatio((float)width / height); + m_EntityShader->setAspectRatio(static_cast(width) / height); glViewport(0, 0, width, height); } diff --git a/src/render/loader/GLLoader.cpp b/src/render/loader/GLLoader.cpp index d74c4ca..98a0c09 100644 --- a/src/render/loader/GLLoader.cpp +++ b/src/render/loader/GLLoader.cpp @@ -42,8 +42,8 @@ VertexBuffer::~VertexBuffer() { VertexBuffer::VertexBuffer(const std::vector& data, unsigned int stride) : m_DataStride(stride) { glGenBuffers(1, &m_ID); bind(); - glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), nullptr, GL_STATIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, 0, data.size() * sizeof(float), data.data()); + glBufferData(GL_ARRAY_BUFFER, static_cast(data.size() * sizeof(float)), nullptr, GL_STATIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast(data.size() * sizeof(float)), data.data()); unbind(); } @@ -66,7 +66,7 @@ void VertexBuffer::addVertexAttribPointer(unsigned int index, unsigned int coord void VertexBuffer::bindVertexAttribs() const { for (const VertexAttribPointer& pointer : m_VertexAttribs) { glEnableVertexAttribArray(pointer.m_Index); - glVertexAttribPointer(pointer.m_Index, pointer.m_Size, GL_FLOAT, false, m_DataStride * sizeof(float), (void*)(intptr_t)pointer.m_Offset); + glVertexAttribPointer(pointer.m_Index, static_cast(pointer.m_Size), GL_FLOAT, false, m_DataStride * sizeof(float), reinterpret_cast(pointer.m_Offset)); } } } \ No newline at end of file diff --git a/src/render/loader/TextureLoader.cpp b/src/render/loader/TextureLoader.cpp index ef611a7..7e6f5af 100644 --- a/src/render/loader/TextureLoader.cpp +++ b/src/render/loader/TextureLoader.cpp @@ -12,7 +12,8 @@ #include "render/GL.h" namespace TextureLoader { -const unsigned int loadGLTexture(const char* fileName) { + +unsigned int loadGLTexture(const char* fileName) { int width, height, comp; @@ -41,4 +42,5 @@ const unsigned int loadGLTexture(const char* fileName) { stbi_image_free((void*)image); return textureID; } + } diff --git a/src/render/shaders/EntityShader.cpp b/src/render/shaders/EntityShader.cpp index 78dabbd..d06eeea 100644 --- a/src/render/shaders/EntityShader.cpp +++ b/src/render/shaders/EntityShader.cpp @@ -97,11 +97,11 @@ void EntityShader::loadShader() { } void EntityShader::getAllUniformLocation() { - location_aspect_ratio = getUniformLocation("aspectRatio"); - location_zoom = getUniformLocation("zoom"); - location_cam = getUniformLocation("camPos"); - location_translation = getUniformLocation("translation"); - location_viewtype = getUniformLocation("isometricView"); + location_aspect_ratio = static_cast(getUniformLocation("aspectRatio")); + location_zoom = static_cast(getUniformLocation("zoom")); + location_cam = static_cast(getUniformLocation("camPos")); + location_translation = static_cast(getUniformLocation("translation")); + location_viewtype = static_cast(getUniformLocation("isometricView")); } void EntityShader::setCamPos(const glm::vec2& camPos) { diff --git a/src/render/shaders/ShaderProgram.cpp b/src/render/shaders/ShaderProgram.cpp index 496b0aa..32664ae 100755 --- a/src/render/shaders/ShaderProgram.cpp +++ b/src/render/shaders/ShaderProgram.cpp @@ -9,6 +9,7 @@ #include #include +#include #include @@ -17,7 +18,7 @@ #endif ShaderProgram::ShaderProgram() : - programID(0), vertexShaderID(0), fragmentShaderID(0) { + m_ProgramID(0), m_VertexShaderID(0), m_FragmentShaderID(0) { } ShaderProgram::~ShaderProgram() { @@ -25,7 +26,7 @@ ShaderProgram::~ShaderProgram() { } void ShaderProgram::start() const { - glUseProgram(programID); + glUseProgram(m_ProgramID); } void ShaderProgram::stop() const { @@ -33,74 +34,78 @@ void ShaderProgram::stop() const { } int ShaderProgram::getUniformLocation(const std::string& uniformName) const { - const int location = glGetUniformLocation(programID, uniformName.c_str()); + const int location = glGetUniformLocation(m_ProgramID, uniformName.c_str()); if (location == -1) { std::cout << "Warning ! Uniform variable " << uniformName << " not found !\n"; } return location; } -void ShaderProgram::loadFloat(const int location, const float value) const { - glUniform1f(location, value); +void ShaderProgram::loadFloat(unsigned int location, float value) const { + glUniform1f(static_cast(location), value); } -void ShaderProgram::loadInt(const int& location, const int& value) const { - glUniform1i(location, value); +void ShaderProgram::loadInt(unsigned int location, int value) const { + glUniform1i(static_cast(location), value); } -void ShaderProgram::loadVector(const int& location, +void ShaderProgram::loadVector(unsigned int location, const glm::vec2& vector) const { - glUniform2f(location, vector.x, vector.y); + glUniform2f(static_cast(location), vector.x, vector.y); } -void ShaderProgram::loadVector(const int& location, +void ShaderProgram::loadVector(unsigned int location, const glm::vec3& vector) const { - glUniform3f(location, vector.x, vector.y, vector.z); + glUniform3f(static_cast(location), vector.x, vector.y, vector.z); } -void ShaderProgram::loadVector(const int& location, +void ShaderProgram::loadVector(unsigned int location, const glm::vec4& vector) const { - glUniform4f(location, vector.x, vector.y, vector.z, vector.w); + glUniform4f(static_cast(location), vector.x, vector.y, vector.z, vector.w); } -void ShaderProgram::loadBoolean(const int& location, const bool& value) const { - glUniform1i(location, value); +void ShaderProgram::loadBoolean(unsigned int location, bool value) const { + glUniform1i(static_cast(location), value); +} + +void ShaderProgram::loadMatrix(unsigned int location, const glm::mat4& matrix) const { + glUniformMatrix4fv(static_cast(location), 1, false, glm::value_ptr(matrix)); } void ShaderProgram::cleanUp() const { stop(); - glDetachShader(programID, vertexShaderID); - glDetachShader(programID, fragmentShaderID); - glDeleteShader(vertexShaderID); - glDeleteShader(fragmentShaderID); - glDeleteProgram(programID); + glDetachShader(m_ProgramID, m_VertexShaderID); + glDetachShader(m_ProgramID, m_FragmentShaderID); + glDeleteShader(m_VertexShaderID); + glDeleteShader(m_FragmentShaderID); + glDeleteProgram(m_ProgramID); } void ShaderProgram::loadProgramFile(const std::string& vertexFile, const std::string& fragmentFile) { - vertexShaderID = loadShaderFromFile(vertexFile, GL_VERTEX_SHADER); - fragmentShaderID = loadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER); - programID = glCreateProgram(); - glAttachShader(programID, vertexShaderID); - glAttachShader(programID, fragmentShaderID); - glLinkProgram(programID); - glValidateProgram(programID); + m_VertexShaderID = static_cast(loadShaderFromFile(vertexFile, GL_VERTEX_SHADER)); + m_FragmentShaderID = static_cast(loadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER)); + m_ProgramID = glCreateProgram(); + glAttachShader(m_ProgramID, m_VertexShaderID); + glAttachShader(m_ProgramID, m_FragmentShaderID); + glLinkProgram(m_ProgramID); + glValidateProgram(m_ProgramID); getAllUniformLocation(); } void ShaderProgram::loadProgram(const std::string& vertexSource, const std::string& fragmentSource) { - vertexShaderID = loadShader(vertexSource, GL_VERTEX_SHADER); - fragmentShaderID = loadShader(fragmentSource, GL_FRAGMENT_SHADER); - programID = glCreateProgram(); - glAttachShader(programID, vertexShaderID); - glAttachShader(programID, fragmentShaderID); - glLinkProgram(programID); - glValidateProgram(programID); + m_VertexShaderID = static_cast(loadShader(vertexSource, GL_VERTEX_SHADER)); + m_FragmentShaderID = static_cast(loadShader(fragmentSource, GL_FRAGMENT_SHADER)); + m_ProgramID = glCreateProgram(); + glAttachShader(m_ProgramID, m_VertexShaderID); + glAttachShader(m_ProgramID, m_FragmentShaderID); + glLinkProgram(m_ProgramID); + glValidateProgram(m_ProgramID); getAllUniformLocation(); } -int ShaderProgram::loadShader(const std::string& source, GLenum type) { +unsigned int ShaderProgram::loadShader(const std::string& source, GLenum type) { unsigned int shaderID = glCreateShader(type); const char* c_str = source.c_str(); @@ -113,9 +118,9 @@ int ShaderProgram::loadShader(const std::string& source, GLenum type) { std::cout << "Could not compile shader !\n"; GLsizei size; glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size); - char error[size]; - glGetShaderInfoLog(shaderID, size, &size, error); - std::cout << error << std::endl; + std::vector shaderError(static_cast(size)); + glGetShaderInfoLog(shaderID, size, &size, shaderError.data()); + std::cout << shaderError.data() << std::endl; #ifdef __ANDROID__ __android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "Could not compile shader !\n %s", error); #endif @@ -123,35 +128,15 @@ int ShaderProgram::loadShader(const std::string& source, GLenum type) { return shaderID; } -int ShaderProgram::loadShaderFromFile(const std::string& file, GLenum type) { - std::string shaderSource = ""; +unsigned int ShaderProgram::loadShaderFromFile(const std::string& file, GLenum type) { + std::stringstream stream; std::ifstream fileStream(file); - if (fileStream.is_open()) { - std::string line; - while (getline(fileStream, line)) { - shaderSource += line + "\n"; - } - fileStream.close(); - } - unsigned int shaderID = glCreateShader(type); - const char* c_str = shaderSource.c_str(); - int* null = 0; - glShaderSource(shaderID, 1, &c_str, null); // @suppress("Function cannot be resolved") - glCompileShader(shaderID); - GLint compilesuccessful; - glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compilesuccessful); - if (compilesuccessful == false) { - std::cout << "Could not compile shader !\n"; - GLsizei size; - glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size); - char error[size]; - glGetShaderInfoLog(shaderID, size, &size, error); - std::cout << error << std::endl; + if(fileStream) { + stream << fileStream.rdbuf(); + } else { + return 0; } - return shaderID; -} -void ShaderProgram::loadMatrix(const int& location, const glm::mat4& matrix) { - glUniformMatrix4fv(location, 1, false, glm::value_ptr(matrix)); + return loadShader(stream.str(), type); } diff --git a/src/render/shaders/WorldShader.cpp b/src/render/shaders/WorldShader.cpp index 6079fe6..ed6b911 100644 --- a/src/render/shaders/WorldShader.cpp +++ b/src/render/shaders/WorldShader.cpp @@ -98,10 +98,10 @@ void WorldShader::loadShader() { } void WorldShader::getAllUniformLocation() { - location_aspect_ratio = getUniformLocation("aspectRatio"); - location_zoom = getUniformLocation("zoom"); - location_cam = getUniformLocation("camPos"); - location_viewtype = getUniformLocation("isometricView"); + location_aspect_ratio = static_cast(getUniformLocation("aspectRatio")); + location_zoom = static_cast(getUniformLocation("zoom")); + location_cam = static_cast(getUniformLocation("camPos")); + location_viewtype = static_cast(getUniformLocation("isometricView")); } void WorldShader::setCamPos(const glm::vec2& camPos) {