added gbuffer class and shader

This commit is contained in:
Anselme 2015-11-22 21:07:33 +01:00
parent 8f0d9d5fc7
commit 4aaf417cde
13 changed files with 285 additions and 57 deletions

View File

@ -15,7 +15,7 @@ set(LIB_SRC_LIST
asciientity.cpp
asciimodule.cpp
framebuffer.cpp
gbuffermodule.cpp
gbuffer.cpp
lights.cpp
meshbuilder.cpp
phongentity.cpp
@ -25,8 +25,9 @@ set(LIB_SRC_LIST
skyboxmodule.cpp
sparrowrenderer.cpp
parametricmesh.cpp
texture.cpp
entityloader.cpp
texture.cpp
deferredmodule.cpp
shadersource.cpp
)
set(LIBRARY_NAME ${PROJECT_NAME})
@ -42,7 +43,9 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(CPP_DEFINES -DRENDER_DEBUG)
endif()
add_library(${LIBRARY_NAME} STATIC ${LIB_SRC_LIST})
file(GLOB LIBRARY_RES_FILES *.h *.frag *.vert)
add_library(${LIBRARY_NAME} STATIC ${LIB_SRC_LIST} ${LIBRARY_RES_FILES})
add_definitions(-std=c++11 ${CPP_DEFINES})
include_directories(

1
deferredmodule.cpp Normal file
View File

@ -0,0 +1 @@
#include "deferredmodule.h"

34
deferredmodule.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef DEFERREDMODULE_H
#define DEFERREDMODULE_H
#include "module.h"
#include <vector>
#include <cstddef>
#include <glew/glew.h>
#include "lights.h"
class Shader;
class PhongEntity;
class DeferredModule : public Module
{
public:
DeferredModule(Lights::Light* myDirLight, Lights* myPointLights);
//void addEntity(PhongEntity* myEntity);
//void clearEntities();
virtual void renderGL(Camera* myCamera) = 0;
virtual bool requiresModernOpenGL() {return true;}
private:
/*Lights::Light* dirLight;
Lights* pointLights;
GLuint dirLightLocation;
GLuint nbPointLightsLocation;
GLuint pointLightsLocation;
std::vector<PhongEntity*> entities;
static Shader* shaders[NB_SHADERS];*/
};
#endif // DEFERREDMODULE_H

View File

@ -1,7 +0,0 @@
#include "entityloader.h"
EntityLoader::EntityLoader()
{
}

View File

@ -1,10 +0,0 @@
#ifndef ENTITYLOADER_H
#define ENTITYLOADER_H
class EntityLoader
{
public:
EntityLoader();
};
#endif // ENTITYLOADER_H

56
gbuffer.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "gbuffer.h"
#include "sparrowrenderer.h"
#include "glassert.h"
GBuffer::GBuffer(int width, int height)
{
glAssert(glGenFramebuffers(1, &fbo));
glAssert(glBindFramebuffer(GL_FRAMEBUFFER, fbo));
// - Normal buffer
glAssert(glGenTextures(1, textures + NORMAL));
glAssert(glBindTexture(GL_TEXTURE_2D, textures[NORMAL]));
glAssert(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, NULL));
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
glAssert(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[NORMAL], 0));
// - Color + Specular exponent buffer
glAssert(glGenTextures(1, textures + COLOR));
glAssert(glBindTexture(GL_TEXTURE_2D, textures[COLOR]));
glAssert(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
glAssert(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, textures[COLOR], 0));
// - Specular color + objectId buffer
glAssert(glGenTextures(1, textures + SPECULAR));
glAssert(glBindTexture(GL_TEXTURE_2D, textures[SPECULAR]));
glAssert(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
glAssert(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, textures[SPECULAR], 0));
// - depth buffer
glAssert(glGenTextures(1, textures + DEPTH));
glAssert(glBindTexture(GL_TEXTURE_2D, textures[DEPTH]));
glAssert(glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL));
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
glAssert(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textures[DEPTH], 0));
// - Tell OpenGL which color attachments we'll use (of this framebuffer) for rendering
GLuint attachments[4] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_DEPTH_ATTACHMENT};
glAssert(glDrawBuffers(4, attachments));
}
void GBuffer::bind()
{
glAssert(glBindFramebuffer(GL_FRAMEBUFFER, fbo));
}
void GBuffer::bindTexture(TextureType type, int slot)
{
glAssert(glActiveTexture(GL_TEXTURE0 + slot));
glAssert(glBindTexture(GL_TEXTURE_2D, textures[type]));
}

26
gbuffer.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef GBUFFER_H
#define GBUFFER_H
#include <glew/glew.h>
class GBuffer
{
// fbo
GLuint fbo;
// textures
enum TextureType
{
NORMAL,
COLOR,
SPECULAR,
DEPTH,
NB_TEXTURES
};
GLuint textures[NB_TEXTURES];
public:
GBuffer(int width, int height);
void bind();
void bindTexture(TextureType type, int slot);
};
#endif // GBUFFER_H

View File

@ -1,13 +0,0 @@
#include "gbuffermodule.h"
#include "camera.h"
#include "mesh.h"
void GBufferModule::addMesh(Mesh* myMesh)
{
}
void GBufferModule::renderGL(Camera* myCamera)
{
}

View File

@ -1,23 +0,0 @@
#ifndef GBUFFERMODULE_H
#define GBUFFERMODULE_H
#include "module.h"
#include <vector>
#include <cstddef>
class Shader;
class Mesh;
class GBufferModule : public Module
{
public:
Shader* shader;
std::vector<Mesh*> meshes;
GBufferModule(Shader* myShader = NULL) : shader(myShader) {}
void addMesh(Mesh* myMesh);
void virtual renderGL(Camera* myCamera);
};
#endif // GBUFFERMODULE_H

29
shadersource.cpp Normal file
View File

@ -0,0 +1,29 @@
#include "shadersource.h"
#include <string>
#include "shader.h"
ShaderSource::ShaderSource();
void ShaderSource::addSource(const char *source, SourceType type)
{
}
Shader* ShaderSource::compile(int nbDefines = 0, const char** defines = NULL)
{
if(sources[VERTEX] == NULL || sources[FRAGMENT] == NULL)
return NULL;
std::string plop(sources[VERTEX]);
plop.find("#if")
{
}
// read lines
else if(sources[GEOMETRY] != NULL)
return new Shader(sources[VERTEX], sources[GEOMETRY], sources[FRAGMENT]);
else
return new Shader(sources[VERTEX], sources[FRAGMENT]);
}

29
shadersource.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef SHADERSOURCE_H
#define SHADERSOURCE_H
#include <string>
class Shader;
class ShaderSource
{
public:
enum SourceType
{
VERTEX,
GEOMETRY,
FRAGMENT,
NB_TYPES
};
ShaderSource();
void addSource(const char *source, SourceType type);
Shader* compile(int nbDefines = 0, const char** defines = NULL);
private:
char* sources[NB_TYPES];
};
#endif // SHADERSOURCE_H

62
togbuffer.frag Normal file
View File

@ -0,0 +1,62 @@
#version 330 core
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec4 outColor;
layout (location = 2) out vec4 outSpecular;
uniform float materialNs;
uniform int objectId;
#ifdef ALPHA_MASK
uniform sampler2D alphaMask;
#endif
#ifdef DIFFUSE_TEXTURE
uniform sampler2D diffuseTexture;
#else
uniform vec3 materialKd;
#endif
#ifdef SPECULAR_TEXTURE
uniform sampler2D specularTexture;
#else
uniform vec3 materialKs;
#endif
#ifdef NORMAL_MAP
uniform sampler2D normalMap;
in mat3 tangentSpace;
#else
in vec3 varNormal;
#endif
in vec2 varTexCoord;
void main()
{
#ifdef ALPHA_MASK
if(texture(alphaMask, varTexCoord).r < 0.5)
discard;
#endif
#ifdef NORMAL_MAP
vec3 varNormal = texture(normalMap, varTexCoord).xyz * tangentSpace;
#endif
outNormal = normalize(varNormal);
#ifdef DIFFUSE_TEXTURE
outColor.rgb = texture(diffuseTexture, varTexCoord).rgb;
#else
outColor.rgb = materialKd;
#endif
outColor.a = materialNs;
#ifdef SPECULAR_TEXTURE
outSpecular.rgb = texture(specularTexture, varTexCoord).rgb;
#else
outSpecular.rgb = materialKs;
#endif
outSpecular.a = objectId;
}

41
togbuffer.vert Normal file
View File

@ -0,0 +1,41 @@
#version 330 core
#ifdef NORMAL_MAP
out mat3 tangentSpace;
#else
out vec3 varNormal;
#endif
out vec2 varTexCoord;
// Matrices
uniform mat4 modelViewMatrix;
uniform mat4 MVP;
uniform mat4 viewMatrix;
uniform mat3 normalMatrix;
layout(location = 0)in vec3 inPosition;
layout(location = 1)in vec2 inTexCoord;
layout(location = 2)in vec3 inNormal;
#ifdef NORMAL_MAP
layout(location = 3)in vec3 inTangent;
layout(location = 4)in vec3 inBinormal;
#endif
out vec3 varNormal;
out vec2 varTexCoord;
void main(void)
{
#ifdef NORMAL_MAP
tangentSpace = mat3(normalize(normalMatrix*inNormal),
normalize(normalMatrix*inTangent),
normalize(normalMatrix*inBinormal));
#else
varNormal = normalize(normalMatrix*inNormal);
#endif
varTexCoord = inTexCoord.xy;
gl_Position = MVP * vec4(inPosition, 1.0);
}