bug fixes and minor features added

This commit is contained in:
Anselme 2016-01-24 16:50:20 +01:00
parent fb8ff13751
commit 080f3f6c53
13 changed files with 135 additions and 146 deletions

View File

@ -55,6 +55,8 @@ void ForwardModule::lightPass(Camera* myCamera, Scene* scene, Light* light)
for(j=0; j<lightFlagList.size(); ++j)
if(lightFlagList[j] == Light::getFlags(light))
break;
if(j == lightFlagList.size())
continue; // WARNING : missing shader for the light
Shader* shader = shaders[i*lightFlagList.size() + j];
shader->bind();

View File

@ -82,6 +82,7 @@ void Light::initShadowMap(int resWidth, int resHeight, glm::vec3 dim)
// Depth buffer
Texture* tex = new Texture(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, resWidth, resHeight, GL_FLOAT);
tex->setFiltering(GL_LINEAR);
tex->setWrap(GL_CLAMP_TO_EDGE);
shadowMap->addTexture(tex, GL_DEPTH_ATTACHMENT);
shadowMap->initColorAttachments();
@ -107,25 +108,27 @@ void Light::generateShadowMap(Scene* scene)
for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry();
geometryIt->isValid(); geometryIt->next())
{
// compute matrix attributes
GeometryNode* node = geometryIt->getItem();
glm::mat4 lightMVP = getProjectionMatrix() * (getViewMatrix() * node->modelMatrix);
// loop over material groups
Material* mat = node->mesh->material;
if(mat->getFlags() & ALPHA_MASK_FLAG)
if(((PhongMaterial*)mat)->castShadow)
{
PhongMaterial* pmat = (PhongMaterial*)mat;
shaders[1]->bind();
pmat->alpha_mask->bind(ALPHA_MASK);
shaders[1]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP);
shaders[1]->bindInteger(shaders[1]->getLocation("alphaMask"), ALPHA_MASK);
node->mesh->draw(shaders[1], false, true, false);
}
else
{
shaders[0]->bind();
shaders[0]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP);
node->mesh->draw(shaders[0], false, false, false);
// compute matrix attributes
glm::mat4 lightMVP = getProjectionMatrix() * (getViewMatrix() * node->modelMatrix);
if(mat->getFlags() & ALPHA_MASK_FLAG)
{
PhongMaterial* pmat = (PhongMaterial*)mat;
shaders[1]->bind();
pmat->alpha_mask->bind(ALPHA_MASK);
shaders[1]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP);
shaders[1]->bindInteger(shaders[1]->getLocation("alphaMask"), ALPHA_MASK);
node->mesh->draw(shaders[1], false, true, false);
}
else
{
shaders[0]->bind();
shaders[0]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP);
node->mesh->draw(shaders[0], false, false, false);
}
}
}
glAssert(glCullFace(GL_BACK));
@ -136,6 +139,12 @@ Texture* Light::getShadowMap()
return shadowMap->getTexture(0);
}
void Light::setPosition(glm::vec3 new_pos)
{
position = new_pos;
viewMatrix = glm::lookAt(position, position+direction, glm::vec3(0, 1, 0));
}
unsigned int Light::getFlags(Light* l)
{
if(l == NULL)

View File

@ -32,7 +32,7 @@ public:
static const glm::mat4 biasMatrix;
Light();
void initDirectionnalLight(glm::vec3 dir = glm::vec3(1, 0, 0), glm::vec3 lightColor = glm::vec3(1));
void initDirectionnalLight(glm::vec3 dir = glm::vec3(0, -1, 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));
@ -49,6 +49,8 @@ public:
Texture* getShadowMap();
void setAttenuation(float a) {attenuation = a;}
void setPosition(glm::vec3 new_pos);
void setColor(glm::vec3 new_color) {color = new_color;}
// camera inheritance
virtual glm::mat4 getProjectionMatrix() {return projectionMatrix;}

View File

@ -10,8 +10,7 @@
Mesh::Mesh() :
material(NULL),
vao(0),
nb_buffers(0),
vbo(NULL)
nb_buffers(0)
{}
Mesh::~Mesh()
@ -21,7 +20,7 @@ Mesh::~Mesh()
void Mesh::initGL(bool isDynamic)
{
if(vbo != NULL)
if(vao != 0)
destroyGL();
GLenum buffer_type = isDynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW;
@ -30,17 +29,8 @@ void Mesh::initGL(bool isDynamic)
glAssert(glGenVertexArrays(1, &vao));
glAssert(glBindVertexArray(vao));
nb_buffers = 2; // positions and indices buffers
if(hasNormals())
++nb_buffers;
if(hasTexCoords())
++nb_buffers;
if(hasTangents())
++nb_buffers;
// create VBOs
vbo = new GLuint[nb_buffers]();
glAssert(glGenBuffers(nb_buffers, vbo));
glAssert(glGenBuffers(NB_BUFFERS, vbo));
// init indices vbos
glAssert(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[INDICES_BUFFER]));
@ -57,7 +47,7 @@ void Mesh::initGL(bool isDynamic)
glAssert(glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), normals.data(), buffer_type));
}
if(hasNormals())
if(hasTexCoords())
{
// init texCoords vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]));
@ -145,11 +135,8 @@ void Mesh::destroyGL()
if(vbo != NULL)
{
glAssert(glDeleteVertexArrays(1, &vao));
glAssert(glDeleteBuffers(nb_buffers, vbo));
delete[] vbo;
glAssert(glDeleteBuffers(NB_BUFFERS, vbo));
vao = 0;
nb_buffers = 0;
vbo = NULL;
}
}

View File

@ -38,15 +38,17 @@ protected:
enum {
// required buffers
POSITION_BUFFER,
// indices buffers
INDICES_BUFFER,
// optionnal buffers :
NORMAL_BUFFER, TEXCOORD_BUFFER, TANGENT_BUFFER,
// indices buffers
INDICES_BUFFER
NB_BUFFERS
};
GLuint vao;
int nb_buffers;
GLuint* vbo;
GLuint vbo[NB_BUFFERS];
public:
Mesh();

View File

@ -13,6 +13,7 @@ public:
glm::vec3 diffuse;
glm::vec3 specular;
float shininess;
bool castShadow;
Texture* ambient_texture;
Texture* diffuse_texture;
Texture* specular_texture;
@ -24,6 +25,7 @@ public:
diffuse(0.5f),
specular(0.5f),
shininess(10),
castShadow(true),
ambient_texture(NULL),
diffuse_texture(NULL),
specular_texture(NULL),

View File

@ -19,7 +19,8 @@ PostEffectModule::PostEffectModule(int width, int height) :
frameBuffers(NULL),
blur(NULL),
redux(NULL),
blurSource(NULL)
blurSource(NULL),
bloom_threshold(0.7f)
{
for(int i=0; i<NB_SHADERS; ++i)
shaders[i] = NULL;
@ -59,6 +60,7 @@ void PostEffectModule::resize(int w, int h)
delete[](frameBuffers);
}
frameBuffers = new FrameBuffer[NB_FBO];
// TODO : don't delete, forward module reference is lost
// creating input FBO
@ -144,7 +146,7 @@ void PostEffectModule::luminanceStep()
shaders[LUMINANCE_SHADER]->bindInteger(shaders[LUMINANCE_SHADER]->getLocation("colorSampler"), 0);
frameBuffers[INPUT_FBO].getTexture(0)->bind(0);
shaders[LUMINANCE_SHADER]->bindFloat(shaders[LUMINANCE_SHADER]->getLocation("threshold"), BLOOM_LUMINANCE_THRESHOLD);
shaders[LUMINANCE_SHADER]->bindFloat(shaders[LUMINANCE_SHADER]->getLocation("threshold"), bloom_threshold);
glAssert(glDrawArrays(GL_TRIANGLES, 0, 3));
}

View File

@ -7,8 +7,6 @@
#include "module.h"
#include <string>
#define BLOOM_LUMINANCE_THRESHOLD 0.5f
class Shader;
class FrameBuffer;
class Texture;
@ -59,6 +57,9 @@ class PostEffectModule : public Module
ShaderSource* blurSource;
TextureRedux* redux;
// arguments
float bloom_threshold;
public:
PostEffectModule(int width, int height);
~PostEffectModule();
@ -78,6 +79,8 @@ public:
FrameBuffer* getInputFBO() {return frameBuffers;}
void setRenderTarget(FrameBuffer* renderTarget) {outputFBO = renderTarget;}
void setBloomThreshold(float threshold) {bloom_threshold = threshold;}
};
#endif // POSTEFFECTMODULE_H

View File

@ -15,10 +15,10 @@ class SceneIterator
{
public:
virtual SceneIterator& operator++() = 0;
virtual T& operator*() = 0;
virtual T operator*() = 0;
virtual bool isValid() = 0;
void next() {if(isValid()) operator++();}
T& getItem() {return operator*();}
T getItem() {return operator*();}
};
struct GeometryNode
@ -44,7 +44,7 @@ class ArraySceneIterator : public SceneIterator<T>
public:
ArraySceneIterator(std::vector<T> &myVec, int myId=0) : vec(myVec), id(myId) {}
virtual SceneIterator<T>& operator++() {++id; return *this;}
virtual T& operator*() {return vec[id];}
virtual T operator*() {return vec[id];}
virtual bool isValid() {return id < vec.size();}
};

View File

@ -5,39 +5,32 @@
#include "shader.h"
#include "texture.h"
#include "camera.h"
#include "framebuffer.h"
#include "glassert.h"
#include "sparrowrenderer.h"
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
SkyboxModule::SkyboxModule(Texture* myCubeMap)
{
cubeMap = myCubeMap;
glAssert(glGenVertexArrays(1, &vao));
glAssert(glBindVertexArray(vao));
glAssert(glGenBuffers(2, vbos));
glAssert(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[0]));
glAssert(glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLubyte), skyboxIndices, GL_STATIC_DRAW));
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbos[1]));
glAssert(glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), skyboxVertices, GL_STATIC_DRAW));
if(SparrowRenderer::isModernOpenGLAvailable())
{
renderTarget = FrameBuffer::screen;
shader = new Shader(vertSource, fragSource);
mvpLocation = shader->getLocation("MVP");
cubeMap = myCubeMap;
}
// set up vao
glAssert(glGenVertexArrays(1, &vao));
glAssert(glBindVertexArray(vao));
glAssert(glGenBuffers(1, &vbo));
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo));
glAssert(glBufferData(GL_ARRAY_BUFFER, 108 * sizeof(GLfloat), skyboxVertices, GL_STATIC_DRAW));
glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, NULL));
glAssert(glEnableVertexAttribArray(0));
glAssert(glBindVertexArray(0));
}
else
{
displayList = glAssert(glGenLists(1));
glAssert(glNewList(displayList, GL_COMPILE));
glAssert(glDisable(GL_LIGHTING));
glAssert(glEnable(GL_TEXTURE_CUBE_MAP));
myCubeMap->bind(0);
drawCube();
glAssert(glDisable(GL_TEXTURE_CUBE_MAP));
glAssert(glEnable(GL_LIGHTING));
glAssert(glEndList());
}
glAssert(glBindVertexArray(0));
}
SkyboxModule::~SkyboxModule()
@ -45,7 +38,7 @@ SkyboxModule::~SkyboxModule()
if(SparrowRenderer::isModernOpenGLAvailable())
{
glAssert(glDeleteVertexArrays(1, &vao));
glAssert(glDeleteBuffers(1, &vbo));
glAssert(glDeleteBuffers(2, vbos));
}
}
@ -53,6 +46,7 @@ void SkyboxModule::renderGL(Camera* myCamera, Scene* scene)
{
glm::mat4 viewMatrix = glm::mat4(glm::mat3(myCamera->getViewMatrix()));
glm::mat4 projectionMatrix = myCamera->getProjectionMatrix();
glAssert(glDisable(GL_CULL_FACE));
glAssert(glDepthMask(GL_FALSE));
if(!SparrowRenderer::isModernOpenGLAvailable())
{
@ -60,97 +54,78 @@ void SkyboxModule::renderGL(Camera* myCamera, Scene* scene)
glAssert(glLoadMatrixf(glm::value_ptr(viewMatrix)));
glAssert(glMatrixMode(GL_PROJECTION));
glAssert(glLoadMatrixf(glm::value_ptr(projectionMatrix)));
glAssert(glCallList(displayList));
}
else
{
renderTarget->bindFBO();
shader->bind();
shader->bindMat4(mvpLocation, projectionMatrix * viewMatrix);
}
cubeMap->bind(0);
glAssert(glBindVertexArray(vao));
glAssert(glDrawArrays(GL_TRIANGLES, 0, 36));
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbos[1]));
if(SparrowRenderer::isModernOpenGLAvailable())
{
glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(0));
}
else
{
glAssert(glDisable(GL_LIGHTING));
glAssert(glEnable(GL_TEXTURE_CUBE_MAP));
glAssert(glEnableClientState(GL_VERTEX_ARRAY));
glAssert(glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)));
glAssert(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
glAssert(glTexCoordPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)));
}
glAssert(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[0]));
glAssert(glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, NULL));
glAssert(glBindVertexArray(0));
if(!SparrowRenderer::isModernOpenGLAvailable())
{
glAssert(glEnable(GL_LIGHTING));
glAssert(glDisable(GL_TEXTURE_CUBE_MAP));
glAssert(glDisableClientState(GL_VERTEX_ARRAY));
glAssert(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
}
glAssert(glEnable(GL_CULL_FACE));
glAssert(glDepthMask(GL_TRUE));
}
void SkyboxModule::drawCube()
void SkyboxModule::setRenderTarget(const FrameBuffer* target)
{
const float t = 1;
glAssert(glBegin(GL_QUADS));
glTexCoord3f(-t,-t,-t); glVertex3f(-t,-t,-t);
glTexCoord3f(-t,t,-t); glVertex3f(-t,t,-t);
glTexCoord3f(-t,t,t); glVertex3f(-t,t,t);
glTexCoord3f(-t,-t,t); glVertex3f(-t,-t,t);
glTexCoord3f(t, -t,-t); glVertex3f(t,-t,-t);
glTexCoord3f(t,-t,t); glVertex3f(t,-t,t);
glTexCoord3f(t,t,t); glVertex3f(t,t,t);
glTexCoord3f(t,t,-t); glVertex3f(t,t,-t);
glTexCoord3f(-t,-t,-t); glVertex3f(-t,-t,-t);
glTexCoord3f(-t,-t,t); glVertex3f(-t,-t,t);
glTexCoord3f(t,-t,t); glVertex3f(t,-t,t);
glTexCoord3f(t, -t,-t); glVertex3f(t,-t,-t);
glTexCoord3f(-t,t,-t); glVertex3f(-t,t,-t);
glTexCoord3f(t,t,-t); glVertex3f(t,t,-t);
glTexCoord3f(t,t,t); glVertex3f(t,t,t);
glTexCoord3f(-t,t,t); glVertex3f(-t,t,t);
glTexCoord3f(-t,-t,-t); glVertex3f(-t,-t,-t);
glTexCoord3f(t, -t,-t); glVertex3f(t,-t,-t);
glTexCoord3f(t,t,-t); glVertex3f(t,t,-t);
glTexCoord3f(-t,t,-t); glVertex3f(-t,t,-t);
glTexCoord3f(-t,-t,t); glVertex3f(-t,-t,t);
glTexCoord3f(-t,t,t); glVertex3f(-t,t,t);
glTexCoord3f(t,t,t); glVertex3f(t,t,t);
glTexCoord3f(t,-t,t); glVertex3f(t,-t,t);
glAssert(glEnd());
if(target != NULL)
renderTarget = target;
}
const GLfloat SkyboxModule::skyboxVertices[] = {
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f
};
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f
const GLubyte SkyboxModule::skyboxIndices[] = {
0, 6, 2,
0, 4, 6,
1, 3, 5,
3, 7, 5,
0, 3, 1,
0, 2, 3,
4, 5, 6,
5, 7, 6,
0, 5, 4,
0, 1, 5,
2, 6, 3,
6, 7, 3
};
const std::string SkyboxModule::vertSource =

View File

@ -7,29 +7,30 @@
class Shader;
class Texture;
class FrameBuffer;
class SkyboxModule : public Module
{
static const GLfloat skyboxVertices[];
static const GLubyte skyboxIndices[];
static const std::string vertSource;
static const std::string fragSource;
// modern opengl variables
GLuint vao;
GLuint vbo;
GLuint vbos[2];
GLuint mvpLocation;
Shader* shader;
Texture* cubeMap;
//crappy opengl variables
GLuint displayList;
void drawCube();
const FrameBuffer* renderTarget;
public:
SkyboxModule(Texture* myCubeMap);
~SkyboxModule();
virtual void renderGL(Camera* myCamera, Scene* scene = NULL);
virtual bool requiresModernOpenGL() {return false;}
void setRenderTarget(const FrameBuffer* target);
};
#endif // SKYBOXMODULE_H

View File

@ -60,7 +60,8 @@ void SparrowRenderer::resizeGL(int w, int h)
width = w;
height = h;
glAssert(glViewport(0, 0, width, height));
camera->resize(width, height);
if(camera != NULL)
camera->resize(width, height);
for(ModuleNode &mn : modules)
mn.module->resize(w, h);
}
@ -133,6 +134,7 @@ void SparrowRenderer::setModuleEnabled(std::string module, bool isEnabled)
void SparrowRenderer::setCamera(Camera* myCamera)
{
camera = myCamera;
camera->resize(width, height);
}
Camera* SparrowRenderer::getCamera()

View File

@ -53,6 +53,8 @@ Texture::Texture(Image* myImage) :
Texture::Texture(Image* myCubemapImages[6]) :
m_target(GL_TEXTURE_CUBE_MAP),
m_width(myCubemapImages[0]->width),
m_height(myCubemapImages[0]->height),
m_dataType(GL_UNSIGNED_BYTE)
{
glAssert(glGenTextures(1, &texId));