BIG REFACTOR Part 2
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
#define RENDER_LOADER_TEXTURELOADER_H_
|
||||
|
||||
namespace TextureLoader {
|
||||
const unsigned int loadGLTexture(const char* fileName);
|
||||
|
||||
unsigned int loadGLTexture(const char* fileName);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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_ */
|
||||
|
||||
@@ -46,7 +46,7 @@ void Timer::reset() {
|
||||
|
||||
bool CooldownTimer::update(std::uint64_t delta) {
|
||||
if (m_Cooldown > 0) {
|
||||
m_Cooldown = std::max(static_cast<std::int64_t>(0), static_cast<std::int64_t>(m_Cooldown - delta));
|
||||
m_Cooldown = static_cast<std::uint64_t>(std::max(static_cast<std::int64_t>(0), static_cast<std::int64_t>(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::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count();
|
||||
return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count());
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
|
||||
@@ -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<std::uint32_t>((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<std::uint32_t>((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<std::uint32_t>((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<std::uint32_t>((octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3]);
|
||||
}
|
||||
|
||||
IPAddress IPAddress::LocalAddress() {
|
||||
|
||||
@@ -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<GLsizei>(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<GLsizei>(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<float>(m_IsometricView)) {
|
||||
float step = static_cast<float>(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<float>(width) / height);
|
||||
m_EntityShader->start();
|
||||
m_EntityShader->setAspectRatio((float)width / height);
|
||||
m_EntityShader->setAspectRatio(static_cast<float>(width) / height);
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
|
||||
@@ -42,8 +42,8 @@ VertexBuffer::~VertexBuffer() {
|
||||
VertexBuffer::VertexBuffer(const std::vector<float>& 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<GLsizeiptr>(data.size() * sizeof(float)), nullptr, GL_STATIC_DRAW);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(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<GLint>(pointer.m_Size), GL_FLOAT, false, m_DataStride * sizeof(float), reinterpret_cast<void*>(pointer.m_Offset));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<unsigned int>(getUniformLocation("aspectRatio"));
|
||||
location_zoom = static_cast<unsigned int>(getUniformLocation("zoom"));
|
||||
location_cam = static_cast<unsigned int>(getUniformLocation("camPos"));
|
||||
location_translation = static_cast<unsigned int>(getUniformLocation("translation"));
|
||||
location_viewtype = static_cast<unsigned int>(getUniformLocation("isometricView"));
|
||||
}
|
||||
|
||||
void EntityShader::setCamPos(const glm::vec2& camPos) {
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
@@ -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<GLint>(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<GLint>(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<GLint>(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<GLint>(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<GLint>(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<GLint>(location), value);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadMatrix(unsigned int location, const glm::mat4& matrix) const {
|
||||
glUniformMatrix4fv(static_cast<GLint>(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<unsigned int>(loadShaderFromFile(vertexFile, GL_VERTEX_SHADER));
|
||||
m_FragmentShaderID = static_cast<unsigned int>(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<unsigned int>(loadShader(vertexSource, GL_VERTEX_SHADER));
|
||||
m_FragmentShaderID = static_cast<unsigned int>(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<char> shaderError(static_cast<std::size_t>(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);
|
||||
}
|
||||
|
||||
@@ -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<unsigned int>(getUniformLocation("aspectRatio"));
|
||||
location_zoom = static_cast<unsigned int>(getUniformLocation("zoom"));
|
||||
location_cam = static_cast<unsigned int>(getUniformLocation("camPos"));
|
||||
location_viewtype = static_cast<unsigned int>(getUniformLocation("isometricView"));
|
||||
}
|
||||
|
||||
void WorldShader::setCamPos(const glm::vec2& camPos) {
|
||||
|
||||
Reference in New Issue
Block a user