Skip to content

Commit

Permalink
Simplify gbuffers example
Browse files Browse the repository at this point in the history
  • Loading branch information
scheibel committed Jun 18, 2014
1 parent 195ac15 commit ddb22c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 41 deletions.
10 changes: 0 additions & 10 deletions data/gbuffers/gbufferchoice.frag
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,22 @@ void main()
{
case 1:
fragColor = texture(colorSource, v_uv);

//fragColor = vec4(0.0, 1.0, 0.0, 1.0);
break;
case 2:
fragColor = vec4((texture(normalSource, v_uv).rgb + 1.0) * 0.5, 1.0);

//fragColor = vec4(0.0, 0.0, 1.0, 1.0);
break;
case 3:
fragColor = vec4(texture(worldCoordSource, v_uv).rgb * 0.5 + 0.5, 1.0);

//fragColor = vec4(1.0, 1.0, 0.0, 1.0);
break;
case 4:
float depth = texture(depthSource, v_uv).r;

float d = (nearZ*farZ/4.0) / (farZ-depth*(farZ-nearZ));

fragColor = vec4(vec3(d), 1.0);

//fragColor = vec4(0.0, 0.0, 0.0, 1.0);
break;
default:
fragColor = texture(postprocessedSource, v_uv);

//fragColor = vec4(1.0, 0.0, 0.0, 1.0);
break;
}
}
60 changes: 29 additions & 31 deletions source/examples/gbuffers/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
#include <glow/Program.h>
#include <glow/Shader.h>
#include <glow/Buffer.h>
#include <glow/logging.h>
#include <glow/FrameBufferObject.h>
#include <glow/VertexArrayObject.h>
#include <glow/debugmessageoutput.h>
#include <glow/Texture.h>

#include <glowutils/Timer.h>
#include <glowutils/AxisAlignedBoundingBox.h>
#include <glowutils/Icosahedron.h>
#include <glowutils/Camera.h>
#include <glowutils/AdaptiveGrid.h>
#include <glowutils/AbstractCoordinateProvider.h>
#include <glowutils/WorldInHandNavigation.h>
#include <glowutils/FlightNavigation.h>
#include <glowutils/glowutils.h>
#include <glowutils/StringTemplate.h>
#include <glowutils/ScreenAlignedQuad.h>
Expand Down Expand Up @@ -60,17 +56,22 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi

gl::glClearColor(1.0f, 1.0f, 1.0f, 0.f);

m_icosahedron = new glowutils::Icosahedron(2);

m_sphere = new glow::Program();
glowutils::StringTemplate* vertexShaderSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/sphere.vert"));
glowutils::StringTemplate* fragmentShaderSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/sphere.frag"));
auto vertexShaderSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/sphere.vert"));
auto fragmentShaderSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/sphere.frag"));
auto postprocessingSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/postprocessing.frag"));
auto gBufferChoiceSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/gbufferchoice.frag"));

#ifdef MAC_OS
vertexShaderSource->replace("#version 140", "#version 150");
fragmentShaderSource->replace("#version 140", "#version 150");
postprocessingSource->replace("#version 140", "#version 150");
gBufferChoiceSource->replace("#version 140", "#version 150");
#endif


m_icosahedron = new glowutils::Icosahedron(2);

m_sphere = new glow::Program();

m_sphere->attach(
new glow::Shader(gl::GL_VERTEX_SHADER, vertexShaderSource),
new glow::Shader(gl::GL_FRAGMENT_SHADER, fragmentShaderSource)
Expand All @@ -82,17 +83,11 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
m_geometryTexture = glow::Texture::createDefault(gl::GL_TEXTURE_2D);

m_sphereFBO = new glow::FrameBufferObject;

m_sphereFBO->attachTexture(gl::GL_COLOR_ATTACHMENT0, m_colorTexture);
m_sphereFBO->attachTexture(gl::GL_COLOR_ATTACHMENT1, m_normalTexture);
m_sphereFBO->attachTexture(gl::GL_COLOR_ATTACHMENT2, m_geometryTexture);
m_sphereFBO->attachTexture(gl::GL_DEPTH_ATTACHMENT, m_depthTexture);

glowutils::StringTemplate* postprocessingSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/postprocessing.frag"));

#ifdef MAC_OS
postprocessingSource->replace("#version 140", "#version 150");
#endif
m_sphereFBO->setDrawBuffers({ gl::GL_COLOR_ATTACHMENT0, gl::GL_COLOR_ATTACHMENT1, gl::GL_COLOR_ATTACHMENT2 });

m_postprocessing = new glowutils::ScreenAlignedQuad(new glow::Shader(gl::GL_FRAGMENT_SHADER, postprocessingSource));
m_postprocessing->program()->setUniform<gl::GLint>("colorSource", 0);
Expand All @@ -103,14 +98,8 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
m_postprocessedTexture = glow::Texture::createDefault(gl::GL_TEXTURE_2D);

m_postprocessingFBO = new glow::FrameBufferObject;

m_postprocessingFBO->attachTexture(gl::GL_COLOR_ATTACHMENT0, m_postprocessedTexture);

glowutils::StringTemplate* gBufferChoiceSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/gbufferchoice.frag"));

#ifdef MAC_OS
gBufferChoiceSource->replace("#version 140", "#version 150");
#endif
m_postprocessingFBO->setDrawBuffer(gl::GL_COLOR_ATTACHMENT0);

m_gBufferChoice = new glowutils::ScreenAlignedQuad(new glow::Shader(gl::GL_FRAGMENT_SHADER, gBufferChoiceSource));
m_gBufferChoice->program()->setUniform<gl::GLint>("postprocessedSource", 0);
Expand All @@ -126,6 +115,8 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
m_gBufferChoice->program()->setUniform<gl::GLfloat>("farZ", m_camera.zFar());

window.addTimer(0, 0, false);

cameraChanged();
}

virtual void finalize(Window &) override
Expand All @@ -142,26 +133,30 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi

m_camera.setViewport(event.width(), event.height());

cameraChanged();

m_colorTexture->image2D(0, gl::GL_RGBA8, event.width(), event.height(), 0, gl::GL_RGBA, gl::GL_UNSIGNED_BYTE, nullptr);
m_normalTexture->image2D(0, gl::GL_RGBA16F, event.width(), event.height(), 0, gl::GL_RGBA, gl::GL_FLOAT, nullptr);
m_geometryTexture->image2D(0, gl::GL_RGBA16F, event.width(), event.height(), 0, gl::GL_RGBA, gl::GL_FLOAT, nullptr);
m_depthTexture->image2D(0, gl::GL_DEPTH_COMPONENT, event.width(), event.height(), 0, gl::GL_DEPTH_COMPONENT, gl::GL_FLOAT, nullptr);
m_postprocessedTexture->image2D(0, gl::GL_RGBA8, event.width(), event.height(), 0, gl::GL_RGBA, gl::GL_UNSIGNED_BYTE, nullptr);
}

void cameraChanged()
{
m_sphere->setUniform("transform", m_camera.viewProjection());
m_sphere->setUniform("modelView", m_camera.view());
m_sphere->setUniform("normalMatrix", m_camera.normal());
}

virtual void paintEvent(PaintEvent &) override
{
// Sphere Pass

m_sphereFBO->bind();
m_sphereFBO->setDrawBuffers({ gl::GL_COLOR_ATTACHMENT0, gl::GL_COLOR_ATTACHMENT1, gl::GL_COLOR_ATTACHMENT2 });

gl::glClear(gl::GL_COLOR_BUFFER_BIT | gl::GL_DEPTH_BUFFER_BIT);

m_sphere->setUniform("transform", m_camera.viewProjection());
m_sphere->setUniform("modelView", m_camera.view());
m_sphere->setUniform("normalMatrix", m_camera.normal());

m_sphere->use();
m_icosahedron->draw();
m_sphere->release();
Expand All @@ -171,7 +166,6 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
// Postprocessing Pass

m_postprocessingFBO->bind();
m_postprocessingFBO->setDrawBuffer(gl::GL_COLOR_ATTACHMENT0);

gl::glClear(gl::GL_COLOR_BUFFER_BIT);

Expand All @@ -191,7 +185,7 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi

// GBuffer Choice Pass (including blitting)

glow::FrameBufferObject::defaultFBO()->bind();
// If no FBO is bound to GL_FRAMEBUFFER the default FBO is bound to GL_FRAMEBUFFER

gl::glClear(gl::GL_COLOR_BUFFER_BIT | gl::GL_DEPTH_BUFFER_BIT);

Expand Down Expand Up @@ -241,6 +235,7 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
m_camera.setCenter(vec3());
m_camera.setEye(vec3(0.f, 1.f, 4.0f));
m_camera.setUp(vec3(0,1,0));
cameraChanged();
break;
}
}
Expand Down Expand Up @@ -268,11 +263,13 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
case glowutils::WorldInHandNavigation::PanInteraction:
m_nav.panProcess(event.pos());
event.accept();
cameraChanged();
break;

case glowutils::WorldInHandNavigation::RotateInteraction:
m_nav.rotateProcess(event.pos());
event.accept();
cameraChanged();
break;
case glowutils::WorldInHandNavigation::NoInteraction:
break;
Expand Down Expand Up @@ -302,6 +299,7 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi

m_nav.scaleAtMouse(event.pos(), -event.offset().y * 0.1f);
event.accept();
cameraChanged();
}

virtual float depthAt(const ivec2 & windowCoordinates) const override
Expand Down

0 comments on commit ddb22c2

Please sign in to comment.