Skip to content

Commit

Permalink
Add a deferred renderer pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Aug 3, 2015
1 parent 2a6b866 commit cba709b
Show file tree
Hide file tree
Showing 43 changed files with 849 additions and 61 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ set(SRCS components/sdlutil/graphicswindow.cpp
components/dfosg/texloader.cpp
components/dfosg/meshloader.cpp
src/input/input.cpp
src/render/pipeline.cpp
src/render/renderer.cpp
src/gui/gui.cpp
src/class/placeable.cpp
Expand Down Expand Up @@ -72,6 +73,7 @@ set(HDRS misc/sparsearray.hpp
components/dfosg/texloader.hpp
components/dfosg/meshloader.hpp
src/input/input.hpp
src/render/pipeline.hpp
src/render/renderer.hpp
src/gui/iface.hpp
src/gui/gui.hpp
Expand Down
4 changes: 2 additions & 2 deletions components/mygui_osg/rendermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ResizeHandler : public osgGA::GUIEventHandler {
osg::StateSet *setShaderProgram(osg::Node *node, const std::string &vert, const std::string &frag)
{
osg::StateSet *ss = node->getOrCreateStateSet();
if(0)
if(1)
{
osg::ref_ptr<osg::Program> program = new osg::Program();
program->addShader(osgDB::readShaderFile(osg::Shader::VERTEX, vert));
Expand Down Expand Up @@ -143,7 +143,7 @@ void RenderManager::initialise()
camera->setViewMatrix(osg::Matrix::identity());
camera->setRenderOrder(osg::Camera::POST_RENDER);
camera->setClearMask(GL_NONE);
osg::StateSet *state = setShaderProgram(camera.get(), "shaders/quad_2d.vert", "shaders/quad_2d.frag");
osg::StateSet *state = setShaderProgram(camera.get(), "shaders/quad_ui.vert", "shaders/quad_ui.frag");
state->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
state->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
state->setAttributeAndModes(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL));
Expand Down
32 changes: 25 additions & 7 deletions components/resource/meshmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <osg/Geometry>
#include <osg/Texture>
#include <osg/AlphaFunc>
#include <osgDB/ReadFile>

#include "components/dfosg/meshloader.hpp"

Expand Down Expand Up @@ -198,28 +199,45 @@ osg::ref_ptr<osg::Node> MeshManager::loadFlat(size_t texid, bool centered, size_
(*texcrds)[1] = osg::Vec2(0.0f, 0.0f);
(*texcrds)[2] = osg::Vec2(0.0f, 1.0f);
(*texcrds)[3] = osg::Vec2(1.0f, 1.0f);
osg::ref_ptr<osg::Vec3Array> nrms(new osg::Vec3Array());
nrms->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));
osg::ref_ptr<osg::Vec4ubArray> colors(new osg::Vec4ubArray());
colors->push_back(osg::Vec4ub(255, 255, 255, 255));
osg::ref_ptr<osg::Vec3Array> nrms(new osg::Vec3Array(4));
(*nrms)[0] = osg::Vec3(0.0f, 0.0f, -1.0f);
(*nrms)[1] = osg::Vec3(0.0f, 0.0f, -1.0f);
(*nrms)[2] = osg::Vec3(0.0f, 0.0f, -1.0f);
(*nrms)[3] = osg::Vec3(0.0f, 0.0f, -1.0f);
osg::ref_ptr<osg::Vec4ubArray> colors(new osg::Vec4ubArray(4));
(*colors)[0] = osg::Vec4ub(255, 255, 255, 255);
(*colors)[1] = osg::Vec4ub(255, 255, 255, 255);
(*colors)[2] = osg::Vec4ub(255, 255, 255, 255);
(*colors)[3] = osg::Vec4ub(255, 255, 255, 255);
colors->setNormalize(true);

osg::ref_ptr<osg::VertexBufferObject> vbo(new osg::VertexBufferObject());
vtxs->setVertexBufferObject(vbo);
texcrds->setVertexBufferObject(vbo);
nrms->setVertexBufferObject(vbo);
colors->setVertexBufferObject(vbo);

osg::ref_ptr<osg::Geometry> geometry(new osg::Geometry);
geometry->setVertexArray(vtxs);
geometry->setTexCoordArray(0, texcrds, osg::Array::BIND_PER_VERTEX);
geometry->setNormalArray(nrms, osg::Array::BIND_OVERALL);
geometry->setColorArray(colors, osg::Array::BIND_OVERALL);
geometry->setNormalArray(nrms, osg::Array::BIND_PER_VERTEX);
geometry->setColorArray(colors, osg::Array::BIND_PER_VERTEX);
geometry->setUseDisplayList(false);
geometry->setUseVertexBufferObjects(true);
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));

osg::StateSet *ss = geometry->getOrCreateStateSet();
ss->setTextureAttributeAndModes(0, tex);
ss->setAttributeAndModes(new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.5f));
// Alpha test is reversed, because the shader will set alpha=0 for texels
// that should be kept, and consequently have no specular, and alpha=1 for
// texels that should be dropped.
ss->setAttributeAndModes(new osg::AlphaFunc(osg::AlphaFunc::LESS, 0.5f));

osg::ref_ptr<osg::Program> program = new osg::Program();
program->addShader(osgDB::readShaderFile(osg::Shader::VERTEX, "shaders/sprite.vert"));
program->addShader(osgDB::readShaderFile(osg::Shader::FRAGMENT, "shaders/sprite.frag"));

ss->setAttributeAndModes(program.get());

if(centered)
bb->addDrawable(geometry);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions data/shaders/combiner.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 130
#extension GL_ARB_texture_rectangle : enable

uniform sampler2DRect ColorTex;
uniform sampler2DRect DiffuseTex;
uniform sampler2DRect SpecularTex;

in vec4 TexCoord0;

out vec4 ColorOutput;

void main()
{
vec3 color = texture2DRect(ColorTex, TexCoord0.xy).rgb;
vec3 diffuse = texture2DRect(DiffuseTex, TexCoord0.xy).rgb;
vec3 specular = texture2DRect(SpecularTex, TexCoord0.xy).rgb;

ColorOutput = vec4(color*diffuse + specular, 1.0);
}
14 changes: 14 additions & 0 deletions data/shaders/combiner.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 130

uniform mat4 osg_ProjectionMatrix;

in vec4 osg_Vertex;
in vec4 osg_MultiTexCoord0;

out vec4 TexCoord0;

void main()
{
gl_Position = osg_ProjectionMatrix * osg_Vertex;
TexCoord0 = osg_MultiTexCoord0;
}
42 changes: 42 additions & 0 deletions data/shaders/dir_light.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#version 130
#extension GL_ARB_texture_rectangle : enable

in vec3 localLightDir;

uniform vec4 ambient_color;
uniform vec4 diffuse_color;
uniform vec4 specular_color;

uniform sampler2DRect ColorTex;
uniform sampler2DRect NormalTex;
uniform sampler2DRect PosTex;

out vec4 DiffuseData;
out vec4 SpecularData;

void main()
{
vec4 c_viewspace = texture2DRect(ColorTex, gl_FragCoord.xy);
vec3 n_viewspace = texture2DRect(NormalTex, gl_FragCoord.xy).xyz*2.0 - vec3(1.0);
vec3 p_viewspace = texture2DRect(PosTex, gl_FragCoord.xy).xyz;
vec3 s_viewspace = vec3(1.0);

// Direction from point to light (not vice versa!)
vec3 lightDir_viewspace = normalize(-localLightDir);

// Lambertian diffuse color.
vec3 diff = max(ambient_color.rgb,
diffuse_color.rgb * s_viewspace * dot(lightDir_viewspace, n_viewspace)
);

// Direction from point to camera.
vec3 viewDir_viewspace = normalize(-p_viewspace);

// Blinn-Phong specular highlights.
vec3 h_viewspace = normalize(lightDir_viewspace + viewDir_viewspace);
float amount = max(0.0, dot(h_viewspace, n_viewspace));
vec3 spec = specular_color.rgb * s_viewspace * pow(amount, 32.0) * c_viewspace.a;

DiffuseData = vec4(diff, 1.0);
SpecularData = vec4(spec, 1.0);
}
18 changes: 18 additions & 0 deletions data/shaders/dir_light.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 130

uniform mat4 osg_ProjectionMatrix;
uniform mat4 osg_ModelViewMatrix;

uniform vec3 light_direction;

in vec4 osg_Vertex;

out vec3 localLightDir;

void main()
{
localLightDir = mat3(osg_ModelViewMatrix) * light_direction;

// Vertex position in main camera Screen space.
gl_Position = osg_ProjectionMatrix * osg_Vertex;
}
32 changes: 32 additions & 0 deletions data/shaders/object.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#version 130

uniform vec4 illumination_color;

uniform sampler2D diffuseTex;

in vec3 pos_viewspace;
in vec3 n_viewspace;
in vec3 t_viewspace;
in vec3 b_viewspace;
in vec4 TexCoords;
in vec4 Color;

out vec4 ColorData;
out vec4 NormalData;
out vec4 PositionData;
out vec4 IlluminationData;

void main()
{
vec4 color = vec4(texture2D(diffuseTex, TexCoords.xy).rgb, 0.0);
vec4 nn = vec4(0.5, 0.5, 1.0, 1.0);

mat3 nmat = mat3(normalize(t_viewspace),
normalize(b_viewspace),
normalize(n_viewspace));

ColorData = color * vec4(Color.rgb, 1.0);
NormalData = vec4(nmat*(nn.xyz - vec3(0.5)) + vec3(0.5), nn.w);
PositionData = vec4(pos_viewspace, gl_FragCoord.z);
IlluminationData = illumination_color;
}
30 changes: 30 additions & 0 deletions data/shaders/object.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#version 130

uniform mat4 osg_ModelViewProjectionMatrix;
uniform mat4 osg_ModelViewMatrix;

in vec4 osg_Vertex;
in vec3 osg_Normal;
in vec4 osg_Color;
in vec4 osg_MultiTexCoord0;

out vec3 pos_viewspace;
out vec3 n_viewspace;
out vec3 t_viewspace;
out vec3 b_viewspace;
out vec4 TexCoords;
out vec4 Color;

void main()
{
gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;
TexCoords = osg_MultiTexCoord0;
Color = osg_Color;

pos_viewspace = (osg_ModelViewMatrix * osg_Vertex).xyz;

vec3 binormal = cross(osg_Normal, vec3(1.0, 0.0, 0.0));
n_viewspace = normalize(mat3(osg_ModelViewMatrix) * osg_Normal);
t_viewspace = normalize(mat3(osg_ModelViewMatrix) * cross(osg_Normal, binormal));
b_viewspace = normalize(mat3(osg_ModelViewMatrix) * binormal);
}
13 changes: 13 additions & 0 deletions data/shaders/quad_rect.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 130
#extension GL_ARB_texture_rectangle : enable

uniform sampler2DRect TexImage;

in vec4 TexCoord0;

out vec4 ColorOutput;

void main()
{
ColorOutput = texture2DRect(TexImage, TexCoord0.xy);
}
14 changes: 14 additions & 0 deletions data/shaders/quad_rect.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 130

uniform mat4 osg_ProjectionMatrix;

in vec4 osg_Vertex;
in vec4 osg_MultiTexCoord0;

out vec4 TexCoord0;

void main()
{
gl_Position = osg_ProjectionMatrix * osg_Vertex;
TexCoord0 = osg_MultiTexCoord0;
}
13 changes: 13 additions & 0 deletions data/shaders/quad_ui.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 130

uniform sampler2D TexImage;

in vec4 Color;
in vec4 TexCoord0;

out vec4 ColorOutput;

void main()
{
ColorOutput = texture2D(TexImage, TexCoord0.xy) * Color;
}
17 changes: 17 additions & 0 deletions data/shaders/quad_ui.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#version 130

uniform mat4 osg_ProjectionMatrix;

in vec4 osg_Vertex;
in vec4 osg_Color;
in vec4 osg_MultiTexCoord0;

out vec4 Color;
out vec4 TexCoord0;

void main()
{
gl_Position = osg_ProjectionMatrix * osg_Vertex;
TexCoord0 = osg_MultiTexCoord0;
Color = osg_Color;
}
33 changes: 33 additions & 0 deletions data/shaders/sprite.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#version 130

uniform vec4 illumination_color;

uniform sampler2D diffuseTex;

in vec3 pos_viewspace;
in vec3 n_viewspace;
in vec3 t_viewspace;
in vec3 b_viewspace;
in vec4 TexCoords;
in vec4 Color;

out vec4 ColorData;
out vec4 NormalData;
out vec4 PositionData;
out vec4 IlluminationData;

void main()
{
vec4 color = texture2D(diffuseTex, TexCoords.xy);
color.a = ((color.a < 0.5) ? 1.0 : 0.0);
vec4 nn = vec4(0.5, 0.5, 1.0, 1.0);

mat3 nmat = mat3(normalize(t_viewspace),
normalize(b_viewspace),
normalize(n_viewspace));

ColorData = color * vec4(Color.rgb, 1.0);
NormalData = vec4(nmat*(nn.xyz - vec3(0.5)) + vec3(0.5), nn.w);
PositionData = vec4(pos_viewspace, gl_FragCoord.z);
IlluminationData = illumination_color;
}
30 changes: 30 additions & 0 deletions data/shaders/sprite.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#version 130

uniform mat4 osg_ModelViewProjectionMatrix;
uniform mat4 osg_ModelViewMatrix;

in vec4 osg_Vertex;
in vec3 osg_Normal;
in vec4 osg_Color;
in vec4 osg_MultiTexCoord0;

out vec3 pos_viewspace;
out vec3 n_viewspace;
out vec3 t_viewspace;
out vec3 b_viewspace;
out vec4 TexCoords;
out vec4 Color;

void main()
{
gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;
TexCoords = osg_MultiTexCoord0;
Color = osg_Color;

pos_viewspace = (osg_ModelViewMatrix * osg_Vertex).xyz;

vec3 binormal = cross(osg_Normal, vec3(1.0, 0.0, 0.0));
n_viewspace = normalize(mat3(osg_ModelViewMatrix) * osg_Normal);
t_viewspace = normalize(mat3(osg_ModelViewMatrix) * cross(osg_Normal, binormal));
b_viewspace = normalize(mat3(osg_ModelViewMatrix) * binormal);
}
3 changes: 2 additions & 1 deletion settings.cfg.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Set data-root to your Daggerfall's arena2 directory
# data-root = $HOME/.wine/drive_c/Games/Daggerfall/arena2
data = ${DATA_PATH}/MyGUI_Media
data = ${DATA_PATH}/data
data = ${DATA_PATH}/data/MyGUI_Media
Loading

0 comments on commit cba709b

Please sign in to comment.