Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Orangeatang

Member Since 05 Oct 2004
Online Last Active Today, 09:13 AM
-----

Topics I've Started

Coloured Light Maps

26 April 2013 - 06:17 AM

Hi there!

 

I'm trying to add colour to my 2D light maps, but am having a bit of trouble getting things right.

 

I'm using two render targets, a lighting target and a colour target. I use a stencil buffer with the colour target so I can exclude objects from the lighting effects. After drawing to the colour target, I blend with the lighting target and then draw the final scene to the back buffer.

 

When I render my lights with their colour set to white everything looks great :

 

lLzYVsv.png

 

But when I set the colour of the light to something like green, the light map looks right :

 

OElx0dw.png

 

But the objects themselves aren't draw with any lighting effects, only the ambient lighting colour (which is the clear colour for the lighting buffer)... none of the green tint that  I'm hoping for :

 

oUxyl9e.png

 

After finishing the drawing of my objects to the colour buffer, I use the following to blend with my light map :

 

Effect lightEffect = anEffectManager.GetEffect( myLightMapEffectID );
            
lightEffect.Parameters["ColourMap"].SetValue( myColourTarget );

aSpriteBatch.Begin( SpriteSortMode.Deferred, BlendState.AlphaBlend, null, myRenderLightingState, null, lightEffect );
aSpriteBatch.Draw( myLightingTarget, aDrawRectangle, Color.White );
aSpriteBatch.End();

 

So my shader takes the colour map as a parameter, and tries to blend the lighting target with the current render target (which is the colour map). The render lighting state only draws pixels that have a stencil value of 1, set when I'm drawing all of my objects to the colour buffer.

 

And this is the shader I'm using :

 

// ----------- Global Variables -----------

texture ColourMap;


// --------------- Samplers ---------------

sampler mainSampler : register(s0); // light map texture
sampler colourMapSampler = sampler_state{ Texture = ColourMap; }; // colour buffer


// ---------------- Inputs ----------------

// Pixel shader input
struct PixelShaderInput
{
    float2 myTextureCoords : TEXCOORD0;
};


// --------------- Functions --------------

// Pixel shader 
float4 PixelShaderFunction( PixelShaderInput anInput ) : COLOR0
{
	// Get the colour map and light map colours
	float4 lightMapColour = tex2D(mainSampler, anInput.myTextureCoords);
	float4 colourMap = tex2D(colourMapSampler, anInput.myTextureCoords);
	
    return lightMapColour * colourMap;
}


// ----------------- Passes ---------------

technique Lighting
{
    pass Pass1
    {
        // The pixel shader combines the light map texture, the main scene being rendered and the ambient lighting
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

 

Any ideas what I'm doing wrong here? The colour & lighting render targets are both the same dimensions, so I don't think it's a problem with the texture coordinates (plus it works with the colour set to white). I've tried all of the BlendState possibilities for the light map blending, but nothing really works.

 

Thanks!


Stencil Buffer Issue

23 April 2013 - 06:26 AM

Hi there,

 

I have a 2D scene with three layers - background, center and foreground. I'm rendering the central layer first so I can perform lighting calculations, and then want to render the background followed by the foreground.

 

I'm currently trying to set up a stencil buffer when rendering the central layer, so any objects drawn in the background pass will skip pixels that already have central there.


Only trouble is the stencil test doesn't seem to apply (unless I'm completely missing how it's supposed to work), the background objects always overwrite the foreground objects (as they're second).

 

I'm setting everything up like this :

 

myColourTarget = new RenderTarget2D( aGraphicsDevice, width, height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8 );

...

graphicsDevice.SetRenderTarget( myColourTarget );
graphicsDevice.Clear( ClearOptions.Stencil | ClearOptions.Target, ClearColor, -1.0f, 0 );

myDepthStencilState.StencilEnable = true;
myDepthStencilState.StencilFunction = CompareFunction.Always;
myDepthStencilState.ReferenceStencil = 1;
myDepthStencilState.StencilPass = StencilOperation.Replace;
myDepthStencilState.DepthBufferEnable = false;

spriteBatch.Begin( SpriteSortMode.Immediate, BlendState.AlphaBlend, null, myDepthStencilState, null, objectEffect);

// draw all central objects

spriteBatch.End()

myDepthStencilState.StencilEnable = true;
myDepthStencilState.StencilFunction = CompareFunction.Equal;
myDepthStencilState.ReferenceStencil = 0;
myDepthStencilState.StencilPass = StencilOperation.Keep;
myDepthStencilState.DepthBufferEnable = false;

spriteBatch.Begin( SpriteSortMode.Immediate, BlendState.AlphaBlend, null, myDepthStencilState, null, objectEffect);

// draw all background objects

spriteBatch.End();

 

 

I've tried to do the same sort of effect using depth testing, but I can't seem to get the alpha blending correct - pixels from central objects with an alpha value of 0 always overwrite background images, and the clear colour for the render target bleeds through ( http://i.imgur.com/lRkh3Mj.png )

 

 

 

Any ideas what I'm doing wrong with the stencil test... I figured that the first stencil state would write a 1 whenever a pixel value is written, and the second state would set things up sot that it will only keep the pixel if there's a 0 in the stencil buffer.

 

Cheers!


Vertex Shader Position Offset?

22 April 2013 - 04:00 AM

Hi there,
 
I'm trying to set up a custom effect so I can modify the world matrix for each object that I'm drawing.
 
I'm drawing everything in 2D using a sprite batch, and I don't want to have to have separate calls to SpriteBatch.Begin() every time I need to use a different matrix within single frame (I'm modifying the world matrix to account for parallax scrolling).
 
I've tried to replicate the functionality of MonoGame's BasicEffect for the vertex shader :
 
float4x4 World;
float4x4 View;
float4x4 Projection;

struct VertexShaderInput
{
    float4 myColour : COLOR0;
    float2 myTexCoord : TEXCOORD0;
    float4 myPosition : POSITION0;
};

VertexShaderOutput VS( VertexShaderInput input )
{
    VertexShaderOutput output;

    float4 worldPosition  = mul(input.myPosition, World);
    float4 viewPosition  = mul(worldPosition, View);
    
    output.myPosition  = mul(viewPosition, Projection);
    output.myTexCoord  = input.myTexCoord;

    return output;
}

 

 

 
When using the default shader, objects are rendered at the correct position :
 
MebFiAJ.png
 
But when I use my vertex shader, they're rendered at an offset :
 
YvdEqSK.png
 
My view matrix (myPosition defaults to (0,0) ) :
 
Matrix.CreateLookAt( new Vector3(myPosition, 0.0f), new Vector3(myPosition, 1.0f), Vector3.Up );

 

 

 
My projection matrix :
 
 public Matrix Projection( PresentationParameters someParameters )
 {
     int width = someParameters.BackBufferWidth;
     int height = someParameters.BackBufferHeight;

     return Matrix.CreateOrthographic( width, height, -1.0f, 1.0f );
 }

 

 

 
There shouldn't be a problem with my world matrix, as I used to use it directly in the SpriteBatch.Begin() method.
 
The way I'm drawing my objects :
 
Vector2 objectCenter = new Vector2(texture.Size / 2.0f, texture.Width / 2.0f);

spriteBatch.Draw( texture, myObject.Position, null, Color.White, myObject.Rotation, objectCenter, myObject.Scale, SpriteEffects.None, 0.0f );
 
Any ideas what I might be doing wrong here? I figure it must be in either the view or the projection matrices, since the world matrix I'm using works to offset the sprite batch when I'm using the default effect. 
 
I'd debug the shaders with GLDebugger or something similar, but GLDebugger doesn't seem to pick up on my GLControl draw calls (I'm using a GLControl in a WindowsFormsHost... it's a WPF application I'm building).
 
Cheers!

Blending Render Targets

20 April 2013 - 05:44 AM

Hi there!

 

I'm building a 2D editor at the moment (using Monogame) and was wondering about the best way to blend render targets.

 

Each scene in the engine is composed of three layers, background, central and foreground... that way I can add objects to a particular layer, apply the effects and lighting I want on said layer without affecting objects in other layers.

 

Until recently I've been using a single render target to draw the scene (and a light map render target, blending the two together using a simple shader), but since I want lights to take affect in their assigned layer (not across the entire scene) I'm going to have to use separate render targets for each layer, and do a lighting pass for each one.

 

So at the end of this I'm going to be left with three render targets (lighting + objects in the layer) that I need to apply any post processing effects to, and then blend in to the final scene... and was wondering about the best way to go about this. I assume the only way to really do this is using a pixel shader (to sample each render target and blend the result), but wanted to get suggestions before I go ahead with this.

 

Also, is this complete overkill? I've not really built a 2D rendering system before, so I'm kind of winging it. I intend to add dynamic shadows on a per-layer basis as well.

 

Thanks for the help! 


Fraps?

15 April 2013 - 05:04 AM

Hi there, 

I'm using a GLControl in a WPF application (the control is hosted in a windows forms host) with Monogame, but when I run the application FRAPS doesn't pick up on it. 

Is there anything I have to do with the initialisation of the GLControl (or the GraphicDevice) that I might be missing? 

I've noticed that I'm unable to use any GL debuggers either. 

Thanks for your help!


PARTNERS