|
@@ -7,9 +7,8 @@
|
|
|
#include "client/rendering/Renderer.h"
|
|
|
#include "client/math/Plane.h"
|
|
|
|
|
|
-Engine::Engine(Shaders& shaders, Framebuffers& fb, Camera& camera, const WindowSize& size,
|
|
|
- RenderSettings& renderSettings) :
|
|
|
-shaders(shaders), fb(fb), camera(camera), size(size), renderSettings(renderSettings), frustum(60.0f, 0.1f, 80.0f) {
|
|
|
+Engine::Engine(Shaders& shaders, Framebuffers& fb, const WindowSize& size, RenderSettings& renderSettings) :
|
|
|
+shaders(shaders), fb(fb), size(size), renderSettings(renderSettings), frustum(60.0f, 0.1f, 80.0f) {
|
|
|
rectangle.add( {-1, -1, 0, 0, 0, 0, 0, 0});
|
|
|
rectangle.add( {1, 1, 0, 1, 1, 0, 0, 0});
|
|
|
rectangle.add( {-1, 1, 0, 0, 1, 0, 0, 0});
|
|
@@ -20,7 +19,6 @@ shaders(shaders), fb(fb), camera(camera), size(size), renderSettings(renderSetti
|
|
|
}
|
|
|
|
|
|
void Engine::renderTick(float lag, const Game& game) {
|
|
|
- camera.update(lag);
|
|
|
updateWorldProjection();
|
|
|
updateWorldView();
|
|
|
|
|
@@ -41,7 +39,7 @@ void Engine::renderShadow(float lag, const Game& game) {
|
|
|
shaders.shadow.use();
|
|
|
worldShadowProjView.set(worldShadowProj).mul(worldShadowView);
|
|
|
shaders.shadow.setMatrix("projView", worldShadowProjView.getValues());
|
|
|
- Renderer renderer(shaders.shadow, model);
|
|
|
+ Renderer renderer(shaders.shadow, model, worldView);
|
|
|
game.renderWorld(lag, renderer);
|
|
|
}
|
|
|
|
|
@@ -55,14 +53,14 @@ void Engine::renderWorld(float lag, const Game& game) {
|
|
|
|
|
|
shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
|
|
|
shaders.world.setMatrix("proj", worldProj.getValues());
|
|
|
- shaders.world.setMatrix("view", worldView.getValues());
|
|
|
+ shaders.world.setMatrix("view", worldView.setToIdentity().getValues());
|
|
|
model.clear();
|
|
|
shaders.world.setMatrix("model", model.get().getValues());
|
|
|
fb.shadow.bindDepthTexture(1);
|
|
|
shaders.world.setInt("shadows", renderSettings.shadows);
|
|
|
shaders.world.setFloat("radius", renderSettings.testRadius);
|
|
|
shaders.world.setFloat("bias", renderSettings.testBias);
|
|
|
- Renderer renderer(shaders.world, model);
|
|
|
+ Renderer renderer(shaders.world, model, worldView);
|
|
|
game.renderWorld(lag, renderer);
|
|
|
}
|
|
|
|
|
@@ -110,11 +108,10 @@ void Engine::renderTextOverlay(float lag, const Game& game) {
|
|
|
m.translate(-1.0f, 1.0f, 0.0f).scale(2.0f / size.width, -2.0f / size.height, 1.0f);
|
|
|
shaders.text.setMatrix("view", m.getValues());
|
|
|
model.clear();
|
|
|
- model.get().scale(2.0f, 2.0f, 2.0f);
|
|
|
shaders.text.setMatrix("model", model.get().getValues());
|
|
|
|
|
|
GLWrapper::enableBlending();
|
|
|
- Renderer renderer(shaders.text, model);
|
|
|
+ Renderer renderer(shaders.text, model, m);
|
|
|
game.renderTextOverlay(lag, renderer, fontRenderer);
|
|
|
GLWrapper::disableBlending();
|
|
|
}
|
|
@@ -125,153 +122,21 @@ void Engine::updateWorldProjection() {
|
|
|
if(!renderSettings.shadows) {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- // http://cgvr.informatik.uni-bremen.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
|
|
|
- const float fovY = 60.0f;
|
|
|
- const float nearClip = 0.1f;
|
|
|
- const float farClip = 80.0f;
|
|
|
-
|
|
|
- float tan = tanf((0.5f * fovY) * M_PI / 180.0f);
|
|
|
- float aspect = (float) size.width / size.height;
|
|
|
-
|
|
|
- float closeFarClip = 16;
|
|
|
- float nearHigh = tan * nearClip;
|
|
|
- float nearWidth = nearHigh * aspect;
|
|
|
- float farHigh = tan * closeFarClip;
|
|
|
- float farWidth = farHigh * aspect;
|
|
|
-
|
|
|
- Vector farCenter = camera.getPosition();
|
|
|
- farCenter.addMul(camera.getBack(), -closeFarClip);
|
|
|
-
|
|
|
- Vector farTopLeft = farCenter;
|
|
|
- farTopLeft.addMul(camera.getRight(), -farWidth).addMul(camera.getUp(), farHigh);
|
|
|
-
|
|
|
- Vector farBottomLeft = farCenter;
|
|
|
- farBottomLeft.addMul(camera.getRight(), -farWidth).addMul(camera.getUp(), -farHigh);
|
|
|
-
|
|
|
- Vector farTopRight = farCenter;
|
|
|
- farTopRight.addMul(camera.getRight(), farWidth).addMul(camera.getUp(), farHigh);
|
|
|
-
|
|
|
- Vector farBottomRight = farCenter;
|
|
|
- farBottomRight.addMul(camera.getRight(), farWidth).addMul(camera.getUp(), -farHigh);
|
|
|
-
|
|
|
- Vector nearCenter = camera.getPosition();
|
|
|
- nearCenter.addMul(camera.getBack(), -nearClip);
|
|
|
-
|
|
|
- Vector nearTopLeft = nearCenter;
|
|
|
- nearTopLeft.addMul(camera.getRight(), -nearWidth).addMul(camera.getUp(), nearHigh);
|
|
|
-
|
|
|
- Vector nearBottomLeft = nearCenter;
|
|
|
- nearBottomLeft.addMul(camera.getRight(), -nearWidth).addMul(camera.getUp(), -nearHigh);
|
|
|
-
|
|
|
- Vector nearTopRight = nearCenter;
|
|
|
- nearTopRight.addMul(camera.getRight(), nearWidth).addMul(camera.getUp(), nearHigh);
|
|
|
-
|
|
|
- Vector nearBottomRight = nearCenter;
|
|
|
- nearBottomRight.addMul(camera.getRight(), nearWidth).addMul(camera.getUp(), -nearHigh);
|
|
|
-
|
|
|
- Vector light(-0.280166, -0.573576, -0.769751);
|
|
|
- Vector lightLeft = light;
|
|
|
- lightLeft.cross(0.0f, 1.0f, 0.0f);
|
|
|
- Vector lightUp = lightLeft;
|
|
|
- lightUp.cross(light);
|
|
|
-
|
|
|
- Plane plane;
|
|
|
- plane.set(Vector(), light, lightUp);
|
|
|
- float f[8];
|
|
|
- f[0] = plane.getSignedDistance(farTopLeft);
|
|
|
- f[1] = plane.getSignedDistance(farBottomLeft);
|
|
|
- f[2] = plane.getSignedDistance(farTopRight);
|
|
|
- f[3] = plane.getSignedDistance(farBottomRight);
|
|
|
- f[4] = plane.getSignedDistance(nearTopLeft);
|
|
|
- f[5] = plane.getSignedDistance(nearBottomLeft);
|
|
|
- f[6] = plane.getSignedDistance(nearTopRight);
|
|
|
- f[7] = plane.getSignedDistance(nearBottomRight);
|
|
|
- float min = FLT_MAX;
|
|
|
- float max = -FLT_MAX;
|
|
|
- for(uint i = 0; i < 8; i++) {
|
|
|
- if(f[i] < min) {
|
|
|
- min = f[i];
|
|
|
- }
|
|
|
- if(f[i] > max) {
|
|
|
- max = f[i];
|
|
|
- }
|
|
|
- }
|
|
|
- float lightWidth = max - min;
|
|
|
-
|
|
|
- plane.set(Vector(), light, lightLeft);
|
|
|
- f[0] = plane.getSignedDistance(farTopLeft);
|
|
|
- f[1] = plane.getSignedDistance(farBottomLeft);
|
|
|
- f[2] = plane.getSignedDistance(farTopRight);
|
|
|
- f[3] = plane.getSignedDistance(farBottomRight);
|
|
|
- f[4] = plane.getSignedDistance(nearTopLeft);
|
|
|
- f[5] = plane.getSignedDistance(nearBottomLeft);
|
|
|
- f[6] = plane.getSignedDistance(nearTopRight);
|
|
|
- f[7] = plane.getSignedDistance(nearBottomRight);
|
|
|
-
|
|
|
- min = FLT_MAX;
|
|
|
- max = -FLT_MAX;
|
|
|
- for(uint i = 0; i < 8; i++) {
|
|
|
- if(f[i] < min) {
|
|
|
- min = f[i];
|
|
|
- }
|
|
|
- if(f[i] > max) {
|
|
|
- max = f[i];
|
|
|
- }
|
|
|
- }
|
|
|
- float lightHeight = max - min;
|
|
|
-
|
|
|
- // not the real center, but good guess
|
|
|
- renderSettings.testOrthoCenter = nearCenter;
|
|
|
- renderSettings.testOrthoCenter.addMul(camera.getBack(), -closeFarClip * 0.5f);
|
|
|
-
|
|
|
- if(renderSettings.ortho) {
|
|
|
- worldProj.setToIdentity();
|
|
|
- worldProj.set(0, 2.0f / lightWidth);
|
|
|
- worldProj.set(5, 2.0f / lightHeight);
|
|
|
- worldProj.set(10, -2.0f / (farClip - nearClip));
|
|
|
- }
|
|
|
-
|
|
|
worldShadowProj.setToIdentity();
|
|
|
- worldShadowProj.set(0, 2.0f / lightWidth);
|
|
|
- worldShadowProj.set(5, 2.0f / lightHeight);
|
|
|
- worldShadowProj.set(10, -2.0f / (farClip - nearClip));
|
|
|
+ worldShadowProj.set(0, 2.0f / 40.0f);
|
|
|
+ worldShadowProj.set(5, 2.0f / 30.0f);
|
|
|
+ worldShadowProj.set(10, -2.0f / frustum.getClipDifference());
|
|
|
}
|
|
|
|
|
|
void Engine::updateWorldView() {
|
|
|
- Vector right = camera.getRight();
|
|
|
- Vector up = camera.getUp();
|
|
|
- Vector back = camera.getBack();
|
|
|
- Vector pos = camera.getPosition();
|
|
|
-
|
|
|
- if(renderSettings.ortho) {
|
|
|
- right.set(0.939693f, 0.0f, -0.34202f);
|
|
|
- back.set(0.280166f, 0.573576f, 0.769751f);
|
|
|
- up.set(-0.196175f, 0.819152f, -0.538986f);
|
|
|
- pos = renderSettings.testOrthoCenter;
|
|
|
- }
|
|
|
-
|
|
|
- worldView.set(0, right.getX());
|
|
|
- worldView.set(1, up.getX());
|
|
|
- worldView.set(2, back.getX());
|
|
|
- worldView.set(4, right.getY());
|
|
|
- worldView.set(5, up.getY());
|
|
|
- worldView.set(6, back.getY());
|
|
|
- worldView.set(8, right.getZ());
|
|
|
- worldView.set(9, up.getZ());
|
|
|
- worldView.set(10, back.getZ());
|
|
|
- worldView.set(12, right.dotInverse(pos));
|
|
|
- worldView.set(13, up.dotInverse(pos));
|
|
|
- worldView.set(14, back.dotInverse(pos));
|
|
|
-
|
|
|
if(!renderSettings.shadows) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- right.set(0.939693f, 0.0f, -0.34202f);
|
|
|
- back.set(0.280166f, 0.573576f, 0.769751f);
|
|
|
- up.set(-0.196175f, 0.819152f, -0.538986f);
|
|
|
- pos = renderSettings.testOrthoCenter;
|
|
|
+ Vector right(0.939693f, 0.0f, -0.34202f);
|
|
|
+ Vector back(0.280166f, 0.573576f, 0.769751f);
|
|
|
+ Vector up(-0.196175f, 0.819152f, -0.538986f);
|
|
|
+ Vector center(16.0f, 24.0f, 24.0f);
|
|
|
|
|
|
worldShadowView.set(0, right.getX());
|
|
|
worldShadowView.set(1, up.getX());
|
|
@@ -282,7 +147,7 @@ void Engine::updateWorldView() {
|
|
|
worldShadowView.set(8, right.getZ());
|
|
|
worldShadowView.set(9, up.getZ());
|
|
|
worldShadowView.set(10, back.getZ());
|
|
|
- worldShadowView.set(12, right.dotInverse(pos));
|
|
|
- worldShadowView.set(13, up.dotInverse(pos));
|
|
|
- worldShadowView.set(14, back.dotInverse(pos));
|
|
|
+ worldShadowView.set(12, right.dotInverse(center));
|
|
|
+ worldShadowView.set(13, up.dotInverse(center));
|
|
|
+ worldShadowView.set(14, back.dotInverse(center));
|
|
|
}
|