fixed mesh duplicates algorithm (and some render issues)

This commit is contained in:
Anselme 2016-01-07 19:55:53 +01:00
parent 160ebc0d2d
commit 698ce5b62a
10 changed files with 86 additions and 39 deletions

View File

@ -42,8 +42,15 @@ in vec4 posInLightSpace;
in vec2 varTexCoord;
#ifndef AMBIENT_LIGHT
in vec3 lightDirInView;
in vec3 halfVecInView;
#endif
#ifdef POINT_LIGHT
in vec3 lightDistInView;
uniform float attenuation;
#endif
layout(location = 0)out vec4 outColor;
@ -95,10 +102,17 @@ void main(void) {
float shadow = 1;
#endif
float att = 0;
#ifdef POINT_LIGHT
att = length(lightDistInView)*attenuation;
if(att > 1)
discard;
#endif
#ifdef AMBIENT_LIGHT
outColor = vec4(0, 0, 0, 1);
outColor = vec4(diffuse*0.1f, 1);
#else
vec3 light = phongLighting(diffuse, specular, materialNs, lightColor, normal, lightDirInView, halfVecInView);
outColor = vec4(shadow*light, 1);
outColor = vec4(light*shadow*(1+cos(1.57 + att*1.57)), 1);
#endif
}

View File

@ -34,6 +34,10 @@ out vec3 lightDirInView;
out vec3 halfVecInView;
#endif
#ifdef POINT_LIGHT
out vec3 lightDistInView;
#endif
#ifdef SHADOWMAP
uniform mat4 lightMVP;
out vec4 posInLightSpace;
@ -52,8 +56,9 @@ void main(void) {
#endif
#ifdef POINT_LIGHT
vec4 lightPosInView = viewMatrix*vec4(pointLight, 1);
lightDirInView = normalize(lightPosInView.xyz - posInView);
vec3 lightPosInView = vec3(viewMatrix*vec4(pointLight, 1));
lightDistInView = lightPosInView - posInView;
lightDirInView = normalize(lightDistInView);
halfVecInView = normalize(lightDirInView - normalize(posInView));
#endif

View File

@ -2,7 +2,7 @@
uniform sampler2DRect colorSampler;
uniform float exposition;
uniform vec3 minMaxMean;
uniform float gamma;
layout(location = 0)out vec4 outColor;
@ -11,8 +11,9 @@ void main(void) {
ivec2 pos = ivec2(gl_FragCoord.xy);
vec3 color = texelFetch(colorSampler, pos).xyz;
vec3 mapped = (color-minMaxMean.r)/(minMaxMean.g-minMaxMean.r);
// Exposure tone mapping
vec3 mapped = vec3(1.0) - exp(-color * 1);
//vec3 mapped = vec3(1.0) - exp(-color * exposition);
// Gamma correction
mapped = pow(mapped, vec3(1.0 / gamma));

View File

@ -43,6 +43,11 @@ void ForwardModule::renderGL(Camera* myCamera, Scene* scene)
void ForwardModule::lightPass(Camera* myCamera, Scene* scene, Light* light)
{
if(isWireframe){
glAssert(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE));
}else{
glAssert(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL));
}
// loop over all types of geometry
for(int i=0; i<geometryFlagList.size(); ++i)
{
@ -75,7 +80,7 @@ void ForwardModule::lightPass(Camera* myCamera, Scene* scene, Light* light)
case Light::POINT:
shader->bindVec3(shader->getLocation("pointLight"), light->getPos());
shader->bindVec3(shader->getLocation("lightColor"), light->getColor());
// TODO add attenuation
shader->bindFloat(shader->getLocation("attenuation"), light->getAttenuation());
break;
case Light::SPOT:
shader->bindVec3(shader->getLocation("lightColor"), light->getColor());

View File

@ -18,7 +18,8 @@ class ForwardModule : public Module
public:
ForwardModule() :
shaderSources(NULL),
renderTarget(FrameBuffer::screen)
renderTarget(FrameBuffer::screen),
isWireframe(false)
{}
virtual void renderGL(Camera* myCamera, Scene* scene);
@ -30,6 +31,7 @@ public:
void compileShaders(Scene* scene);
void setRenderTarget(const FrameBuffer* target);
void setWireframe(bool wireframe) {isWireframe = wireframe;}
private:
static const char* const flagStr[NB_FLAGS];
@ -40,6 +42,8 @@ private:
std::vector<unsigned int> lightFlagList;
const FrameBuffer* renderTarget;
bool isWireframe;
void lightPass(Camera* myCamera, Scene* scene, Light* light);
};

View File

@ -39,7 +39,7 @@ void Light::initDirectionnalLight(glm::vec3 dir, glm::vec3 lightColor)
viewMatrix = glm::lookAt(position, position+direction, glm::vec3(0, 1, 0));
}
void Light::initPointLight(glm::vec3 pos, glm::vec3 lightColor)
void Light::initPointLight(glm::vec3 pos, glm::vec3 lightColor, float att)
{
type = POINT;
position = pos;
@ -48,6 +48,7 @@ void Light::initPointLight(glm::vec3 pos, glm::vec3 lightColor)
shadowCaster = false;
isDir = false;
viewMatrix = glm::mat4();
attenuation = att;
}
void Light::initSpotLight(glm::vec3 pos, glm::vec3 dir, float spotAngle, glm::vec3 lightColor)
@ -95,6 +96,7 @@ void Light::initShadowMap(int resWidth, int resHeight, glm::vec3 dim)
void Light::generateShadowMap(Scene* scene)
{
glAssert(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL));
glAssert(glViewport(0, 0, shadowMapWidth, shadowMapHeight));
shadowMap->bindFBO();
glAssert(glClearDepth(1.0));

View File

@ -33,7 +33,7 @@ public:
Light();
void initDirectionnalLight(glm::vec3 dir = glm::vec3(1, 0, 0), glm::vec3 lightColor = glm::vec3(1));
void initPointLight(glm::vec3 pos = glm::vec3(0), glm::vec3 lightColor = glm::vec3(1));
void initPointLight(glm::vec3 pos = glm::vec3(0), glm::vec3 lightColor = glm::vec3(1), float att = 1);
void initSpotLight(glm::vec3 pos = glm::vec3(0), glm::vec3 dir = glm::vec3(1, 0, 0), float spotAngle = 360, glm::vec3 lightColor = glm::vec3(1));
bool isDirectionnal();
@ -41,12 +41,15 @@ public:
glm::vec3 getDir() {return direction;}
glm::vec3 getPos() {return position;}
glm::vec3 getColor() {return color;}
float getAttenuation() {return attenuation;}
bool isShadowCaster() {return shadowCaster;}
void initShadowMap(int resWidth, int resHeight, glm::vec3 dim = glm::vec3(1));
void generateShadowMap(Scene* scene);
Texture* getShadowMap();
void setAttenuation(float a) {attenuation = a;}
// camera inheritance
virtual glm::mat4 getProjectionMatrix() {return projectionMatrix;}
virtual glm::mat4 getViewMatrix() {return viewMatrix;}

View File

@ -191,45 +191,57 @@ Mesh* VertexComparator::mesh = NULL;
void Mesh::mergeVertices()
{
std::vector<int> swapped;
bool *deleted = new bool[positions.size()];
int *offsets = new int[positions.size()];
std::set<int, VertexComparator> vertexSet;
VertexComparator::setMesh(this);
int swappedOffset = positions.size() - 1;
for(int i=0; i<indices.size(); ++i)
{
if(indices[i] >= positions.size())
indices[i] = swapped[swappedOffset - indices[i]];
std::pair<std::set<int,VertexComparator>::iterator,bool> ret = vertexSet.insert(indices[i]);
if(!ret.second) // duplicate found
deleted[indices[i]] = !ret.second && *(ret.first) != indices[i];
if(deleted[indices[i]])
{
// updating indices references
int toDelete = indices[i];
swapped.push_back(toDelete);
indices[i] = *(ret.first);
// deleting the duplicate
positions[toDelete] = positions.back();
positions.pop_back();
if(hasTexCoords())
{
texCoords[toDelete] = texCoords.back();
texCoords.pop_back();
}
if(hasNormals())
{
normals[toDelete] = normals.back();
normals.pop_back();
}
if(hasTangents())
{
tangents[indices[i]].tangent += tangents[toDelete].tangent;
tangents[indices[i]].binormal += tangents[toDelete].binormal;
tangents[toDelete] = tangents.back();
tangents.pop_back();
tangents[*(ret.first)].tangent += tangents[indices[i]].tangent;
tangents[*(ret.first)].binormal += tangents[indices[i]].binormal;
}
indices[i] = *(ret.first);
}
}
int offset = 0;
int pos = 0;
for(int i=0; i<positions.size(); ++i)
{
if(deleted[i])
++offset;
else
{
offsets[i] = offset;
if(offset != 0)
{
positions[pos] = positions[i];
if(hasTexCoords())
texCoords[pos] = texCoords[i];
if(hasNormals())
normals[pos] = normals[i];
if(hasTangents())
tangents[pos] = tangents[i];
}
++pos;
}
}
for(int i=0; i<indices.size(); ++i)
indices[i] -= offsets[indices[i]];
positions.resize(positions.size()-offset);
if(hasTexCoords())
texCoords.resize(texCoords.size()-offset);
if(hasNormals())
normals.resize(normals.size()-offset);
if(hasTangents())
tangents.resize(tangents.size()-offset);
for(Tangents &t : tangents)
{
t.tangent = glm::normalize(t.tangent);

View File

@ -114,6 +114,7 @@ void PostEffectModule::renderGL(Camera* myCamera, Scene* scene)
{
if(shaders[0] != NULL)
{
glAssert(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL));
glAssert(glDisable(GL_BLEND));
glAssert(glDisable(GL_DEPTH_TEST));
@ -178,7 +179,6 @@ void PostEffectModule::bloomStep()
void PostEffectModule::hdrStep()
{
glm::vec3 minMaxMean = redux->redux();
float exposition = 1/(2*minMaxMean.z);
float gamma = 2.2f;
glAssert(glViewport(0, 0, width, height));
@ -189,7 +189,7 @@ void PostEffectModule::hdrStep()
shaders[HDR_SHADER]->bindInteger(shaders[HDR_SHADER]->getLocation("colorSampler"), 0);
frameBuffers[BLOOM_FBO].getTexture(1)->bind(0);
shaders[HDR_SHADER]->bindFloat(shaders[HDR_SHADER]->getLocation("exposition"), exposition);
shaders[HDR_SHADER]->bindVec3(shaders[HDR_SHADER]->getLocation("minMaxMean"), minMaxMean);
shaders[HDR_SHADER]->bindFloat(shaders[HDR_SHADER]->getLocation("gamma"), gamma);
glAssert(glDrawArrays(GL_TRIANGLES, 0, 3));

View File

@ -59,6 +59,7 @@ public:
void clearScene() {clearLights(); clearEntities();}
void addMesh(GeometryNode* node) {geometry.push_back(node);}
void addLight(Light* myLight) {lights.push_back(myLight);}
Mesh* getMesh(int id) {return geometry[id]->mesh;}
virtual SceneIterator<Light*>* getLights() {return new ArraySceneIterator<Light*>(lights);}
virtual SceneIterator<GeometryNode*>* getGeometry() {return new ArraySceneIterator<GeometryNode*>(geometry);}