Finished Forward Shading option as a means of FPS comparison

This commit is contained in:
Hurlu 2019-06-17 12:29:17 +09:00
parent 937c23c1d7
commit 59c3fbd4f6
8 changed files with 42 additions and 37 deletions

View File

@ -12,8 +12,6 @@ uniform sampler2D tex;
void main()
{
gPosition = FragPos;
//
//gPosition = normalize(abs(FragPos));
gNormal = normalize(Normal);
gAlbedoSpec.rgb = texture(tex, TexCoords).rgb;
gAlbedoSpec.a = 1;

View File

@ -29,12 +29,13 @@ void main()
vec3 viewDir = normalize(view_pos - FragPos);
for (int i = 0; i < NLights; ++i)
{
// diffuse
vec3 lightDir = normalize(lights[i].Position - FragPos);
vec3 diffuse = max(dot(Normal, lightDir), 0.0) * Albedo * lights[i].Color;
vec3 L = normalize(lights[i].Position.xyz - FragPos);
vec3 V = normalize(view_pos - FragPos);
vec3 H = normalize(V + L);
vec3 diffuse = max(dot(Normal, L), 0.0) * Albedo * lights[i].Color;
// specular
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(Normal, halfwayDir), 0.0), 16.0);
float spec = pow(max(dot(Normal, H), 0.0), 16.0);
vec3 specular = lights[i].Color * spec * Specular;
lighting += diffuse + specular;
}

View File

@ -370,15 +370,12 @@ void Mesh::draw(SceneContext &ctx) {
// Setting the space matrixes uniques to the object
ctx.modelMatrix = model.getMatrix();
ctx.mvpMatrix = ctx.projectionMatrix * ctx.viewMatrix * ctx.modelMatrix;
ctx.modelViewMatrix = ctx.viewMatrix * ctx.modelMatrix;
//ctx.modelViewMatrix = ctx.viewMatrix * ctx.modelMatrix;
ctx.normalMatrix = glm::transpose(glm::inverse(glm::mat3(ctx.modelMatrix)));
shader->addUniform("mvp", ctx.mvpMatrix);
shader->addUniform("NormalMatrix", ctx.normalMatrix);
if (ctx.renderMode == FORWARD_LIGHT)
shader->addUniform("ModelViewMatrix", ctx.modelViewMatrix);
else
shader->addUniform("ModelMatrix", ctx.modelMatrix);
shader->addUniform("ModelMatrix", ctx.modelMatrix);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures["0"].tex_ref);

View File

@ -79,6 +79,9 @@ void Multipass::gBufferSetup(SceneContext &scnctx, std::map<std::string, Mesh *>
void Multipass::recomputeDeferredLights(SceneContext &scnctx)
{
if (scnctx.renderMode != DEFERRED_LIGHT)
return;
int i = 0;
for (auto light : scnctx.lights)
{

View File

@ -249,7 +249,19 @@ void MyGlWindow::drawForwardLight()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (auto it = meshes.begin(); it != meshes.end(); it++)
{
(*it).second->shader->addUniform("view_pos", scnctx.camPos);
int i = 0;
for (auto light : scnctx.lights)
{
(*it).second->shader->addUniform("lights[" + std::to_string(i) + "].Position", glm::vec3(light.location));
(*it).second->shader->addUniform("lights[" + std::to_string(i) + "].Color", light.intensity);
i++;
}
(*it).second->shader->addUniform("NLights", (int)scnctx.lights.size());
(*it).second->draw(scnctx);
}
}

View File

@ -288,13 +288,13 @@ int loop(GLFWwindow *window)
ImGui::ListBox("Lights", &current_light, strings, lights_strs.size());
if (ImGui::SliderFloat("Selected light X", &glWin.scnctx.lights[current_light].location.x, -10, 10)
&& glWin.scnctx.renderMode != GBUF_DEBUG)
&& glWin.scnctx.renderMode == DEFERRED_LIGHT)
glWin.multipassManager.recomputeDeferredLights(glWin.scnctx);
if (ImGui::SliderFloat("Selected light Y", &glWin.scnctx.lights[current_light].location.y, -10, 10)
&& glWin.scnctx.renderMode != GBUF_DEBUG)
&& glWin.scnctx.renderMode == DEFERRED_LIGHT)
glWin.multipassManager.recomputeDeferredLights(glWin.scnctx);
if (ImGui::SliderFloat("Selected light Z", &glWin.scnctx.lights[current_light].location.z, -10, 10)
&& glWin.scnctx.renderMode != GBUF_DEBUG)
&& glWin.scnctx.renderMode == DEFERRED_LIGHT)
glWin.multipassManager.recomputeDeferredLights(glWin.scnctx);

View File

@ -3,26 +3,22 @@
in vec3 f_color;
out vec4 FragColors;
uniform vec3 Kd;
uniform vec3 Ka;
uniform vec3 Ks;
uniform float Shininess;
struct Light {
vec4 Position;
vec3 Position;
vec3 Color;
};
uniform Light lights[64];
uniform int NLights;
in vec3 fNormal;
in vec3 pos;
in vec2 texCoord;
uniform sampler2D tex;
uniform vec3 view_pos;
void main()
{
vec3 finalColor;
@ -30,21 +26,21 @@ void main()
vec3 specular_sum;
vec3 ambient;
ambient = Ka * lights[0].Color;
vec4 texColor = texture(tex, texCoord);
//Hardcoded ambient
ambient = texColor.rgb * 0.1;
for (int i = 0; i < NLights; i++)
{
vec3 L = normalize(lights[i].Position.xyz - pos);
vec3 N = fNormal;
vec3 V = normalize(-pos);
vec3 V = normalize(view_pos - pos);
vec3 H = normalize(V + L);
vec3 diffuse = Kd * lights[i].Color * max(dot(L, N), 0.0);
vec3 specular = Ks * lights[i].Color * pow(max(dot(H, N), 0.0), Shininess);
vec3 diffuse = lights[i].Color * texColor.rgb * max(dot(L, N), 0.0);
vec3 specular = lights[i].Color * pow(max(dot(H, N), 0.0), 16.0) * 0.5;
diffuse_sum += diffuse;
specular_sum += specular;
}
vec4 texColor = texture(tex, texCoord);
FragColors = (vec4(diffuse_sum + ambient, 1) * texColor + vec4(specular_sum, 1.0));
FragColors = texColor + (FragColors * 0.01);
FragColors = (vec4(ambient + diffuse_sum + specular_sum, 1));
}

View File

@ -7,7 +7,7 @@ layout(location=3) in vec2 v_texmap;
uniform mat4 mvp;
uniform mat3 NormalMatrix;
uniform mat4 ModelViewMatrix;
uniform mat4 ModelMatrix;
out vec3 fNormal;
out vec3 pos;
@ -16,9 +16,7 @@ out vec2 texCoord;
void main(void)
{
fNormal = normalize(NormalMatrix * v_normal);
pos = (ModelViewMatrix * vec4(coord3d, 1.0)).xyz;
pos = (ModelMatrix * vec4(coord3d, 1.0)).xyz;
texCoord = v_texmap;
gl_Position = mvp * vec4(coord3d, 1.0f);
}