Compare commits

...

18 Commits

Author SHA1 Message Date
253df31799 Less lights but more powerful ones in the cube scenes, to serve as a better comparison between the two types of lighting shading. 2019-06-18 18:40:00 +09:00
93271a1113 Added teapot Scene 2019-06-18 11:47:39 +09:00
3bbb233716 Proof that deferred shading is way better at handling a lot of lights 2019-06-17 22:32:53 +09:00
59c3fbd4f6 Finished Forward Shading option as a means of FPS comparison 2019-06-17 12:29:17 +09:00
937c23c1d7 Deferred lighting is working, currently adding forward redering as a comparison, also working on the sceneswapping and light manipulation tools 2019-06-16 23:52:49 +09:00
284650a034 Started workaround for textured .objs (not really finished), addded depth buffer and fixed emplacement of the glClear 2019-06-14 17:32:07 +09:00
Hugo Willaume
03be372af2 merge 2019-06-14 16:23:12 +09:00
Hugo Willaume
c03e998ef3 change of workstation 2019-06-12 17:19:09 +09:00
Hugo Willaume
fe58218c7e Looks like the G-buffer debug is working alright 2019-06-11 19:03:36 +09:00
Hugo Willaume
551b408fe3 Looks like the G-buffer debug is working alright 2019-06-10 14:02:11 +09:00
fe5a3b2541 mobility commit 2019-06-07 11:16:16 +09:00
Hugo Willaume
a9fc5d0cf5 Decided to start off a cleaner base, deleted most of uneeded code for this specific demo 2019-06-05 15:57:28 +09:00
32071d672e Adding nanosuit for testing purposes, struggling on making that god damned Gbuffer work 2019-06-03 17:45:39 +09:00
Hugo Willaume
ea937db226 Progress done, changing workstation 2019-06-03 13:09:09 +09:00
Hugo Willaume
81446dee6a Base scene running around 13 FPS with battery on in VS 2019-05-29 09:16:18 +09:00
Hugo Willaume
0775c60b38 Fix merge problems 2019-05-27 15:53:55 +09:00
Hugo Willaume
41798d428a Merge branch 'DeferredShading' 2019-05-27 15:48:40 +09:00
Hugo Willaume
152cb7ca88 Changes to the depth map display - first part of shadow mapping done, second part commented out 2019-05-01 09:21:39 +09:00
74 changed files with 53428 additions and 1469 deletions

View File

@ -95,7 +95,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<AdditionalLibraryDirectories>C:\Graphics\Tools\SOIL\lib;C:\Graphics\Tools\Assimp\lib\x86;C:\Graphics\Tools\glfw\lib\x86;C:\Graphics\Tools\glew\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>assimp.lib;SOIL.lib;glfw3.lib;glew32.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>assimp.lib;SOIL.lib;glfw3.lib;glew32.lib;opengl32.lib;FreeImage.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -147,7 +147,6 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Dataset.cpp" />
<ClCompile Include="fboManager.cpp" />
<ClCompile Include="imgui\imgui.cpp" />
<ClCompile Include="imgui\imgui_demo.cpp" />
<ClCompile Include="imgui\imgui_draw.cpp" />
@ -159,16 +158,12 @@
<ClCompile Include="Multipass.cpp" />
<ClCompile Include="MyGLWindow.cpp" />
<ClCompile Include="Shader.cpp" />
<ClCompile Include="Skybox.cpp" />
<ClCompile Include="Source.cpp" />
<ClCompile Include="Texture.cpp" />
<ClCompile Include="textureManager.cpp" />
<ClCompile Include="TextureViewer.cpp" />
<ClCompile Include="Viewer.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Dataset.h" />
<ClInclude Include="fboManager.h" />
<ClInclude Include="imgui\imconfig.h" />
<ClInclude Include="imgui\imgui.h" />
<ClInclude Include="imgui\imgui_impl_glfw.h" />
@ -186,10 +181,7 @@
<ClInclude Include="MyGLWindow.h" />
<ClInclude Include="SceneContext.h" />
<ClInclude Include="Shader.h" />
<ClInclude Include="Skybox.h" />
<ClInclude Include="Texture.h" />
<ClInclude Include="textureManager.h" />
<ClInclude Include="TextureViewer.h" />
<ClInclude Include="Viewer.h" />
</ItemGroup>
<ItemGroup>
@ -201,8 +193,12 @@
<None Include="fog.frag" />
<None Include="fog.vert" />
<None Include="base_light.vert" />
<None Include="light_pov.frag" />
<None Include="light_pov.vert" />
<None Include="nmap.frag" />
<None Include="nmap.vert" />
<None Include="shadow_light.frag" />
<None Include="shadow_light.vert" />
<None Include="skybox.frag" />
<None Include="skybox.vert" />
<None Include="spotlight.frag" />
@ -233,6 +229,7 @@
</ItemGroup>
<ItemGroup>
<Image Include="brick1.jpg" />
<Image Include="default.jpg" />
<Image Include="earth.jpg" />
<Image Include="moss.png" />
</ItemGroup>

View File

@ -22,8 +22,8 @@
<Filter Include="Models">
<UniqueIdentifier>{af32f844-b061-4424-a685-e3f537232cf8}</UniqueIdentifier>
</Filter>
<Filter Include="MankyuCode">
<UniqueIdentifier>{b0c228ca-8e29-48df-a9f6-59fb58accad5}</UniqueIdentifier>
<Filter Include="Shaders\PPShaders">
<UniqueIdentifier>{be4b14ab-faab-4412-8a5b-ce9c1510772f}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
@ -69,18 +69,6 @@
<ClCompile Include="Texture.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Skybox.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="fboManager.cpp">
<Filter>MankyuCode</Filter>
</ClCompile>
<ClCompile Include="textureManager.cpp">
<Filter>MankyuCode</Filter>
</ClCompile>
<ClCompile Include="TextureViewer.cpp">
<Filter>MankyuCode</Filter>
</ClCompile>
<ClCompile Include="Multipass.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@ -143,18 +131,6 @@
<ClInclude Include="Texture.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Skybox.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="fboManager.h">
<Filter>MankyuCode</Filter>
</ClInclude>
<ClInclude Include="textureManager.h">
<Filter>MankyuCode</Filter>
</ClInclude>
<ClInclude Include="TextureViewer.h">
<Filter>MankyuCode</Filter>
</ClInclude>
<ClInclude Include="Multipass.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -217,23 +193,27 @@
<None Include="skybox.frag">
<Filter>Shaders</Filter>
</None>
<None Include="textureViewer.vert">
<Filter>MankyuCode</Filter>
</None>
<None Include="light_pov.frag" />
<None Include="light_pov.vert" />
<None Include="shadow_light.frag" />
<None Include="shadow_light.vert" />
<None Include="textureViewer.frag">
<Filter>MankyuCode</Filter>
<Filter>Shaders\PPShaders</Filter>
</None>
<None Include="DSGeometryPass.vert">
<Filter>Shaders</Filter>
</None>
<None Include="DSGeometryPass.frag">
<Filter>Shaders</Filter>
</None>
<None Include="DSLightPass.vert">
<Filter>Shaders</Filter>
<None Include="textureViewer.vert">
<Filter>Shaders\PPShaders</Filter>
</None>
<None Include="DSLightPass.frag">
<Filter>Shaders</Filter>
<Filter>Shaders\PPShaders</Filter>
</None>
<None Include="DSGeometryPass.frag">
<Filter>Shaders\PPShaders</Filter>
</None>
<None Include="DSGeometryPass.vert">
<Filter>Shaders\PPShaders</Filter>
</None>
<None Include="DSLightPass.vert">
<Filter>Shaders\PPShaders</Filter>
</None>
</ItemGroup>
<ItemGroup>
@ -246,5 +226,8 @@
<Image Include="moss.png">
<Filter>Resource Files</Filter>
</Image>
<Image Include="default.jpg">
<Filter>Resource Files</Filter>
</Image>
</ItemGroup>
</Project>

View File

@ -1,10 +1,18 @@
#version 440
//Copy of simple.frag
layout (location = 0) out vec3 gPosition;
layout (location = 1) out vec3 gNormal;
layout (location = 2) out vec4 gAlbedoSpec;
in vec3 f_color;
out vec4 FragColors;
in vec2 TexCoords;
in vec3 FragPos;
in vec3 Normal;
uniform sampler2D tex;
void main()
{
FragColors = vec4(f_color, 1.f);
{
gPosition = FragPos;
gNormal = normalize(Normal);
gAlbedoSpec.rgb = texture(tex, TexCoords).rgb;
gAlbedoSpec.a = 1;
}

View File

@ -1,18 +1,22 @@
#version 440
// Copy of simple.vert
layout(location=0) in vec3 coord3d;
layout(location=1) in vec3 v_normal;
layout(location=2) in vec3 v_color;
layout(location=3) in vec2 v_texmap;
out vec3 f_color;
out vec2 TexCoords;
out vec3 FragPos;
out vec3 Normal;
uniform mat4 mvp;
uniform mat3 NormalMatrix;
uniform mat4 ModelMatrix;
void main(void)
{
f_color = v_color;
{
Normal = NormalMatrix * v_normal;
TexCoords = v_texmap;
FragPos = (ModelMatrix * vec4(coord3d, 1.0f)).xyz;
gl_Position = mvp * vec4(coord3d, 1.0f);
}

View File

@ -1,10 +1,44 @@
#version 440
//Copy of simple.frag
out vec4 FragColor;
in vec3 f_color;
out vec4 FragColors;
in vec2 uv;
uniform sampler2D pos_tex;
uniform sampler2D normal_tex;
uniform sampler2D color_tex;
struct Light {
vec3 Position;
vec3 Color;
};
uniform int NLights;
uniform Light lights[64];
uniform vec3 view_pos;
void main()
{
FragColors = vec4(f_color, 1.f);
vec3 FragPos = texture(pos_tex, uv).rgb;
vec3 Normal = texture(normal_tex, uv).rgb;
vec3 Albedo = texture(color_tex, uv).rgb;
float Specular = 0.5f;
// then calculate lighting as usual
vec3 lighting = Albedo * 0.1; // hard-coded ambient component
vec3 viewDir = normalize(view_pos - FragPos);
for (int i = 0; i < NLights; ++i)
{
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
float spec = pow(max(dot(Normal, H), 0.0), 16.0);
vec3 specular = lights[i].Color * spec * Specular;
lighting += diffuse + specular;
}
FragColor = vec4(lighting, 1.0);
}

View File

@ -1,18 +1,12 @@
#version 440
// Copy of simple.vert
#version 430
layout(location=0) in vec3 coord3d;
layout(location=1) in vec3 v_normal;
layout(location=2) in vec3 v_color;
layout(location=3) in vec2 v_texmap;
layout(location = 0) in vec2 vPosition;
layout(location = 1) in vec2 vUV;
out vec2 uv;
out vec3 f_color;
uniform mat4 mvp;
void main(void)
void main()
{
f_color = v_color;
gl_Position = mvp * vec4(coord3d, 1.0f);
}
gl_Position = vec4(vPosition,0.0,1.0);
uv = vUV;
}

View File

@ -107,6 +107,12 @@ void Dataset::simpleFloor()
colors = { { 0.7, 0.7, 0.7 },{ 0.7, 0.7, 0.7 },{ 0.7, 0.7, 0.7 },
{ 0.7, 0.7, 0.7 },{ 0.7, 0.7, 0.7 },{ 0.7, 0.7, 0.7 } };
tex_mapping = {
{ 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle
{ 1.0f, 1.0f },{ 0.0f, 1.0f },{ 0.0f, 0.0f } //the other triangle
};
genNormals();
}

View File

@ -0,0 +1,37 @@
#version 430
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D gPosition;
uniform sampler2D gNormal;
uniform sampler2D gAlbedoSpec;
subroutine vec4 shading_t();
subroutine uniform shading_t Shading;
subroutine(shading_t)
vec4 DisplayAlbedo()
{
vec3 Albedo = texture(gAlbedoSpec, TexCoords).rgb;
return vec4(Albedo, 0);
}
subroutine(shading_t)
vec4 DisplayPositions()
{
vec3 FragPos = texture(gPosition, TexCoords).rgb;
return vec4(FragPos, 0);
}
subroutine(shading_t)
vec4 DisplayNormals()
{
vec3 Normal = texture(gNormal, TexCoords).rgb;
return vec4(Normal, 0);
}
void main(void)
{
FragColor = Shading();
}

View File

@ -0,0 +1,12 @@
#version 430
layout(location = 0) in vec2 vPosition;
layout(location = 1) in vec2 vUV;
out vec2 TexCoords;
void main()
{
gl_Position = vec4(vPosition,0.0,1.0);
TexCoords = vUV;
}

View File

@ -21,3 +21,10 @@ Light::Light() : location(glm::vec4(0)), intensity(glm::vec3(1))
Light::~Light()
{
}
std::string Light::toString()
{
return "(" + std::to_string(location.x) + ", " +
std::to_string(location.y) + ", " +
std::to_string(location.z) + ")";
}

View File

@ -2,6 +2,7 @@
#include "glm/vec3.hpp"
#include "glm/vec4.hpp"
#include <string>
class Light
{
@ -18,6 +19,8 @@ public:
float spot_exponent, glm::vec4 lookAt);
~Light();
std::string toString();
bool activated = true;
LightType type;
glm::vec3 intensity;

View File

@ -96,7 +96,9 @@ private:
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &shaderStatus);
if (shaderStatus == GL_FALSE)
{
throw std::runtime_error(shaderTypeString + " compilation failed: " + getInfoLog(ObjectType::SHADER, shaderId));
std::string err = getInfoLog(ObjectType::SHADER, shaderId);
std::cout << err << std::endl;
throw std::runtime_error(shaderTypeString + " compilation failed: " + err);
}
else
{
@ -372,10 +374,11 @@ public:
{
// Add the uniform location value for the uniformName key
uniformMap[uniformName] = glGetUniformLocation(programId, uniformName.c_str());
// Check to ensure that the shader contains a uniform with this name
if (uniformMap[uniformName] == -1)
{
{
int error = glGetError();
throw std::runtime_error("Could not add uniform: " + uniformName + " - location returned -1.");
}
else // Valid uniform location? Inform user if we're in debug mode.

View File

@ -20,7 +20,6 @@ public:
void glTranslate(float x, float y, float z)
{
glm::mat4 Trans = glm::translate(glm::mat4(1.0f), glm::vec3(x, y, z));
modelstack.top() = modelstack.top() * Trans;
}

View File

@ -278,9 +278,8 @@ Mesh::MeshEntry::~MeshEntry() {
/**
* Renders this MeshEntry
**/
void Mesh::MeshEntry::render(SceneContext &ctx, Shader *shd) {
textureBinding(shd);
void Mesh::MeshEntry::render(SceneContext &ctx, Shader &shd) {
glBindVertexArray(vao);
if (renderType == NO_INDEX)
{
@ -291,39 +290,16 @@ void Mesh::MeshEntry::render(SceneContext &ctx, Shader *shd) {
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(unsigned int), GL_UNSIGNED_INT, NULL);
}
textureUnbinding();
}
glBindVertexArray(0);
}
void Mesh::MeshEntry::textureBinding(Shader * shd)
{
for (GLuint i = 0; i < textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, textures[i].tex_ref);
shd->addUniform("tex[" + std::to_string(i) + "]", (int)i);
}
if (textures.size() > 0)
shd->addUniform("TexCount", (int)textures.size());
}
void Mesh::MeshEntry::textureUnbinding()
{
for (GLuint i = 0; i < textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
/**
* Mesh constructor, loads the specified filename if supported by Assimp
**/
Mesh::Mesh(const char *filename, Shader *sh)
Mesh::Mesh(const char *filename, std::string vert_shd, std::string frag_shd)
{
shader = sh;
shader = std::make_shared<Shader>(vert_shd, frag_shd);
std::string fullname;
fullname = std::string("./Models/")+ std::string(filename);
directory = fullname;
@ -337,36 +313,33 @@ Mesh::Mesh(const char *filename, Shader *sh)
// Check for errors
if (!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) // if is Not Zero
{
std::cout << "ERROR::ASSIMP:: " << importer.GetErrorString() << std::endl;
return;
std::cout << "ERROR::ASSIMP:: " << importer.GetErrorString() << std::endl;
throw new std::exception(("Error loading mesh at the following location : " + fullname).c_str());
}
// Load all textures
for (unsigned int i = 0; i < scene->mNumMaterials; i++)
{
aiMaterial* material = scene->mMaterials[i];
if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0) { //we only care diffuse texture
aiString Path;
if (material->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL,
NULL, NULL) == AI_SUCCESS) {
std::string FullPath = directory + Path.data; //texture file
textures.emplace(FullPath, Texture(FullPath));
}
}
}
for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
meshEntries.push_back(new Mesh::MeshEntry(scene->mMeshes[i], scene, this));
for (unsigned int i = 0; i < scene->mNumMaterials; i++)
{
aiMaterial* material = scene->mMaterials[i];
if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0)
{
aiString Path;
if (material->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL,
NULL, NULL) == AI_SUCCESS)
{
std::string FullPath = directory + Path.data; //texture file
textures.emplace("0", Texture(FullPath));
}
}
}
}
}
Mesh::Mesh(Dataset &set, Shader *sh)
{
shader = sh;
Mesh::Mesh(Dataset &set, std::string vert_shd, std::string frag_shd)
{
shader = std::make_shared<Shader>(vert_shd, frag_shd);
meshEntries.push_back(new Mesh::MeshEntry(set, this));
}
@ -382,124 +355,41 @@ Mesh::~Mesh(void)
meshEntries.clear();
}
void Mesh::enableCulling()
{
if (cullMode == NONE)
return;
glEnable(GL_CULL_FACE);
glCullFace(cullMode);
}
void Mesh::disableCulling()
{
if (cullMode == NONE)
return;
glDisable(GL_CULL_FACE);
}
void Mesh::textureUnbinding()
{
for (GLuint i = 0; i < textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
void Mesh::textureBinding(Shader *shd)
{
int nmap_counter = 0;
int i = 0;
for (std::map<std::string, Texture>::iterator it = textures.begin(); it != textures.end(); ++it)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, it->second.tex_ref);
shd->applyTextureMaterial(it->second);
if (it->second.isNmap)
{
shd->addUniform("nmap", (int)i);
nmap_counter++;
}
else
shd->addUniform("tex[" + std::to_string(i - nmap_counter) + "]", (int)i);
i++;
}
if (textures.size() > 0)
shd->addUniform("TexCount", (int)textures.size() - nmap_counter);
}
void Mesh::draw(SceneContext &ctx) {
shader->enable();
textureBinding(shader);
enableCulling();
for (unsigned int i = 0; i < meshEntries.size(); ++i) {
MeshEntry * m = meshEntries[i];
// Moving the object to his set position
// Moving the object to his set position
model.glPushMatrix();
effectTransformations();
//Retrieving material data from the vertex
float shininess = meshEntries.at(i)->shininessStrength;
glm::vec3 diffuse = glm::vec3(meshEntries.at(i)->dcolor.r, meshEntries.at(i)->dcolor.g, meshEntries.at(i)->dcolor.b);
glm::vec3 specular = glm::vec3(meshEntries.at(i)->scolor.r, meshEntries.at(i)->scolor.g, meshEntries.at(i)->scolor.b);
glm::vec3 ambient = glm::vec3(meshEntries.at(i)->acolor.r, meshEntries.at(i)->acolor.g, meshEntries.at(i)->acolor.b);
if (glm::length(ambient) == 0 && textures.size() > 0 && textures.begin()->second.mat.enabled)
ambient = textures.begin()->second.mat.ka;
else if (glm::length(ambient) == 0)
ambient = glm::vec3(0.1, 0.1, 0.1);
if (glm::length(diffuse) == 0 && textures.size() > 0 && textures.begin()->second.mat.enabled)
diffuse = textures.begin()->second.mat.ks;
else if (glm::length(diffuse) == 0)
diffuse = glm::vec3(0.9, 0.9, 0.9);
if (glm::length(specular) == 0 && textures.size() > 0 && textures.begin()->second.mat.enabled)
specular = textures.begin()->second.mat.ks;
else if (glm::length(specular) == 0)
specular = glm::vec3(0.4, 0.4, 0.4);
if (shininess == 0 && textures.size() > 0 && textures.begin()->second.mat.enabled)
shininess = textures.begin()->second.mat.shininess;
else if (shininess == 0)
shininess = 150.0f;
shader->mat.ka = ambient;
shader->mat.kd = diffuse;
shader->mat.ks = specular;
shader->mat.shininess = shininess;
// Setting the space matrixes uniques to the object
ctx.modelMatrix = model.getMatrix();
if (shader->uniformFlags & MVP_FLAG)
ctx.mvpMatrix = ctx.projectionMatrix * ctx.viewMatrix * ctx.modelMatrix;
if (shader->uniformFlags & MODELVIEW_FLAG)
ctx.modelViewMatrix = ctx.viewMatrix * ctx.modelMatrix;
if (shader->uniformFlags & NORMAL_MAT_FLAG)
ctx.normalMatrix = glm::mat3(glm::transpose(glm::inverse(ctx.modelViewMatrix)));
ctx.modelMatrix = model.getMatrix();
ctx.mvpMatrix = ctx.projectionMatrix * 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);
shader->addUniform("ModelMatrix", ctx.modelMatrix);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures["0"].tex_ref);
meshEntries.at(i)->render(ctx, *shader);
//Sending the uniform data to the shader
glBindTexture(GL_TEXTURE_2D, 0);
shader->setUniforms(ctx);
meshEntries.at(i)->render(ctx, shader);
model.glPopMatrix();
}
disableCulling();
textureUnbinding();
shader->disable();
}
void Mesh::assignTexture(Texture &texture)
{
//textures.emplace_back(texture);
}
void Mesh::effectTransformations()
{
for (auto pair : _transformations)

View File

@ -60,15 +60,13 @@ public:
MeshEntry(Dataset &set, Mesh *m);
~MeshEntry();
Mesh * parent;
void render(SceneContext &ctx, Shader *shd);
private:
void textureBinding(Shader *shd);
void textureUnbinding();
void render(SceneContext &ctx, Shader &shd);
};
public:
Mesh(const char *filename, Shader *sh);
Mesh(Dataset &dataset, Shader *sh);
Mesh(const char *filename, std::string vert_shd, std::string frag_shd);
Mesh(Dataset &dataset, std::string vert_shd, std::string frag_shd);
~Mesh(void);
std::string directory;
std::vector<MeshEntry*> meshEntries;
@ -77,10 +75,6 @@ public:
private:
std::vector<std::pair<glm::vec4, Transformation>> _transformations;
void enableCulling();
void textureBinding(Shader *shd);
void textureUnbinding();
void disableCulling();
public:
Model model;
CullFace cullMode = NONE;
@ -97,8 +91,7 @@ public:
void removeLastTransformations(int n);
glm::vec3 getPosition();
Shader *shader;
std::shared_ptr<Shader> shader;
void draw(SceneContext &ctx);
void assignTexture(Texture &texture);
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 562 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 846 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 386 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 621 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 840 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 277 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 570 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 640 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 976 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 513 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 729 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

View File

@ -0,0 +1,79 @@
# Blender MTL File: 'nanosuit.blend'
# Material Count: 6
newmtl Arm
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Bump arm_showroom_ddn.png
map_Ka arm_showroom_refl.png
map_Kd arm_dif.png
map_Ks arm_showroom_spec.png
newmtl Body
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Kd body_dif.png
map_Bump body_showroom_ddn.png
map_Ka body_showroom_refl.png
map_Ks body_showroom_spec.png
newmtl Glass
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Bump glass_ddn.png
map_Ka glass_refl.png
map_Kd glass_dif.png
newmtl Hand
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Bump hand_showroom_ddn.png
map_Ka hand_showroom_refl.png
map_Kd hand_dif.png
map_Ks hand_showroom_spec.png
newmtl Helmet
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Bump helmet_showroom_ddn.png
map_Ka helmet_showroom_refl.png
map_Kd helmet_diff.png
map_Ks helmet_showroom_spec.png
newmtl Leg
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Bump leg_showroom_ddn.png
map_Ka leg_showroom_refl.png
map_Kd leg_dif.png
map_Ks leg_showroom_spec.png

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
#include "Multipass.h"
Multipass::Multipass() : shader("textureViewer.vert", "textureViewer.frag")
Multipass::Multipass()
{
shader = new Shader("textureViewer.vert", "textureViewer.frag");
_draw_buffers_size = 0;
glGenFramebuffers(1, &_fboId);
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
@ -13,8 +13,9 @@ Multipass::Multipass() : shader("textureViewer.vert", "textureViewer.frag")
void Multipass::setDrawBuffers()
{
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
glDrawBuffers(_draw_buffers_size, _draw_buffers);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
glDrawBuffers(_draw_buffers_size, _draw_buffers);
int framebuffer_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (framebuffer_status != GL_FRAMEBUFFER_COMPLETE)
throw std::runtime_error("Error during framebuffer initialisation");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
@ -25,8 +26,8 @@ void Multipass::bindToFrameBuffer(GLenum type, GLenum texture, std::string texNa
glFramebufferTexture2D(GL_FRAMEBUFFER, type, texture, _pass_textures[texName], 0);
if (type != GL_DEPTH_ATTACHMENT) {
_draw_buffers[_draw_buffers_size] = type;
_draw_buffers_size++;
}
_draw_buffers_size++;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
@ -35,17 +36,21 @@ Multipass::~Multipass()
glDeleteFramebuffers(1, &_fboId);
}
void Multipass::addTexture(const std::string & tex_name, GLuint filter, GLuint type, GLuint type_2,
bool depth, SceneContext &scnctx)
void Multipass::addTexture(const std::string & tex_name, GLuint filter, GLuint type, GLuint type_2, GLuint type_3,
bool depth, SceneContext &scnctx, GLuint height, GLuint width)
{
height = (height == 0) ? scnctx.height : height;
width = (width == 0) ? scnctx.width : width;
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
glGenTextures(1, &_pass_textures[tex_name]);;
glBindTexture(GL_TEXTURE_2D, _pass_textures[tex_name]);
glTexImage2D(GL_TEXTURE_2D, 0, type, scnctx.width, scnctx.height, 0, type_2, GL_FLOAT, 0);
glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type_2, type_3, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if (depth)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
@ -61,23 +66,128 @@ void Multipass::enableFrameBufferTexture(const std::string tex_name)
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
}
void Multipass::drawResultToScreen(SceneContext & scnctx)
void Multipass::gBufferSetup(SceneContext &scnctx, std::vector<std::shared_ptr<Mesh>>& meshes)
{
//EXAMPLE CODE
glBindFramebuffer(GL_FRAMEBUFFER, 0);
if (shader != nullptr)
delete(shader);
shader = new Shader("textureViewer.vert", "textureViewer.frag");
shader->addUniform("tex", 0);
for (auto mesh : meshes)
mesh->shader = std::make_shared<Shader>("DSGeometryPass.vert", "DSGeometryPass.frag");
scnctx.firstRedraw = false;
}
void Multipass::recomputeDeferredLights(SceneContext &scnctx)
{
if (scnctx.renderMode != DEFERRED_LIGHT)
return;
int i = 0;
for (auto light : scnctx.lights)
{
shader->addUniform("lights[" + std::to_string(i) + "].Position", glm::vec3(light.location));
shader->addUniform("lights[" + std::to_string(i) + "].Color", light.intensity);
i++;
}
shader->addUniform("NLights", (int)scnctx.lights.size());
}
void Multipass::deferredLightSetup(SceneContext &scnctx, std::vector<std::shared_ptr<Mesh>>& meshes)
{
if (shader != nullptr)
delete(shader);
shader = new Shader("DSLightPass.vert", "DSLightPass.frag");
shader->addUniform("pos_tex", 0);
shader->addUniform("normal_tex", 1);
shader->addUniform("color_tex", 2);
for (auto mesh : meshes)
mesh->shader = std::make_shared<Shader>("DSGeometryPass.vert", "DSGeometryPass.frag");
recomputeDeferredLights(scnctx);
scnctx.firstRedraw = false;
}
void Multipass::drawDeferredLightToScreen(SceneContext &scnctx)
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(scnctx.bg.r, scnctx.bg.g, scnctx.bg.b, scnctx.bg.a);
glViewport(0, 0, scnctx.width, scnctx.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.enable();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _pass_textures[_current_tex]);
shader.addUniform("tex", 0);
glBindTexture(GL_TEXTURE_2D, _pass_textures["position_buffer"]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, _pass_textures["normal_buffer"]);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, _pass_textures["color_buffer"]);
shader->enable();
shader->addUniform("view_pos", scnctx.camPos);
glBindVertexArray(_quad_vao);
glBindBuffer(GL_ARRAY_BUFFER, _quad_vbo);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
shader->disable();
}
void Multipass::forwardLightSetup(SceneContext &scnctx, std::vector<std::shared_ptr<Mesh>>& meshes)
{
if (shader != nullptr)
delete(shader);
shader = nullptr;
for (auto mesh : meshes)
{
mesh->shader = std::make_shared<Shader>("tex_base_light.vert", "tex_base_light.frag");
mesh->shader->addUniform("tex", 0);
}
scnctx.firstRedraw = false;
}
void Multipass::drawGBufferToScreen(SceneContext & scnctx)
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, scnctx.width, scnctx.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader->enable();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _pass_textures[_current_tex]);
glBindVertexArray(_quad_vao);
glBindBuffer(GL_ARRAY_BUFFER, _quad_vbo);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
shader.disable();
shader->disable();
}
GLuint Multipass::getCurrentTexture()
{
return _pass_textures[_current_tex];
}
void Multipass::_createQuad()

View File

@ -3,6 +3,7 @@
#include <map>
#include <GL/glew.h>
#include "Shader.h"
#include "Models/Mesh.h"
#include "SceneContext.h"
class Multipass
@ -15,13 +16,25 @@ public:
void setDrawBuffers();
void bindToFrameBuffer(GLenum type, GLenum texture, std::string texName);
void addTexture(const std::string &tex_name, GLuint filter, GLuint type, GLuint type_2,
bool depth, SceneContext &scnctx);
void addTexture(const std::string &tex_name, GLuint filter, GLuint type, GLuint type_2, GLuint type_3,
bool depth, SceneContext &scnctx, GLuint height = 0, GLuint width = 0);
void enableFrameBufferTexture(const std::string tex_name);
void drawResultToScreen(SceneContext &scnctx);
void enableFrameBufferTexture(const std::string tex_name);
Shader shader;
void gBufferSetup(SceneContext &scnctx, std::vector<std::shared_ptr<Mesh>>& meshes);
void recomputeDeferredLights(SceneContext &scnctx);
void drawGBufferToScreen(SceneContext &scnctx);
void deferredLightSetup(SceneContext &scnctx, std::vector<std::shared_ptr<Mesh>>& meshes);
void drawDeferredLightToScreen(SceneContext &scnctx);
void forwardLightSetup(SceneContext &scnctx, std::vector<std::shared_ptr<Mesh>>& meshes);
GLuint getCurrentTexture();
Shader *shader;
private:
void _createQuad();

View File

@ -1,4 +1,5 @@
#include "MyGlWindow.h"
//Getting the projection matrix
glm::mat4x4 perspective(float fovy, float aspect, float near, float far)
{
@ -40,20 +41,152 @@ MyGlWindow::MyGlWindow(int w, int h) :
m_width = w;
m_height = h;
_scnctx.height = m_height;
_scnctx.width = m_width;
scnctx.height = m_height;
scnctx.width = m_width;
setup();
}
MyGlWindow::~MyGlWindow()
{
shaders.clear();
{
}
void MyGlWindow::draw()
{
if (scnctx.renderMode == GBUF_DEBUG)
drawDebugGBuffer();
else if (scnctx.renderMode == DEFERRED_LIGHT)
drawDeferredLight();
else if (scnctx.renderMode == FORWARD_LIGHT)
drawForwardLight();
}
void MyGlWindow::setBgColor(float bgColor[3])
{
_scnctx.bg = glm::vec4(bgColor[0], bgColor[1], bgColor[2], 1);
scnctx.bg = glm::vec4(bgColor[0], bgColor[1], bgColor[2], 1);
}
void MyGlWindow::orbitLights(float timePassed, float speed)
{
float angle = timePassed * speed;
for (auto &&light : scnctx.lights)
{
glm::mat4 rotate = glm::rotate(glm::mat4(1.0f),
glm::radians(angle), glm::vec3(0, 1, 0));
light.location = rotate * light.location;
}
if (scnctx.renderMode == DEFERRED_LIGHT)
multipassManager.recomputeDeferredLights(scnctx);
}
void MyGlWindow::loadScene(SceneChoice scene)
{
meshes.clear();
scnctx.lights.clear();
float pos_x = -20;
float pos_z = -20;
Dataset moddata;
switch (scene)
{
case MOUNTAIN:
meshes.emplace_back(std::make_shared<Mesh>("mountain/mount.blend1.obj", "DSGeometryPass.vert", "DSGeometryPass.frag"));
scnctx.lights.emplace_back(glm::vec3(1, 1, 1), glm::vec4(0, 2, 2, 1));
break;
case CUBES:
moddata.simpleCube();
std::srand(18);
for (int i = 1; i < 401; i++)
{
meshes.emplace_back(std::make_shared<Mesh>(moddata, "DSGeometryPass.vert", "DSGeometryPass.frag"));
meshes[meshes.size() - 1]->textures["0"] = scnctx.textures["BrickTex"];
meshes[meshes.size() - 1]->addStartRotation(glm::vec4(1, 0, 0, 90));
meshes[meshes.size() - 1]->addStartTranslation(glm::vec4(pos_x, 0, 0, 0));
meshes[meshes.size() - 1]->addStartTranslation(glm::vec4(0, 0, pos_z, 0));
pos_x += 2;
if (i % 20 == 0)
{
pos_x = -20;
pos_z += 2;
}
}
for (int i = 0; i < 5; i++)
{
float light_r = (std::rand() % 60) / 100.f;
float light_g = (std::rand() % 60) / 100.f;
float light_b = (std::rand() % 60) / 100.f;
scnctx.lights.emplace_back(glm::vec3(light_r, light_g, light_b),
glm::vec4(std::rand() % 40 - 20, std::rand() % 3 + 3, std::rand() % 40 - 20, 1));
}
break;
case TEAPOTS:
moddata.simpleFloor();
meshes.emplace_back(std::make_shared<Mesh>(moddata, "DSGeometryPass.vert", "DSGeometryPass.frag"));
meshes[meshes.size() - 1]->textures["0"] = scnctx.textures["BrickTex"];
meshes[meshes.size() - 1]->addStartScaling(glm::vec4(3, 1, 3, 0));
meshes[meshes.size() - 1]->addStartTranslation(glm::vec4(0, -1, 0, 0));
moddata.simpleCube();
meshes.emplace_back(std::make_shared<Mesh>(moddata, "DSGeometryPass.vert", "DSGeometryPass.frag"));
meshes[meshes.size() - 1]->textures["0"] = scnctx.textures["DefaultTex"];
meshes[meshes.size() - 1]->addStartTranslation(glm::vec4(-30, 0, 0, 0));
meshes[meshes.size() - 1]->addStartScaling(glm::vec4(1, 5, 30, 0));
meshes.emplace_back(std::make_shared<Mesh>(moddata, "DSGeometryPass.vert", "DSGeometryPass.frag"));
meshes[meshes.size() - 1]->textures["0"] = scnctx.textures["DefaultTex"];
meshes[meshes.size() - 1]->addStartTranslation(glm::vec4(30, 0, 0, 0));
meshes[meshes.size() - 1]->addStartScaling(glm::vec4(1, 5, 30, 0));
meshes.emplace_back(std::make_shared<Mesh>(moddata, "DSGeometryPass.vert", "DSGeometryPass.frag"));
meshes[meshes.size() - 1]->textures["0"] = scnctx.textures["DefaultTex"];
meshes[meshes.size() - 1]->addStartTranslation(glm::vec4(0, 0, 30, 0));
meshes[meshes.size() - 1]->addStartScaling(glm::vec4(30, 5, 1, 0));
meshes.emplace_back(std::make_shared<Mesh>(moddata, "DSGeometryPass.vert", "DSGeometryPass.frag"));
meshes[meshes.size() - 1]->textures["0"] = scnctx.textures["DefaultTex"];
meshes[meshes.size() - 1]->addStartTranslation(glm::vec4(0, 0, -30, 0));
meshes[meshes.size() - 1]->addStartScaling(glm::vec4(30, 5, 1, 0));
pos_x = -30 + 4.5;
pos_z = -30 + 4.5;
for (int i = 1; i < 141; i++)
{
meshes.emplace_back(std::make_shared<Mesh>("teapot.obj", "DSGeometryPass.vert", "DSGeometryPass.frag"));
meshes[meshes.size() - 1]->textures["0"] = scnctx.textures["DefaultTex"];
meshes[meshes.size() - 1]->addStartTranslation(glm::vec4(pos_x, 0, pos_z, 0));
pos_x += 5.9;
if (i % 10 == 0)
{
pos_x = -30 + 4.5;
pos_z += 4;
}
}
for (int i = 0; i < 64; i++)
{
float light_r = (std::rand() % 20) / 100.f;
float light_g = (std::rand() % 20) / 100.f;
float light_b = (std::rand() % 20) / 100.f;
scnctx.lights.emplace_back(glm::vec3(light_r, light_g, light_b),
glm::vec4(std::rand() % 60 - 30, std::rand() % 100 / 100, std::rand() % 60 - 30, 1));
}
break;
default:
break;
}
this->scene = scene;
scnctx.firstRedraw = true;
}
void MyGlWindow::textureSetup()
@ -63,219 +196,156 @@ void MyGlWindow::textureSetup()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
_scnctx.textures.emplace("BrickTex", Texture("brick1.jpg"));
_scnctx.textures.emplace("MossTex", Texture("moss.png"));
_scnctx.textures.emplace("EarthTex", Texture("earth.jpg"));
_scnctx.textures.emplace("OgreTex", Texture("Models/ogre/ogre_diffuse.png"));
_scnctx.textures["OgreTex"].mat.shininess = 3.0f;
_scnctx.textures["OgreTex"].mat.ks = glm::vec3(0.1f, 0.1f, 0.1f);
_scnctx.textures["OgreTex"].mat.ka = glm::vec3(0.3f, 0.3f, 0.3f);
_scnctx.textures["OgreTex"].mat.enabled = true;
_scnctx.textures.emplace("OgreNmap", Texture("Models/ogre/ogre_normalmap.png"));
_scnctx.textures["OgreNmap"].isNmap = true;
_scnctx.textures.emplace("CubeTex", Texture("Models/cube/color_map.jpg"));
_scnctx.textures["CubeTex"].mat.shininess = 3.0f;
_scnctx.textures["CubeTex"].mat.ks = glm::vec3(0.1f, 0.1f, 0.1f);
_scnctx.textures["CubeTex"].mat.ka = glm::vec3(0.3f, 0.3f, 0.3f);
_scnctx.textures["CubeTex"].mat.enabled = true;
_scnctx.textures.emplace("CubeNmap", Texture("Models/cube/normal_map.jpg"));
_scnctx.textures["CubeNmap"].isNmap = true;
scnctx.textures.emplace("BrickTex", Texture("brick1.jpg"));
scnctx.textures.emplace("MossTex", Texture("moss.png"));
scnctx.textures.emplace("EarthTex", Texture("earth.jpg"));
scnctx.textures.emplace("DefaultTex", Texture("default.jpg"));
scnctx.textures.emplace("OgreTex", Texture("Models/ogre/ogre_diffuse.png"));
scnctx.textures["OgreTex"].mat.shininess = 3.0f;
scnctx.textures["OgreTex"].mat.ks = glm::vec3(0.1f, 0.1f, 0.1f);
scnctx.textures["OgreTex"].mat.ka = glm::vec3(0.3f, 0.3f, 0.3f);
scnctx.textures["OgreTex"].mat.enabled = true;
scnctx.textures.emplace("OgreNmap", Texture("Models/ogre/ogre_normalmap.png"));
scnctx.textures["OgreNmap"].isNmap = true;
scnctx.textures.emplace("CubeTex", Texture("Models/cube/color_map.jpg"));
scnctx.textures["CubeTex"].mat.shininess = 3.0f;
scnctx.textures["CubeTex"].mat.ks = glm::vec3(0.1f, 0.1f, 0.1f);
scnctx.textures["CubeTex"].mat.ka = glm::vec3(0.3f, 0.3f, 0.3f);
scnctx.textures["CubeTex"].mat.enabled = true;
scnctx.textures.emplace("CubeNmap", Texture("Models/cube/normal_map.jpg"));
scnctx.textures["CubeNmap"].isNmap = true;
}
void MyGlWindow::shaderSetup()
{
shaders["Simple"] = new Shader("simple.vert", "simple.frag");
shaders["Simple"]->uniformFlags = ShaderFlags::MVP_FLAG;
shaders["BaseLight"] = new Shader("base_light.vert", "base_light.frag");
shaders["Fog"] = new Shader("fog.vert", "fog.frag");
shaders["TexBaseLight"] = new Shader("tex_base_light.vert", "tex_base_light.frag");
shaders["TexNmapLight"] = new Shader("nmap.vert", "nmap.frag");
shaders["TexNmapLight"]->uniformFlags &= ~ShaderFlags::KD_FLAG;
shaders["SpotLight"] = new Shader("spotlight.vert", "spotlight.frag");
shaders["SpotLight"]->light_type = Light::LightType::SPOT;
shaders["TexSpotLight"] = new Shader("tex_spotlight.vert", "tex_spotlight.frag");
shaders["TexSpotLight"]->light_type = Light::LightType::SPOT;
shaders["Silhouette"] = new Shader("silhouette.vert", "silhouette.frag");
shaders["Silhouette"]->uniformFlags = ShaderFlags::MVP_FLAG;
shaders["Silhouette"]->addUniform("fColor", glm::vec3(237 / 255, 229 / 255, 194 / 255));
shaders["Silhouette"]->addUniform("sil_offset", 0.1f);
shaders["Toon"] = new Shader("base_light.vert", "toon.frag");
//Removing useless specular component
shaders["Toon"]->uniformFlags &= ~ShaderFlags::KS_FLAG;
shaders["Toon"]->uniformFlags &= ~ShaderFlags::SHINE_FLAG;
shaders["Skybox"] = new Shader("skybox.vert", "skybox.frag");
shaders["Skybox"]->uniformFlags = MVP_FLAG | MODEL_MATRIX_FLAG | SKYBOX_TEX_FLAG;
shaders["Skybox"]->addUniform("RefractionIndex", glm::vec3(0.65, 0.67, 0.69));
shaders["DSGeometryPass"] = new Shader("DSGeometryPass.vert", "DSGeometryPass.frag");
shaders["DSGeometryPass"]->uniformFlags = MVP_FLAG | KA_FLAG | KD_FLAG | KS_FLAG | SHINE_FLAG;
shaders["DSLightPass"] = new Shader("DSLightPass.vert", "DSLightPass.frag");
shaders["DSLightPass"]->uniformFlags = LIGHTS_FLAG;
}
void MyGlWindow::lightSetup()
{
//Showcase lights
_scnctx.lights.emplace("Spotlight1", Light(glm::vec3(0.8f), glm::vec4(10, 10, 10, 1)));
// 24, 12, 2, glm::vec4(0, 1, 0, 1)));
//Party lights
//_scnctx.lights.emplace("Light1", Light(glm::vec3(0.0f, 0.5f, 0.5f), glm::vec4(10, 10, 0, 1)));
//_scnctx.lights.emplace("Light2", Light(glm::vec3(0.0f, 0.0f, 0.5f), glm::vec4(3.09, 10, 9.51, 1)));
//_scnctx.lights.emplace("Light3", Light(glm::vec3(0.5f, 0.0f, 0.0f), glm::vec4(-8.09, 10, 5.87, 1)));
//_scnctx.lights.emplace("Light4", Light(glm::vec3(0.0f, 0.5f, 0.0f), glm::vec4(-8.09, 10, -5.87, 1)));
//_scnctx.lights.emplace("Light5", Light(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec4(3.09, 10, -9.51, 1)));
}
void MyGlWindow::multipassSetup()
{
_multipassManager.addTexture("render_tex", GL_NEAREST, GL_RGB, GL_RGB, false, _scnctx);
_multipassManager.bindToFrameBuffer(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, "render_tex");
{
multipassManager.addTexture("position_buffer", GL_NEAREST, GL_RGB16F, GL_RGB, GL_FLOAT, false, scnctx);
multipassManager.addTexture("normal_buffer", GL_NEAREST, GL_RGB16F, GL_RGB, GL_UNSIGNED_BYTE, false, scnctx);
multipassManager.addTexture("color_buffer", GL_NEAREST, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, false, scnctx);
multipassManager.addTexture("depth_tex", GL_LINEAR, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_FLOAT, true, scnctx);
multipassManager.bindToFrameBuffer(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, "position_buffer");
multipassManager.bindToFrameBuffer(GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, "normal_buffer");
multipassManager.bindToFrameBuffer(GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, "color_buffer");
multipassManager.bindToFrameBuffer(GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, "depth_tex");
_multipassManager.addTexture("depth_tex", GL_LINEAR, GL_DEPTH_COMPONENT24,
GL_DEPTH_COMPONENT, true, _scnctx);
_multipassManager.bindToFrameBuffer(GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, "depth_tex");
_multipassManager.setDrawBuffers();
_multipassManager.shader.addSubroutine(GL_FRAGMENT_SHADER, "depthing");
_multipassManager.shader.addSubroutine(GL_FRAGMENT_SHADER, "blurring");
_multipassManager.shader.addSubroutine(GL_FRAGMENT_SHADER, "sharpening");
_multipassManager.shader.addSubroutine(GL_FRAGMENT_SHADER, "sepia");
_multipassManager.shader.addSubroutine(GL_FRAGMENT_SHADER, "grayscale");
_multipassManager.shader.addSubroutine(GL_FRAGMENT_SHADER, "sobel_filter");
_multipassManager.shader.addSubroutine(GL_FRAGMENT_SHADER, "absolutely_no_postprocess");
multipassManager.setDrawBuffers();
}
void MyGlWindow::setup()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_BUFFER);
glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_2D);
scnctx.bg = glm::vec4(0.7, 0.7, 0.9, 1);
textureSetup();
shaderSetup();
skybox.initialize("Models/Skybox/", shaders["Skybox"]);
skybox.scale = 10;
_scnctx.skybox_tex = skybox.getTexID();
lightSetup();
multipassSetup();
Dataset moddata;
moddata.checkeredFloor(100, 100, glm::vec3(0.1, 0.1, 0.1), glm::vec3(0.7, 0.7, 0.7));
//TODO : replace by specific light shader that supports color channel (and not just materials)
meshes.emplace("Floor", new Mesh(moddata, shaders["BaseLight"]));
moddata.simpleCube();
//Hardcoded seed for easy scene replication
std::srand(18);
int zob = std::rand();
for (int i = 0; i < 100; i++)
{
std::string cube_name = "Cube" + std::to_string(i);
meshes.emplace(cube_name, new Mesh(moddata, shaders["Simple"]));
meshes[cube_name]->addStartTranslation(glm::vec4(0, 1, 0, 0));
meshes[cube_name]->addStartTranslation(glm::vec4(std::rand() % 100 - 50, 0, 0, 0));
meshes[cube_name]->addStartTranslation(glm::vec4(0, 0, std::rand() % 100 - 50, 0));
meshes[cube_name]->addStartRotation(glm::vec4(1, 0, 0, std::rand() % 360));
meshes[cube_name]->addStartRotation(glm::vec4(0, 1, 0, std::rand() % 360));
meshes[cube_name]->addStartRotation(glm::vec4(0, 0, 1, std::rand() % 360));
}
//meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["TexNmapLight"]));
//meshes["Ogre"]->addTranslation(glm::vec4(-0.5, 1, 0, 0));
//meshes["Ogre"]->assignTexture(_scnctx.textures["OgreTex"]);
//meshes["Ogre"]->assignTexture(_scnctx.textures["OgreNmap"]);
//meshes.emplace("Cube", new Mesh("cube/cube.obj", shaders["TexNmapLight"]));
//meshes["Cube"]->addTranslation(glm::vec4(0.5, 1, 0, 0));
//meshes["Cube"]->assignTexture(_scnctx.textures["CubeTex"]);
//meshes["Cube"]->assignTexture(_scnctx.textures["CubeNmap"]);
//meshes["Buddha"]->addScaling(glm::vec4(4, 4, 4, 0));
//meshes["Buddha"]->addRotation(glm::vec4(0, 1, 0, 180));
//meshes.emplace("Buddha", new Mesh("buddha.obj", shaders["BaseLight"]));
//meshes["Buddha"]->addTranslation(glm::vec4(-4, 3, -4, 0));
//meshes["Buddha"]->addScaling(glm::vec4(4, 4, 4, 0));
//meshes["Buddha"]->addRotation(glm::vec4(0, 1, 0, 180));
//Dataset moddata;
//moddata.simpleCube();
//meshes.emplace("Cube", new Mesh(moddata, shaders["TexBaseLight"]));
//meshes["Cube"]->assignTexture(_scnctx.textures["BrickTex"]);
//meshes["Cube"]->assignTexture(_scnctx.textures["MossTex"]);
//meshes["Cube"]->addTranslation(glm::vec4(4, 3, -4, 0));
//meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"]));
//meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"]));
//meshes["Sponza"]->addTranslation(glm::vec4(0, -200, 0, 1));
//moddata.checkeredFloor(20, 20, glm::vec3(0.1, 0.1, 0.1), glm::vec3(0.7, 0.7, 0.7));
//meshes.emplace("Floor", new Mesh(moddata, shaders["BaseLight"]));
//meshes["Floor"]->addTranslation(glm::vec4(0, -0.05, 0, 1));
//meshes["Floor"]->cullMode = BACK;
//moddata.simpleCube();
//meshes.emplace("Cube", new Mesh(moddata, shaders["BrickBaseLight"]));
//meshes["Cube"]->addTranslation(glm::vec4(0, 1, 0, 1));
//moddata.sphere(1, 100, 100);
//meshes.emplace("Sphere", new Mesh(moddata, shaders["TexBaseLight"]));
//meshes["Sphere"]->assignTexture(_scnctx.textures["EarthTex"]);
//meshes["Sphere"]->addTranslation(glm::vec4(-5, 1, -3, 1));
//meshes.emplace("TeapotSilhouette", new Mesh("teapot.obj", shaders["Silhouette"]));
//meshes["TeapotSilhouette"]->addTranslation(glm::vec4(5, 0 ,3, 1));
//meshes["TeapotSilhouette"]->cullMode = FRONT;
//meshes.emplace("Teapot", new Mesh("teapot.obj", shaders["Toon"]));
//meshes["Teapot"]->addTranslation(glm::vec4(5, 0, 3, 1));
//meshes["Teapot"]->cullMode = BACK;
//meshes.emplace("Teapot", new Mesh("teapot.obj", shaders["Skybox"]));
loadScene(TEAPOTS);
}
void MyGlWindow::draw()
{
_scnctx.height = m_height;
_scnctx.width = m_width;
void MyGlWindow::drawDeferredLight()
{
if (scnctx.firstRedraw)
multipassManager.deferredLightSetup(scnctx, meshes);
scnctx.height = m_height;
scnctx.width = m_width;
glm::vec3 eye(viewer.getViewPoint().x, viewer.getViewPoint().y, viewer.getViewPoint().z);
glm::vec3 look(viewer.getViewCenter().x, viewer.getViewCenter().y, viewer.getViewCenter().z);
glm::vec3 up(viewer.getUpVector().x, viewer.getUpVector().y, viewer.getUpVector().z);
glm::mat4 view = lookAt(eye, look, up); //Calculate view matrix from parameters of m_viewer
glm::mat4 projection = perspective(45.0f, (float)_scnctx.width / (float)_scnctx.height, 0.1f, 2000.0f);
glm::mat4 projection = perspective(45.0f, (float)scnctx.width / (float)scnctx.height, 0.1f, 1000.0f);
_scnctx.viewMatrix = view;
_scnctx.projectionMatrix = projection;
_multipassManager.enableFrameBufferTexture("render_tex");
glClearColor(_scnctx.bg.r, _scnctx.bg.g, _scnctx.bg.b, _scnctx.bg.a);
glViewport(0, 0, _scnctx.width, _scnctx.height);
scnctx.camPos = eye;
scnctx.viewMatrix = view;
scnctx.projectionMatrix = projection;
glClearColor(scnctx.bg.r, scnctx.bg.g, scnctx.bg.b, scnctx.bg.a);
glViewport(0, 0, scnctx.width, scnctx.height);
multipassManager.enableFrameBufferTexture(scnctx.fboDisplayName);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (auto it = meshes.begin(); it != meshes.end(); it++)
(*it).second->draw(_scnctx);
_multipassManager.drawResultToScreen(_scnctx);
for (auto it = meshes.begin(); it != meshes.end(); it++)
(*it)->draw(scnctx);
multipassManager.drawDeferredLightToScreen(scnctx);
}
void MyGlWindow::drawDebugGBuffer()
{
if (scnctx.firstRedraw)
multipassManager.gBufferSetup(scnctx, meshes);
scnctx.height = m_height;
scnctx.width = m_width;
glm::vec3 eye(viewer.getViewPoint().x, viewer.getViewPoint().y, viewer.getViewPoint().z);
glm::vec3 look(viewer.getViewCenter().x, viewer.getViewCenter().y, viewer.getViewCenter().z);
glm::vec3 up(viewer.getUpVector().x, viewer.getUpVector().y, viewer.getUpVector().z);
glm::mat4 view = lookAt(eye, look, up); //Calculate view matrix from parameters of m_viewer
glm::mat4 projection = perspective(45.0f, (float)scnctx.width / (float)scnctx.height, 0.1f, 1000.0f);
scnctx.camPos = eye;
scnctx.viewMatrix = view;
scnctx.projectionMatrix = projection;
glClearColor(scnctx.bg.r, scnctx.bg.g, scnctx.bg.b, scnctx.bg.a);
glViewport(0, 0, scnctx.width, scnctx.height);
multipassManager.enableFrameBufferTexture(scnctx.fboDisplayName);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (auto it = meshes.begin(); it != meshes.end(); it++)
(*it)->draw(scnctx);
multipassManager.drawGBufferToScreen(scnctx);
}
void MyGlWindow::drawForwardLight()
{
if (scnctx.firstRedraw)
multipassManager.forwardLightSetup(scnctx, meshes);
scnctx.height = m_height;
scnctx.width = m_width;
glm::vec3 eye(viewer.getViewPoint().x, viewer.getViewPoint().y, viewer.getViewPoint().z);
glm::vec3 look(viewer.getViewCenter().x, viewer.getViewCenter().y, viewer.getViewCenter().z);
glm::vec3 up(viewer.getUpVector().x, viewer.getUpVector().y, viewer.getUpVector().z);
glm::mat4 view = lookAt(eye, look, up); //Calculate view matrix from parameters of m_viewer
glm::mat4 projection = perspective(45.0f, (float)scnctx.width / (float)scnctx.height, 0.1f, 1000.0f);
scnctx.camPos = eye;
scnctx.viewMatrix = view;
scnctx.projectionMatrix = projection;
glClearColor(scnctx.bg.r, scnctx.bg.g, scnctx.bg.b, scnctx.bg.a);
glViewport(0, 0, scnctx.width, scnctx.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (auto it = meshes.begin(); it != meshes.end(); it++)
{
(*it)->shader->addUniform("view_pos", scnctx.camPos);
int i = 0;
for (auto light : scnctx.lights)
{
(*it)->shader->addUniform("lights[" + std::to_string(i) + "].Position", glm::vec3(light.location));
(*it)->shader->addUniform("lights[" + std::to_string(i) + "].Color", light.intensity);
i++;
}
(*it)->shader->addUniform("NLights", (int)scnctx.lights.size());
(*it)->draw(scnctx);
}
}
void MyGlWindow::resize(int w, int h)
{

View File

@ -4,7 +4,6 @@
#include "GL/glew.h"
#include <string>
#include <vector>
#include "Skybox.h"
#include "Shader.h"
#include "ModelView.h"
#include "Viewer.h"
@ -14,6 +13,13 @@
#include "imgui/stb_image.h"
#include "Multipass.h"
enum SceneChoice
{
MOUNTAIN,
CUBES,
TEAPOTS
};
struct vertexAttr {
GLfloat posX, posY, posZ;
GLfloat r, g, b;
@ -23,32 +29,35 @@ class MyGlWindow {
public:
MyGlWindow(int w, int h);
~MyGlWindow();
void draw();
void setBgColor(float bgColor[3]);
std::map<std::string, Shader *> shaders;
std::map<std::string, Mesh *> meshes;
void orbitLights(float timePassed, float speed);
void loadScene(SceneChoice scene);
std::vector<std::shared_ptr<Mesh>> meshes;
void resize(int w, int h);
Viewer viewer;
SceneContext _scnctx;
SceneContext scnctx;
Multipass multipassManager;
SceneChoice scene;
private:
int m_width;
int m_height;
float _bgColor[3];
Multipass _multipassManager;
Skybox skybox;
GLuint _vaoHandle;
GLuint _iboHandle;
void drawDebugGBuffer();
void drawForwardLight();
void drawDeferredLight();
void textureSetup();
void shaderSetup();
void lightSetup();
void multipassSetup();
void setup();
};

View File

@ -6,9 +6,16 @@
#include "Texture.h"
#include "Light.h"
enum RenderMode
{
GBUF_DEBUG,
DEFERRED_LIGHT,
FORWARD_LIGHT
};
struct SceneContext
{
std::map<std::string, Light> lights;
std::vector<Light> lights;
std::map<std::string, float> floatValues;
glm::mat4x4 viewMatrix;
@ -19,7 +26,7 @@ struct SceneContext
glm::mat4x4 modelMatrix;
glm::mat3x3 normalMatrix;
GLuint skybox_tex;
GLuint skyboxTex;
std::map<std::string, Texture> textures;
@ -27,15 +34,9 @@ struct SceneContext
GLuint width;
glm::vec4 bg;
void adjustSpots()
{
for (auto it : lights)
{
if (it.second.type == Light::SPOT)
{
it.second.location = it.second.location * viewMatrix;
it.second.direction = (viewMatrix * it.second.lookAt - it.second.location * viewMatrix);
}
}
}
std::string fboDisplayName;
glm::vec3 camPos;
RenderMode renderMode;
bool firstRedraw;
};

View File

@ -19,75 +19,3 @@ void Shader::disable()
_program.disable();
}
int Shader::addSubroutine(GLenum shadertype, const std::string uniformName)
{
return _program.addSubroutine(shadertype, uniformName);
}
void Shader::enableSubroutine(const std::string uniformName)
{
return _program.enableSubroutine(uniformName);
}
void Shader::setMaterial(SceneContext ctx)
{
if ((uniformFlags & ShaderFlags::KA_FLAG) == ShaderFlags::KA_FLAG)
glUniform3fv(_program.addUniform("Ka"), 1, glm::value_ptr(mat.ka));
if ((uniformFlags & ShaderFlags::KD_FLAG) == ShaderFlags::KD_FLAG)
glUniform3fv(_program.addUniform("Kd"), 1, glm::value_ptr(mat.kd));
if ((uniformFlags & ShaderFlags::KS_FLAG) == ShaderFlags::KS_FLAG)
glUniform3fv(_program.addUniform("Ks"), 1, glm::value_ptr(mat.ks));
if ((uniformFlags & ShaderFlags::SHINE_FLAG) == ShaderFlags::SHINE_FLAG)
glUniform1f(_program.addUniform("Shininess"), mat.shininess);
}
void Shader::setCamera(SceneContext ctx)
{
if ((uniformFlags & ShaderFlags::MVP_FLAG) == ShaderFlags::MVP_FLAG)
glUniformMatrix4fv(_program.addUniform("mvp"), 1, GL_FALSE, glm::value_ptr(ctx.mvpMatrix));
if ((uniformFlags & ShaderFlags::NORMAL_MAT_FLAG) == ShaderFlags::NORMAL_MAT_FLAG)
glUniformMatrix3fv(_program.addUniform("NormalMatrix"), 1, GL_FALSE, glm::value_ptr(ctx.normalMatrix));
if ((uniformFlags & ShaderFlags::MODELVIEW_FLAG) == ShaderFlags::MODELVIEW_FLAG)
glUniformMatrix4fv(_program.addUniform("ModelViewMatrix"), 1, GL_FALSE, glm::value_ptr(ctx.modelViewMatrix));
if ((uniformFlags & ShaderFlags::MODEL_MATRIX_FLAG) == ShaderFlags::MODEL_MATRIX_FLAG)
glUniformMatrix4fv(_program.addUniform("ModelMatrix"), 1, GL_FALSE, glm::value_ptr(ctx.modelMatrix));
}
void Shader::setLights(SceneContext ctx)
{
int i = 0;
if ((uniformFlags & ShaderFlags::LIGHTS_FLAG) != ShaderFlags::LIGHTS_FLAG)
return;
for (auto light: ctx.lights)
{
if (light.second.activated && light.second.type == light_type)
{
std::string lightname = "Light[" + std::to_string(i) + "].";
glUniform4fv(_program.addUniform(lightname + "Position"), 1, glm::value_ptr(ctx.viewMatrix * light.second.location));
glUniform3fv(_program.addUniform(lightname + "Intensity"), 1, glm::value_ptr(light.second.intensity));
if (light.second.type == Light::LightType::SPOT)
{
glUniform1f(_program.addUniform(lightname + "SpotCutoff"), light.second.spot_cutoff);
glUniform1f(_program.addUniform(lightname + "SpotInnerCutoff"), light.second.spot_inner_cutoff);
glUniform1f(_program.addUniform(lightname + "SpotExponent"), light.second.spot_exponent);
glUniform3fv(_program.addUniform(lightname + "SpotDirection"), 1, glm::value_ptr(light.second.direction));
}
i++;
}
}
glUniform1i(_program.addUniform("LightCount"), i);
}
void Shader::setUniforms(SceneContext ctx)
{
setCamera(ctx);
setLights(ctx);
setMaterial(ctx);
if ((uniformFlags & ShaderFlags::SKYBOX_TEX_FLAG) == ShaderFlags::SKYBOX_TEX_FLAG)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, ctx.skybox_tex);
}
}

View File

@ -11,38 +11,18 @@
#include "Loader.h"
#include "Texture.h"
enum ShaderFlags
{
NO_UNIFORM = 0,
KD_FLAG = 1,
KA_FLAG = 2,
KS_FLAG = 4,
SHINE_FLAG = 8,
MVP_FLAG = 16,
NORMAL_MAT_FLAG = 32,
MODELVIEW_FLAG = 64,
LIGHTS_FLAG = 128,
MODEL_MATRIX_FLAG = 256,
SKYBOX_TEX_FLAG = 512,
};
class Shader
{
public:
Shader() {};
Shader(const std::string vtx_name, const std::string frag_name);
~Shader();
int uniformFlags = KD_FLAG | KA_FLAG | KS_FLAG | SHINE_FLAG | MVP_FLAG | NORMAL_MAT_FLAG | MODELVIEW_FLAG | LIGHTS_FLAG;
Light::LightType light_type = Light::LightType::BASE;
Material mat;
void enable();
void disable();
int addSubroutine(GLenum shadertype, const std::string uniformName);
void enableSubroutine(const std::string uniformName);
public:
void setUniforms(SceneContext ctx);
@ -62,25 +42,15 @@ public:
_program.use(); glUniform1fv(_program.addUniform(name), 1, &data);
}
void addUniform(const std::string name, glm::mat3x3 &data)
{
_program.use(); glUniformMatrix3fv(_program.addUniform(name), 1, GL_FALSE, glm::value_ptr(data));
}
void addUniform(const std::string name, glm::mat4x4 &data)
{
_program.use(); glUniformMatrix4fv(_program.addUniform(name), 1, GL_FALSE, glm::value_ptr(data));
}
void applyTextureMaterial(Texture &tex)
{
if (mat.enabled)
{
addUniform("Ka", tex.mat.ka);
addUniform("Kd", tex.mat.kd);
addUniform("Ks", tex.mat.ks);
}
}
private:
void setMaterial(SceneContext ctx);
void setCamera(SceneContext ctx);
void setLights(SceneContext ctx);
}
ShaderProgram _program;
};

View File

@ -1,141 +0,0 @@
#include "Skybox.h"
#include "imgui/stb_image.h"
Skybox::Skybox()
{
float side = 50.0f; //size of cube
float side2 = side / 2.0f;
float v[24 * 3] = {
// Front
-side2, -side2, side2,
side2, -side2, side2,
side2, side2, side2,
-side2, side2, side2,
// Right
side2, -side2, side2,
side2, -side2, -side2,
side2, side2, -side2,
side2, side2, side2,
// Back
-side2, -side2, -side2,
-side2, side2, -side2,
side2, side2, -side2,
side2, -side2, -side2,
// Left
-side2, -side2, side2, //12
-side2, side2, side2, //13
-side2, side2, -side2, //14
-side2, -side2, -side2, //15
// Bottom
-side2, -side2, side2,
-side2, -side2, -side2,
side2, -side2, -side2,
side2, -side2, side2,
// Top
-side2, side2, side2,
side2, side2, side2,
side2, side2, -side2,
-side2, side2, -side2
};
GLuint el[] = {
0,2,1, 0,3,2, 4,6,5, 4,7,6,
8,10,9, 8,11,10, 12,14,13, 12,15,14,
16,18,17, 16,19,18, 20,22,21, 20,23,22
};
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vector_vbo);
glBindBuffer(GL_ARRAY_BUFFER, vector_vbo);
glBufferData(GL_ARRAY_BUFFER, 24 * 3 * sizeof(GLfloat), v, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, NULL);
glEnableVertexAttribArray(0);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLuint), el, GL_STATIC_DRAW);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(4);
}
void Skybox::initialize(std::string skybox_dir, Shader *sky)
{
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texID); //set the texID as a member variable
glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
const char * suffixes[] = { "left", "right", "top", "down", "back", "front"};
GLuint targets[] = {
GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
};
for (int i = 0; i < 6; i++) {
int channel, width, height;
unsigned char *image;
std::string filename;
filename = std::string(skybox_dir + suffixes[i]) + std::string(".JPG");
image = stbi_load(filename.c_str(), &width, &height, &channel, 0);
glTexImage2D(targets[i], 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(image);
}
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
sky->enable();
sky->addUniform("DrawSkyBox", (int)GL_FALSE);
sky->addUniform("CubeMapTex", (int)0);
sky->disable();
}
Skybox::~Skybox()
{
glDeleteBuffers(1, &vector_vbo);
glDeleteBuffers(1, &ibo);
glDeleteVertexArrays(1, &vao);
}
// //Things to add
// //Per model :
// // - ModelMatrix
// // - MaterialColor
void Skybox::draw(Shader *sky_shader, SceneContext &ctx)
{
sky_shader->enable();
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glm::mat4x4 modelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(scale));
glm::mat4x4 mvpMatrix = ctx.projectionMatrix * ctx.viewMatrix * modelMatrix;
sky_shader->addUniform("mvp", mvpMatrix);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
glBindVertexArray(vao);
sky_shader->addUniform("DrawSkyBox", (int)GL_TRUE);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLuint), GL_UNSIGNED_INT, 0);
sky_shader->addUniform("DrawSkyBox", (int)GL_FALSE);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
glDisable(GL_CULL_FACE);
sky_shader->disable();
}

View File

@ -1,30 +0,0 @@
#pragma once
#include <string>
#include <iostream>
#include <vector>
#include <GL/glew.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Shader.h"
#include "SceneContext.h"
class Skybox
{
private:
unsigned int vao;
unsigned int vector_vbo;
unsigned int ibo;
GLuint texID;
public:
Skybox();
float scale = 1;
void initialize(std::string skybox_dir, Shader *sky);
GLuint getTexID() { return texID; }
~Skybox();
void draw(Shader *sky_shader, SceneContext &ctx);
};

View File

@ -18,6 +18,8 @@ double cx, cy;
int g_width;
int g_height;
bool reset = false;
void window_size_callback(GLFWwindow* window, int width, int height)
{
g_width = width;
@ -25,9 +27,11 @@ void window_size_callback(GLFWwindow* window, int width, int height)
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
if (key == GLFW_KEY_R && action == GLFW_PRESS)
reset = true;
}
static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos)
@ -155,6 +159,54 @@ bool createWindow(GLFWwindow **window)
return true;
}
void FPSDisplay(int width)
{
static int frameCount = 0;
static int fps = 60;
static double previousTime = glfwGetTime();
double currentTime = glfwGetTime();
frameCount++;
// If a second has passed.
if (currentTime - previousTime >= 1.0)
{
fps = frameCount;
frameCount = 0;
previousTime = currentTime;
}
if (ImGui::Begin("FPSCounterOutline1", false,
ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoFocusOnAppearing))
ImGui::TextColored(ImVec4(0, 0, 0, 1), std::to_string(fps).c_str());
ImGui::End();
if (ImGui::Begin("FPSCounterOutline2", false,
ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoFocusOnAppearing))
ImGui::TextColored(ImVec4(0, 0, 0, 1), std::to_string(fps).c_str());
ImGui::End();
if (ImGui::Begin("FPSCounterOutline3", false,
ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoFocusOnAppearing))
ImGui::TextColored(ImVec4(0, 0, 0, 1), std::to_string(fps).c_str());
ImGui::End();
if (ImGui::Begin("FPSCounterOutline4", false,
ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoFocusOnAppearing))
ImGui::TextColored(ImVec4(0, 0, 0, 1), std::to_string(fps).c_str());
ImGui::End();
if (ImGui::Begin("FPSCounter", false,
ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoFocusOnAppearing))
ImGui::TextColored(ImVec4(1, 1, 1, 1), std::to_string(fps).c_str());
ImGui::End();
ImGui::SetWindowPos("FPSCounterOutline1", ImVec2(width - 49.f, 20.f), ImGuiCond_Once);
ImGui::SetWindowPos("FPSCounterOutline2", ImVec2(width - 51.f, 20.f), ImGuiCond_Once);
ImGui::SetWindowPos("FPSCounterOutline3", ImVec2(width - 50.f, 19.f), ImGuiCond_Once);
ImGui::SetWindowPos("FPSCounterOutline4", ImVec2(width - 50.f, 21.f), ImGuiCond_Once);
ImGui::SetWindowPos("FPSCounter", ImVec2(width - 50.f, 20.f), ImGuiCond_Once);
}
int loop(GLFWwindow *window)
{
std::printf("OpenGL %s, GLSL %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION));
@ -165,56 +217,141 @@ int loop(GLFWwindow *window)
glfwGetWindowSize(window, &width, &heigth);
MyGlWindow glWin(width, heigth);
int frameCount = 0;
int fps = 60;
double previousTime = glfwGetTime();
glWin.scnctx.fboDisplayName = "position_buffer";
bool is_selected = false;
glWin.scnctx.renderMode = DEFERRED_LIGHT;
glWin.scnctx.firstRedraw = true;
std::vector<std::string> lights_strs;
char const **strings = nullptr;
int current_light = 0;
bool orbiting_lights = true;
float light_speed = 50.0f;
double prev_time = glfwGetTime();
double sinceLastFrame;
while (!glfwWindowShouldClose(window))
{
sinceLastFrame = glfwGetTime() - prev_time;
prev_time = glfwGetTime();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if (ImGui::Begin("First Window"))
if (ImGui::Begin("Scene settings"))
{
ImGui::SetWindowPos(ImVec2(20, 20));
if (ImGui::BeginCombo("Debug Buffer", glWin.scnctx.fboDisplayName.c_str()))
{
if (ImGui::Selectable("Position buffer", &is_selected, 0, ImVec2(120, 10)))
glWin.scnctx.fboDisplayName = "position_buffer";
if (ImGui::Selectable("Normal buffer", &is_selected, 0, ImVec2(120, 10)))
glWin.scnctx.fboDisplayName = "normal_buffer";
if (ImGui::Selectable("Color buffer", &is_selected, 0, ImVec2(120, 10)))
glWin.scnctx.fboDisplayName = "color_buffer";
static float bgColor[3] = { 0.6f, 0.6f, 0.9f };
ImGui::ColorEdit3("Background", bgColor, 0);
glWin.setBgColor(bgColor);
ImGui::EndCombo();
}
ImGui::SliderFloat("Light X", &(glWin._scnctx.lights["Spotlight1"].location.x), -20, 20);
ImGui::SliderFloat("Light Y", &(glWin._scnctx.lights["Spotlight1"].location.y), -20, 20);
ImGui::SliderFloat("Light Z", &(glWin._scnctx.lights["Spotlight1"].location.z), -20, 20);
ImGui::SliderFloat("Light X Intensity", &(glWin._scnctx.lights["Spotlight1"].intensity.x), 0, 1);
ImGui::SliderFloat("Light Y Intensity", &(glWin._scnctx.lights["Spotlight1"].intensity.y), 0, 1);
ImGui::SliderFloat("Light Z Intensity", &(glWin._scnctx.lights["Spotlight1"].intensity.z), 0, 1);
if (ImGui::BeginCombo("Render Mode",
(glWin.scnctx.renderMode == DEFERRED_LIGHT) ? "Deferred Lights" : (
(glWin.scnctx.renderMode == FORWARD_LIGHT) ? "Forward Lights" : "Debug GBuffers")))
{
if (ImGui::Selectable("Draw Debug GBuffers", &is_selected, 0, ImVec2(120, 10)))
{
glWin.scnctx.renderMode = GBUF_DEBUG;
glWin.scnctx.firstRedraw = true;
}
if (ImGui::Selectable("Draw with Deferred Lights", &is_selected, 0, ImVec2(120, 10)))
{
glWin.scnctx.renderMode = DEFERRED_LIGHT;
glWin.scnctx.firstRedraw = true;
}
if (ImGui::Selectable("Draw with Forward Lights", &is_selected, 0, ImVec2(120, 10)))
{
glWin.scnctx.renderMode = FORWARD_LIGHT;
glWin.scnctx.firstRedraw = true;
}
ImGui::EndCombo();
}
if (ImGui::BeginCombo("Scene Choice",
(glWin.scene == MOUNTAIN) ? "Mountain" :
(glWin.scene == CUBES) ? "Cubes" : "Teapots"))
{
if (ImGui::Selectable("Mountain", &is_selected, 0, ImVec2(100, 10)))
glWin.loadScene(SceneChoice::MOUNTAIN);
if (ImGui::Selectable("Cubes", &is_selected, 0, ImVec2(100, 10)))
glWin.loadScene(SceneChoice::CUBES);
if (ImGui::Selectable("Teapots", &is_selected, 0, ImVec2(100, 10)))
glWin.loadScene(SceneChoice::TEAPOTS);
ImGui::EndCombo();
}
ImGui::SetWindowPos(ImVec2(20, 20));
}
ImGui::End();
double currentTime = glfwGetTime();
frameCount++;
// If a second has passed.
if (currentTime - previousTime >= 1.0)
{
fps = frameCount;
frameCount = 0;
previousTime = currentTime;
if (reset)
{
glWin.loadScene(SceneChoice::MOUNTAIN);
reset = false;
}
if (ImGui::Begin("FPSCounter", false,
ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoFocusOnAppearing))
{
ImGui::TextColored(ImVec4(1, 1, 0, 1), std::to_string(fps).c_str());
if (ImGui::Begin("Light management"))
{
lights_strs.clear();
for (auto light : glWin.scnctx.lights)
lights_strs.push_back(light.toString());
std::free(strings);
strings = (char const **)std::malloc(sizeof(char *) * (lights_strs.size() + 1));
std::memset((void *)strings, 0, sizeof(char *) * (lights_strs.size() + 1));
for (int i = 0; i < lights_strs.size(); i++)
strings[i] = lights_strs[i].c_str();
ImGui::ListBox("Lights", &current_light, strings, lights_strs.size());
if (glWin.scnctx.lights.size() > 0)
{
if (ImGui::SliderFloat("Selected light X", &glWin.scnctx.lights[current_light].location.x, -10, 10)
&& 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 == 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 == DEFERRED_LIGHT)
glWin.multipassManager.recomputeDeferredLights(glWin.scnctx);
if (ImGui::SliderFloat("Selected light R", &glWin.scnctx.lights[current_light].intensity.x, 0, 1)
&& glWin.scnctx.renderMode == DEFERRED_LIGHT)
glWin.multipassManager.recomputeDeferredLights(glWin.scnctx);
if (ImGui::SliderFloat("Selected light G", &glWin.scnctx.lights[current_light].intensity.y, 0, 1)
&& glWin.scnctx.renderMode == DEFERRED_LIGHT)
glWin.multipassManager.recomputeDeferredLights(glWin.scnctx);
if (ImGui::SliderFloat("Selected light B", &glWin.scnctx.lights[current_light].intensity.z, 0, 1)
&& glWin.scnctx.renderMode == DEFERRED_LIGHT)
glWin.multipassManager.recomputeDeferredLights(glWin.scnctx);
}
ImGui::Checkbox("Orbit Lights", &orbiting_lights);
ImGui::SliderFloat("Orbit degrees / second", &light_speed, 0, 360);
}
ImGui::End();
ImGui::SetWindowPos("FPSCounter", ImVec2(width - 50, 20), ImGuiCond_Once);
FPSDisplay(width);
glfwSwapBuffers(window);
glWin.draw();
glfwSwapBuffers(window);
glWin.draw();
if (orbiting_lights)
glWin.orbitLights(sinceLastFrame, light_speed);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
@ -234,9 +371,6 @@ int loop(GLFWwindow *window)
return 0;
}
// upload to padlet.com/mksung89/cameraControl2
int main()
{
GLFWwindow *win;

View File

@ -1,135 +0,0 @@
#include "TextureViewer.h"
TextureViewer::TextureViewer()
{
this->s = new ShaderProgram();
this->fs = "default.frag";
this->vs = "default.vs";
this->depth = false;
setUpShaders();
}
TextureViewer::TextureViewer(GLuint tex, std::string vs, std::string fs) {
this->s = new ShaderProgram();
this->texture = tex;
this->fs = fs;
this->vs = vs;
this->depth = false;
setUpShaders();
}
TextureViewer::~TextureViewer()
{
delete s;
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &VAO);
}
void TextureViewer::draw() {
//Bind VAO
glBindVertexArray(VAO);
//Bind VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
s->use();
glUniform1i(s->uniform("depth"), this->depth);
//Bind texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->texture);
//Draw
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
//Unbind texture for sure
glBindTexture(GL_TEXTURE_2D, 0);
s->disable();
//Unbind VBO - just for sure
glBindBuffer(GL_ARRAY_BUFFER, 0);
//Unbind VAO
glBindVertexArray(0);
}
void TextureViewer::setDepthOnly(bool depth) {
this->depth = depth;
}
//Called only once in constructor
void TextureViewer::setUpShaders()
{
//We need only vertex and fragment shaders
s->initFromFiles(this->vs,this->fs);
s->use();
//Create uniforms and attributes (filled later)
s->addUniform("tex");
s->addUniform("depth");
//Quad verticles - omitted z coord, because it will always be 1
float pos[] = {
-1.0, 1.0,
1.0, 1.0,
-1.0, -1.0,
1.0, -1.0
};
float uv[] = {
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0
};
//Generate VAO
glGenVertexArrays(1, &VAO);
//Bind VAO
glBindVertexArray(VAO);
//Generate VBO
glGenBuffers(1, &VBO);
//Bind VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//Alocate buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(pos)+sizeof(uv), NULL, GL_STATIC_DRAW);
//Fill VBO
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(pos), pos);
glBufferSubData(GL_ARRAY_BUFFER, 8 * sizeof(float), sizeof(uv), uv);
//Fill attributes and uniforms
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, (sizeof(float)* 2), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, (sizeof(float)* 2), (GLvoid*)(sizeof(float)* 8));
glUniform1i(s->uniform("tex"), 0);
s->disable();
//unbind buffer
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
//Setters
void TextureViewer::setTexture(GLuint tex) {
this->texture = tex;
}
//Getters
GLuint TextureViewer::getTexture() {
return this->texture;
}

View File

@ -1,57 +0,0 @@
#define GLM_ENABLE_EXPERIMENTAL
#ifndef CTEXTUREVIEWER_H
#define CTEXTUREVIEWER_H
#include <iostream>
#include <map>
#include <vector>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Loader.h"
/*
* Simple class, which will render texture on screen
*/
class TextureViewer
{
private:
GLuint texture;
//VBO - don't need EBO, i'll use glDrawArrays()
GLuint VBO;
//VAO - needed for glDrawArrays()
GLuint VAO;
ShaderProgram * s;
//Default shaders
std::string vs;
std::string fs;
bool depth;
void setUpShaders();
public:
TextureViewer();
TextureViewer(GLuint tex, std::string vs, std::string fs);
void draw();
//Setters
void setTexture(GLuint tex);
void setDepthOnly(bool depth);
//Getters
GLuint getTexture();
~TextureViewer();
};
#endif

Binary file not shown.

Before

Width:  |  Height:  |  Size: 266 B

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -1,118 +0,0 @@
#include "fboManager.h"
#include <iostream>
FboManager::FboManager() {
useRenderDepthBuffer = false;
attachmentCount = 0;
/*for(int i=0;i<5;i++) {
mrt[i] = GL_COLOR_ATTACHMENT0+i;
}*/
}
FboManager::~FboManager() {
glDeleteFramebuffers(1, &_fboId);
if(useRenderDepthBuffer) {
glDeleteRenderbuffers(1, &_renderDepthBufferId);
}
}
//create FBO
void FboManager::initFbo() {
glGenFramebuffers(1, &_fboId);
}
//generate and bind depth buffer
void FboManager::genRenderDepthBuffer(unsigned w, unsigned h) {
glGenRenderbuffers(1,&_renderDepthBufferId);
glBindRenderbuffer(GL_RENDERBUFFER, _renderDepthBufferId);
glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT,w,h);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
//bind depth buffer
void FboManager::bindRenderDepthBuffer() {
glBindFramebuffer(GL_FRAMEBUFFER,_fboId);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,_renderDepthBufferId);
//glBindFramebuffer(GL_FRAMEBUFFER,0);
}
void FboManager::setDrawBuffers() {
glDrawBuffers(attachmentCount, mrt);
}
void FboManager::bindToFbo(GLenum type, GLenum texture, GLuint textureId) {
glBindFramebuffer(GL_FRAMEBUFFER,_fboId);
glFramebufferTexture2D(GL_FRAMEBUFFER,type,texture,textureId,0);
if(type != GL_DEPTH_ATTACHMENT) {
mrt[attachmentCount] = type;
attachmentCount += 1;
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
}
//glBindFramebuffer(GL_FRAMEBUFFER,0);
}
void FboManager::bind3DTextureToFbo(GLenum type, GLuint textureId) {
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
glFramebufferTexture(GL_FRAMEBUFFER, type, textureId, 0);
//glFramebufferTexture3D(GL_FRAMEBUFFER, type, GL_TEXTURE_3D, textureId, 0, 0);
if (type != GL_DEPTH_ATTACHMENT) {
mrt[attachmentCount] = type;
attachmentCount += 1;
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
}
}
GLuint FboManager::getFboId() {
return _fboId;
}
bool FboManager::checkFboStatus() {
// check FBO status
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
switch(status)
{
case GL_FRAMEBUFFER_COMPLETE_EXT:
std::cout << "Framebuffer complete." << std::endl;
return true;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
std::cout << "[ERROR] Framebuffer incomplete: Attachment is NOT complete." << std::endl;
return false;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
std::cout << "[ERROR] Framebuffer incomplete: No image is attached to FBO." << std::endl;
return false;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
std::cout << "[ERROR] Framebuffer incomplete: Attached images have different dimensions." << std::endl;
return false;
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
std::cout << "[ERROR] Framebuffer incomplete: Color attached images have different internal formats." << std::endl;
return false;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
std::cout << "[ERROR] Framebuffer incomplete: Draw buffer." << std::endl;
return false;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
std::cout << "[ERROR] Framebuffer incomplete: Read buffer." << std::endl;
return false;
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
std::cout << "[ERROR] Unsupported by FBO implementation." << std::endl;
return false;
default:
std::cout << "[ERROR] Unknow error." << std::endl;
return false;
}
}

View File

@ -1,49 +0,0 @@
/*
* File: fboManager.h
* Desc: Jednoduchy "manager" pro FBO objekty
*
*/
#define GLM_ENABLE_EXPERIMENTAL
#ifndef FBOMANAGER_H
#define FBOMANAGER_H
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtc/type_ptr.hpp>
class FboManager {
public:
FboManager();
~FboManager();
//check fbo status
bool checkFboStatus();
//create a renderbuffer object
void genRenderDepthBuffer(unsigned w, unsigned h);
//bind a renderbuffer object
void bindRenderDepthBuffer();
void bindToFbo(GLenum type, GLenum texture, GLuint textureId);
void bind3DTextureToFbo(GLenum type, GLuint textureId);
void initFbo();
void setDrawBuffers();
GLuint getFboId();
private:
GLuint _fboId;
GLuint _renderDepthBufferId;
bool useRenderDepthBuffer;
unsigned attachmentCount;
GLenum mrt[6];
};
#endif

View File

@ -45,7 +45,7 @@ Collapsed=0
[Window][First Window]
Pos=20,20
Size=357,188
Size=330,60
Collapsed=0
[Window][FPSCounter]
@ -53,3 +53,38 @@ Pos=1030,20
Size=85,32
Collapsed=0
[Window][Scene settings]
Pos=20,20
Size=314,106
Collapsed=0
[Window][FPSCounterOutline]
Pos=1031,21
Size=32,32
Collapsed=0
[Window][FPSCounterOutline4]
Pos=1030,21
Size=32,32
Collapsed=0
[Window][FPSCounterOutline3]
Pos=1030,19
Size=32,32
Collapsed=0
[Window][FPSCounterOutline2]
Pos=1029,20
Size=32,32
Collapsed=0
[Window][FPSCounterOutline1]
Pos=1031,20
Size=32,32
Collapsed=0
[Window][Light management]
Pos=422,2
Size=475,163
Collapsed=0

View File

@ -0,0 +1,5 @@
#version 440
void main(void)
{
}

View File

@ -0,0 +1,13 @@
#version 440
layout(location=0) in vec3 coord3d;
layout(location=1) in vec3 v_normal;
layout(location=2) in vec3 v_color;
layout(location=3) in vec2 v_texmap;
uniform mat4 lightmvp;
void main(void)
{
gl_Position = lightmvp * vec4(coord3d, 1.0f);
}

View File

@ -0,0 +1,69 @@
#version 440
out vec4 FragColor;
in vec2 TexCoords;
in vec3 fragPos;
in vec3 fragNormal;
in vec4 fragPosLightSpace;
uniform vec3 Kd;
uniform vec3 Ka;
uniform vec3 Ks;
uniform float Shininess;
struct LightInfo {
vec4 Position;
vec3 Intensity;
};
uniform LightInfo Light[64];
uniform int LightCount;
uniform uint hasTextureDiffuse;
uniform sampler2D tex;
uniform sampler2D depth_tex;
float ShadowCalculation(vec4 PInLightSpace)
{
// perform perspective divide
vec3 projCoords = PInLightSpace.xyz / PInLightSpace.w;
// transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5; //why? because it has a values [-1, 1], convert to [0, 1]
// get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closestDepth = texture(depth_tex, projCoords.xy).r;
// get depth of current fragment from light's perspective
float currentDepth = projCoords.z;
// check whether current frag pos is in shadow
float bias = 0.005;
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
// Supress the limit of the far / close plane
if (projCoords.z > 1.0)
shadow = 0.0;
return shadow;
}
void main()
{
//calculate diffuse, specular, ambient
vec3 c = vec3(1.0);
if (hasTextureDiffuse == 1) {
c = texture(tex, TexCoords).rgb;
}
float shadow = ShadowCalculation(fragPosLightSpace);
vec3 L = normalize(Light[LightCount - 1].Position.xyz - fragPos);
vec3 N = fragNormal;
vec3 V = normalize(-fragPos);
//vec3 R = normalize(reflect(-L, N));
vec3 H = normalize(V + L);
vec3 ambient = Ka * Light[LightCount - 1].Intensity;
vec3 diffuse = Kd * Light[LightCount - 1].Intensity * max(dot(L, N), 0.0);
vec3 specular = Ks * Light[LightCount - 1].Intensity * pow(max(dot(H, N), 0.0), Shininess);
vec3 final = (ambient + (1-shadow)*(diffuse + specular))*c;
FragColor = vec4(final,1.0);
}

View File

@ -0,0 +1,25 @@
#version 440
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 v_color;
layout (location = 3) in vec2 texCoords;
out vec2 TexCoords;
out vec3 fragPos;
out vec3 fragNormal;
out vec4 fragPosLightSpace;
uniform mat4 mvp;
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 lightmvp;
void main()
{
fragNormal = normalize(NormalMatrix * normal);
fragPos = vec3(ModelViewMatrix * vec4(position,1.0));
TexCoords = texCoords;
fragPosLightSpace = lightmvp * vec4(position,1.0);
gl_Position = mvp * vec4(position, 1.0f);
}

View File

@ -3,57 +3,44 @@
in vec3 f_color;
out vec4 FragColors;
uniform vec3 Kd;
uniform vec3 Ka;
uniform vec3 Ks;
uniform float Shininess;
struct LightInfo {
vec4 Position;
vec3 Intensity;
struct Light {
vec3 Position;
vec3 Color;
};
uniform LightInfo Light[64];
uniform int LightCount;
uniform Light lights[64];
uniform int NLights;
in vec3 fNormal;
in vec3 pos;
in vec2 texCoord;
uniform int TexCount;
uniform sampler2D tex[32];
uniform sampler2D tex;
uniform vec3 view_pos;
void main()
{
vec3 finalColor;
vec3 diffuse_sum;
vec3 specular_sum;
vec3 ambient;
ambient = Ka * Light[0].Intensity;
for (int i=0; i<LightCount; i++)
vec4 texColor = texture(tex, texCoord);
//Hardcoded ambient
ambient = texColor.rgb * 0.1;
for (int i = 0; i < NLights; i++)
{
vec3 L = normalize(Light[i].Position.xyz - pos);
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 * Light[i].Intensity * max(dot(L, N), 0.0);
vec3 specular = Ks * Light[i].Intensity * 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[0], texCoord);
for (int i=1; i < TexCount; i++)
{
vec4 new_tex = texture(tex[i], texCoord);
texColor = mix(new_tex, texColor, new_tex.a);
}
FragColors = (vec4(diffuse_sum + ambient, 1) * texColor + vec4(specular_sum, 1.0));
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);
}

View File

@ -1,101 +0,0 @@
#include "textureManager.h"
#define MAX_GRID_SIZE 32
TextureManager::TextureManager() : clearTextureExtension(true) {
}
TextureManager::~TextureManager() {
textures.clear();
}
void TextureManager::add(const string& texture) {
GLuint tmpId;
glGenTextures(1, &tmpId);
textures[texture] = tmpId;
}
void TextureManager::createTexture(const string& texture, const string filePath,unsigned w, unsigned h, GLuint filter, GLuint type, GLuint type_2, bool depth = false)
{
// SDL_Surface *surface;
GLuint textureid;
int mode;
add(texture);
glBindTexture(GL_TEXTURE_2D, textures[texture]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//No image data - for frameBuffer
if(filePath.empty()) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexImage2D(GL_TEXTURE_2D, 0 ,type, w, h, 0, type_2, GL_FLOAT, 0);
glTexImage2D(GL_TEXTURE_2D, 0 ,type, w, h, 0, type_2, GL_FLOAT, 0);
if (depth) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
//glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
}
}
}
void TextureManager::createRGBA16F3DTexture(const string& texture, glm::vec3 dim, GLuint filter, GLuint wrap) {
GLuint tex;
add(texture);
glBindTexture(GL_TEXTURE_3D, textures[texture]);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, wrap);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, wrap);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, wrap);
//glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F, w, h, d, 0, GL_RGBA, GL_FLOAT, NULL);
//glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F, w, h, d, 0, GL_RGBA, GL_FLOAT, &emptyData[0]);
std::vector<GLfloat> emptyData(dim.x * dim.y * dim.z * sizeof(float), 0.0);
glBindTexture(GL_TEXTURE_3D, textures[texture]);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F, dim.x, dim.y, dim.z, 0, GL_RGBA, GL_FLOAT, &emptyData[0]);
glBindTexture(GL_TEXTURE_3D, 0);
//clear3Dtexture(textures[texture], dim);
//glBindTexture(GL_TEXTURE_3D, 0);
}
void TextureManager::clear3Dtexture(GLuint texture) {
if (clearTextureExtension) {
GLfloat data[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
glClearTexImage(texture, 0, GL_RGBA, GL_FLOAT, &data[0]);
}
else {
//MUCH SLOWER version, but should work on version lower than 4.4
std::vector<GLfloat> emptyData(MAX_GRID_SIZE * MAX_GRID_SIZE * MAX_GRID_SIZE * sizeof(float), 0.0);
glBindTexture(GL_TEXTURE_3D, texture);
//glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F, MAX_GRID_SIZE, MAX_GRID_SIZE, MAX_GRID_SIZE, 0, GL_RGBA, GL_FLOAT, &emptyData[0]);
//or
glTexSubImage3D(GL_TEXTURE_3D, 0, 0,0,0, MAX_GRID_SIZE, MAX_GRID_SIZE, MAX_GRID_SIZE, GL_RGBA, GL_FLOAT, &emptyData[0]);
glBindTexture(GL_TEXTURE_3D, 0);
}
}
void TextureManager::createRGBA3DTexture(const string& texture, glm::vec3 dim, GLuint filter, GLuint wrap) {
GLuint tex;
add(texture);
glBindTexture(GL_TEXTURE_3D, textures[texture]);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, wrap);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, wrap);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, wrap);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, dim.x, dim.y, dim.z, 0, GL_RGBA, GL_FLOAT, NULL);
//glBindTexture(GL_TEXTURE_3D, 0);
}
GLuint TextureManager::operator[] (const string& texture) {
return textures[texture];
}

View File

@ -1,41 +0,0 @@
#define GLM_ENABLE_EXPERIMENTAL
#ifndef TEXTUREMANAGER_H
#define TEXTUREMANAGER_H
#include <iostream>
#include <map>
#include <vector>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtc/type_ptr.hpp>
using namespace std;
class TextureManager {
public:
TextureManager();
~TextureManager();
GLuint operator[](const string& texture);
void add(const string& texture);
void createTexture(const string& texture, const string filePath, unsigned w, unsigned h,
GLuint filter, GLuint type, GLuint type_2, bool depth);
void createRGBA16F3DTexture(const string& texture, glm::vec3 dim, GLuint filter, GLuint wrap);
void createRGBA3DTexture(const string& texture, glm::vec3 dim, GLuint filter, GLuint wrap);
void clear3Dtexture(GLuint texture);
void setClearTextureExtension(bool v) {
clearTextureExtension = v;
}
private:
map<string,GLuint> textures;
bool clearTextureExtension;
};
#endif

View File

@ -6,144 +6,7 @@ out vec4 final_color;
uniform sampler2D tex;
subroutine vec4 shading_t(vec4 color);
subroutine uniform shading_t Shading;
const float f = 1.0 / 300.0;
vec2 offsets[9] = vec2[](
vec2(-f, f), vec2(0.0, f), vec2(f, f),
vec2(-f, 0.0), vec2(0.0, 0.0), vec2(f, 0.0),
vec2(-f, -f), vec2(0.0, -f), vec2(f, -f)
);
subroutine(shading_t)
vec4 absolutely_no_postprocess(vec4 color)
{
return color;
}
subroutine(shading_t)
vec4 sepia(vec4 color)
{
return vec4(color.r * .393 + color.g * .769 + color.b * .189,
color.r * .349 + color.g * .686 + color.b * .168,
color.r * .272 + color.g * .534 + color.b * .131, 1.0);
}
subroutine(shading_t)
vec4 grayscale(vec4 color)
{
float average = color.r * 0.2126 + color.g * 0.7152 + color.b * 0.0722;
return vec4(average, average, average, 1);
}
subroutine(shading_t)
vec4 depthing(vec4 color)
{
float zNear = 0.1f; // zNear of your perspective projection
float zFar = 2000.0f; // zFar of your perspective projection
float depth = color.x;
float r = (2.0 * zNear) / (zFar + zNear - depth * (zFar - zNear));
return vec4(r, r, r, 1);
}
subroutine(shading_t)
vec4 blurring(vec4 color)
{
float blur_kernel[9] = float[](
1.0 / 16, 2.0 / 16, 1.0 / 16,
2.0 / 16, 4.0 / 16, 2.0 / 16,
1.0 / 16, 2.0 / 16, 1.0 / 16
);
vec3 texblur = vec3(0,0,0);
for (int i = 0; i < 9; i++)
texblur += vec3(texture(tex, uv + offsets[i]).rgb * blur_kernel[i]);
return vec4(texblur, 1);
}
subroutine(shading_t)
vec4 sharpening(vec4 color)
{
float sharp_kernel[9] = float[](
-1, -1, -1,
-1, 9, -1,
-1, -1, -1
);
vec3 texsharp = vec3(0,0,0);
for (uint i = 0; i < 9; i++)
texsharp += texture(tex, uv + offsets[i]).rgb * sharp_kernel[i];
return vec4(texsharp, 1);
}
subroutine(shading_t)
vec4 sobel_filter(vec4 color)
{
float sobel_x[9] = float[](
-1, 0, 1,
-2, 0, 2,
-1, 0, 1
);
float sobel_y[9] = float[](
-1, -2, -1,
0, 0, 0,
1, 2, 1
);
vec3 vec_x = vec3(0);
vec3 vec_y = vec3(0);
for (int i = 0; i < 9; i++)
{
vec_x += texture(tex, uv + offsets[i]).rgb * sobel_x[i];
vec_y += texture(tex, uv + offsets[i]).rgb * sobel_y[i];
}
vec4 sobeled = vec4(sqrt(vec_x * vec_x + vec_y * vec_y), 1);
return grayscale(sobeled);
}
void main()
{
//rrra because depth textures are not usual textures, they have only one channel
final_color = texture(tex, uv).rgba;
final_color = Shading(final_color);
// if (depth) {
// float d;
// d = LinearizeDepth(uv);
// final_color = vec4(d,d,d,1.0);
// }
// else if (blur)
// {
// final_color = vec4(blurring_func(uv), 1);
// }
// else if (sharpen)
// {
// final_color = vec4(sharpening_func(uv));
// }
// else if (sepia)
// {
// final_color = vec4(final_color.r * .393 + final_color.g * .769 + final_color.b * .189,
// final_color.r * .349 + final_color.g * .686 + final_color.b * .168,
// final_color.r * .272 + final_color.g * .534 + final_color.b * .131, 1.0);
// }
// else if (grayscale)
// {
// float average = final_color.r * 0.2126 + final_color.g * 0.7152 + final_color.b * 0.0722;
// final_color = vec4(average, average, average, 1.0);
// }
// else if (sobel)
// {
// vec3 sobeled = sobel_filter(final_color.rgb);
// float average = sobeled.r * 0.2126 + sobeled.g * 0.7152 + sobeled.b * 0.0722;
// final_color = vec4(average, average, average, 1.0);
// }
{
final_color = texture(tex, uv).rgba;
}

View File

@ -9,4 +9,4 @@ void main()
{
gl_Position = vec4(vPosition,0.0,1.0);
uv = vUV;
}
}