|
@@ -1,4 +1,5 @@
|
|
|
#include "Wrapper.h"
|
|
|
+#include <random>
|
|
|
|
|
|
DummyClient DummyClient::dummy;
|
|
|
|
|
@@ -14,7 +15,12 @@ GLuint Engine::worldFrameBuffer = 0;
|
|
|
GLuint Engine::worldPositionTexture = 0;
|
|
|
GLuint Engine::worldNormalTexture = 0;
|
|
|
GLuint Engine::worldColorTexture = 0;
|
|
|
-GLuint Engine::worldDepthRenderBuffer = 0;
|
|
|
+GLuint Engine::worldDepthTexture = 0;
|
|
|
+
|
|
|
+
|
|
|
+ShaderProgram Engine::ssaoShader;
|
|
|
+GLuint Engine::ssaoFrameBuffer = 0;
|
|
|
+GLuint Engine::ssaoTexture = 0;
|
|
|
|
|
|
|
|
|
GLuint Engine::postVba = 0;
|
|
@@ -31,6 +37,11 @@ float Engine::testX = 0;
|
|
|
float Engine::testY = 0;
|
|
|
float Engine::testZ = 0;
|
|
|
|
|
|
+
|
|
|
+Vector3D Engine::ssaoKernel[ssaoKernelAmount];
|
|
|
+GLuint Engine::noiseTexture = 0;
|
|
|
+Matrix3D Engine::testMat;
|
|
|
+
|
|
|
bool Engine::init(int width, int height, const char* name)
|
|
|
{
|
|
|
Engine::width = width;
|
|
@@ -76,6 +87,14 @@ bool Engine::init(int width, int height, const char* name)
|
|
|
}
|
|
|
activeProgram = worldShader.getProgram();
|
|
|
|
|
|
+ ssaoShader.compile("shader/ssaoVertex.vs", "shader/ssaoFragment.fs");
|
|
|
+ if(!ssaoShader.isValid())
|
|
|
+ {
|
|
|
+ glfwDestroyWindow(window);
|
|
|
+ glfwTerminate();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
postShader.compile("shader/postVertex.vs", "shader/postFragment.fs");
|
|
|
if(!postShader.isValid())
|
|
|
{
|
|
@@ -265,6 +284,8 @@ void Engine::onInit()
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, NULL);
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, worldPositionTexture, 0);
|
|
|
|
|
|
glGenTextures(1, &worldNormalTexture);
|
|
@@ -286,12 +307,13 @@ void Engine::onInit()
|
|
|
GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2
|
|
|
};
|
|
|
glDrawBuffers(3, attachments);
|
|
|
-
|
|
|
- glGenRenderbuffers(1, &worldDepthRenderBuffer);
|
|
|
- glBindRenderbuffer(GL_RENDERBUFFER, worldDepthRenderBuffer);
|
|
|
- glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
|
|
|
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, worldDepthRenderBuffer);
|
|
|
- glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
|
|
+
|
|
|
+ glGenTextures(1, &worldDepthTexture);
|
|
|
+ glBindTexture(GL_TEXTURE_2D, worldDepthTexture);
|
|
|
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, width, height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
|
|
|
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, worldDepthTexture, 0);
|
|
|
|
|
|
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
{
|
|
@@ -323,6 +345,53 @@ void Engine::onInit()
|
|
|
};
|
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 24, data, GL_STATIC_DRAW);
|
|
|
+
|
|
|
+
|
|
|
+ glGenFramebuffers(1, &ssaoFrameBuffer);
|
|
|
+ glBindFramebuffer(GL_FRAMEBUFFER, ssaoFrameBuffer);
|
|
|
+
|
|
|
+ glGenTextures(1, &ssaoTexture);
|
|
|
+ glBindTexture(GL_TEXTURE_2D, ssaoTexture);
|
|
|
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RGB, GL_FLOAT, NULL);
|
|
|
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ssaoTexture, 0);
|
|
|
+
|
|
|
+ if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
+ {
|
|
|
+ cout << "ssao frame buffer is not complete!" << endl;
|
|
|
+ }
|
|
|
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
+
|
|
|
+ std::uniform_real_distribution<float> randomF(0.0, 1.0);
|
|
|
+ std::default_random_engine gen;
|
|
|
+ for(int i = 0; i < ssaoKernelAmount; i++)
|
|
|
+ {
|
|
|
+ ssaoKernel[i].set(randomF(gen) * 2.0 - 1.0, randomF(gen) * 2.0 - 1.0, randomF(gen));
|
|
|
+ ssaoKernel[i].normalize();
|
|
|
+ ssaoKernel[i].mul(randomF(gen));
|
|
|
+
|
|
|
+ float scale = (float) i / ssaoKernelAmount;
|
|
|
+ scale = 0.1f + (scale * scale) * 0.9f;
|
|
|
+ ssaoKernel[i].mul(scale);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ float noise[48];
|
|
|
+ for(int i = 0; i < 16; i++)
|
|
|
+ {
|
|
|
+ noise[i * 3] = randomF(gen) * 2.0 - 1.0;
|
|
|
+ noise[i * 3 + 1] = randomF(gen) * 2.0 - 1.0;
|
|
|
+ noise[i * 3 + 2] = 0.0f;
|
|
|
+ }
|
|
|
+
|
|
|
+ glGenTextures(1, &noiseTexture);
|
|
|
+ glBindTexture(GL_TEXTURE_2D, noiseTexture);
|
|
|
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, 4, 4, 0, GL_RGB, GL_FLOAT, noise);
|
|
|
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
}
|
|
|
|
|
|
void Engine::onRenderTick(float lag)
|
|
@@ -358,18 +427,36 @@ void Engine::onRenderTick(float lag)
|
|
|
glBindTexture(GL_TEXTURE_2D, worldNormalTexture);
|
|
|
glActiveTexture(GL_TEXTURE3);
|
|
|
glBindTexture(GL_TEXTURE_2D, worldColorTexture);
|
|
|
+ glActiveTexture(GL_TEXTURE4);
|
|
|
+ glBindTexture(GL_TEXTURE_2D, worldDepthTexture);
|
|
|
+ glActiveTexture(GL_TEXTURE5);
|
|
|
+ glBindTexture(GL_TEXTURE_2D, noiseTexture);
|
|
|
|
|
|
- activeProgram = postShader.getProgram();
|
|
|
+ activeProgram = ssaoShader.getProgram();
|
|
|
glUseProgram(activeProgram);
|
|
|
|
|
|
- glUniform3f(glGetUniformLocation(activeProgram, "viewPos"), testX, testY, testZ);
|
|
|
+ for(int i = 0; i < ssaoKernelAmount; i++)
|
|
|
+ {
|
|
|
+ glUniform3f(glGetUniformLocation(activeProgram, (string("ssaoKernel[") + std::to_string(i) + "]").c_str()),
|
|
|
+ ssaoKernel[i].getX(), ssaoKernel[i].getY(), ssaoKernel[i].getZ());
|
|
|
+ }
|
|
|
+
|
|
|
+ Engine::setMatrix(glGetUniformLocation(activeProgram, "projMatrix"), testMat.getValues());
|
|
|
|
|
|
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
+ glBindFramebuffer(GL_FRAMEBUFFER, ssaoFrameBuffer);
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
glBindVertexArray(postVba);
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, postVbo);
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
+
|
|
|
+ glActiveTexture(GL_TEXTURE6);
|
|
|
+ glBindTexture(GL_TEXTURE_2D, ssaoTexture);
|
|
|
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
+ glClear(GL_COLOR_BUFFER_BIT);
|
|
|
+ activeProgram = postShader.getProgram();
|
|
|
+ glUseProgram(activeProgram);
|
|
|
+ glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
}
|
|
|
|
|
|
void Engine::onTerm()
|
|
@@ -381,7 +468,7 @@ void Engine::onTerm()
|
|
|
glDeleteTextures(1, &worldPositionTexture);
|
|
|
glDeleteTextures(1, &worldNormalTexture);
|
|
|
glDeleteTextures(1, &worldColorTexture);
|
|
|
- glDeleteRenderbuffers(1, &worldDepthRenderBuffer);
|
|
|
+ glDeleteTextures(1, &worldDepthTexture);
|
|
|
}
|
|
|
|
|
|
void Engine::setLineMode(bool mode)
|