nice
This commit is contained in:
123
src/td/render/shader/EntityShader.cpp
Normal file
123
src/td/render/shader/EntityShader.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#include <td/render/shader/EntityShader.h>
|
||||
|
||||
namespace td {
|
||||
namespace shader {
|
||||
|
||||
// TODO: update ES shaders
|
||||
|
||||
#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 vec2 textureCoords;
|
||||
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform vec3 modelPosition;
|
||||
|
||||
out vec2 pass_textureCoords;
|
||||
|
||||
void main(void){
|
||||
pass_textureCoords = textureCoords;
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
static const char fragmentSource[] = R"(
|
||||
#version 330
|
||||
|
||||
in vec2 pass_textureCoords;
|
||||
|
||||
out vec4 out_color;
|
||||
|
||||
uniform vec3 ColorEffect;
|
||||
uniform sampler2D textureSampler;
|
||||
|
||||
void main(void){
|
||||
|
||||
vec4 color = vec4(ColorEffect, 1.0) * texture(textureSampler, pass_textureCoords);
|
||||
|
||||
if (color.a <= 0.1)
|
||||
discard;
|
||||
|
||||
out_color = color;
|
||||
|
||||
}
|
||||
)";
|
||||
#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 td
|
||||
145
src/td/render/shader/ShaderProgram.cpp
Executable file
145
src/td/render/shader/ShaderProgram.cpp
Executable file
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* ShaderProgram.cpp
|
||||
*
|
||||
* Created on: 31 janv. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
#include <td/render/shader/ShaderProgram.h>
|
||||
#include <td/misc/Log.h>
|
||||
#include <td/misc/Format.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
namespace td {
|
||||
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 td
|
||||
102
src/td/render/shader/WorldShader.cpp
Normal file
102
src/td/render/shader/WorldShader.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include <td/render/shader/WorldShader.h>
|
||||
|
||||
namespace td {
|
||||
namespace shader {
|
||||
|
||||
// TODO: GLES Shaders
|
||||
|
||||
#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() {
|
||||
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 td
|
||||
Reference in New Issue
Block a user