Fixed out uniforms not always working, advancing on dataset rendering

This commit is contained in:
Hurlu 2019-03-13 23:32:49 +09:00
parent 8f595cab38
commit 9b42d26f05
14 changed files with 236 additions and 31 deletions

View File

@ -184,6 +184,8 @@
<ClInclude Include="Viewer.h" />
</ItemGroup>
<ItemGroup>
<None Include="fog.frag" />
<None Include="fog.vert" />
<None Include="light.frag" />
<None Include="light.vert">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Geometry</ShaderType>
@ -197,6 +199,8 @@
<FileType>Document</FileType>
</None>
<None Include="onelight.frag" />
<None Include="silhouette.frag" />
<None Include="silhouette.vert" />
<None Include="simple.vert" />
<None Include="toon.frag" />
</ItemGroup>

View File

@ -148,5 +148,17 @@
<None Include="toon.frag">
<Filter>Shaders</Filter>
</None>
<None Include="silhouette.vert">
<Filter>Shaders</Filter>
</None>
<None Include="silhouette.frag">
<Filter>Shaders</Filter>
</None>
<None Include="fog.frag">
<Filter>Shaders</Filter>
</None>
<None Include="fog.vert">
<Filter>Shaders</Filter>
</None>
</ItemGroup>
</Project>

View File

@ -5,4 +5,9 @@ class Dataset
public:
Dataset();
~Dataset();
unsigned int nFaces;
unsigned int nVertices;
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> normals;
};

View File

@ -18,6 +18,54 @@
* Constructor, loading the specified aiMesh
**/
Mesh::MeshEntry::MeshEntry(Dataset &set, Mesh *m)
{
parent = m;
vbo[VERTEX_BUFFER] = NULL;
vbo[TEXCOORD_BUFFER] = NULL;
vbo[NORMAL_BUFFER] = NULL;
vbo[INDEX_BUFFER] = NULL;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
elementCount = set.nFaces * 3;
float *vertices = new float[set.nVertices * 3];
for (unsigned int i = 0; i < set.nVertices; ++i) {
vertices[i * 3] = set.vertices[i].x;
vertices[i * 3 + 1] = set.vertices[i].y;
vertices[i * 3 + 2] = set.vertices[i].z;
}
glGenBuffers(1, &vbo[VERTEX_BUFFER]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, 3 * set.nVertices * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
delete[] vertices;
float *normals = new float[set.nVertices * 3];
for (unsigned int i = 0; i < set.nVertices; ++i) {
normals[i * 3] = set.normals[i].x;
normals[i * 3 + 1] = set.normals[i].y;
normals[i * 3 + 2] = set.normals[i].z;
}
glGenBuffers(1, &vbo[NORMAL_BUFFER]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, 3 * set.nVertices * sizeof(GLfloat), normals, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(1);
delete[] normals;
}
Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
{
parent = m;
@ -186,6 +234,13 @@ Mesh::Mesh(const char *filename, Shader *sh)
}
Mesh::Mesh(Dataset &set, Shader *sh)
{
shader = sh;
meshEntries.push_back(new Mesh::MeshEntry(set, this));
}
/**
* Clears all loaded MeshEntries
**/
@ -198,9 +253,21 @@ Mesh::~Mesh(void)
meshEntries.clear();
}
void Mesh::enableCulling()
{
glEnable(GL_CULL_FACE);
glCullFace(cullMode);
}
void Mesh::disableCulling()
{
glDisable(GL_CULL_FACE);
}
void Mesh::draw(SceneContext ctx) {
shader->enable();
enableCulling();
for (unsigned int i = 0; i < meshEntries.size(); ++i) {
MeshEntry * m = meshEntries[i];
@ -249,6 +316,7 @@ void Mesh::draw(SceneContext ctx) {
meshEntries.at(i)->render();
model.glPopMatrix();
}
disableCulling();
shader->disable();
}

View File

@ -20,6 +20,14 @@ enum Transformation
Scaling
};
enum CullFace
{
NONE = GL_NONE,
FRONT = GL_FRONT,
BACK = GL_BACK,
FRONT_BACK = GL_FRONT_AND_BACK
};
class Mesh
{
public:
@ -36,6 +44,7 @@ public:
aiColor3D scolor;
float shininessStrength;
MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m);
MeshEntry(Dataset &set, Mesh *m);
~MeshEntry();
Mesh * parent;
void render();
@ -49,8 +58,12 @@ public:
private:
std::vector<std::pair<glm::vec4, Transformation>> _transformations;
void enableCulling();
void disableCulling();
public:
Model model;
CullFace cullMode = NONE;
void effectTransformations();
void addRotation(glm::vec4 vec);
void addTranslation(glm::vec4 vec);

View File

@ -63,20 +63,27 @@ void MyGlWindow::setup()
_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)));
//shaders["ToonShader"] = Shader("light.vert", "toon.frag");
shaders["StandardLight"] = new Shader("light.vert", "light.frag");
//shaders["FogShader"] = new Shader("fog.vert", "fog.frag");
//shaders["SilhouetteShader"] = new Shader("silhouette.vert", "silhouette.frag");
//shaders["SilhouetteShader"]->uniformFlags = ShaderFlags::MVP_FLAG;
//shaders["SilhouetteShader"]->addUniform("fColor", glm::vec3(237/255, 229/255, 194/255));
//shaders["SilhouetteShader"]->addUniform("sil_offset", 0.1f);
//
//shaders["ToonShader"] = new Shader("light.vert", "toon.frag");
////Removing useless specular component
//shaders["ToonShader"].uniformFlags &= ~ShaderFlags::KS_FLAG;
//shaders["ToonShader"].uniformFlags &= ~ShaderFlags::SHINE_FLAG;
//meshes.emplace_back("teapot.obj", &shaders["ToonShader"]);
ToonShader = new Shader("light.vert", "toon.frag");
ToonShader->uniformFlags &= ~ShaderFlags::KS_FLAG;
ToonShader->uniformFlags &= ~ShaderFlags::SHINE_FLAG;
meshes.emplace_back("teapot.obj", ToonShader);
//shaders["ToonShader"]->uniformFlags &= ~ShaderFlags::KS_FLAG;
//shaders["ToonShader"]->uniformFlags &= ~ShaderFlags::SHINE_FLAG;
//
meshes.push_back(new Mesh("quad.obj", shaders["StandardLight"]));
/*meshes.push_back(new Mesh("teapot.obj", shaders["SilhouetteShader"]));
meshes.back()->cullMode = FRONT;
meshes.push_back(new Mesh("teapot.obj", shaders["ToonShader"]));
meshes.back()->cullMode = BACK;*/
}
void MyGlWindow::draw()
@ -85,12 +92,7 @@ void MyGlWindow::draw()
glViewport(0, 0, m_width, m_height);
glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_BUFFER);
glEnable(GL_BLEND);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//setting the view data in the scene context
glm::vec3 eye(viewer.getViewPoint().x, viewer.getViewPoint().y, viewer.getViewPoint().z);
glm::vec3 look(viewer.getViewCenter().x, viewer.getViewCenter().y, viewer.getViewCenter().z);
@ -100,9 +102,8 @@ void MyGlWindow::draw()
_scnctx.viewMatrix = view;
_scnctx.projectionMatrix = projection;
static int i = 0;
for (auto it = meshes.begin(); it != meshes.end(); ++it)
it->draw(_scnctx);
for (auto it = meshes.begin(); it != meshes.end(); it++)
(*it)->draw(_scnctx);
}
void MyGlWindow::resize(int w, int h)

View File

@ -24,14 +24,13 @@ public:
void draw();
void setBgColor(float bgColor[3]);
std::map<std::string, Shader> shaders;
std::vector<Mesh> meshes;
std::map<std::string, Shader *> shaders;
std::vector<Mesh *> meshes;
void resize(int w, int h);
Viewer viewer;
private:
Shader *ToonShader;
SceneContext _scnctx;
int m_width;
int m_height;
@ -41,7 +40,5 @@ private:
GLuint _vaoHandle;
GLuint _iboHandle;
std::vector<Mesh> _drawables;
void setup();
};

View File

@ -3,6 +3,7 @@
#include <vector>
#include <memory>
#include <GL/glew.h>
#include <functional>
#include "glm/gtc/type_ptr.hpp"
#include "Material.h"
@ -39,6 +40,14 @@ public:
public:
void setUniforms(SceneContext ctx);
void addUniform(const std::string name, glm::vec3 data)
{
_program.use(); glUniform3fv(_program.addUniform(name), 1, glm::value_ptr(data)); ; _program.disable();
}
void addUniform(const std::string name, float data)
{
_program.use(); glUniform1fv(_program.addUniform(name), 1, &data); _program.disable();
}
private:
void setMaterial(SceneContext ctx);
void setCamera(SceneContext ctx);

53
BaseGLProject/fog.frag Normal file
View File

@ -0,0 +1,53 @@
#version 430
in vec3 f_color;
out vec4 FragColors;
uniform vec3 Kd;
uniform vec3 Ka;
uniform vec3 Ks;
uniform float Shininess;
uniform int LightCount;
struct LightInfo {
vec4 Position;
vec3 Intensity;
};
uniform LightInfo Light[64];
in vec3 fNormal;
in vec3 pos;
void main()
{
vec3 finalColor;
vec3 ambient;
ambient = Ka * Light[0].Intensity;
for (int i=0; i< LightCount; i++)
{
vec3 L = normalize(Light[i].Position.xyz - pos);
vec3 N = fNormal;
vec3 V = normalize(-pos);
//vec3 R = normalize(reflect(-L, N));
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);
finalColor = finalColor + diffuse + specular;
}
finalColor = finalColor + ambient;
//fog calculus
float z = abs(pos.z);
float f = (30 - z) / (30 - 1);
vec3 fogColor = vec3(1, 1, 1);
f = clamp(f, 0.0, 1.0);
finalColor = mix(fogColor, finalColor, f);
FragColors = vec4(finalColor, 1);
}

21
BaseGLProject/fog.vert Normal file
View File

@ -0,0 +1,21 @@
#version 430
layout(location=0) in vec3 coord3d;
layout(location=1) in vec3 v_normal;
layout(location=2) in vec3 v_color;
uniform mat4 mvp;
uniform mat3 NormalMatrix;
uniform mat4 ModelViewMatrix;
out vec3 fNormal;
out vec3 pos;
void main(void)
{
fNormal = normalize(NormalMatrix * v_normal);
pos = (ModelViewMatrix * vec4(coord3d, 1.0)).xyz;
gl_Position = mvp * vec4(coord3d, 1.0f);
}

View File

@ -13,7 +13,8 @@ struct LightInfo {
vec3 Intensity;
};
uniform LightInfo Light[5];
uniform LightInfo Light[64];
uniform int LightCount;
in vec3 fNormal;
@ -26,7 +27,7 @@ void main()
vec3 ambient;
ambient = Ka * Light[0].Intensity;
for (int i=0; i<5; i++)
for (int i=0; i<LightCount; i++)
{
vec3 L = normalize(Light[i].Position.xyz - pos);
vec3 N = fNormal;

View File

@ -11,7 +11,6 @@ uniform mat4 ModelViewMatrix;
out vec3 fNormal;
out vec3 pos;
void main(void)
{
fNormal = normalize(NormalMatrix * v_normal);

View File

@ -0,0 +1,9 @@
#version 430
uniform vec3 fColor;
out vec4 FragColors;
void main()
{
FragColors = vec4(fColor, 1.f);
}

View File

@ -0,0 +1,13 @@
#version 430
layout(location=0) in vec3 coord3d;
layout(location=1) in vec3 v_normal;
layout(location=2) in vec3 v_color;
uniform mat4 mvp;
uniform float sil_offset;
void main(void)
{
gl_Position = mvp * vec4(coord3d + v_normal * sil_offset, 1.0f);
}