Skip to content

Commit

Permalink
Renderer|FX|libgui: Working on accessing the depth buffer from a shader
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Nov 28, 2013
1 parent 57f2860 commit 23a7552
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 13 deletions.
15 changes: 4 additions & 11 deletions doomsday/client/data/renderer.pack/shaders/lensflares.fsh
Expand Up @@ -5,16 +5,9 @@ varying highp vec2 vUV;
void main(void) {
highp vec4 tex = texture2D(uTex, vUV);
gl_FragColor = tex * vColor;

/*
highp vec4 original = texture2D(uTex, vUV);
highp float intens =
(0.2125 * original.r) +
(0.7154 * original.g) +
(0.0721 * original.b);
gl_FragColor = vec4(vec3(intens), 1.0);
if(uFadeInOut < 1.0) {
gl_FragColor = mix(original, gl_FragColor, uFadeInOut);

// Discard fragments without alpha.
if(gl_FragColor.a <= 0.0) {
discard;
}
*/
}
20 changes: 20 additions & 0 deletions doomsday/client/data/renderer.pack/shaders/lensflares.vsh
@@ -1,10 +1,13 @@
uniform highp mat4 uMvpMatrix;
uniform highp vec2 uViewUnit;
uniform sampler2D uDepthBuf;

attribute highp vec4 aVertex;
attribute highp vec4 aColor;
attribute highp vec2 aUV;
attribute highp vec2 aUV2;
attribute highp vec2 aUV3;

varying highp vec4 vColor;
varying highp vec2 vUV;

Expand All @@ -14,9 +17,26 @@ void main(void) {
// Position on the axis that passes through the center of the view.
highp float axisPos = aUV3.s;
gl_Position.xy *= axisPos;

// Is this occluded in the depth buffer?
highp vec2 depthUv = gl_Position.xy / gl_Position.w / 2.0 + vec2(0.5, 0.5);
float depth = texture2D(uDepthBuf, depthUv).r;
/* if(depth < (gl_Position.z/gl_Position.w + 1.0)/2.0) {
// Occluded!
vUV = aUV;
vColor = vec4(0.0, 0.0, 0.0, 0.0);
return;
} */

gl_Position.xy += aUV2 * uViewUnit * vec2(gl_Position.w);

vUV = aUV;
vColor = aColor;

if(depth < (gl_Position.z/gl_Position.w + 1)/2) {
// Occluded!
vColor = vec4(1.0, 0.0, 0.0, 1.0);
return;
}

}
5 changes: 4 additions & 1 deletion doomsday/client/src/render/fx/lensflares.cpp
Expand Up @@ -228,6 +228,7 @@ DENG2_PIMPL(LensFlares)
GLUniform uMvpMatrix;
GLUniform uViewUnit;
GLUniform uAtlas;
GLUniform uDepthBuf;

Instance(Public *i)
: Base(i)
Expand All @@ -236,6 +237,7 @@ DENG2_PIMPL(LensFlares)
, uMvpMatrix("uMvpMatrix", GLUniform::Mat4)
, uViewUnit ("uViewUnit", GLUniform::Vec2)
, uAtlas ("uTex", GLUniform::Sampler2D)
, uDepthBuf ("uDepthBuf", GLUniform::Sampler2D)
{}

~Instance()
Expand All @@ -254,9 +256,10 @@ DENG2_PIMPL(LensFlares)
drawable.addBuffer(buffer);
self.shaders().build(drawable.program(), "fx.lensflares")
<< uMvpMatrix << uViewUnit
<< uAtlas;
<< uAtlas << uDepthBuf;

uAtlas = res->atlas;
//uDepthBuf = ClientWindow::main().canvas().depthTexture();
}

void glDeinit()
Expand Down
7 changes: 7 additions & 0 deletions doomsday/libgui/include/de/gui/canvas.h
Expand Up @@ -160,6 +160,13 @@ class LIBGUI_PUBLIC Canvas : public QGLWidget, public KeyEventSource, public Mou
*/
GLTarget &renderTarget() const;

/**
* Returns the GL texture containing the depth buffer.
*
* @return GL depth target.
*/
GLTexture &depthTexture() const;

protected:
void initializeGL();
void resizeGL(int w, int h);
Expand Down
12 changes: 12 additions & 0 deletions doomsday/libgui/include/de/gui/gltarget.h
Expand Up @@ -54,6 +54,7 @@ class LIBGUI_PUBLIC GLTarget : public Asset

ColorDepth = Color | Depth,
ColorDepthStencil = Color | Depth | Stencil,
ColorStencil = Color | Stencil,
DepthStencil = Depth | Stencil,

NoAttachments = 0,
Expand Down Expand Up @@ -101,6 +102,17 @@ class LIBGUI_PUBLIC GLTarget : public Asset

Flags flags() const;

/**
* Changes the configuration of the render target. Any previously allocated
* renderbuffers are released.
*
* @param attachment Where to attach @a texture.
* @param texture Texture to render on.
* @param otherAttachments Other supporting attachments (renderbuffers).
*/
void configure(Flags const &attachment, GLTexture &texture,
Flags const &otherAttachments = NoAttachments);

/**
* Activates this render target as the one where GL drawing is being done.
*/
Expand Down
4 changes: 4 additions & 0 deletions doomsday/libgui/include/de/gui/gltexture.h
Expand Up @@ -150,6 +150,10 @@ class LIBGUI_PUBLIC GLTexture : public Asset
*/
void setUndefinedImage(gl::CubeFace face, Size const &size, Image::Format format, int level = 0);

void setUndefinedContent(Size const &size, Image::GLFormat const &glFormat, int level = 0);

void setUndefinedContent(gl::CubeFace face, Size const &size, Image::GLFormat const &glFormat, int level = 0);

/**
* Sets the image content of the texture at a particular level. The format
* of the image determines which GL format is chosen for the texture.
Expand Down
14 changes: 13 additions & 1 deletion doomsday/libgui/src/canvas.cpp
Expand Up @@ -20,6 +20,7 @@
#include "de/Canvas"
#include "de/CanvasWindow"
#include "de/GLState"
#include "de/GLTexture"
#include "de/gui/opengl.h"

#include <de/App>
Expand All @@ -44,6 +45,8 @@ static const int MOUSE_WHEEL_CONTINUOUS_THRESHOLD_MS = 100;
DENG2_PIMPL(Canvas)
{
GLTarget target;
GLTexture depthTexture; ///< Texture where framebuffer depth values are available.

CanvasWindow *parent;
bool readyNotified;
Size currentSize;
Expand All @@ -62,7 +65,7 @@ DENG2_PIMPL(Canvas)
readyNotified(false),
//mouseDisabled(false),
mouseGrabbed(false)
{
{
wheelDir[0] = wheelDir[1] = 0;
#ifdef WIN32
altIsDown = false;
Expand Down Expand Up @@ -268,6 +271,11 @@ GLTarget &Canvas::renderTarget() const
return d->target;
}

GLTexture &Canvas::depthTexture() const
{
return d->depthTexture;
}

void Canvas::initializeGL()
{
LOG_AS("Canvas");
Expand Down Expand Up @@ -320,6 +328,10 @@ void Canvas::notifyReady()

d->readyNotified = true;

// Set up a rendering target with depth texture.
//d->depthTexture.setUndefinedContent(size(), Image::GLFormat(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 2));
//d->target.configure(GLTarget::Depth, d->depthTexture, GLTarget::Color);

LOG_DEBUG("Notifying GL ready");

DENG2_FOR_AUDIENCE(GLReady, i) i->canvasGLReady(*this);
Expand Down
13 changes: 13 additions & 0 deletions doomsday/libgui/src/gltarget.cpp
Expand Up @@ -247,6 +247,19 @@ GLTarget::GLTarget(Vector2ui const &size, Flags const &flags)
d->alloc();
}

void GLTarget::configure(Flags const &attachment, GLTexture &texture, Flags const &otherAttachments)
{
d->release();

// Set new configuration.
d->texture = &texture;
d->textureAttachment = attachment;
d->flags = attachment | otherAttachments;
d->size = texture.size();

d->alloc();
}

void GLTarget::glBind() const
{
if(!isReady()) return;
Expand Down
28 changes: 28 additions & 0 deletions doomsday/libgui/src/gltexture.cpp
Expand Up @@ -309,6 +309,34 @@ void GLTexture::setUndefinedImage(CubeFace face, GLTexture::Size const &size,
setState(Ready);
}

void GLTexture::setUndefinedContent(Size const &size, Image::GLFormat const &glFormat, int level)
{
d->texTarget = GL_TEXTURE_2D;
d->size = size;
d->format = Image::Unknown;

d->alloc();
d->glBind();
d->glImage(level, size, glFormat, NULL);
d->glUnbind();

setState(Ready);
}

void GLTexture::setUndefinedContent(CubeFace face, Size const &size, Image::GLFormat const &glFormat, int level)
{
d->texTarget = GL_TEXTURE_CUBE_MAP;
d->size = size;
d->format = Image::Unknown;

d->alloc();
d->glBind();
d->glImage(level, size, glFormat, NULL, face);
d->glUnbind();

setState(Ready);
}

void GLTexture::setImage(Image const &image, int level)
{
d->texTarget = GL_TEXTURE_2D;
Expand Down

0 comments on commit 23a7552

Please sign in to comment.