Finished deferred renderer for now
This commit is contained in:
parent
be4df5c853
commit
bf649f5d9f
@ -14,18 +14,18 @@ void MainState::onEnter() {
|
|||||||
uiTex.create(nf::BaseAssets::logo, nf::Vec2(0.025, 0.025), 0.5);
|
uiTex.create(nf::BaseAssets::logo, nf::Vec2(0.025, 0.025), 0.5);
|
||||||
button.create(nf::Vec2(0.8, 0.025), "Reset");
|
button.create(nf::Vec2(0.8, 0.025), "Reset");
|
||||||
button2.create(nf::Vec2(0.6, 0.025), "Play Sound");
|
button2.create(nf::Vec2(0.6, 0.025), "Play Sound");
|
||||||
light.create(nf::Vec3(-5.0, 10.0, 5.0), nf::Vec3(1.0, 1.0, 1.0));
|
light.create(nf::Vec3(0.0, 20.0, 0.0), nf::Vec3(1.0, 1.0, 1.0));
|
||||||
|
light2.create(nf::Vec3(-10.0, 20.0, -10.0), nf::Vec3(1.0, 1.0, 1.0));
|
||||||
|
light3.create(nf::Vec3(10.0, 20.0, 10.0), nf::Vec3(1.0, 1.0, 1.0));
|
||||||
cm.create(nf::BaseAssets::cubemap);
|
cm.create(nf::BaseAssets::cubemap);
|
||||||
|
|
||||||
sound.create(ap["sound.wav"]);
|
sound.create(ap["sound.wav"]);
|
||||||
|
|
||||||
for (int x = 0; x < 10; x++) {
|
for (int x = 0; x < 10; x++) {
|
||||||
for (int y = 0; y < 10; y++) {
|
for (int y = 0; y < 10; y++) {
|
||||||
for (int z = 0; z < 10; z++) {
|
|
||||||
entities.push_back(new nf::Entity);
|
entities.push_back(new nf::Entity);
|
||||||
entities.back()->create(ap["spec.obj"]);
|
entities.back()->create(ap["spec.obj"]);
|
||||||
entities.back()->setPosition(5.0 + x * 2.1, 0.05 + z * 2.1, -5.0 + y * 2.1);
|
entities.back()->setPosition(5.0 + x * 2.1, 0.05, -5.0 + y * 2.1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,7 +61,7 @@ void MainState::update(double deltaTime) {
|
|||||||
xrot += offset;
|
xrot += offset;
|
||||||
if (app->isKeyHeld(NFI_RIGHT))
|
if (app->isKeyHeld(NFI_RIGHT))
|
||||||
xrot -= offset;
|
xrot -= offset;
|
||||||
plane.setRotation(nf::Vec3(yrot * 10.0, 0.0, yrot * 10.0));
|
light.setPosition(nf::Vec3(0.0, yrot / 20.0 + 5.0, 0.0));
|
||||||
|
|
||||||
text.setText("FPS: " + std::to_string(app->getFPS()));
|
text.setText("FPS: " + std::to_string(app->getFPS()));
|
||||||
|
|
||||||
@ -78,6 +78,8 @@ void MainState::render(nf::Renderer& renderer) {
|
|||||||
renderer.render(test);
|
renderer.render(test);
|
||||||
renderer.render(plane);
|
renderer.render(plane);
|
||||||
renderer.render(light);
|
renderer.render(light);
|
||||||
|
renderer.render(light2);
|
||||||
|
renderer.render(light3);
|
||||||
renderer.render(text);
|
renderer.render(text);
|
||||||
renderer.render(uiTex);
|
renderer.render(uiTex);
|
||||||
renderer.render(button);
|
renderer.render(button);
|
||||||
@ -91,4 +93,6 @@ void MainState::render(nf::Renderer& renderer) {
|
|||||||
void MainState::onExit() {
|
void MainState::onExit() {
|
||||||
Log("MainState onExit!");
|
Log("MainState onExit!");
|
||||||
xrot = yrot = 0.0f;
|
xrot = yrot = 0.0f;
|
||||||
|
|
||||||
|
entities.clear();
|
||||||
}
|
}
|
@ -18,6 +18,8 @@ private:
|
|||||||
nf::Button button;
|
nf::Button button;
|
||||||
nf::Button button2;
|
nf::Button button2;
|
||||||
nf::Light light;
|
nf::Light light;
|
||||||
|
nf::Light light2;
|
||||||
|
nf::Light light3;
|
||||||
nf::Cubemap cm;
|
nf::Cubemap cm;
|
||||||
|
|
||||||
nf::Sound sound;
|
nf::Sound sound;
|
||||||
|
@ -3,5 +3,5 @@
|
|||||||
layout(location = 0) in vec2 pos;
|
layout(location = 0) in vec2 pos;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = vec4(pos.xy, 0.0, 1.0);
|
gl_Position = vec4(pos, 0.0, 1.0);
|
||||||
}
|
}
|
@ -25,8 +25,10 @@ layout(location = 3) out vec3 specular;
|
|||||||
void main() {
|
void main() {
|
||||||
pos = fragPos;
|
pos = fragPos;
|
||||||
|
|
||||||
if (material.hasNormTex)
|
if (material.hasNormTex) {
|
||||||
normals = texture(material.normalTexture, texCoord).xyz;
|
normals = texture(material.normalTexture, texCoord).xyz;
|
||||||
|
normals = normalize(normals * 2.0 - 1.0);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
normals = normalize(normal);
|
normals = normalize(normal);
|
||||||
|
|
||||||
@ -40,4 +42,5 @@ void main() {
|
|||||||
specular.g = texture(material.specularTexture, texCoord).r;
|
specular.g = texture(material.specularTexture, texCoord).r;
|
||||||
else
|
else
|
||||||
specular.g = 1.0;
|
specular.g = 1.0;
|
||||||
|
specular.b = 1.0;
|
||||||
}
|
}
|
@ -1,8 +1,6 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
in vec2 texCoord;
|
in vec2 texCoord;
|
||||||
in vec3 normals;
|
|
||||||
in vec3 fragPos;
|
|
||||||
in vec4 fragPosLightSpace[16];
|
in vec4 fragPosLightSpace[16];
|
||||||
|
|
||||||
struct Camera {
|
struct Camera {
|
||||||
@ -20,11 +18,16 @@ struct Light {
|
|||||||
};
|
};
|
||||||
|
|
||||||
uniform Camera camera;
|
uniform Camera camera;
|
||||||
uniform Light light[100];
|
uniform Light light[12];
|
||||||
uniform int numberOfLights;
|
uniform int numberOfLights;
|
||||||
uniform bool isContinued;
|
uniform bool isContinued;
|
||||||
uniform float farPlane;
|
uniform float farPlane;
|
||||||
|
|
||||||
|
uniform sampler2D gBPos;
|
||||||
|
uniform sampler2D gBNorm;
|
||||||
|
uniform sampler2D gBDiff;
|
||||||
|
uniform sampler2D gBSpec;
|
||||||
|
|
||||||
out vec4 outColor;
|
out vec4 outColor;
|
||||||
|
|
||||||
float calcShadowDirectional(int lightNum, vec4 fragPosLight, vec3 no, vec3 lDir) {
|
float calcShadowDirectional(int lightNum, vec4 fragPosLight, vec3 no, vec3 lDir) {
|
||||||
@ -54,14 +57,14 @@ vec3 offsets[20] = vec3[](
|
|||||||
vec3(0, 1, 1), vec3(0, -1, 1), vec3(0, -1, -1), vec3(0, 1, -1)
|
vec3(0, 1, 1), vec3(0, -1, 1), vec3(0, -1, -1), vec3(0, 1, -1)
|
||||||
);
|
);
|
||||||
|
|
||||||
float calcShadowPoint(int lightNum, vec3 no, vec3 lDir) {
|
float calcShadowPoint(int lightNum, vec3 no, vec3 lDir, vec3 fp) {
|
||||||
vec3 fragLight = fragPos - light[lightNum].pos;
|
vec3 fragLight = fp - light[lightNum].pos;
|
||||||
float current = length(fragLight);
|
float current = length(fragLight);
|
||||||
float bias = 0.15;
|
float bias = 0.15;
|
||||||
float closest = 0.0f;
|
float closest = 0.0f;
|
||||||
float shad = 0.0f;
|
float shad = 0.0f;
|
||||||
int samples = 20;
|
int samples = 20;
|
||||||
float viewDist = length(camera.pos - fragPos);
|
float viewDist = length(camera.pos - fp);
|
||||||
float disk = (1.0 + (viewDist / farPlane)) / 50.0f;
|
float disk = (1.0 + (viewDist / farPlane)) / 50.0f;
|
||||||
for (int i = 0; i < samples; i++) {
|
for (int i = 0; i < samples; i++) {
|
||||||
closest = texture(light[lightNum].pointDepthTex, fragLight + offsets[i] * disk).r;
|
closest = texture(light[lightNum].pointDepthTex, fragLight + offsets[i] * disk).r;
|
||||||
@ -76,7 +79,16 @@ float calcShadowPoint(int lightNum, vec3 no, vec3 lDir) {
|
|||||||
void main() {
|
void main() {
|
||||||
vec3 color = vec3(0.0);
|
vec3 color = vec3(0.0);
|
||||||
|
|
||||||
vec3 matDiff;
|
vec3 fragPos = texture(gBPos, texCoord).xyz;
|
||||||
|
vec3 norm = texture(gBNorm, texCoord).xyz;
|
||||||
|
vec3 matDiff = texture(gBDiff, texCoord).rgb;
|
||||||
|
vec3 specTemp = texture(gBSpec, texCoord).rgb;
|
||||||
|
float specPower = specTemp.r;
|
||||||
|
float matSpec = specTemp.g;
|
||||||
|
if (specTemp.b != 1.0)
|
||||||
|
discard;
|
||||||
|
|
||||||
|
/*vec3 matDiff;
|
||||||
if (material.hasDiffuseTex)
|
if (material.hasDiffuseTex)
|
||||||
matDiff = texture(material.diffuseTexture, texCoord).rgb;
|
matDiff = texture(material.diffuseTexture, texCoord).rgb;
|
||||||
else
|
else
|
||||||
@ -93,9 +105,9 @@ void main() {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
norm = normalize(normals);
|
norm = normalize(normals);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
float ambientStrength = 0.2f;
|
float ambientStrength = 0.1f;
|
||||||
vec3 ambient = ambientStrength * matDiff;
|
vec3 ambient = ambientStrength * matDiff;
|
||||||
if (!isContinued)
|
if (!isContinued)
|
||||||
color += ambient;
|
color += ambient;
|
||||||
@ -108,7 +120,7 @@ void main() {
|
|||||||
|
|
||||||
vec3 viewDir = normalize(camera.pos - fragPos);
|
vec3 viewDir = normalize(camera.pos - fragPos);
|
||||||
vec3 reflectDir = reflect(-lightDir, norm);
|
vec3 reflectDir = reflect(-lightDir, norm);
|
||||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.specPower);
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), specPower);
|
||||||
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.0f);
|
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.0f);
|
||||||
|
|
||||||
float shadow = calcShadowDirectional(i, fragPosLightSpace[i], norm, lightDir);
|
float shadow = calcShadowDirectional(i, fragPosLightSpace[i], norm, lightDir);
|
||||||
@ -118,22 +130,22 @@ void main() {
|
|||||||
if (light[i].type == 2) {
|
if (light[i].type == 2) {
|
||||||
vec3 lightDir = normalize(light[i].pos - fragPos);
|
vec3 lightDir = normalize(light[i].pos - fragPos);
|
||||||
float diff = max(dot(norm, lightDir), 0.0);
|
float diff = max(dot(norm, lightDir), 0.0);
|
||||||
vec3 diffuse = light[i].color * diff * matDiff * light[i].strength;
|
vec3 diffuse = light[i].color * diff * matDiff * (light[i].strength / 2.5f);
|
||||||
|
|
||||||
vec3 viewDir = normalize(camera.pos - fragPos);
|
vec3 viewDir = normalize(camera.pos - fragPos);
|
||||||
vec3 halfway = normalize(lightDir + viewDir);
|
vec3 halfway = normalize(lightDir + viewDir);
|
||||||
float spec = pow(max(dot(norm, halfway), 0.0), material.specPower);
|
float spec = pow(max(dot(norm, halfway), 0.0), specPower);
|
||||||
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.5f);
|
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.5f);
|
||||||
|
|
||||||
float length = length(light[i].pos - fragPos);
|
float length = length(light[i].pos - fragPos);
|
||||||
float att = clamp(10.0 / length, 0.0, 1.0) * light[i].strength;
|
float att = clamp(10.0 / length, 0.0, 1.0) * light[i].strength;
|
||||||
|
|
||||||
float shadow = calcShadowPoint(i, norm, lightDir);
|
float shadow = calcShadowPoint(i, norm, lightDir, fragPos);
|
||||||
color += ((diffuse + specular) * (1.0 - shadow) * att);
|
color += ((diffuse + specular) * (1.0 - shadow) * att);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//TODO: Move this to a post-processing pass
|
//TODO: Move this to a post-processing pass (what about the UI textures?)
|
||||||
float gamma = 2.2;
|
float gamma = 2.2;
|
||||||
color.rgb = pow(color.rgb, vec3(1.0 / gamma));
|
color.rgb = pow(color.rgb, vec3(1.0 / gamma));
|
||||||
outColor = vec4(color, 1.0);
|
outColor = vec4(color, 1.0);
|
||||||
|
@ -1,26 +1,20 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
layout(location = 0) in vec3 pos;
|
layout(location = 0) in vec2 pos;
|
||||||
layout(location = 1) in vec2 texCoords;
|
layout(location = 1) in vec2 texCoords;
|
||||||
layout(location = 2) in vec3 normal;
|
|
||||||
|
|
||||||
uniform mat4 model;
|
|
||||||
uniform mat4 view;
|
|
||||||
uniform mat4 proj;
|
|
||||||
uniform mat4 lightSpaceMat[16];
|
uniform mat4 lightSpaceMat[16];
|
||||||
uniform int numMats;
|
uniform int numMats;
|
||||||
|
uniform sampler2D gBPos;
|
||||||
|
|
||||||
out vec2 texCoord;
|
out vec2 texCoord;
|
||||||
out vec3 normals;
|
|
||||||
out vec3 fragPos;
|
|
||||||
out vec4 fragPosLightSpace[16];
|
out vec4 fragPosLightSpace[16];
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
texCoord = texCoords;
|
texCoord = texCoords;
|
||||||
normals = mat3(transpose(inverse(model))) * normal;
|
vec3 fragPos = texture(gBPos, texCoord).xyz;
|
||||||
fragPos = vec3(model * vec4(pos, 1.0));
|
|
||||||
for (int i = 0; i < numMats; i++) {
|
for (int i = 0; i < numMats; i++) {
|
||||||
fragPosLightSpace[i] = lightSpaceMat[i] * vec4(fragPos, 1.0);
|
fragPosLightSpace[i] = lightSpaceMat[i] * vec4(fragPos, 1.0);
|
||||||
}
|
}
|
||||||
gl_Position = proj * view * vec4(fragPos, 1.0);
|
gl_Position = vec4(pos, 0.0, 1.0);
|
||||||
}
|
}
|
@ -156,12 +156,12 @@ namespace nf {
|
|||||||
1.0, 1.0,
|
1.0, 1.0,
|
||||||
1.0, 0.0
|
1.0, 0.0
|
||||||
};
|
};
|
||||||
glActiveTexture(GL_TEXTURE10);
|
glActiveTexture(GL_TEXTURE4);
|
||||||
glBindTexture(GL_TEXTURE_2D, c.texID);
|
glBindTexture(GL_TEXTURE_2D, c.texID);
|
||||||
m_vao->bind();
|
m_vao->bind();
|
||||||
m_vao->setBufferData(0, vb, sizeof(vb));
|
m_vao->setBufferData(0, vb, sizeof(vb));
|
||||||
m_vao->setBufferData(1, tc, sizeof(tc));
|
m_vao->setBufferData(1, tc, sizeof(tc));
|
||||||
shader->setUniform("text", 10);
|
shader->setUniform("text", 4);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
currX += (c.advance >> 6) * scale * m_scale;
|
currX += (c.advance >> 6) * scale * m_scale;
|
||||||
}
|
}
|
||||||
|
@ -114,7 +114,7 @@ namespace nf {
|
|||||||
gBufferShader->setUniform("view", view);
|
gBufferShader->setUniform("view", view);
|
||||||
|
|
||||||
glm::vec3 pos(m_position.x, m_position.y, m_position.z);
|
glm::vec3 pos(m_position.x, m_position.y, m_position.z);
|
||||||
//lightingShader->setUniform("camera.pos", pos);
|
lightingShader->setUniform("camera.pos", pos);
|
||||||
|
|
||||||
glm::mat4 cubemapView = glm::mat4(glm::mat3(view));
|
glm::mat4 cubemapView = glm::mat4(glm::mat3(view));
|
||||||
cubemapShader->setUniform("view", cubemapView);
|
cubemapShader->setUniform("view", cubemapView);
|
||||||
|
@ -54,9 +54,6 @@ namespace nf {
|
|||||||
//TODO: Blit depth buffer for transparent objects later
|
//TODO: Blit depth buffer for transparent objects later
|
||||||
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, prevFBO);
|
glBindFramebuffer(GL_FRAMEBUFFER, prevFBO);
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, m_textures[1]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GBuffer::bindTextures(Shader* shader) {
|
void GBuffer::bindTextures(Shader* shader) {
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
Renderer::Renderer(Application* app) :
|
Renderer::Renderer(Application* app) :
|
||||||
|
m_app(app),
|
||||||
m_gBuffer(nullptr),
|
m_gBuffer(nullptr),
|
||||||
m_shadowMapFBO(0),
|
m_shadowMapFBO(0),
|
||||||
m_directionalDepthTexSize(0),
|
m_directionalDepthTexSize(0),
|
||||||
@ -29,7 +30,6 @@ namespace nf {
|
|||||||
m_fadeNoText(false),
|
m_fadeNoText(false),
|
||||||
m_fadeOutComplete(false)
|
m_fadeOutComplete(false)
|
||||||
{
|
{
|
||||||
m_app = app;
|
|
||||||
m_hdc = GetDC(m_app->getWindow());
|
m_hdc = GetDC(m_app->getWindow());
|
||||||
PIXELFORMATDESCRIPTOR pfd = {
|
PIXELFORMATDESCRIPTOR pfd = {
|
||||||
sizeof(PIXELFORMATDESCRIPTOR),
|
sizeof(PIXELFORMATDESCRIPTOR),
|
||||||
@ -60,11 +60,12 @@ namespace nf {
|
|||||||
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
||||||
WGL_CONTEXT_MINOR_VERSION_ARB, 3,
|
WGL_CONTEXT_MINOR_VERSION_ARB, 3,
|
||||||
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
||||||
0, 0
|
0
|
||||||
};
|
};
|
||||||
wglDeleteContext(m_hglrc);
|
wglDeleteContext(m_hglrc);
|
||||||
m_hglrc = wglCreateContextAttribsARB(m_hdc, NULL, attrib);
|
m_hglrc = wglCreateContextAttribsARB(m_hdc, NULL, attrib);
|
||||||
wglMakeCurrent(m_hdc, m_hglrc);
|
wglMakeCurrent(m_hdc, m_hglrc);
|
||||||
|
//TODO: Configure V-Sync with a custom max FPS
|
||||||
wglSwapIntervalEXT(0);
|
wglSwapIntervalEXT(0);
|
||||||
Log("OpenGL version: " + std::string((char*)glGetString(GL_VERSION)));
|
Log("OpenGL version: " + std::string((char*)glGetString(GL_VERSION)));
|
||||||
glDepthFunc(GL_LESS);
|
glDepthFunc(GL_LESS);
|
||||||
@ -72,7 +73,7 @@ namespace nf {
|
|||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
loadBaseAssets();
|
loadBaseAssets();
|
||||||
|
|
||||||
@ -98,21 +99,22 @@ namespace nf {
|
|||||||
SendMessage(m_app->getWindow(), WM_SETICON, ICON_SMALL, (LPARAM)windowIcon);
|
SendMessage(m_app->getWindow(), WM_SETICON, ICON_SMALL, (LPARAM)windowIcon);
|
||||||
}
|
}
|
||||||
|
|
||||||
float fadeVB[] = {
|
float quadVB[] = {
|
||||||
-1.0, -1.0,
|
-1.0, -1.0, 0.0, 0.0,
|
||||||
1.0, -1.0,
|
1.0, -1.0, 1.0, 0.0,
|
||||||
1.0, 1.0,
|
1.0, 1.0, 1.0, 1.0,
|
||||||
-1.0, 1.0
|
-1.0, 1.0, 0.0, 1.0
|
||||||
};
|
};
|
||||||
unsigned int fadeIB[] = {
|
unsigned int quadIB[] = {
|
||||||
0, 1, 2,
|
0, 1, 2,
|
||||||
2, 3, 0
|
2, 3, 0
|
||||||
};
|
};
|
||||||
m_fadeVAO = new VertexArray;
|
m_quadVAO = new VertexArray;
|
||||||
m_fadeVAO->addBuffer(fadeVB, sizeof(fadeVB));
|
m_quadVAO->addBuffer(quadVB, sizeof(quadVB));
|
||||||
m_fadeVAO->push<float>(2);
|
m_quadVAO->push<float>(2);
|
||||||
m_fadeVAO->finishBufferLayout();
|
m_quadVAO->push<float>(2);
|
||||||
m_fadeIB = new IndexBuffer(fadeIB, 6);
|
m_quadVAO->finishBufferLayout();
|
||||||
|
m_quadIB = new IndexBuffer(quadIB, 6);
|
||||||
m_loadingText.create("NFLoadingText", Vec2(0.025, 0.044), Vec3(0.7, 0.7, 0.7));
|
m_loadingText.create("NFLoadingText", Vec2(0.025, 0.044), Vec3(0.7, 0.7, 0.7));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,60 +158,60 @@ namespace nf {
|
|||||||
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 10000.0f);
|
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 10000.0f);
|
||||||
camera->bind(m_gBufferShader, m_lightingShader, m_cubemapShader);
|
camera->bind(m_gBufferShader, m_lightingShader, m_cubemapShader);
|
||||||
|
|
||||||
//First, draw the cubemap if one is currently set
|
//First, fill the gBuffer with entities
|
||||||
|
m_gBufferShader->setUniform("proj", proj);
|
||||||
|
m_gBuffer->render(m_lGame, m_gBufferShader);
|
||||||
|
|
||||||
|
//Light entities using the gBuffer
|
||||||
|
unsigned int lightsRemaining = m_lights.size();
|
||||||
|
if (!lightsRemaining) {
|
||||||
|
m_quadVAO->bind();
|
||||||
|
m_quadIB->bind();
|
||||||
|
m_lightingShader->bind();
|
||||||
|
m_gBuffer->bindTextures(m_lightingShader);
|
||||||
|
glDrawElements(GL_TRIANGLES, m_quadIB->getCount(), GL_UNSIGNED_INT, nullptr);
|
||||||
|
}
|
||||||
|
unsigned int drawCount = 0;
|
||||||
|
while (lightsRemaining > 0) {
|
||||||
|
unsigned int currLightsDrawn;
|
||||||
|
if (lightsRemaining > m_texSlots)
|
||||||
|
currLightsDrawn = m_texSlots;
|
||||||
|
else
|
||||||
|
currLightsDrawn = lightsRemaining;
|
||||||
|
lightsRemaining -= currLightsDrawn;
|
||||||
|
m_lightingShader->setUniform("numberOfLights", (int)currLightsDrawn);
|
||||||
|
if(drawCount == 0)
|
||||||
|
m_lightingShader->setUniform("isContinued", false);
|
||||||
|
else {
|
||||||
|
m_lightingShader->setUniform("isContinued", true);
|
||||||
|
glBlendFunc(GL_ONE, GL_ONE);
|
||||||
|
glDepthFunc(GL_LEQUAL);
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < currLightsDrawn; i++)
|
||||||
|
m_lights[i]->bind(m_lightingShader, i);
|
||||||
|
renderShadowMaps(currLightsDrawn);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
|
||||||
|
m_quadVAO->bind();
|
||||||
|
m_quadIB->bind();
|
||||||
|
m_lightingShader->bind();
|
||||||
|
m_gBuffer->bindTextures(m_lightingShader);
|
||||||
|
glDrawElements(GL_TRIANGLES, m_quadIB->getCount(), GL_UNSIGNED_INT, nullptr);
|
||||||
|
m_lights.erase(m_lights.begin(), m_lights.begin() + currLightsDrawn);
|
||||||
|
drawCount++;
|
||||||
|
}
|
||||||
|
m_lGame.clear();
|
||||||
|
m_lights.clear();
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
glDepthFunc(GL_LESS);
|
||||||
|
|
||||||
|
//Draw the cubemap if one is currently set
|
||||||
if (m_cubemap != nullptr) {
|
if (m_cubemap != nullptr) {
|
||||||
m_cubemapShader->setUniform("proj", proj);
|
m_cubemapShader->setUniform("proj", proj);
|
||||||
m_cubemap->render(m_cubemapShader);
|
m_cubemap->render(m_cubemapShader);
|
||||||
}
|
}
|
||||||
m_cubemap = nullptr;
|
m_cubemap = nullptr;
|
||||||
|
|
||||||
//Fill gBuffer with entities
|
|
||||||
m_gBufferShader->setUniform("proj", proj);
|
|
||||||
m_gBuffer->render(m_lGame, m_gBufferShader);
|
|
||||||
m_lGame.clear();
|
|
||||||
|
|
||||||
//Light entities using the gBuffer
|
|
||||||
/*for (Entity* draw : m_lGame) {
|
|
||||||
Entity& curr = *draw;
|
|
||||||
unsigned int drawCount = (unsigned int)std::ceil(m_lights.size() / (double)m_texSlots);
|
|
||||||
if (drawCount == 0)
|
|
||||||
drawCount++;
|
|
||||||
unsigned int lightsRemaining = m_lights.size();
|
|
||||||
int currLight;
|
|
||||||
if (lightsRemaining > m_texSlots)
|
|
||||||
currLight = -(int)m_texSlots;
|
|
||||||
else
|
|
||||||
currLight = -(int)lightsRemaining;
|
|
||||||
for (unsigned int i = 0; i < drawCount; i++) {
|
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
||||||
m_lightingShader->setUniform("isContinued", false);
|
|
||||||
if (i != 0) {
|
|
||||||
glBlendFunc(GL_ONE, GL_ONE);
|
|
||||||
glDepthFunc(GL_LEQUAL);
|
|
||||||
m_lightingShader->setUniform("isContinued", true);
|
|
||||||
}
|
|
||||||
unsigned int currLightsDrawn;
|
|
||||||
if (lightsRemaining >= m_texSlots)
|
|
||||||
currLightsDrawn = m_texSlots;
|
|
||||||
else
|
|
||||||
currLightsDrawn = lightsRemaining;
|
|
||||||
lightsRemaining -= currLightsDrawn;
|
|
||||||
currLight += (int)currLightsDrawn;
|
|
||||||
m_lightingShader->setUniform("numberOfLights", (int)currLightsDrawn);
|
|
||||||
for (unsigned int j = 0; j < currLightsDrawn; j++) {
|
|
||||||
m_lights[j + (unsigned int)currLight]->bind(m_lightingShader, j);
|
|
||||||
}
|
|
||||||
renderShadowMaps(currLight, currLightsDrawn);
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
||||||
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
|
|
||||||
curr.render(m_lightingShader);
|
|
||||||
}
|
|
||||||
glDepthFunc(GL_LESS);
|
|
||||||
}
|
|
||||||
m_lGame.clear();*/
|
|
||||||
m_lights.clear();
|
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
||||||
|
|
||||||
//Draw UI elements
|
//Draw UI elements
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
proj = glm::ortho(0.0f, (float)m_app->getConfig().width, 0.0f, (float)m_app->getConfig().height);
|
proj = glm::ortho(0.0f, (float)m_app->getConfig().width, 0.0f, (float)m_app->getConfig().height);
|
||||||
@ -233,12 +235,13 @@ namespace nf {
|
|||||||
}
|
}
|
||||||
m_lUI.clear();
|
m_lUI.clear();
|
||||||
|
|
||||||
|
//Fade over everything when states change
|
||||||
if (m_fadeIn) {
|
if (m_fadeIn) {
|
||||||
static double opacity = 1.0;
|
static double opacity = 1.0;
|
||||||
m_fadeShader->setUniform("opacity", (float)opacity);
|
m_fadeShader->setUniform("opacity", (float)opacity);
|
||||||
m_fadeVAO->bind();
|
m_quadVAO->bind();
|
||||||
m_fadeIB->bind();
|
m_quadIB->bind();
|
||||||
glDrawElements(GL_TRIANGLES, m_fadeIB->getCount(), GL_UNSIGNED_INT, nullptr);
|
glDrawElements(GL_TRIANGLES, m_quadIB->getCount(), GL_UNSIGNED_INT, nullptr);
|
||||||
if (!m_fadeNoText) {
|
if (!m_fadeNoText) {
|
||||||
m_textShader->setUniform("proj", proj);
|
m_textShader->setUniform("proj", proj);
|
||||||
m_loadingText.setOpacity(opacity);
|
m_loadingText.setOpacity(opacity);
|
||||||
@ -256,9 +259,9 @@ namespace nf {
|
|||||||
else if (m_fadeOut) {
|
else if (m_fadeOut) {
|
||||||
static double opacity = 0.0;
|
static double opacity = 0.0;
|
||||||
m_fadeShader->setUniform("opacity", (float)opacity);
|
m_fadeShader->setUniform("opacity", (float)opacity);
|
||||||
m_fadeVAO->bind();
|
m_quadVAO->bind();
|
||||||
m_fadeIB->bind();
|
m_quadIB->bind();
|
||||||
glDrawElements(GL_TRIANGLES, m_fadeIB->getCount(), GL_UNSIGNED_INT, nullptr);
|
glDrawElements(GL_TRIANGLES, m_quadIB->getCount(), GL_UNSIGNED_INT, nullptr);
|
||||||
if (!m_fadeNoText) {
|
if (!m_fadeNoText) {
|
||||||
m_textShader->setUniform("proj", proj);
|
m_textShader->setUniform("proj", proj);
|
||||||
m_loadingText.setOpacity(opacity);
|
m_loadingText.setOpacity(opacity);
|
||||||
@ -278,17 +281,16 @@ namespace nf {
|
|||||||
if (err != GL_NO_ERROR)
|
if (err != GL_NO_ERROR)
|
||||||
Error("OpenGL error " + std::to_string(err));
|
Error("OpenGL error " + std::to_string(err));
|
||||||
|
|
||||||
|
//Show completed frame
|
||||||
SwapBuffers(m_hdc);
|
SwapBuffers(m_hdc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::renderShadowMaps(unsigned int startingLight, unsigned int count) {
|
void Renderer::renderShadowMaps(unsigned int count) {
|
||||||
float nearP = 0.1f, farP = 400.0f;
|
float nearP = 0.1f, farP = 400.0f;
|
||||||
glm::mat4 directionalLightProj = glm::ortho(-50.0f, 50.0f, -50.0f, 50.0f, nearP, farP);
|
glm::mat4 directionalLightProj = glm::ortho(-50.0f, 50.0f, -50.0f, 50.0f, nearP, farP);
|
||||||
glm::mat4 pointLightProj = glm::perspective(glm::radians(90.0f), 1.0f, nearP, farP);
|
glm::mat4 pointLightProj = glm::perspective(glm::radians(90.0f), 1.0f, nearP, farP);
|
||||||
glm::mat4 lightView;
|
glm::mat4 lightView;
|
||||||
glm::mat4 lightSpaceMat;
|
glm::mat4 lightSpaceMat;
|
||||||
int prevFBO;
|
|
||||||
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prevFBO);
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, m_shadowMapFBO);
|
glBindFramebuffer(GL_FRAMEBUFFER, m_shadowMapFBO);
|
||||||
for (unsigned int i = 0; i < count; i++) {
|
for (unsigned int i = 0; i < count; i++) {
|
||||||
Light::Type type = m_lights[i]->getType();
|
Light::Type type = m_lights[i]->getType();
|
||||||
@ -300,7 +302,7 @@ namespace nf {
|
|||||||
glDrawBuffer(GL_NONE);
|
glDrawBuffer(GL_NONE);
|
||||||
glReadBuffer(GL_NONE);
|
glReadBuffer(GL_NONE);
|
||||||
glClear(GL_DEPTH_BUFFER_BIT);
|
glClear(GL_DEPTH_BUFFER_BIT);
|
||||||
Vec3 posTemp = m_lights[startingLight + i]->getPosition();
|
Vec3 posTemp = m_lights[i]->getPosition();
|
||||||
glm::vec3 lightPos(posTemp.x, posTemp.y, posTemp.z);
|
glm::vec3 lightPos(posTemp.x, posTemp.y, posTemp.z);
|
||||||
lightView = glm::lookAt(lightPos, glm::vec3(0.0), glm::vec3(0.0, 1.0, 0.0));
|
lightView = glm::lookAt(lightPos, glm::vec3(0.0), glm::vec3(0.0, 1.0, 0.0));
|
||||||
lightSpaceMat = directionalLightProj * lightView;
|
lightSpaceMat = directionalLightProj * lightView;
|
||||||
@ -315,9 +317,9 @@ namespace nf {
|
|||||||
stringPos = "light[";
|
stringPos = "light[";
|
||||||
stringPos += std::to_string(i);
|
stringPos += std::to_string(i);
|
||||||
stringPos += "].directionalDepthTex";
|
stringPos += "].directionalDepthTex";
|
||||||
glActiveTexture(GL_TEXTURE3 + i);
|
glActiveTexture(GL_TEXTURE4 + i);
|
||||||
glBindTexture(GL_TEXTURE_2D, tex);
|
glBindTexture(GL_TEXTURE_2D, tex);
|
||||||
m_lightingShader->setUniform(stringPos, 3 + (int)i);
|
m_lightingShader->setUniform(stringPos, 4 + (int)i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Light::Type::POINT: {
|
case Light::Type::POINT: {
|
||||||
@ -326,7 +328,7 @@ namespace nf {
|
|||||||
glDrawBuffer(GL_NONE);
|
glDrawBuffer(GL_NONE);
|
||||||
glReadBuffer(GL_NONE);
|
glReadBuffer(GL_NONE);
|
||||||
glClear(GL_DEPTH_BUFFER_BIT);
|
glClear(GL_DEPTH_BUFFER_BIT);
|
||||||
Vec3 posTemp = m_lights[startingLight + i]->getPosition();
|
Vec3 posTemp = m_lights[i]->getPosition();
|
||||||
glm::vec3 lightPos(posTemp.x, posTemp.y, posTemp.z);
|
glm::vec3 lightPos(posTemp.x, posTemp.y, posTemp.z);
|
||||||
std::vector<glm::mat4> lightSpaceMats;
|
std::vector<glm::mat4> lightSpaceMats;
|
||||||
lightSpaceMats.push_back(pointLightProj * glm::lookAt(lightPos, lightPos + glm::vec3(1.0, 0.0, 0.0), glm::vec3(0.0, -1.0, 0.0)));
|
lightSpaceMats.push_back(pointLightProj * glm::lookAt(lightPos, lightPos + glm::vec3(1.0, 0.0, 0.0), glm::vec3(0.0, -1.0, 0.0)));
|
||||||
@ -349,17 +351,15 @@ namespace nf {
|
|||||||
std::string stringPos = "light[";
|
std::string stringPos = "light[";
|
||||||
stringPos += std::to_string(i);
|
stringPos += std::to_string(i);
|
||||||
stringPos += "].pointDepthTex";
|
stringPos += "].pointDepthTex";
|
||||||
glActiveTexture(GL_TEXTURE3 + i);
|
glActiveTexture(GL_TEXTURE4 + i);
|
||||||
glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
|
glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
|
||||||
m_lightingShader->setUniform(stringPos, 3 + (int)i);
|
m_lightingShader->setUniform(stringPos, 4 + (int)i);
|
||||||
m_lightingShader->setUniform("farPlane", farP);
|
m_lightingShader->setUniform("farPlane", farP);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_lightingShader->setUniform("numMats", (int)count);
|
m_lightingShader->setUniform("numMats", (int)count);
|
||||||
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, prevFBO);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::loadBaseAssets() {
|
void Renderer::loadBaseAssets() {
|
||||||
@ -369,7 +369,7 @@ namespace nf {
|
|||||||
m_gBufferShader = new Shader(gBufferVertex, gBufferFragment);
|
m_gBufferShader = new Shader(gBufferVertex, gBufferFragment);
|
||||||
const char* lightingVertex = m_baseAP["lightingVertex.shader"]->data;
|
const char* lightingVertex = m_baseAP["lightingVertex.shader"]->data;
|
||||||
const char* lightingFragment = m_baseAP["lightingFragment.shader"]->data;
|
const char* lightingFragment = m_baseAP["lightingFragment.shader"]->data;
|
||||||
//m_lightingShader = new Shader(lightingVertex, lightingFragment);
|
m_lightingShader = new Shader(lightingVertex, lightingFragment);
|
||||||
const char* textVertex = m_baseAP["textVertex.shader"]->data;
|
const char* textVertex = m_baseAP["textVertex.shader"]->data;
|
||||||
const char* textFragment = m_baseAP["textFragment.shader"]->data;
|
const char* textFragment = m_baseAP["textFragment.shader"]->data;
|
||||||
m_textShader = new Shader(textVertex, textFragment);
|
m_textShader = new Shader(textVertex, textFragment);
|
||||||
@ -403,9 +403,8 @@ namespace nf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::createShadowMaps() {
|
void Renderer::createShadowMaps() {
|
||||||
m_texSlots = 13;
|
m_texSlots = 12;
|
||||||
glGenFramebuffers(1, &m_shadowMapFBO);
|
glGenFramebuffers(1, &m_shadowMapFBO);
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, m_shadowMapFBO);
|
|
||||||
for (unsigned int i = 0; i < m_texSlots; i++) {
|
for (unsigned int i = 0; i < m_texSlots; i++) {
|
||||||
unsigned int directionalDepthMap, pointDepthMap;
|
unsigned int directionalDepthMap, pointDepthMap;
|
||||||
glGenTextures(1, &directionalDepthMap);
|
glGenTextures(1, &directionalDepthMap);
|
||||||
@ -442,8 +441,8 @@ namespace nf {
|
|||||||
delete m_fadeShader;
|
delete m_fadeShader;
|
||||||
delete m_directionalShadowShader;
|
delete m_directionalShadowShader;
|
||||||
delete m_gBuffer;
|
delete m_gBuffer;
|
||||||
delete m_fadeVAO;
|
delete m_quadVAO;
|
||||||
delete m_fadeIB;
|
delete m_quadIB;
|
||||||
ReleaseDC(m_app->getWindow(), m_hdc);
|
ReleaseDC(m_app->getWindow(), m_hdc);
|
||||||
wglMakeCurrent(NULL, NULL);
|
wglMakeCurrent(NULL, NULL);
|
||||||
wglDeleteContext(m_hglrc);
|
wglDeleteContext(m_hglrc);
|
||||||
|
@ -23,6 +23,7 @@ namespace nf {
|
|||||||
class Drawable;
|
class Drawable;
|
||||||
class Shader;
|
class Shader;
|
||||||
class Model;
|
class Model;
|
||||||
|
class GBuffer;
|
||||||
class AudioEngine;
|
class AudioEngine;
|
||||||
|
|
||||||
class Renderer {
|
class Renderer {
|
||||||
@ -41,7 +42,7 @@ namespace nf {
|
|||||||
|
|
||||||
~Renderer();
|
~Renderer();
|
||||||
private:
|
private:
|
||||||
void renderShadowMaps(unsigned int startingLight, unsigned int count);
|
void renderShadowMaps(unsigned int count);
|
||||||
|
|
||||||
void loadBaseAssets();
|
void loadBaseAssets();
|
||||||
void createShadowMaps();
|
void createShadowMaps();
|
||||||
@ -53,6 +54,8 @@ namespace nf {
|
|||||||
|
|
||||||
AssetPack m_baseAP;
|
AssetPack m_baseAP;
|
||||||
|
|
||||||
|
GBuffer* m_gBuffer;
|
||||||
|
|
||||||
unsigned int m_shadowMapFBO;
|
unsigned int m_shadowMapFBO;
|
||||||
int m_directionalDepthTexSize;
|
int m_directionalDepthTexSize;
|
||||||
int m_pointDepthTexSize;
|
int m_pointDepthTexSize;
|
||||||
@ -65,7 +68,7 @@ namespace nf {
|
|||||||
Cubemap* m_cubemap;
|
Cubemap* m_cubemap;
|
||||||
std::vector<UIElement*> m_lUI;
|
std::vector<UIElement*> m_lUI;
|
||||||
Shader* m_gBufferShader;
|
Shader* m_gBufferShader;
|
||||||
Shader* m_entityShader;
|
Shader* m_lightingShader;
|
||||||
Shader* m_textShader;
|
Shader* m_textShader;
|
||||||
Shader* m_uiTextureShader;
|
Shader* m_uiTextureShader;
|
||||||
Shader* m_cubemapShader;
|
Shader* m_cubemapShader;
|
||||||
@ -77,8 +80,9 @@ namespace nf {
|
|||||||
bool m_fadeNoText;
|
bool m_fadeNoText;
|
||||||
bool m_fadeOutComplete;
|
bool m_fadeOutComplete;
|
||||||
Text m_loadingText;
|
Text m_loadingText;
|
||||||
VertexArray* m_fadeVAO;
|
|
||||||
IndexBuffer* m_fadeIB;
|
VertexArray* m_quadVAO;
|
||||||
|
IndexBuffer* m_quadIB;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Application {
|
class Application {
|
||||||
|
@ -33,7 +33,7 @@ namespace nf {
|
|||||||
|
|
||||||
~Renderer();
|
~Renderer();
|
||||||
private:
|
private:
|
||||||
void renderShadowMaps(unsigned int startingLight, unsigned int count);
|
void renderShadowMaps(unsigned int count);
|
||||||
|
|
||||||
void loadBaseAssets();
|
void loadBaseAssets();
|
||||||
void createShadowMaps();
|
void createShadowMaps();
|
||||||
@ -71,7 +71,8 @@ namespace nf {
|
|||||||
bool m_fadeNoText;
|
bool m_fadeNoText;
|
||||||
bool m_fadeOutComplete;
|
bool m_fadeOutComplete;
|
||||||
Text m_loadingText;
|
Text m_loadingText;
|
||||||
VertexArray* m_fadeVAO;
|
|
||||||
IndexBuffer* m_fadeIB;
|
VertexArray* m_quadVAO;
|
||||||
|
IndexBuffer* m_quadIB;
|
||||||
};
|
};
|
||||||
}
|
}
|
Reference in New Issue
Block a user