• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Deferred shading - light accumulation issue

7 posts in this topic

I'm working on deferred shading and all has gone well until it comes time to render multiple lights. The first shader pass renders my scene to a G-buffer with color (albedo), position, normal, and material (specular, etc) data written to color attachments. The next shader passes create and blur a SSAO texture from the normal data and a randomizer, and it looks nice. Then I do the final lighting pass. I start experimenting by rendering out a sphere to represent a large point light (see fig. 1) and it looks mostly like what I would expect. So far so good.

My goal is to draw a ton of lights in my scene. When I add a second light, it occludes the first (see fig. 2). I've tried using [font=courier new,courier,monospace]glAccum()[/font] to no avail. When I render a second light which overlaps the first in screen space (see fig. 3 and fig. 4), that pixel is overwritten. I'm having trouble understanding how to create and display a light accumulation buffer in a single shader pass. I could ping-pong FBOs between lights, but that seems like it would be really slow, and has not been suggested in any material I've read.

My vertex shader (see fig. 5) and fragment shader (see fig. 6) in my lighting pass are below.

Fig 1. One light

Fig 2. Two lights, occluding instead of accumulating

Fig 3. Spheres representing lights

Fig 4. Zoom out of fig. 3

Fig. 5 Lighting pass vertex shader
varying vec4 lightPosition;

void main( void )
lightPosition = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position = lightPosition;

Fig. 6 Lighting pass fragment shader
uniform vec3 eyePoint; // Camera eye point
uniform vec4 lightAmbient; // Light ambient color
uniform vec3 lightCenter; // Center of light shape
uniform vec4 lightDiffuse; // Light diffuse color
uniform float lightRadius; // Size of light
uniform vec4 lightSpecular; // Light specular color
uniform vec2 pixel; // To convert pixel coords to [0,0]-[1,1]
uniform sampler2D texAlbedo; // Color data
uniform sampler2D texMaterial; // R=spec level, G=spec power, B=emissive
uniform sampler2D texNormal; // Normal-depth map
uniform sampler2D texPosition; // Position data
uniform sampler2D texSsao; // SSAO

varying vec4 lightPosition; // Position of current vertex in light

void main()

// Get screen space coordinate
vec2 uv = gl_FragCoord.xy * pixel;

// Sample G-buffer
vec4 albedo = texture2D( texAlbedo, uv );
vec4 material = texture2D( texMaterial, uv );
vec4 normal = texture2D( texNormal, uv );
vec4 position = texture2D( texPosition, uv );
vec4 ssao = texture2D( texSsao, uv );

// Calculate reflection
vec3 eye = normalize( -eyePoint );
vec3 light = normalize( lightCenter.xyz - position.xyz );
vec3 reflection = normalize( -reflect( light, normal.xyz ) );

// Calculate light values
vec4 ambient = lightAmbient;
vec4 diffuse = clamp( lightDiffuse * max( dot( normal.xyz, light ), 0.0 ), 0.0, 1.0 );
vec4 specular = clamp( material.r * lightSpecular * pow( max( dot( reflection, eye ), 0.0 ), material.g ), 0.0, 1.0 );
vec4 emissive = material.b;

// Combine color and light values
vec4 color = albedo;
color += ambient + diffuse + specular + emissive;

// Apply light falloff
float falloff = 1.0 - distance( lightPosition, position ) / lightRadius;
color *= falloff;

// Apply SSAO
color -= vec4( 1.0 ) * ( 1.0 - ssao.r );

// Set final color
gl_FragColor = color;
[/CODE] Edited by bantherewind

Share this post

Link to post
Share on other sites
Typically when accumulating lights you'll need to enable blending, so that the result of the fragment shader gets combined with what's already in the buffer (i.e. the result from previous lights):

glClear(GL_COLOR_BUFFER_BIT); // clear light accumulation buffer to black here

glBlendFunc(GL_ONE, GL_ONE); // additive blending

// render lights


Share this post

Link to post
Share on other sites
Got it! Needed to disable depth testing.

The lights are blending, but now it seems I can only see from "inside" the lights, or close to it. If I zoom out, I would expect the scene to be illuminated, but smaller. Instead it goes black, as if the only visible area is inside the sphere. Edited by bantherewind

Share this post

Link to post
Share on other sites
[quote name='bantherewind' timestamp='1352323238' post='4998586']
Got it! Needed to disable depth testing.

The lights are blending, but now it seems I can only see from "inside" the lights, or close to it. If I zoom out, I would expect the scene to be illuminated, but smaller. Instead it goes black, as if the only visible area is inside the sphere.

Do you have backface culling enabled? If so, I think you need to flip it when you "enter"/"exit" a light sphere.

From http://www.catalinzima.com/tutorials/deferred-rendering-in-xna/point-lights/ :

"After setting the parameters, we draw the sphere model, using the effect file. But before we do this, we must set the desired culling mode. If we are outside the sphere, we want to draw the exterior of the sphere. Otherwise, when the camera in inside the light volume, we need to draw the inner side of the sphere. Using CullMode.None would apply the lighting calculations twice when the camera is outside the viewing volume, which is not desirable. By switching the culling mode, we make sure that the light is always applied once."

Share this post

Link to post
Share on other sites
So here's the new issue. The lights are blending into each other better (see fig. 1), but I have to be inside (or almost inside) the sphere. Everything goes black if I back out (see fig.2 and fig. 3). The [font=courier new,courier,monospace]falloff[/font] variable in my fragment shader is controlling how much to mix the color. This should should be determined by how far the current light vertex is away from the position sampled from the G-buffer. Both are calculated using [font=courier new,courier,monospace]gl_ModelViewProjectionMatrix * gl_Vertex[/font], so the values remain relative. I'm not sure if the camera's position is somehow reducing this distance or there is something inherent in drawing these spheres that is blocking the view of what's inside.

Fig .1 Lights are now blending into each other

Fig.2 Image gets darker from outside

Fig. 3 Light position when the image gets too dark to see.

Share this post

Link to post
Share on other sites
Well, now I feel like an idiot. I had a typo in the C++ when setting the uniform for my position texture (was spelled "texPositon"). My position was always 0,0,0 which gave me a bad falloff value. Thanks for the help!

It's subtle, but this has two point lights and a directional light blending nicely. And I can see it from far away. [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]


[img]http://bantherewind.com/uploads/accum_09_sm.jpg[/img] Edited by bantherewind

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0