123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #include "client/rendering/Engine.h"
- #include "client/rendering/Renderer.h"
- #include "gaming-core/wrapper/GL.h"
- Engine::Engine(Shaders& shaders, Framebuffers& fb, const Size& size,
- RenderSettings& renderSettings)
- : shaders(shaders), framebuffers(fb), size(size),
- renderSettings(renderSettings), frustum(60.0f, 0.1f, 1000.0f, size) {
- TypedBuffer<Triangle> buffer(2);
- buffer.add(
- Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
- Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f)),
- Vertex(Vector3(-1.0f, 1.0f, 0.0f), Vector2(0.0f, 1.0f))));
- buffer.add(
- Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
- Vertex(Vector3(1.0f, -1.0f, 0.0f), Vector2(1.0f, 0.0f)),
- Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f))));
- rectangle.build(buffer);
- }
- void Engine::renderTick(float lag, Game& game) {
- updateWorldProjection();
- updateWorldView();
- if(renderSettings.shadows) {
- renderShadow(lag, game);
- }
- renderWorld(lag, game);
- if(renderSettings.ssao) {
- renderSSAO();
- }
- renderPostWorld();
- renderTextOverlay(lag, game);
- }
- void Engine::renderShadow(float lag, Game& game) {
- framebuffers.shadow.bindAndClear();
- GL::enableDepthTesting();
- shaders.shadow.use();
- worldShadowProjView = worldShadowProj;
- worldShadowProjView *= worldShadowView;
- shaders.shadow.setMatrix("projView", worldShadowProjView.getValues());
- model.clear();
- shaders.shadow.setMatrix("model", model.peek().getValues());
- Renderer renderer(shaders.shadow, model, worldView);
- game.renderWorld(lag, renderer);
- }
- void Engine::renderWorld(float lag, Game& game) {
- framebuffers.world.bindAndClear();
- GL::enableDepthTesting();
- shaders.world.use();
- Matrix rWorldShadowProjView;
- rWorldShadowProjView.scale(0.5f).translate(Vector3(0.5f, 0.5f, 0.5f));
- rWorldShadowProjView *= worldShadowProjView;
- shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
- shaders.world.setMatrix("proj", worldProj.getValues());
- worldView = Matrix();
- shaders.world.setMatrix("view", worldView.getValues());
- model.clear();
- shaders.world.setMatrix("model", model.peek().getValues());
- framebuffers.shadow.bindTextureTo(0, 1);
- shaders.world.setInt("shadows", renderSettings.shadows);
- shaders.world.setFloat("radius", renderSettings.testRadius);
- shaders.world.setFloat("zbias", renderSettings.testBias);
- Renderer renderer(shaders.world, model, worldView);
- game.renderWorld(lag, renderer);
- }
- void Engine::renderSSAO() {
- shaders.ssao.use();
- Matrix rProj;
- rProj.scale(0.5f).translate(Vector3(0.5f, 0.5f, 0.5f));
- rProj *= worldProj;
- shaders.ssao.setMatrix("proj", rProj.getValues());
- shaders.ssao.setInt("width", size.width);
- shaders.ssao.setInt("height", size.height);
- framebuffers.world.bindTextureTo(0, 0);
- framebuffers.world.bindTextureTo(4, 1);
- ssaoNoise.bindTo(2);
- framebuffers.ssao.bindAndClear();
- rectangle.draw();
- shaders.ssaoBlur.use();
- framebuffers.ssao.bindTextureTo(0, 0);
- framebuffers.ssaoBlur.bindAndClear();
- rectangle.draw();
- }
- void Engine::renderPostWorld() {
- GL::bindMainFramebuffer();
- GL::clear();
- shaders.postWorld.use();
- framebuffers.world.bindTextureTo(2, 0);
- framebuffers.ssaoBlur.bindTextureTo(0, 1);
- framebuffers.world.bindTextureTo(3, 2);
- framebuffers.world.bindTextureTo(1, 3);
- shaders.postWorld.setInt("ssao", renderSettings.ssao);
- shaders.postWorld.setInt("shadows", renderSettings.shadows);
- rectangle.draw();
- }
- void Engine::renderTextOverlay(float lag, Game& game) {
- GL::disableDepthTesting();
- shaders.text.use();
- Matrix m;
- shaders.text.setMatrix("proj", m.getValues());
- m.scale(Vector3(2.0f / size.width, -2.0f / size.height, 1.0f))
- .translate(Vector3(-1.0f, 1.0f, 0.0f));
- shaders.text.setMatrix("view", m.getValues());
- model.clear();
- shaders.text.setMatrix("model", model.peek().getValues());
- GL::enableBlending();
- Renderer renderer(shaders.text, model, m);
- game.renderTextOverlay(lag, renderer, fontRenderer);
- GL::disableBlending();
- }
- void Engine::updateWorldProjection() {
- worldProj = frustum.updateProjection();
- if(!renderSettings.shadows) {
- return;
- }
- worldShadowProj.set(0, Vector4(2.0f / 40.0f, 0.0f, 0.0f, 0.0f));
- worldShadowProj.set(1, Vector4(0.0f, 2.0f / 30.0f, 0.0f, 0.0f));
- worldShadowProj.set(2, Vector4(0.0f, 0.0f, -2.0f / (1000.0f - 0.1f), 0.0f));
- worldShadowProj.set(3, Vector4(0.0f, 0.0f, 0.0f, 1.0f));
- }
- void Engine::updateWorldView() {
- if(!renderSettings.shadows) {
- return;
- }
- Vector3 right(0.939693f, 0.0f, -0.34202f);
- Vector3 back(0.280166f, 0.573576f, 0.769751f);
- Vector3 up(-0.196175f, 0.819152f, -0.538986f);
- Vector3 center(16.0f, 24.0f, 24.0f);
- worldShadowView.set(
- 0, Vector4(right[0], right[1], right[2], right.dot(-center)));
- worldShadowView.set(1, Vector4(up[0], up[1], up[2], up.dot(-center)));
- worldShadowView.set(2,
- Vector4(back[0], back[1], back[2], back.dot(-center)));
- worldShadowView.set(3, Vector4(0.0f, 0.0f, 0.0f, 1.0f));
- }
|