Skip to content

Commit ddb22c2

Browse files
committed
Simplify gbuffers example
1 parent 195ac15 commit ddb22c2

File tree

2 files changed

+29
-41
lines changed

2 files changed

+29
-41
lines changed

data/gbuffers/gbufferchoice.frag

-10
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,22 @@ void main()
2121
{
2222
case 1:
2323
fragColor = texture(colorSource, v_uv);
24-
25-
//fragColor = vec4(0.0, 1.0, 0.0, 1.0);
2624
break;
2725
case 2:
2826
fragColor = vec4((texture(normalSource, v_uv).rgb + 1.0) * 0.5, 1.0);
29-
30-
//fragColor = vec4(0.0, 0.0, 1.0, 1.0);
3127
break;
3228
case 3:
3329
fragColor = vec4(texture(worldCoordSource, v_uv).rgb * 0.5 + 0.5, 1.0);
34-
35-
//fragColor = vec4(1.0, 1.0, 0.0, 1.0);
3630
break;
3731
case 4:
3832
float depth = texture(depthSource, v_uv).r;
3933

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

4236
fragColor = vec4(vec3(d), 1.0);
43-
44-
//fragColor = vec4(0.0, 0.0, 0.0, 1.0);
4537
break;
4638
default:
4739
fragColor = texture(postprocessedSource, v_uv);
48-
49-
//fragColor = vec4(1.0, 0.0, 0.0, 1.0);
5040
break;
5141
}
5242
}

source/examples/gbuffers/main.cpp

+29-31
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@
44
#include <glow/Program.h>
55
#include <glow/Shader.h>
66
#include <glow/Buffer.h>
7-
#include <glow/logging.h>
87
#include <glow/FrameBufferObject.h>
98
#include <glow/VertexArrayObject.h>
109
#include <glow/debugmessageoutput.h>
1110
#include <glow/Texture.h>
1211

13-
#include <glowutils/Timer.h>
1412
#include <glowutils/AxisAlignedBoundingBox.h>
1513
#include <glowutils/Icosahedron.h>
1614
#include <glowutils/Camera.h>
17-
#include <glowutils/AdaptiveGrid.h>
1815
#include <glowutils/AbstractCoordinateProvider.h>
1916
#include <glowutils/WorldInHandNavigation.h>
20-
#include <glowutils/FlightNavigation.h>
2117
#include <glowutils/glowutils.h>
2218
#include <glowutils/StringTemplate.h>
2319
#include <glowutils/ScreenAlignedQuad.h>
@@ -60,17 +56,22 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
6056

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

63-
m_icosahedron = new glowutils::Icosahedron(2);
64-
65-
m_sphere = new glow::Program();
66-
glowutils::StringTemplate* vertexShaderSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/sphere.vert"));
67-
glowutils::StringTemplate* fragmentShaderSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/sphere.frag"));
59+
auto vertexShaderSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/sphere.vert"));
60+
auto fragmentShaderSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/sphere.frag"));
61+
auto postprocessingSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/postprocessing.frag"));
62+
auto gBufferChoiceSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/gbufferchoice.frag"));
6863

6964
#ifdef MAC_OS
7065
vertexShaderSource->replace("#version 140", "#version 150");
7166
fragmentShaderSource->replace("#version 140", "#version 150");
67+
postprocessingSource->replace("#version 140", "#version 150");
68+
gBufferChoiceSource->replace("#version 140", "#version 150");
7269
#endif
73-
70+
71+
m_icosahedron = new glowutils::Icosahedron(2);
72+
73+
m_sphere = new glow::Program();
74+
7475
m_sphere->attach(
7576
new glow::Shader(gl::GL_VERTEX_SHADER, vertexShaderSource),
7677
new glow::Shader(gl::GL_FRAGMENT_SHADER, fragmentShaderSource)
@@ -82,17 +83,11 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
8283
m_geometryTexture = glow::Texture::createDefault(gl::GL_TEXTURE_2D);
8384

8485
m_sphereFBO = new glow::FrameBufferObject;
85-
8686
m_sphereFBO->attachTexture(gl::GL_COLOR_ATTACHMENT0, m_colorTexture);
8787
m_sphereFBO->attachTexture(gl::GL_COLOR_ATTACHMENT1, m_normalTexture);
8888
m_sphereFBO->attachTexture(gl::GL_COLOR_ATTACHMENT2, m_geometryTexture);
8989
m_sphereFBO->attachTexture(gl::GL_DEPTH_ATTACHMENT, m_depthTexture);
90-
91-
glowutils::StringTemplate* postprocessingSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/postprocessing.frag"));
92-
93-
#ifdef MAC_OS
94-
postprocessingSource->replace("#version 140", "#version 150");
95-
#endif
90+
m_sphereFBO->setDrawBuffers({ gl::GL_COLOR_ATTACHMENT0, gl::GL_COLOR_ATTACHMENT1, gl::GL_COLOR_ATTACHMENT2 });
9691

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

105100
m_postprocessingFBO = new glow::FrameBufferObject;
106-
107101
m_postprocessingFBO->attachTexture(gl::GL_COLOR_ATTACHMENT0, m_postprocessedTexture);
108-
109-
glowutils::StringTemplate* gBufferChoiceSource = new glowutils::StringTemplate(new glow::File("data/gbuffers/gbufferchoice.frag"));
110-
111-
#ifdef MAC_OS
112-
gBufferChoiceSource->replace("#version 140", "#version 150");
113-
#endif
102+
m_postprocessingFBO->setDrawBuffer(gl::GL_COLOR_ATTACHMENT0);
114103

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

128117
window.addTimer(0, 0, false);
118+
119+
cameraChanged();
129120
}
130121

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

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

136+
cameraChanged();
137+
145138
m_colorTexture->image2D(0, gl::GL_RGBA8, event.width(), event.height(), 0, gl::GL_RGBA, gl::GL_UNSIGNED_BYTE, nullptr);
146139
m_normalTexture->image2D(0, gl::GL_RGBA16F, event.width(), event.height(), 0, gl::GL_RGBA, gl::GL_FLOAT, nullptr);
147140
m_geometryTexture->image2D(0, gl::GL_RGBA16F, event.width(), event.height(), 0, gl::GL_RGBA, gl::GL_FLOAT, nullptr);
148141
m_depthTexture->image2D(0, gl::GL_DEPTH_COMPONENT, event.width(), event.height(), 0, gl::GL_DEPTH_COMPONENT, gl::GL_FLOAT, nullptr);
149142
m_postprocessedTexture->image2D(0, gl::GL_RGBA8, event.width(), event.height(), 0, gl::GL_RGBA, gl::GL_UNSIGNED_BYTE, nullptr);
150143
}
151144

145+
void cameraChanged()
146+
{
147+
m_sphere->setUniform("transform", m_camera.viewProjection());
148+
m_sphere->setUniform("modelView", m_camera.view());
149+
m_sphere->setUniform("normalMatrix", m_camera.normal());
150+
}
151+
152152
virtual void paintEvent(PaintEvent &) override
153153
{
154154
// Sphere Pass
155155

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

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

161-
m_sphere->setUniform("transform", m_camera.viewProjection());
162-
m_sphere->setUniform("modelView", m_camera.view());
163-
m_sphere->setUniform("normalMatrix", m_camera.normal());
164-
165160
m_sphere->use();
166161
m_icosahedron->draw();
167162
m_sphere->release();
@@ -171,7 +166,6 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
171166
// Postprocessing Pass
172167

173168
m_postprocessingFBO->bind();
174-
m_postprocessingFBO->setDrawBuffer(gl::GL_COLOR_ATTACHMENT0);
175169

176170
gl::glClear(gl::GL_COLOR_BUFFER_BIT);
177171

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

192186
// GBuffer Choice Pass (including blitting)
193187

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

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

@@ -241,6 +235,7 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
241235
m_camera.setCenter(vec3());
242236
m_camera.setEye(vec3(0.f, 1.f, 4.0f));
243237
m_camera.setUp(vec3(0,1,0));
238+
cameraChanged();
244239
break;
245240
}
246241
}
@@ -268,11 +263,13 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
268263
case glowutils::WorldInHandNavigation::PanInteraction:
269264
m_nav.panProcess(event.pos());
270265
event.accept();
266+
cameraChanged();
271267
break;
272268

273269
case glowutils::WorldInHandNavigation::RotateInteraction:
274270
m_nav.rotateProcess(event.pos());
275271
event.accept();
272+
cameraChanged();
276273
break;
277274
case glowutils::WorldInHandNavigation::NoInteraction:
278275
break;
@@ -302,6 +299,7 @@ class EventHandler : public ExampleWindowEventHandler, glowutils::AbstractCoordi
302299

303300
m_nav.scaleAtMouse(event.pos(), -event.offset().y * 0.1f);
304301
event.accept();
302+
cameraChanged();
305303
}
306304

307305
virtual float depthAt(const ivec2 & windowCoordinates) const override

0 commit comments

Comments
 (0)