shaders to 3d
This commit is contained in:
@@ -6,13 +6,10 @@ namespace td {
|
||||
namespace shader {
|
||||
|
||||
class EntityShader : public ShaderProgram {
|
||||
|
||||
private:
|
||||
unsigned int m_LocationCam = 0;
|
||||
unsigned int m_LocationZoom = 0;
|
||||
unsigned int m_LocationAspectRatio = 0;
|
||||
unsigned int m_LocationTranslation = 0;
|
||||
unsigned int m_LocationViewtype = 0;
|
||||
unsigned int m_LocationProjectionMatrix = 0;
|
||||
unsigned int m_LocationViewMatrix = 0;
|
||||
unsigned int m_LocationPosition = 0;
|
||||
unsigned int m_LocationColorEffect = 0;
|
||||
protected:
|
||||
virtual void GetAllUniformLocation();
|
||||
@@ -20,12 +17,11 @@ public:
|
||||
EntityShader();
|
||||
|
||||
void LoadShader();
|
||||
void SetCamPos(const Vec2f& camPos);
|
||||
void SetZoom(float zoom);
|
||||
void SetAspectRatio(float aspectRatio);
|
||||
void SetModelPos(const Vec2f& modelPos);
|
||||
void SetIsometricView(float isometric);
|
||||
|
||||
void SetColorEffect(const Vec3f& color);
|
||||
void SetProjectionMatrix(const maths::Mat4f& proj) const;
|
||||
void SetViewMatrix(const maths::Mat4f& view) const;
|
||||
void SetModelPos(const Vec2f& pos) const;
|
||||
};
|
||||
|
||||
} // namespace shader
|
||||
|
||||
@@ -7,16 +7,15 @@ namespace shader {
|
||||
|
||||
class WorldShader : public ShaderProgram {
|
||||
private:
|
||||
unsigned int m_LocationCam = 0, m_LocationZoom = 0, m_LocationAspectRatio = 0, m_LocationViewtype = 0;
|
||||
unsigned int m_LocationProjection = 0, m_LocationView = 0;
|
||||
protected:
|
||||
void GetAllUniformLocation();
|
||||
public:
|
||||
WorldShader();
|
||||
void LoadShader();
|
||||
void SetCamPos(const Vec2f& camPos);
|
||||
void SetZoom(float zoom);
|
||||
void SetAspectRatio(float aspectRatio);
|
||||
void SetIsometricView(float isometric);
|
||||
|
||||
void SetProjectionMatrix(const maths::Mat4f& proj) const;
|
||||
void SetViewMatrix(const maths::Mat4f& view) const;
|
||||
};
|
||||
|
||||
} // namespace shader
|
||||
|
||||
@@ -23,11 +23,11 @@ Renderer::~Renderer() {
|
||||
}
|
||||
|
||||
void Renderer::UpdateIsometricView() {
|
||||
float isometricEased = utils::EaseInOutExpo(m_IsometricShade);
|
||||
//float isometricEased = utils::EaseInOutExpo(m_IsometricShade);
|
||||
m_WorldShader->Start();
|
||||
m_WorldShader->SetIsometricView(isometricEased);
|
||||
//m_WorldShader->SetIsometricView(isometricEased);
|
||||
m_EntityShader->Start();
|
||||
m_EntityShader->SetIsometricView(isometricEased);
|
||||
//m_EntityShader->SetIsometricView(isometricEased);
|
||||
}
|
||||
|
||||
void Renderer::InitShaders() {
|
||||
@@ -69,7 +69,7 @@ void Renderer::RenderVAO(const GL::VertexArray& vao) {
|
||||
|
||||
void Renderer::RenderModel(const Model& model) {
|
||||
m_EntityShader->Start();
|
||||
m_EntityShader->SetModelPos(model.positon);
|
||||
//m_EntityShader->SetModelPos(model.positon);
|
||||
m_EntityShader->SetColorEffect(model.color);
|
||||
model.vao->Bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(model.vao->GetVertexCount()));
|
||||
@@ -100,17 +100,17 @@ void Renderer::Prepare() {
|
||||
|
||||
void Renderer::Resize(int width, int height) {
|
||||
m_WorldShader->Start();
|
||||
m_WorldShader->SetAspectRatio(static_cast<float>(width) / height);
|
||||
//m_WorldShader->SetAspectRatio(static_cast<float>(width) / height);
|
||||
m_EntityShader->Start();
|
||||
m_EntityShader->SetAspectRatio(static_cast<float>(width) / height);
|
||||
//m_EntityShader->SetAspectRatio(static_cast<float>(width) / height);
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void Renderer::SetZoom(float zoom) {
|
||||
m_WorldShader->Start();
|
||||
m_WorldShader->SetZoom(zoom);
|
||||
//m_WorldShader->SetZoom(zoom);
|
||||
m_EntityShader->Start();
|
||||
m_EntityShader->SetZoom(zoom);
|
||||
//m_EntityShader->SetZoom(zoom);
|
||||
}
|
||||
|
||||
void Renderer::SetCamMovement(const Vec2f& mov) {
|
||||
@@ -122,9 +122,9 @@ void Renderer::SetCamMovement(const Vec2f& mov) {
|
||||
void Renderer::SetCamPos(const Vec2f& newPos) {
|
||||
m_CamPos = newPos;
|
||||
m_WorldShader->Start();
|
||||
m_WorldShader->SetCamPos(newPos);
|
||||
//m_WorldShader->SetCamPos(newPos);
|
||||
m_EntityShader->Start();
|
||||
m_EntityShader->SetCamPos(newPos);
|
||||
//m_EntityShader->SetCamPos(newPos);
|
||||
}
|
||||
|
||||
void Renderer::SetIsometricView(bool isometric) {
|
||||
|
||||
@@ -9,24 +9,18 @@ R"(#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
layout(location = 0) in vec2 position;
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in int color;
|
||||
|
||||
uniform vec2 camPos;
|
||||
uniform float zoom;
|
||||
uniform float aspectRatio;
|
||||
uniform vec2 translation;
|
||||
uniform float isometricView;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform vec3 modelPosition;
|
||||
|
||||
flat out int pass_color;
|
||||
|
||||
void main(void){
|
||||
float modelX = position.x + translation.x;
|
||||
float modelY = position.y + translation.y;
|
||||
float x = (modelX - camPos.x - (modelY - camPos.y) * isometricView) / aspectRatio * zoom;
|
||||
float y = ((0.5 * (modelX - camPos.x) + 0.5 * (modelY - camPos.y)) * isometricView + (modelY - camPos.y) * (1.0 - isometricView)) * zoom;
|
||||
pass_color = color;
|
||||
gl_Position = vec4(x, -y, 0.0, 1.0);
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -56,24 +50,18 @@ void main(void){
|
||||
static const char vertexSource[] = R"(
|
||||
#version 330
|
||||
|
||||
layout(location = 0) in vec2 position;
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in int color;
|
||||
|
||||
uniform vec2 camPos;
|
||||
uniform float zoom;
|
||||
uniform float aspectRatio;
|
||||
uniform vec2 translation;
|
||||
uniform float isometricView;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform vec3 modelPosition;
|
||||
|
||||
flat out int pass_color;
|
||||
|
||||
void main(void){
|
||||
float modelX = position.x + translation.x;
|
||||
float modelY = position.y + translation.y;
|
||||
float x = (modelX - camPos.x - (modelY - camPos.y) * isometricView) / aspectRatio * zoom;
|
||||
float y = ((0.5 * (modelX - camPos.x) + 0.5 * (modelY - camPos.y)) * isometricView + (modelY - camPos.y) * (1.0 - isometricView)) * zoom;
|
||||
pass_color = color;
|
||||
gl_Position = vec4(x, -y, 0.0, 1.0);
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -106,32 +94,27 @@ void EntityShader::LoadShader() {
|
||||
}
|
||||
|
||||
void EntityShader::GetAllUniformLocation() {
|
||||
m_LocationAspectRatio = static_cast<unsigned int>(GetUniformLocation("aspectRatio"));
|
||||
m_LocationZoom = static_cast<unsigned int>(GetUniformLocation("zoom"));
|
||||
m_LocationCam = static_cast<unsigned int>(GetUniformLocation("camPos"));
|
||||
m_LocationTranslation = static_cast<unsigned int>(GetUniformLocation("translation"));
|
||||
m_LocationViewtype = static_cast<unsigned int>(GetUniformLocation("isometricView"));
|
||||
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::SetCamPos(const Vec2f& camPos) {
|
||||
LoadVector(m_LocationCam, camPos);
|
||||
}
|
||||
void EntityShader::SetZoom(float zoom) {
|
||||
LoadFloat(m_LocationZoom, zoom);
|
||||
}
|
||||
void EntityShader::SetAspectRatio(float aspectRatio) {
|
||||
LoadFloat(m_LocationAspectRatio, aspectRatio);
|
||||
}
|
||||
void EntityShader::SetModelPos(const Vec2f& modelPos) {
|
||||
LoadVector(m_LocationTranslation, modelPos);
|
||||
}
|
||||
void EntityShader::SetIsometricView(float isometric) {
|
||||
LoadFloat(m_LocationViewtype, isometric);
|
||||
}
|
||||
void EntityShader::SetColorEffect(const Vec3f& color) {
|
||||
LoadVector(m_LocationColorEffect, color);
|
||||
}
|
||||
|
||||
void EntityShader::SetProjectionMatrix(const maths::Mat4f& proj) const {
|
||||
LoadMat4(m_LocationProjectionMatrix, proj);
|
||||
}
|
||||
|
||||
void EntityShader::SetViewMatrix(const maths::Mat4f& view) const {
|
||||
LoadMat4(m_LocationViewMatrix, view);
|
||||
}
|
||||
|
||||
void EntityShader::SetModelPos(const Vec2f& pos) const {
|
||||
LoadVector(m_LocationPosition, pos);
|
||||
}
|
||||
|
||||
} // namespace shader
|
||||
} // namespace td
|
||||
@@ -3,27 +3,25 @@
|
||||
namespace td {
|
||||
namespace shader {
|
||||
|
||||
// TODO: GLES Shaders
|
||||
|
||||
#ifdef __ANDROID__
|
||||
static const char vertexSource[] =
|
||||
R"(#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
layout(location = 0) in vec2 position;
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in int color;
|
||||
|
||||
uniform vec2 camPos;
|
||||
uniform float zoom;
|
||||
uniform float aspectRatio;
|
||||
uniform float isometricView;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
|
||||
flat out int pass_color;
|
||||
|
||||
void main(void){
|
||||
float x = (position.x - camPos.x - (position.y - camPos.y) * isometricView) / aspectRatio * zoom;
|
||||
float y = ((0.5 * (position.x - camPos.x) + 0.5 * (position.y - camPos.y)) * isometricView + (position.y - camPos.y) * (1.0 - isometricView)) * zoom;
|
||||
pass_color = color;
|
||||
gl_Position = vec4(x, -y, 0.0, 1.0);
|
||||
pass_color = color;
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -50,21 +48,17 @@ void main(void){
|
||||
static const char vertexSource[] = R"(
|
||||
#version 330
|
||||
|
||||
layout(location = 0) in vec2 position;
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in int color;
|
||||
|
||||
uniform vec2 camPos;
|
||||
uniform float zoom;
|
||||
uniform float aspectRatio;
|
||||
uniform float isometricView;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
|
||||
flat out int pass_color;
|
||||
|
||||
void main(void){
|
||||
float x = (position.x - camPos.x - (position.y - camPos.y) * isometricView) / aspectRatio * zoom;
|
||||
float y = ((0.5 * (position.x - camPos.x) + 0.5 * (position.y - camPos.y)) * isometricView + (position.y - camPos.y) * (1.0 - isometricView)) * zoom;
|
||||
pass_color = color;
|
||||
gl_Position = vec4(x, -y, 0.0, 1.0);
|
||||
pass_color = color;
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -94,23 +88,16 @@ void WorldShader::LoadShader() {
|
||||
}
|
||||
|
||||
void WorldShader::GetAllUniformLocation() {
|
||||
m_LocationAspectRatio = static_cast<unsigned int>(GetUniformLocation("aspectRatio"));
|
||||
m_LocationZoom = static_cast<unsigned int>(GetUniformLocation("zoom"));
|
||||
m_LocationCam = static_cast<unsigned int>(GetUniformLocation("camPos"));
|
||||
m_LocationViewtype = static_cast<unsigned int>(GetUniformLocation("isometricView"));
|
||||
m_LocationProjection = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
|
||||
m_LocationView = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
|
||||
}
|
||||
|
||||
void WorldShader::SetCamPos(const Vec2f& camPos) {
|
||||
LoadVector(m_LocationCam, camPos);
|
||||
void WorldShader::SetProjectionMatrix(const maths::Mat4f& proj) const {
|
||||
LoadMat4(m_LocationProjection, proj);
|
||||
}
|
||||
void WorldShader::SetZoom(float zoom) {
|
||||
LoadFloat(m_LocationZoom, zoom);
|
||||
}
|
||||
void WorldShader::SetAspectRatio(float aspectRatio) {
|
||||
LoadFloat(m_LocationAspectRatio, aspectRatio);
|
||||
}
|
||||
void WorldShader::SetIsometricView(float isometric) {
|
||||
LoadFloat(m_LocationViewtype, isometric);
|
||||
|
||||
void WorldShader::SetViewMatrix(const maths::Mat4f& view) const {
|
||||
LoadMat4(m_LocationView, view);
|
||||
}
|
||||
|
||||
} // namespace shader
|
||||
|
||||
Reference in New Issue
Block a user