nice
This commit is contained in:
26
src/td/render/Camera.cpp
Normal file
26
src/td/render/Camera.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <td/render/Camera.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
void Camera::UpdatePerspective(float a_AspectRatio) {
|
||||
m_ProjectionMatrix = maths::Perspective(80.0f / 180.0f * PI, a_AspectRatio, 0.1f, 160.0f);
|
||||
m_InvProjectionMatrix = maths::Inverse(m_ProjectionMatrix);
|
||||
}
|
||||
|
||||
void Camera::SetCamPos(const Vec3f& a_NewPos) {
|
||||
Vec3f front = {
|
||||
std::cos(m_Yaw) * std::cos(m_Pitch),
|
||||
std::sin(m_Pitch),
|
||||
std::sin(m_Yaw) * std::cos(m_Pitch)
|
||||
};
|
||||
|
||||
m_CamPos = a_NewPos;
|
||||
m_ViewMatrix = maths::Look(m_CamPos, front, { 0, 1, 0 });
|
||||
m_InvViewMatrix = maths::Transpose(maths::Inverse(m_ViewMatrix)); // why transpose ? I don't know
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
26
src/td/render/Renderer.cpp
Normal file
26
src/td/render/Renderer.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <td/render/Renderer.h>
|
||||
|
||||
#include <td/render/OpenGL.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
void Renderer::Render(const GL::VertexArray& a_Vao) {
|
||||
a_Vao.Bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, a_Vao.GetVertexCount());
|
||||
// glDrawElements(GL_TRIANGLES, a_Vao.GetVertexCount(), GL_UNSIGNED_INT, nullptr);
|
||||
a_Vao.Unbind();
|
||||
}
|
||||
|
||||
RenderPipeline::RenderPipeline() {
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnable(GL_BLEND);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_CULL_FACE);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glDepthFunc(GL_LESS);
|
||||
glFrontFace(GL_CCW);
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
99
src/td/render/loader/GLLoader.cpp
Normal file
99
src/td/render/loader/GLLoader.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include <td/render/loader/GLLoader.h>
|
||||
|
||||
#include <td/render/OpenGL.h>
|
||||
|
||||
namespace td {
|
||||
namespace GL {
|
||||
|
||||
VertexArray::~VertexArray() {
|
||||
if (m_ID != 0)
|
||||
glDeleteVertexArrays(1, &m_ID);
|
||||
}
|
||||
|
||||
VertexArray::VertexArray(ElementBuffer&& indicies) : m_ElementBuffer(std::move(indicies)) {
|
||||
glGenVertexArrays(1, &m_ID);
|
||||
Bind();
|
||||
BindElementArrayBuffer();
|
||||
// Unbind();
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
void VertexArray::BindElementArrayBuffer() {
|
||||
m_ElementBuffer.Bind();
|
||||
}
|
||||
|
||||
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 {
|
||||
.m_Index = index,
|
||||
.m_Size = coordinateSize,
|
||||
.m_Offset = offset
|
||||
};
|
||||
m_VertexAttribs.push_back(pointer);
|
||||
}
|
||||
|
||||
void VertexBuffer::BindVertexAttribs() const {
|
||||
for (const VertexAttribPointer& pointer : m_VertexAttribs) {
|
||||
glVertexAttribPointer(pointer.m_Index, static_cast<GLint>(pointer.m_Size), GL_FLOAT, false, m_DataStride * sizeof(float),
|
||||
reinterpret_cast<GLvoid*>(static_cast<std::size_t>(pointer.m_Offset)));
|
||||
glEnableVertexAttribArray(pointer.m_Index);
|
||||
}
|
||||
}
|
||||
|
||||
ElementBuffer::ElementBuffer(const std::vector<unsigned int>& indicies) {
|
||||
m_TriangleCount = indicies.size();
|
||||
glGenBuffers(1, &m_ID);
|
||||
Bind();
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast<GLsizeiptr>(indicies.size() * sizeof(unsigned int)), nullptr, GL_STATIC_DRAW);
|
||||
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(indicies.size() * sizeof(unsigned int)), indicies.data());
|
||||
Unbind();
|
||||
}
|
||||
|
||||
ElementBuffer::~ElementBuffer() {
|
||||
if (m_ID != 0)
|
||||
glDeleteBuffers(1, &m_ID);
|
||||
}
|
||||
|
||||
void ElementBuffer::Bind() const {
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ID);
|
||||
}
|
||||
|
||||
void ElementBuffer::Unbind() const {
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
} // namespace GL
|
||||
} // namespace td
|
||||
218
src/td/render/loader/WorldLoader.cpp
Normal file
218
src/td/render/loader/WorldLoader.cpp
Normal file
@@ -0,0 +1,218 @@
|
||||
#include <td/render/loader/WorldLoader.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
|
||||
#include <td/game/World.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
namespace WorldLoader {
|
||||
|
||||
const static int POSITION_VERTEX_SIZE = 3;
|
||||
const static int TEXTURE_VERTEX_SIZE = 2;
|
||||
|
||||
GL::VertexArray LoadWorldModel(const td::game::World* world) {
|
||||
std::vector<float> positions;
|
||||
std::vector<float> colors;
|
||||
|
||||
for (const auto& chunkInfo : world->GetChunks()) {
|
||||
const td::game::ChunkCoord& coords = chunkInfo.first;
|
||||
td::game::ChunkPtr chunk = chunkInfo.second;
|
||||
|
||||
std::int32_t chunkX = coords.x * td::game::Chunk::ChunkWidth;
|
||||
std::int32_t chunkY = coords.y * td::game::Chunk::ChunkHeight;
|
||||
|
||||
for (int tileY = 0; tileY < td::game::Chunk::ChunkHeight; tileY++) {
|
||||
for (int tileX = 0; tileX < td::game::Chunk::ChunkWidth; tileX++) {
|
||||
int tileNumber = tileY * td::game::Chunk::ChunkWidth + tileX;
|
||||
td::game::TileIndex tileIndex = chunk->GetTileIndex(tileNumber);
|
||||
td::game::TilePtr tile = world->GetTilePtr(tileIndex);
|
||||
|
||||
if (tile == nullptr)
|
||||
continue;
|
||||
|
||||
positions.insert(
|
||||
positions.end(), {static_cast<float>(chunkX + tileX + 1), 0, static_cast<float>(chunkY + tileY),
|
||||
static_cast<float>(chunkX + tileX), 0, static_cast<float>(chunkY + tileY),
|
||||
static_cast<float>(chunkX + tileX), 0, static_cast<float>(chunkY + tileY + 1),
|
||||
|
||||
static_cast<float>(chunkX + tileX + 1), 0, static_cast<float>(chunkY + tileY),
|
||||
static_cast<float>(chunkX + tileX), 0, static_cast<float>(chunkY + tileY + 1),
|
||||
static_cast<float>(chunkX + tileX + 1), 0, static_cast<float>(chunkY + tileY + 1)});
|
||||
|
||||
const td::Color* tileColor = world->GetTileColor(tile);
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
int color = 255;
|
||||
color |= tileColor->r << 24;
|
||||
color |= tileColor->g << 16;
|
||||
color |= tileColor->b << 8;
|
||||
|
||||
int newColorIndex = colors.size();
|
||||
colors.push_back(0);
|
||||
|
||||
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int spawnColor = 0; spawnColor < 2; spawnColor++) {
|
||||
const game::Spawn& spawn = world->GetTeam(TeamColor(spawnColor)).GetSpawn();
|
||||
float fromX = spawn.GetTopLeft().GetX(), toX = spawn.GetBottomRight().GetX();
|
||||
float fromY = spawn.GetTopLeft().GetY(), toY = spawn.GetBottomRight().GetY();
|
||||
|
||||
positions.insert(positions.end(), {fromX, 0, fromY, fromX, 0, toY, toX, 0, fromY, fromX, 0, toY, toX, 0, toY, toX, 0, fromY});
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
int color = 255;
|
||||
color |= world->GetSpawnColor(TeamColor(spawnColor)).r << 24;
|
||||
color |= world->GetSpawnColor(TeamColor(spawnColor)).g << 16;
|
||||
color |= world->GetSpawnColor(TeamColor(spawnColor)).b << 8;
|
||||
|
||||
int newColorIndex = colors.size();
|
||||
colors.push_back(0);
|
||||
|
||||
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
|
||||
}
|
||||
}
|
||||
|
||||
for (int castleColor = 0; castleColor < 2; castleColor++) {
|
||||
const game::TeamCastle& castle = world->GetTeam(TeamColor(castleColor)).GetCastle();
|
||||
float fromX = castle.GetTopLeft().GetX(), toX = castle.GetBottomRight().GetX();
|
||||
float fromY = castle.GetTopLeft().GetY(), toY = castle.GetBottomRight().GetY();
|
||||
|
||||
positions.insert(positions.end(), {fromX, 0, fromY, fromX, 0, toY, toX, 0, fromY, fromX, 0, toY, toX, 0, toY, toX, 0, fromY});
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
int color = 255;
|
||||
color |= world->GetSpawnColor(TeamColor(castleColor)).r << 24;
|
||||
color |= world->GetSpawnColor(TeamColor(castleColor)).g << 16;
|
||||
color |= world->GetSpawnColor(TeamColor(castleColor)).b << 8;
|
||||
|
||||
int newColorIndex = colors.size();
|
||||
colors.push_back(0);
|
||||
|
||||
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
|
||||
}
|
||||
}
|
||||
|
||||
GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE);
|
||||
positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0);
|
||||
GL::VertexBuffer colorVBO(colors, 1);
|
||||
colorVBO.AddVertexAttribPointer(1, 1, 0);
|
||||
|
||||
std::vector<unsigned int> indexes(positions.size() / 3, 0);
|
||||
for (size_t i = 0; i < indexes.size(); i++) {
|
||||
indexes[i] = i + 1;
|
||||
}
|
||||
|
||||
GL::ElementBuffer indexVBO(indexes);
|
||||
|
||||
GL::VertexArray worldVao(std::move(indexVBO)); // each pos = 3 vertecies
|
||||
worldVao.Bind();
|
||||
worldVao.BindVertexBuffer(std::move(positionVBO));
|
||||
worldVao.BindVertexBuffer(std::move(colorVBO));
|
||||
worldVao.Unbind();
|
||||
return worldVao;
|
||||
}
|
||||
|
||||
GL::VertexArray LoadTileSelectModel() {
|
||||
std::vector<float> positions = {
|
||||
-0.5f,
|
||||
-0.5f,
|
||||
-1.0f,
|
||||
|
||||
0.5f,
|
||||
-0.5f,
|
||||
-1.0f,
|
||||
|
||||
0.0f,
|
||||
0.5f,
|
||||
-1.0f,
|
||||
|
||||
1,
|
||||
.01,
|
||||
1,
|
||||
|
||||
0,
|
||||
.01,
|
||||
1,
|
||||
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
};
|
||||
|
||||
int color = 255 << 24 | 255 << 16 | 255 << 8 | 150;
|
||||
float colorFloat;
|
||||
|
||||
memcpy(reinterpret_cast<std::uint8_t*>(&colorFloat), &color, sizeof(float));
|
||||
|
||||
std::vector<float> colors(6, colorFloat);
|
||||
|
||||
GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE);
|
||||
positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0);
|
||||
GL::VertexBuffer colorVBO(colors, 1);
|
||||
colorVBO.AddVertexAttribPointer(1, 1, 0);
|
||||
|
||||
std::vector<unsigned int> indexes(positions.size() / 3, 0);
|
||||
for (size_t i = 0; i < indexes.size(); i++) {
|
||||
indexes[i] = i + 1;
|
||||
}
|
||||
GL::ElementBuffer indexVBO(indexes);
|
||||
|
||||
GL::VertexArray tileSelectVao(std::move(indexVBO));
|
||||
tileSelectVao.Bind();
|
||||
tileSelectVao.BindVertexBuffer(std::move(positionVBO));
|
||||
tileSelectVao.BindVertexBuffer(std::move(colorVBO));
|
||||
tileSelectVao.Unbind();
|
||||
|
||||
return tileSelectVao;
|
||||
}
|
||||
|
||||
RenderData LoadTowerModel(game::TowerPtr tower) {
|
||||
RenderData renderData;
|
||||
|
||||
float towerX, towerDX;
|
||||
float towerY, towerDY;
|
||||
|
||||
if (tower->GetSize() == game::TowerSize::Little) {
|
||||
towerX = tower->GetCenterX() - 1.5f;
|
||||
towerDX = tower->GetCenterX() + 1.5f;
|
||||
|
||||
towerY = tower->GetCenterY() - 1.5f;
|
||||
towerDY = tower->GetCenterY() + 1.5f;
|
||||
} else {
|
||||
towerX = tower->GetCenterX() - 2.5f;
|
||||
towerDX = tower->GetCenterX() + 2.5f;
|
||||
|
||||
towerY = tower->GetCenterY() - 2.5f;
|
||||
towerDY = tower->GetCenterY() + 2.5f;
|
||||
}
|
||||
std::vector<float> positions = {towerDX, 0.001, towerY, towerX, 0.001, towerY, towerX, 0.001, towerDY, towerDX, 0.001, towerY,
|
||||
towerX, 0.001, towerDY, towerDX, 0.001, towerDY};
|
||||
|
||||
renderData.positions = positions;
|
||||
|
||||
std::uint8_t towerType = static_cast<std::uint8_t>(tower->GetType());
|
||||
std::uint8_t r = 10 * towerType + 40, g = 5 * towerType + 30, b = 10 * towerType + 20;
|
||||
|
||||
float colorFloat;
|
||||
int color = r << 24 | g << 16 | b << 8 | 255;
|
||||
memcpy(&colorFloat, &color, sizeof(int));
|
||||
|
||||
std::vector<float> colors(6, colorFloat);
|
||||
renderData.colors = colors;
|
||||
|
||||
return renderData;
|
||||
}
|
||||
|
||||
|
||||
} // namespace WorldLoader
|
||||
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
26
src/td/render/renderer/WorldRenderer.cpp
Normal file
26
src/td/render/renderer/WorldRenderer.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <td/render/renderer/WorldRenderer.h>
|
||||
|
||||
#include <td/render/loader/WorldLoader.h>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
WorldRenderer::WorldRenderer(Camera& a_Camera, const game::World& a_World) : Renderer(a_Camera), m_World(a_World){
|
||||
m_WorldVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::LoadWorldModel(&a_World)));
|
||||
}
|
||||
|
||||
WorldRenderer::~WorldRenderer() {}
|
||||
|
||||
void WorldRenderer::Render() {
|
||||
m_Shader.Start();
|
||||
m_Shader.SetProjectionMatrix(m_Camera.GetProjectionMatrix());
|
||||
m_Shader.SetViewMatrix(m_Camera.GetViewMatrix());
|
||||
Renderer::Render(*m_WorldVao);
|
||||
ImGui::ShowDemoWindow();
|
||||
}
|
||||
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
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