1er commit
This commit is contained in:
133
src/render/Renderer.cpp
Normal file
133
src/render/Renderer.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Renderer.cpp
|
||||
*
|
||||
* Created on: 4 nov. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
#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 "misc/Time.h"
|
||||
|
||||
using namespace gl;
|
||||
|
||||
namespace td{
|
||||
namespace render{
|
||||
|
||||
namespace 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(){
|
||||
worldShader->start();
|
||||
worldShader->setIsometricView(isometricShade);
|
||||
entityShader->start();
|
||||
entityShader->setIsometricView(isometricShade);
|
||||
}
|
||||
|
||||
void initShader(){
|
||||
worldShader = std::make_unique<WorldShader>();
|
||||
worldShader->loadShader();
|
||||
entityShader = std::make_unique<EntityShader>();
|
||||
entityShader->loadShader();
|
||||
setIsometricView(true);
|
||||
updateIsometricView();
|
||||
}
|
||||
|
||||
bool init(){
|
||||
glbinding::Binding::initialize();
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
initShader();
|
||||
return true;
|
||||
}
|
||||
|
||||
void destroy(){
|
||||
worldShader.reset(0);
|
||||
entityShader.reset(0);
|
||||
}
|
||||
|
||||
void renderVAO(const GL::VAO& vao){
|
||||
worldShader->start();
|
||||
vao.bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, vao.getVertexCount());
|
||||
vao.unbind();
|
||||
}
|
||||
|
||||
void renderModel(const Model& model){
|
||||
entityShader->start();
|
||||
entityShader->setModelPos(model.positon);
|
||||
model.vao->bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, model.vao->getVertexCount());
|
||||
model.vao->unbind();
|
||||
}
|
||||
|
||||
void 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;
|
||||
}else{
|
||||
isometricShade -= step;
|
||||
}
|
||||
isometricShade = std::min(isometricShade, 1.0f);
|
||||
isometricShade = std::max(isometricShade, 0.0f);
|
||||
updateIsometricView();
|
||||
}
|
||||
lastTime = utils::getTime();
|
||||
}
|
||||
|
||||
void 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);
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void setZoom(float zoom){
|
||||
worldShader->start();
|
||||
worldShader->setZoom(zoom);
|
||||
entityShader->start();
|
||||
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 setCamPos(const glm::vec2& newPos){
|
||||
camPos = newPos;
|
||||
worldShader->start();
|
||||
worldShader->setCamPos(newPos);
|
||||
entityShader->start();
|
||||
entityShader->setCamPos(newPos);
|
||||
}
|
||||
|
||||
void setIsometricView(bool isometric){
|
||||
isometricView = isometric;
|
||||
}
|
||||
|
||||
} // namespace Renderer
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user