Jump to content

  • Log In with Google      Sign In   
  • Create Account


Multiple light sources


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Slig Commando   Members   -  Reputation: 327

Like
0Likes
Like

Posted 04 March 2012 - 07:11 PM

Using shadow mapping, it is easy enough to get a depth map for each light source you may have, but I am seeing difficulty in figuring out, for example, 5 different light sources, and using there associated depth maps to draw the scene. It seems google is falling short on this subject. Everything about shadow mapping is on theory, or creating a single light source! any ideas guys?

Sponsor:

#2 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 04 March 2012 - 07:24 PM

Deferred or forward rendering?

#3 Slig Commando   Members   -  Reputation: 327

Like
0Likes
Like

Posted 04 March 2012 - 08:02 PM

Could you give an "in a nutshell" explanation? In the mean time I will look into that.

#4 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 04 March 2012 - 08:04 PM

Well, you basically need to find the color that it would be for each single light and add them together.

#5 Slig Commando   Members   -  Reputation: 327

Like
0Likes
Like

Posted 05 March 2012 - 11:24 AM

So I did a some research into deferred lighting, and I have some questions(obviously!). So the first step is to create a G-Buffer. So the term "G-Buffer" is simply a term that was coined to describe a geometry buffer with no lighting applied correct? Its not some sort of render target I am unaware of? So anyways, the g buffer is supposed to hold the vert position, color, normal ect... But here lies my first problem. The only semantics that are supported by pixel shaders are COLORn and DEPTHn. COLORn is float4, so I guess you could hold most of your information in that, but is this the correct way to accomplish this? Am I thinking about the process correctly?

Step 1: draw scene like normal with no lighting applied(easy), but instead of only putting out color(like normal), include vector position and normal(what????!?!?!?).
There is no support for this as far as I am aware.

Please and thank you for any help!

#6 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 05 March 2012 - 03:13 PM

The G-Buffer is just a name for a collection of regular render targets/FBOs. You store normals and positions in color buffers, although for best results you usually want to bump the bpp of the buffers. (for instance, use GL_RGB32F) for position. To do deferred rendering youll pretty much need to use shaders. Then, you'd create color, normal, position, and depth buffers and bind them to the framebuffer, and render your scene to them (fragment shader):
out vec4 outputColor[8];
void main()
{
outputColor[0]=texture2D(textures[0],uv[0]);
if(outputColor[0].a<.01)
  discard;
outputColor[1]=vec4(Normal,1);
outputColor[2]=vec4(Position,1);
}
This would save the texture (diffuse/albedo) to the first FBO in the framebuffer, the normal to the second, etc.
Then you can easily do multipass algorithims by just drawing a fullscreen quad (processing each pixel in your buffers once) to do lighting, etc

#7 Slig Commando   Members   -  Reputation: 327

Like
0Likes
Like

Posted 05 March 2012 - 03:24 PM

So after doing some more research and getting a better understanding of the process, here is what I have thus far.

Here is my new main render loop(updating to deferred renderer)
_d3dDevice->SetRenderTarget(0, colorSurf);
	_d3dDevice->SetRenderTarget(1, normSurf);
	_d3dDevice->SetRenderTarget(2, depthSurf);

	_gBufferFill->SetTechnique("Fill");

	_d3dDevice->BeginScene();	

		//Loop through and draw everything in the world
		for(int n = 0; n < _worlds[_views[view].world].Size(); n++)
		{
			//Set view and projection matrices
			if(_gBufferFill>SetMatrix("cView", &_camera->GetViewViewMatrix(view)) != D3D_OK)
				ErrorMessenger::ReportMessage("Failed to set View Matrix!", __FILE__, __LINE__);

			if(_gBufferFill->SetMatrix("cProj", &_camera->GetViewProjectionMatrix(view)) != D3D_OK)
				ErrorMessenger::ReportMessage("Failed to set Projection Matrix!", __FILE__, __LINE__);
			
			//Draw the item
			_worlds[_views[view].world][n]->GBufferDraw(_d3dDevice, &_gBufferFill);

			//Kill object if requested to
			if(_worlds[_views[view].world][n]->GetKill())
				_worlds[_views[view].world].Free(n);
		}

	_d3dDevice->EndScene();

This is my effect to fill a gbuffer
//GBuffer MRT order-
//1 - Color
//2 - Normal
//3 - Depth
//Bit depth - 32

//Object world
float4x4 oWorld : WORLD;

//Camera view and projection
float4x4 cView : VIEW;
float4x4 cProjection : PROJECTION;

texture objectTexture;

//Object texture sampler
sampler ObjectTexture =
sampler_state
{
	Texture = <objectTexture>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
};

void vsFill(float4 iPos : POSITION0,
			float4 iNorm : NORMAL0,
			float2 iUV : TEXCOORD0,
			out float4 oPos : POSITION0,
			out float4 oNorm : TEXCOORD0,
			out float2 oUV : TEXCOORD1,
			out float4 oDepth : TEXCOORD2)
{
	//Transform vertex position
	float4 vWorldPos = mul(iPos, oWorld);
	float4 vViewPos = mul(vWorldPos, cView);
	oPos = mul(vViewPos, cProjection);

	//Get depth
	oDepth = oPos.z / oPos.w;

	//Get normal into world space
	oNorm = mul(iNorm, oWorld);

	//Pass uv through to pixel shader
	oUV = iUV;
}

void psFill(float4 iNorm : TEXCOORD0,
			float2 iUV : TEXCOORD1,
			float4 iDepth : TEXCOORD2,
			out float4 colorOut : COLOR0,
			out float4 normOut : COLOR1,
			out float4 depthOut : COLOR2)
{
	//Color map
	colorOut = tex2D(ObjectTexture, iUV);
	
	//Normal map
	//TODO understand what is happening here. Taken from (http://www.catalinzima.com/tutorials/deferred-rendering-in-xna/creating-the-g-buffer/)
	normOut.xyz = 0.5f * (normalize(iNorm) + 1.0f);
	normOut.w = 1.0f;
	
	//Depth map
	depthOut = iDepth;
}

technique Fill
{
	pass p0
	{
		VertexShader = compile vs_2_0 vsFill();
		PixelShader = compile ps_2_0 psFill();
	}
}

And how I use the shader for each object I am drawing(GBufferDraw()):
//Set texture, translate object and set world matrix
			if(gBufferFill->SetTexture("objectTexture", GetActiveTexture()) != D3D_OK)
				ErrorMessenger::ReportMessage("Failed to set texture", __FILE__, __LINE__);
				
			//Translate the objects position
			D3DXMatrixTranslation(&GetObjectIndependents()->matTranslate, GetDrawLocation()->x, GetDrawLocation()->y, GetDrawLocation()->z);

			//Set the world
			if(gBufferFill->SetMatrix("oWorld", &(MultiplyIndependents())) != D3D_OK)
				ErrorMessenger::ReportMessage("Failed to set World View Matrix!", __FILE__, __LINE__);

			UINT numPass;
			gBufferFill->Begin(&numPass, 0);

				for(unsigned short i = 0; i < numPass; i++)
				{
					gBufferFill->BeginPass(i);

						device->SetStreamSource(0, p_vertexBuffer, 0, sizeof(LWVERTEX));
						device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, p_verticeCount / 3);

					gBufferFill->EndPass();
				}

			gBufferFill->End();

I cannot seem to get good results to my color, normal, or depth map. I only get all black

#8 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 05 March 2012 - 03:53 PM

Ok, first, you want to start with just drawing some solid and unique color from the first phase to the color buffer, and then just reading that out and displaying it on your second phase to make sure your rendering to texture is correct. Second, I cant really tell what youre drawing in your second phase. The /3 in the draw call makes me assume youre drawing your model again, however you shouldnt need to. You just need to draw a flat polygon over the entire screen. Drawing your model again *may* work, although its pointless processing. If youre doing that way for a test or something, remember to disable the depth buffer, or add a depth offset so your models arent failing the depth test the second time

#9 Slig Commando   Members   -  Reputation: 327

Like
0Likes
Like

Posted 05 March 2012 - 04:20 PM

Thank you for sticking around for this long zacaj =). So I solved my last problem in my last post. I wasn't clearing my 3 render targets(doh!).

The /3 in the draw call makes me assume youre drawing your model again, however you shouldnt need to.


The draw method there is being called for individual objects only once.

So anyways, I am getting good gbuffer content now.

Color
Posted Image

Normal
Posted Image

and Depth
Posted Image

It is at this point I am kind of lost. Not sure, maybe I haven't gotten to that point in the tutorials yet. I will continue on, and check back here regularly for any words of wisdom you may have for me =)

#10 zacaj   Members   -  Reputation: 643

Like
1Likes
Like

Posted 05 March 2012 - 04:24 PM

Ok, feel free to PM me or something if this topic gets buried :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS