fix: change renderer to class

This commit is contained in:
2021-09-18 18:58:32 +02:00
parent 519c6e33e7
commit a8b6a646af
10 changed files with 150 additions and 121 deletions

View File

@@ -6,11 +6,9 @@
*/
#include "render/Renderer.h"
#include "render/shaders/WorldShader.h"
#include "render/shaders/EntityShader.h"
#include <glbinding/gl/gl.h>
#include <stdio.h>
#include <glbinding/Binding.h>
#include <stdio.h>
#include "misc/Time.h"
#include "misc/Easing.h"
@@ -19,35 +17,32 @@ using namespace gl;
namespace td{
namespace render{
namespace Renderer{
Renderer::Renderer(){
#define ANIMATION_SPEED 2.0f
static std::unique_ptr<WorldShader> worldShader;
static std::unique_ptr<EntityShader> entityShader;
static bool isometricView = true;
static float isometricShade = isometricView;
static glm::vec2 camPos{};
void updateIsometricView(){
float isometricEased = utils::easeInOutExpo(isometricShade);
worldShader->start();
worldShader->setIsometricView(isometricEased);
entityShader->start();
entityShader->setIsometricView(isometricEased);
}
void initShader(){
worldShader = std::make_unique<WorldShader>();
worldShader->loadShader();
entityShader = std::make_unique<EntityShader>();
entityShader->loadShader();
Renderer::~Renderer(){
}
void Renderer::updateIsometricView(){
float isometricEased = utils::easeInOutExpo(m_IsometricShade);
m_WorldShader->start();
m_WorldShader->setIsometricView(isometricEased);
m_EntityShader->start();
m_EntityShader->setIsometricView(isometricEased);
}
void Renderer::initShader(){
m_WorldShader = std::make_unique<WorldShader>();
m_WorldShader->loadShader();
m_EntityShader = std::make_unique<EntityShader>();
m_EntityShader->loadShader();
setIsometricView(true);
updateIsometricView();
}
bool init(){
bool Renderer::init(){
glbinding::Binding::initialize();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
@@ -56,83 +51,78 @@ bool init(){
return true;
}
void destroy(){
worldShader.reset(0);
entityShader.reset(0);
}
void renderVAO(const GL::VAO& vao){
worldShader->start();
void Renderer::renderVAO(const GL::VAO& vao){
m_WorldShader->start();
vao.bind();
glDrawArrays(GL_TRIANGLES, 0, vao.getVertexCount());
vao.unbind();
}
void renderModel(const Model& model){
entityShader->start();
entityShader->setModelPos(model.positon);
void Renderer::renderModel(const Model& model){
m_EntityShader->start();
m_EntityShader->setModelPos(model.positon);
model.vao->bind();
glDrawArrays(GL_TRIANGLES, 0, model.vao->getVertexCount());
model.vao->unbind();
}
void updateIsometricFade(){
void Renderer::updateIsometricFade(){
static std::uint64_t lastTime = utils::getTime();
if(isometricShade != (float) isometricView){
float step = (float) (utils::getTime() - lastTime) / 1000.0f * ANIMATION_SPEED;
if(isometricShade < isometricView){
isometricShade += step;
if(m_IsometricShade != (float) m_IsometricView){
float step = (float) (utils::getTime() - lastTime) / 1000.0f * m_AnimationSpeed;
if(m_IsometricShade < m_IsometricView){
m_IsometricShade += step;
}else{
isometricShade -= step;
m_IsometricShade -= step;
}
isometricShade = std::min(isometricShade, 1.0f);
isometricShade = std::max(isometricShade, 0.0f);
m_IsometricShade = std::min(m_IsometricShade, 1.0f);
m_IsometricShade = std::max(m_IsometricShade, 0.0f);
updateIsometricView();
}
lastTime = utils::getTime();
}
void prepare(){
void Renderer::prepare(){
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 0, 0, 0);
updateIsometricFade();
}
void resize(int width, int height){
worldShader->start();
worldShader->setAspectRatio((float)width / height);
entityShader->start();
entityShader->setAspectRatio((float)width / height);
void Renderer::resize(int width, int height){
m_WorldShader->start();
m_WorldShader->setAspectRatio((float)width / height);
m_EntityShader->start();
m_EntityShader->setAspectRatio((float)width / height);
glViewport(0, 0, width, height);
}
void setZoom(float zoom){
worldShader->start();
worldShader->setZoom(zoom);
entityShader->start();
entityShader->setZoom(zoom);
void Renderer::setZoom(float zoom){
m_WorldShader->start();
m_WorldShader->setZoom(zoom);
m_EntityShader->start();
m_EntityShader->setZoom(zoom);
}
void setCamMovement(const glm::vec2& mov){
camPos.x += mov.x * (1 - isometricView) + (0.5 * mov.x - mov.y) * isometricView;
camPos.y += -mov.y * (1 - isometricView) + (-0.5 * mov.x - mov.y) * isometricView;
setCamPos(camPos);
void Renderer::setCamMovement(const glm::vec2& mov){
m_CamPos.x += mov.x * (1 - m_IsometricView) + (0.5 * mov.x - mov.y) * m_IsometricView;
m_CamPos.y += -mov.y * (1 - m_IsometricView) + (-0.5 * mov.x - mov.y) * m_IsometricView;
setCamPos(m_CamPos);
}
void setCamPos(const glm::vec2& newPos){
camPos = newPos;
worldShader->start();
worldShader->setCamPos(newPos);
entityShader->start();
entityShader->setCamPos(newPos);
void Renderer::setCamPos(const glm::vec2& newPos){
m_CamPos = newPos;
m_WorldShader->start();
m_WorldShader->setCamPos(newPos);
m_EntityShader->start();
m_EntityShader->setCamPos(newPos);
}
void setIsometricView(bool isometric){
isometricView = isometric;
void Renderer::setIsometricView(bool isometric){
m_IsometricView = isometric;
}
glm::vec2 getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight){
float isometricEased = utils::easeInOutExpo(isometricShade);
glm::vec2 Renderer::getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight){
float isometricEased = utils::easeInOutExpo(m_IsometricShade);
float relativeX = (cursorPos.x / windowWidth * 2) - 1;
float relativeY = (cursorPos.y / windowHeight * 2) - 1;
@@ -140,13 +130,12 @@ glm::vec2 getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float
float deltaX = relativeX * aspectRatio / zoom;
float deltaY = relativeY / zoom;
float worldX = camPos.x + deltaX * (1 - isometricEased) + (0.5 * deltaX + deltaY) * isometricEased;
float worldY = camPos.y + deltaY * (1 - isometricEased) + (-0.5 * deltaX + deltaY) * isometricEased;
float worldX = m_CamPos.x + deltaX * (1 - isometricEased) + (0.5 * deltaX + deltaY) * isometricEased;
float worldY = m_CamPos.y + deltaY * (1 - isometricEased) + (-0.5 * deltaX + deltaY) * isometricEased;
return {worldX, worldY};
}
} // namespace Renderer
} // namespace render
} // namespace td