add gl helper functions
This commit is contained in:
69
include/render/loader/GLLoader.h
Normal file
69
include/render/loader/GLLoader.h
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Defines.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace GL {
|
||||||
|
|
||||||
|
struct VertexAttribPointer {
|
||||||
|
unsigned int m_Index;
|
||||||
|
unsigned int m_Size;
|
||||||
|
unsigned int m_Offset;
|
||||||
|
};
|
||||||
|
|
||||||
|
class VertexBuffer {
|
||||||
|
private:
|
||||||
|
unsigned int m_ID, m_DataStride;
|
||||||
|
std::vector<VertexAttribPointer> m_VertexAttribs;
|
||||||
|
|
||||||
|
public:
|
||||||
|
VertexBuffer(VertexBuffer&& other) {
|
||||||
|
m_VertexAttribs = std::move(other.m_VertexAttribs);
|
||||||
|
m_ID = other.m_ID;
|
||||||
|
m_DataStride = other.m_DataStride;
|
||||||
|
other.m_ID = 0;
|
||||||
|
other.m_DataStride = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexBuffer(const std::vector<float>& data, unsigned int stride);
|
||||||
|
~VertexBuffer();
|
||||||
|
|
||||||
|
void Bind() const;
|
||||||
|
void Unbind() const;
|
||||||
|
void AddVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset);
|
||||||
|
void BindVertexAttribs() const;
|
||||||
|
|
||||||
|
REMOVE_COPY(VertexBuffer);
|
||||||
|
};
|
||||||
|
|
||||||
|
class VertexArray {
|
||||||
|
private:
|
||||||
|
unsigned int m_ID, m_VertexCount;
|
||||||
|
std::vector<VertexBuffer> m_VertexBuffers; // use to destroy vbos when become unused
|
||||||
|
public:
|
||||||
|
VertexArray(VertexArray&& other) {
|
||||||
|
m_ID = other.m_ID;
|
||||||
|
m_VertexCount = other.m_VertexCount;
|
||||||
|
m_VertexBuffers = std::move(other.m_VertexBuffers);
|
||||||
|
other.m_VertexCount = 0;
|
||||||
|
other.m_ID = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexArray(unsigned int vertexCount);
|
||||||
|
~VertexArray();
|
||||||
|
|
||||||
|
unsigned int GetVertexCount() const {
|
||||||
|
return m_VertexCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BindVertexBuffer(VertexBuffer& vbo);
|
||||||
|
void Bind() const;
|
||||||
|
void Unbind() const;
|
||||||
|
|
||||||
|
REMOVE_COPY(VertexArray);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace GL
|
||||||
|
} // namespace blitz
|
||||||
11
include/render/loader/TextureLoader.h
Normal file
11
include/render/loader/TextureLoader.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace TextureLoader {
|
||||||
|
|
||||||
|
unsigned int LoadGLTexture(const std::string& fileName);
|
||||||
|
|
||||||
|
} // namespace TextureLoader
|
||||||
|
} // namespace blitz
|
||||||
30
include/render/shader/EntityShader.h
Normal file
30
include/render/shader/EntityShader.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ShaderProgram.h"
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace shader {
|
||||||
|
|
||||||
|
class EntityShader : public ShaderProgram {
|
||||||
|
private:
|
||||||
|
unsigned int m_LocationProjectionMatrix = 0;
|
||||||
|
unsigned int m_LocationViewMatrix = 0;
|
||||||
|
unsigned int m_LocationPosition = 0;
|
||||||
|
unsigned int m_LocationColorEffect = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void GetAllUniformLocation();
|
||||||
|
|
||||||
|
public:
|
||||||
|
EntityShader();
|
||||||
|
|
||||||
|
void LoadShader();
|
||||||
|
|
||||||
|
void SetColorEffect(const Vec3f& color);
|
||||||
|
void SetProjectionMatrix(const Mat4f& proj) const;
|
||||||
|
void SetViewMatrix(const Mat4f& view) const;
|
||||||
|
void SetModelPos(const Vec3f& pos) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace shader
|
||||||
|
} // namespace blitz
|
||||||
42
include/render/shader/ShaderProgram.h
Executable file
42
include/render/shader/ShaderProgram.h
Executable file
@@ -0,0 +1,42 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Defines.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace shader {
|
||||||
|
|
||||||
|
class ShaderProgram {
|
||||||
|
public:
|
||||||
|
ShaderProgram();
|
||||||
|
virtual ~ShaderProgram();
|
||||||
|
|
||||||
|
void Start() const;
|
||||||
|
void Stop() const;
|
||||||
|
|
||||||
|
void LoadProgramFile(const std::string& vertexFile, const std::string& fragmentFile);
|
||||||
|
void LoadProgram(const std::string& vertexSource, const std::string& fragmentSource);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void GetAllUniformLocation() = 0;
|
||||||
|
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 Vec2f& vector) const;
|
||||||
|
void LoadVector(unsigned int location, const Vec3f& vector) const;
|
||||||
|
void LoadBoolean(unsigned int location, bool value) const;
|
||||||
|
void LoadMat4(unsigned int location, const Mat4f& mat) const;
|
||||||
|
void CleanUp() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
unsigned int m_ProgramID;
|
||||||
|
unsigned int m_VertexShaderID;
|
||||||
|
unsigned int m_FragmentShaderID;
|
||||||
|
|
||||||
|
unsigned int LoadShaderFromFile(const std::string& file, unsigned int type);
|
||||||
|
unsigned int LoadShader(const std::string& source, unsigned int type);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace shader
|
||||||
|
} // namespace blitz
|
||||||
24
include/render/shader/WorldShader.h
Normal file
24
include/render/shader/WorldShader.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ShaderProgram.h"
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace shader {
|
||||||
|
|
||||||
|
class WorldShader : public ShaderProgram {
|
||||||
|
private:
|
||||||
|
unsigned int m_LocationProjection = 0, m_LocationView = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void GetAllUniformLocation();
|
||||||
|
|
||||||
|
public:
|
||||||
|
WorldShader();
|
||||||
|
void LoadShader();
|
||||||
|
|
||||||
|
void SetProjectionMatrix(const Mat4f& proj) const;
|
||||||
|
void SetViewMatrix(const Mat4f& view) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace shader
|
||||||
|
} // namespace blitz
|
||||||
7559
libs/stb_image.h
Executable file
7559
libs/stb_image.h
Executable file
File diff suppressed because it is too large
Load Diff
68
src/render/loader/GLLoader.cpp
Normal file
68
src/render/loader/GLLoader.cpp
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#include "render/loader/GLLoader.h"
|
||||||
|
|
||||||
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace GL {
|
||||||
|
|
||||||
|
VertexArray::~VertexArray() {
|
||||||
|
if (m_ID != 0)
|
||||||
|
glDeleteVertexArrays(1, &m_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexArray::VertexArray(unsigned int vertexCount) : m_VertexCount(vertexCount) {
|
||||||
|
glGenVertexArrays(1, &m_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexArray::Bind() const {
|
||||||
|
glBindVertexArray(m_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexArray::Unbind() const {
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexArray::BindVertexBuffer(VertexBuffer& VertexBuffer) {
|
||||||
|
VertexBuffer.Bind();
|
||||||
|
VertexBuffer.BindVertexAttribs();
|
||||||
|
m_VertexBuffers.push_back(std::move(VertexBuffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexBuffer::~VertexBuffer() {
|
||||||
|
if (m_ID != 0)
|
||||||
|
glDeleteBuffers(1, &m_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexBuffer::VertexBuffer(const std::vector<float>& data, unsigned int stride) : m_DataStride(stride) {
|
||||||
|
glGenBuffers(1, &m_ID);
|
||||||
|
Bind();
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexBuffer::Bind() const {
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, m_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexBuffer::Unbind() const {
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexBuffer::AddVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset) {
|
||||||
|
VertexAttribPointer pointer;
|
||||||
|
pointer.m_Index = index;
|
||||||
|
pointer.m_Size = coordinateSize;
|
||||||
|
pointer.m_Offset = offset;
|
||||||
|
m_VertexAttribs.push_back(pointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexBuffer::BindVertexAttribs() const {
|
||||||
|
for (const VertexAttribPointer& pointer : m_VertexAttribs) {
|
||||||
|
glEnableVertexAttribArray(pointer.m_Index);
|
||||||
|
glVertexAttribPointer(pointer.m_Index, static_cast<GLint>(pointer.m_Size), GL_FLOAT, false, m_DataStride * sizeof(float),
|
||||||
|
reinterpret_cast<void*>(pointer.m_Offset));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // namespace GL
|
||||||
|
} // namespace blitz
|
||||||
41
src/render/loader/TextureLoader.cpp
Normal file
41
src/render/loader/TextureLoader.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include "render/loader/TextureLoader.h"
|
||||||
|
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include "misc/Log.h"
|
||||||
|
#include "stb_image.h"
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace TextureLoader {
|
||||||
|
|
||||||
|
unsigned int LoadGLTexture(const char* fileName) {
|
||||||
|
|
||||||
|
int width, height, comp;
|
||||||
|
|
||||||
|
const unsigned char* image = stbi_load(fileName, &width, &height, &comp, STBI_rgb_alpha);
|
||||||
|
|
||||||
|
if (image == nullptr) {
|
||||||
|
utils::LOGE("Erreur lors du chargement de la texture !");
|
||||||
|
throw(std::runtime_error("Failed to load texture"));
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint textureID;
|
||||||
|
glGenTextures(1, &textureID);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||||
|
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
|
if (comp == 3)
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
|
||||||
|
else if (comp == 4)
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
stbi_image_free((void*)image);
|
||||||
|
return textureID;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace TextureLoader
|
||||||
|
} // namespace blitz
|
||||||
120
src/render/shader/EntityShader.cpp
Normal file
120
src/render/shader/EntityShader.cpp
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
#include "render/shader/EntityShader.h"
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace shader {
|
||||||
|
|
||||||
|
#ifdef __ANDROID__
|
||||||
|
static const char vertexSource[] =
|
||||||
|
R"(#version 300 es
|
||||||
|
|
||||||
|
precision mediump float;
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 position;
|
||||||
|
layout(location = 1) in int color;
|
||||||
|
|
||||||
|
uniform mat4 viewMatrix;
|
||||||
|
uniform mat4 projectionMatrix;
|
||||||
|
uniform vec3 modelPosition;
|
||||||
|
|
||||||
|
flat out int pass_color;
|
||||||
|
|
||||||
|
void main(void){
|
||||||
|
pass_color = color;
|
||||||
|
gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0);
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
static const char fragmentSource[] =
|
||||||
|
R"(#version 300 es
|
||||||
|
|
||||||
|
precision mediump float;
|
||||||
|
|
||||||
|
flat in int pass_color;
|
||||||
|
|
||||||
|
out vec4 out_color;
|
||||||
|
|
||||||
|
uniform vec3 ColorEffect;
|
||||||
|
|
||||||
|
void main(void){
|
||||||
|
|
||||||
|
float r = float(pass_color >> 24 & 0xFF) / 255.0;
|
||||||
|
float g = float(pass_color >> 16 & 0xFF) / 255.0;
|
||||||
|
float b = float(pass_color >> 8 & 0xFF) / 255.0;
|
||||||
|
float a = float(pass_color & 0xFF) / 255.0;
|
||||||
|
vec3 intermediate_color = vec3(r, g, b) * ColorEffect;
|
||||||
|
out_color = vec4(intermediate_color, a);
|
||||||
|
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
#else
|
||||||
|
static const char vertexSource[] = R"(
|
||||||
|
#version 330
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 position;
|
||||||
|
layout(location = 1) in int color;
|
||||||
|
|
||||||
|
uniform mat4 viewMatrix;
|
||||||
|
uniform mat4 projectionMatrix;
|
||||||
|
uniform vec3 modelPosition;
|
||||||
|
|
||||||
|
flat out int pass_color;
|
||||||
|
|
||||||
|
void main(void){
|
||||||
|
pass_color = color;
|
||||||
|
gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0);
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
static const char fragmentSource[] = R"(
|
||||||
|
#version 330
|
||||||
|
|
||||||
|
flat in int pass_color;
|
||||||
|
|
||||||
|
out vec4 out_color;
|
||||||
|
|
||||||
|
uniform vec3 ColorEffect;
|
||||||
|
|
||||||
|
void main(void){
|
||||||
|
|
||||||
|
float r = float(pass_color >> 24 & 0xFF) / 255.0;
|
||||||
|
float g = float(pass_color >> 16 & 0xFF) / 255.0;
|
||||||
|
float b = float(pass_color >> 8 & 0xFF) / 255.0;
|
||||||
|
float a = float(pass_color & 0xFF) / 255.0;
|
||||||
|
vec3 intermediate_color = vec3(r, g, b) * ColorEffect;
|
||||||
|
out_color = vec4(intermediate_color, a);
|
||||||
|
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
EntityShader::EntityShader() : ShaderProgram() {}
|
||||||
|
|
||||||
|
void EntityShader::LoadShader() {
|
||||||
|
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityShader::GetAllUniformLocation() {
|
||||||
|
m_LocationColorEffect = static_cast<unsigned int>(GetUniformLocation("ColorEffect"));
|
||||||
|
m_LocationViewMatrix = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
|
||||||
|
m_LocationPosition = static_cast<unsigned int>(GetUniformLocation("modelPosition"));
|
||||||
|
m_LocationProjectionMatrix = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityShader::SetColorEffect(const Vec3f& color) {
|
||||||
|
LoadVector(m_LocationColorEffect, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityShader::SetProjectionMatrix(const Mat4f& proj) const {
|
||||||
|
LoadMat4(m_LocationProjectionMatrix, proj);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityShader::SetViewMatrix(const Mat4f& view) const {
|
||||||
|
LoadMat4(m_LocationViewMatrix, view);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityShader::SetModelPos(const Vec3f& pos) const {
|
||||||
|
LoadVector(m_LocationPosition, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace shader
|
||||||
|
} // namespace blitz
|
||||||
135
src/render/shader/ShaderProgram.cpp
Executable file
135
src/render/shader/ShaderProgram.cpp
Executable file
@@ -0,0 +1,135 @@
|
|||||||
|
#include "render/shader/ShaderProgram.h"
|
||||||
|
|
||||||
|
#include "misc/Format.h"
|
||||||
|
#include "misc/Log.h"
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace shader {
|
||||||
|
|
||||||
|
ShaderProgram::ShaderProgram() : m_ProgramID(0), m_VertexShaderID(0), m_FragmentShaderID(0) {}
|
||||||
|
|
||||||
|
ShaderProgram::~ShaderProgram() {
|
||||||
|
CleanUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::Start() const {
|
||||||
|
glUseProgram(m_ProgramID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::Stop() const {
|
||||||
|
glUseProgram(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ShaderProgram::GetUniformLocation(const std::string& uniformName) const {
|
||||||
|
const int location = glGetUniformLocation(m_ProgramID, uniformName.c_str());
|
||||||
|
if (location == -1) {
|
||||||
|
utils::LOGD(utils::Format("Warning ! Uniform variable %s not found !", uniformName.c_str()));
|
||||||
|
}
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::LoadFloat(unsigned int location, float value) const {
|
||||||
|
glUniform1f(static_cast<GLint>(location), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::LoadInt(unsigned int location, int value) const {
|
||||||
|
glUniform1i(static_cast<GLint>(location), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::LoadVector(unsigned int location, const Vec2f& vector) const {
|
||||||
|
glUniform2f(static_cast<GLint>(location), vector.x, vector.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::LoadVector(unsigned int location, const Vec3f& vector) const {
|
||||||
|
glUniform3f(static_cast<GLint>(location), vector.x, vector.y, vector.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::LoadBoolean(unsigned int location, bool value) const {
|
||||||
|
glUniform1i(static_cast<GLint>(location), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::LoadMat4(unsigned int location, const Mat4f& mat) const {
|
||||||
|
glUniformMatrix4fv(static_cast<GLint>(location), 1, false, reinterpret_cast<const float*>(&mat));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::CleanUp() const {
|
||||||
|
Stop();
|
||||||
|
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) {
|
||||||
|
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) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int ShaderProgram::LoadShader(const std::string& source, GLenum type) {
|
||||||
|
unsigned int shaderID = glCreateShader(type);
|
||||||
|
|
||||||
|
const char* c_str = source.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) {
|
||||||
|
GLsizei size;
|
||||||
|
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size);
|
||||||
|
std::vector<char> shaderError(static_cast<std::size_t>(size));
|
||||||
|
glGetShaderInfoLog(shaderID, size, &size, shaderError.data());
|
||||||
|
|
||||||
|
utils::LOGE("Could not compile shader !");
|
||||||
|
|
||||||
|
utils::LOGE(shaderError.data());
|
||||||
|
|
||||||
|
utils::LOGD(utils::Format("\nShader source : \n"
|
||||||
|
"---------------------------------------------------------------------------------------------------"
|
||||||
|
"---------------------------------\n"
|
||||||
|
"%s\n"
|
||||||
|
"---------------------------------------------------------------------------------------------------"
|
||||||
|
"---------------------------------\n",
|
||||||
|
source.c_str()));
|
||||||
|
}
|
||||||
|
return shaderID;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int ShaderProgram::LoadShaderFromFile(const std::string& file, GLenum type) {
|
||||||
|
std::stringstream stream;
|
||||||
|
std::ifstream fileStream(file);
|
||||||
|
|
||||||
|
if (fileStream) {
|
||||||
|
stream << fileStream.rdbuf();
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return LoadShader(stream.str(), type);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace shader
|
||||||
|
} // namespace blitz
|
||||||
102
src/render/shader/WorldShader.cpp
Normal file
102
src/render/shader/WorldShader.cpp
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
#include "render/shader/WorldShader.h"
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace shader {
|
||||||
|
|
||||||
|
#ifdef __ANDROID__
|
||||||
|
static const char vertexSource[] =
|
||||||
|
R"(#version 300 es
|
||||||
|
|
||||||
|
precision mediump float;
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 position;
|
||||||
|
layout(location = 1) in int color;
|
||||||
|
|
||||||
|
uniform mat4 viewMatrix;
|
||||||
|
uniform mat4 projectionMatrix;
|
||||||
|
|
||||||
|
flat out int pass_color;
|
||||||
|
|
||||||
|
void main(void){
|
||||||
|
pass_color = color;
|
||||||
|
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
static const char fragmentSource[] =
|
||||||
|
R"(#version 300 es
|
||||||
|
|
||||||
|
precision mediump float;
|
||||||
|
|
||||||
|
flat in int pass_color;
|
||||||
|
|
||||||
|
out vec4 out_color;
|
||||||
|
|
||||||
|
void main(void){
|
||||||
|
|
||||||
|
float r = float(pass_color >> 24 & 0xFF) / 255.0;
|
||||||
|
float g = float(pass_color >> 16 & 0xFF) / 255.0;
|
||||||
|
float b = float(pass_color >> 8 & 0xFF) / 255.0;
|
||||||
|
float a = float(pass_color & 0xFF) / 255.0;
|
||||||
|
out_color = vec4(r, g, b, a);
|
||||||
|
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
#else
|
||||||
|
static const char vertexSource[] = R"(
|
||||||
|
#version 330
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 position;
|
||||||
|
layout(location = 1) in int color;
|
||||||
|
|
||||||
|
uniform mat4 viewMatrix;
|
||||||
|
uniform mat4 projectionMatrix;
|
||||||
|
|
||||||
|
flat out int pass_color;
|
||||||
|
|
||||||
|
void main(void){
|
||||||
|
pass_color = color;
|
||||||
|
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
static const char fragmentSource[] = R"(
|
||||||
|
#version 330
|
||||||
|
|
||||||
|
flat in int pass_color;
|
||||||
|
|
||||||
|
out vec4 out_color;
|
||||||
|
|
||||||
|
void main(void){
|
||||||
|
|
||||||
|
float r = float(pass_color >> 24 & 0xFF) / 255.0;
|
||||||
|
float g = float(pass_color >> 16 & 0xFF) / 255.0;
|
||||||
|
float b = float(pass_color >> 8 & 0xFF) / 255.0;
|
||||||
|
float a = float(pass_color & 0xFF) / 255.0;
|
||||||
|
out_color = vec4(r, g, b, a);
|
||||||
|
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
WorldShader::WorldShader() : ShaderProgram() {}
|
||||||
|
|
||||||
|
void WorldShader::LoadShader() {
|
||||||
|
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WorldShader::GetAllUniformLocation() {
|
||||||
|
m_LocationProjection = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
|
||||||
|
m_LocationView = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void WorldShader::SetProjectionMatrix(const Mat4f& proj) const {
|
||||||
|
LoadMat4(m_LocationProjection, proj);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WorldShader::SetViewMatrix(const Mat4f& view) const {
|
||||||
|
LoadMat4(m_LocationView, view);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace shader
|
||||||
|
} // namespace blitz
|
||||||
@@ -12,7 +12,7 @@ target("Blitz")
|
|||||||
-- Libraries
|
-- Libraries
|
||||||
add_packages("libsdl", "zlib", "glew")
|
add_packages("libsdl", "zlib", "glew")
|
||||||
|
|
||||||
add_includedirs("libs/imgui")
|
add_includedirs("libs", "libs/imgui")
|
||||||
add_files("libs/imgui/**.cpp")
|
add_files("libs/imgui/**.cpp")
|
||||||
|
|
||||||
-- Assets
|
-- Assets
|
||||||
|
|||||||
Reference in New Issue
Block a user