Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 23 Nov 2007
Offline Last Active Apr 16 2015 11:18 AM

Topics I've Started

DX9 Deferred Shading: I have a couple problems that need solving

02 April 2014 - 03:18 PM

For about a week now I've been learning deferred shading. Things were running smooth until I ran into a couple snags. while implementing my shaders(3.0) I have been getting strange lighting artifacts and I managed to narrow it down to the normal map/buffer of the g-buffer:
If you look along the left edge of the model you'll see the artifact( orangish color ). I've notice that this is a common problem with deferred shaders and have not mange to find any way to resolve the issue. I've tried disabling multi-sampling/anti-aliasing, adjusting the filters to none, point, linear, antistropic, and adjusting the pixel cooridantes to match the texel offset( -=.5/screenWidth, -=.5/screenHeight). Different techniques only minimize the artifacts.
So my first question is, how do you combat lighting artifacts.
Another problem that I have is that when the camera sits above or below the model, along the y-axis, the model is not rendered. Why is this?



If I adjust the camera's x or y value the slightest, say .0001f, then the model is rendered. Is this another down sides of deferred shading?

shader code:

//    G L O B A L     V A R I A B L E S
float4x4    gWorld;
float4x4    gWorldViewProjection;
float        gSpecularIntensity;
float        gSpecularPower;
sampler2D    gColorMap;
vsOutput    vsDeferredShaderGeometryBuffer( vsInput IN )
    vsOutput OUT    = (vsOutput)0;

    OUT.position    = mul( float4(IN.position,1.f), gWorldViewProjection );
    OUT.texcoord    = IN.texcoord;
    OUT.normal        = normalize(mul( IN.normal, (float3x3)gWorld ));
    OUT.depth.x        = OUT.position.z;
    OUT.depth.y        = OUT.position.w;
    return OUT;
psOutput    psDeferredShaderGeometryBuffer( vsOutput IN )
    psOutput OUT    = (psOutput)0;
    OUT.color.rgb    = tex2D( gColorMap, IN.texcoord );
    OUT.color.a        = 1;
    OUT.normal.xyz    = IN.normal * .5f + .5f;
    OUT.normal.z    = 0;
    OUT.depth        = IN.depth.x / IN.depth.y;
    return OUT;

[lighting shader]

//    G L O B A L     V A R I A B L E S
//float4        gAmbient;
//float4        gLightAmbient;
//float4        gMaterialAmbient;
float4x4    gInverseViewProjection;
float4        gLightDiffuse;
float4        gMaterialDiffuse;
float3        gLightDirection;
float3        gCameraPosition;
float        gSpecularIntensity;
float        gSpecularPower;
sampler2D    gColorMap;
sampler2D    gNormalMap;
sampler2D    gDepthMap;
vsOutput vsDeferredShaderDirectionalLighting( vsInput IN )
    vsOutput OUT    = (vsOutput)0;
    OUT.position    = float4( IN.position, 1.f );
    OUT.texcoord    = IN.texcoord;// - float2( .5/800, .5/600 );

    return OUT;
float4    psDeferredShaderDirectionalLighting( vsOutput IN ) : COLOR
    float4 pixel                = tex2D( gColorMap, IN.texcoord );
        if( (pixel.x+pixel.y+pixel.z) <=0 )  return pixel;

    float3 surfaceNormal        = (tex2D( gNormalMap, IN.texcoord )-.5f)*2.f;
    float4 worldPos                = 0;
        worldPos.x                = IN.texcoord.x * 2.f - 1.f;
        worldPos.y                = -( IN.texcoord.y * 2.f - 1.f );
        worldPos.z                = tex2D( gDepthMap, IN.texcoord ).r;
        worldPos.w                = 1.f;
        worldPos                = mul( worldPos, gInverseViewProjection );
        worldPos                /= worldPos.w;
    //if( surfaceNormal.r + surfaceNormal.g + surfaceNormal.b <= 0 )
    //    return 0 ;
    float lightIntensity        = saturate( dot( surfaceNormal, -normalize(gLightDirection) ) );
    float specularIntensity        = saturate( dot( surfaceNormal, normalize(gLightDirection)+(gCameraPosition-worldPos)));
    float specularFinal            = pow( specularIntensity, gSpecularPower ) * gSpecularIntensity;

    //float4 ambient                = ((gAmbient+gLightAmbient)*gMaterialAmbient);

    return float4( ( gMaterialDiffuse * gLightDiffuse * lightIntensity).rgb, specularFinal);


[lighting shader]


Thanks in advance


Having trouble blending the colors of an anti-alias line, help plz!

17 August 2013 - 12:09 PM



I recently learned how to render anti-alias lines using the Wu algorithm. All was going well until I decided to change the background color from black to another color, and the results changed to this:


Attached File  error01.PNG   2.31KB   7 downloads


from this:


Attached File  error02.PNG   2.29KB   5 downloads


To solve the problem I tried to alpha blend the resulting pixel, calculated with the Wu algorithm, with the background pixel underneath it. The results did not look any better. So, is there any way to modify the Wu algorithm to take in account the background pixel when calculating the levels of intensity for the new pixel of the anti-alias line? This article( http://www.codeproject.com/Articles/13360/Antialiasing-Wu-Algorithm ) does it successfully but I can't duplicate the results because I have trouble deciphering the source code. Also are there any smoothing techniques that I could use to smooth out the lines? Because if you look at the lines of the black image you will notice a candy cane like effect on some of the lines.


Here is some source code to help under stand what's going on:

if( fabs(delta.x) >= fabs(delta.y) )	// line is more horizontal
		if( p0.x > p1.x )	// process left to right
			SmyVectorF temp = p0;
			p0		= p1;
			p1		= temp;
			delta	= SmyVectorF( p1.x-p0.x, p1.y-p0.y );

		int x0			= (int)floor(p0.x);		// start x
		int x1			= (int)floor(p1.x);		// end x
		float y			= p0.y;
		float gradient	= delta.y / delta.x;	// slope of the line

			for( int x = x0; x < x1; x++ )
				if( x >= 0 && x < w && y >= 0 && y <= h )
				float intensity1	= y-float(floor(y));
				float intensity0	= 1.f-intensity1;
				UNINT pixel					= (UNINT) (floor(y)*w)+x;
				backbuffer->bits[pixel]		= SMYUTI_rgb( UNCHR(red*intensity0), UNCHR(green*intensity0), UNCHR(blue*intensity0) );
				backbuffer->bits[(pixel+=w)]	= SMYUTI_rgb( UNCHR(red*intensity1), UNCHR(green*intensity1), UNCHR(blue*intensity1) );
				y += gradient;
		if( p0.y >= 0 && p0.x >= 0 && p0.y <= h && p0.x <= w )
			backbuffer->bits[int(p0.y)*w+int(p0.x)]	= m_color;	// render start pixel

		if( p1.y >= 0 && p1.x >= 0 && p1.y <= h && p1.x <= w )
			backbuffer->bits[int(p1.y)*w+int(p1.x)]	= m_color;	// render end pixel

Thanks in advance.

Need help solving GJK woes

17 March 2013 - 03:51 PM

Good afternoon people of Game Dev.
        For the past several days now I have been trying to implement the GJK algorithm for my 2D game with no avail. I fully understand the concept behind the algorithm and theoretically implementing it. But I believe my problems lie elsewhere: lacking knowledge and experience with vectors(resulting in bad calculation), Win32 API(Inverted coordinate), multiple resources whose implementation of the algorithm greatly varies, etc.
        Nevertheless, I'd appreciate it if someone could look at my code and tell me what I'm doing wrong. I believe the problem lies with me failing to  find the correct perpendicular vectors of an edge. I've tried various methods of calculations: perpAB = ABxACxAC, ABxAOxAB. and the last method, posted in the code below, with no conclusive result. I've tried just using the normal/perpendicular, being that for now I'm attempt to implement it in 2D.
The brunt of the Code:

bool                SMYGJK::gjk_work( SmyPoint & D, SmyPoint simplex[], int & count )
    SmyPoint a        = simplex[count-1];            // point a, the newest point added to the simplex
    SmyPoint b        = simplex[0];                // point b
    SmyPoint origin    = -a;                        // ao(direction towards origin) = origin(0,0) - a which equals -a
    SmyPoint ab        = b - a;                    // edge ab( vector between a and b )

    if( count == 2 )// simplex-1( line )
        SmyPoint abp    = ab.normal();

        // Find ab perp in direction of the origin
        if( abp.dot( origin ) < 0 )

        D    = abp;                        // change the direction to where a new point should be located
    if( count == 3 )//simplex-2(triangle)
        SmyPoint c        = simplex[1];
        SmyPoint ac        = c - a;
        SmyPoint abp    = ab.normal();    // possible new search direction if collision not found

        if( abp.dot( c ) >= 0 )            // Check other side if facing wrong way

        if( abp.dot( origin ) > 0 )
            // remove c
            simplex[0]    = b;
            simplex[1]    = a;
            D            = abp;            // change the direction to where a new point should be located
            // check direction ac perp
            SmyPoint acp    = ac.normal();

            if( acp.dot( b ) >= 0 )        // Check other side if facing wrong way
            if( acp.dot( origin ) <= 0 )// collision detected because not in region acperp nor abperp, thus, origin is enclosed
                return true;

            // remove b
            simplex[0]    = c;
            simplex[1]    = a;
            D            = acp;            // Face direction perpendicular to edge ac

    // keep building
    return false;


Help is much appreciated. If you'd like to see the support function or function that contains the loop of the algorithm let me know.

Need help with 2D Elastic collisions

26 March 2010 - 04:21 PM

Hi peoples, I need help modifying my elastic collisions function. The calculations are correct, with one exception: the coefficient of restitution does not seems to be getting calculated into the final results, e.g.: if the COR is 0, then there should be inelastic collisions, but the colliding projectiles still deflects as if the COR is 1. I suspect the problem occurs, within the last two blocks of code... when calculating the final velocities and rotating the axis back to normal. My math is not promising at all, so i could use the much needed help.
template<typename T> SmyLineSeg<T> SMYUTI_ElasticCollAng(SmyLineSeg<T> centerMass, T deg1, T lbs1, T vel1, T deg2, T lbs2, T vel2, T cor)
  // Calculate the scalar "collision line" angle
  float ang = (float)atan2f(centerMass.p1.x-centerMass.p2.x,centerMass.p1.y-centerMass.p2.y);
  // Convert velocity and direction to vector counterparts
  SmyVertice<float> velocity1;
  velocity1.x = cosf( _DegreeToRadian(deg1) ) * vel1;
  velocity1.y = sinf( _DegreeToRadian(deg1) ) * vel1;
  SmyVertice<float> velocity2;
  velocity2.x = cosf( _DegreeToRadian(deg2) ) * vel2;
  velocity2.y = sinf( _DegreeToRadian(deg2) ) * vel2;
  // Convert mass, from pounds to kgs
  float mass1 = SMYGEO_LBS_to_KGS( lbs1, 9.80f );
  float mass2 = SMYGEO_LBS_to_KGS( lbs2, 9.80f );
  // Calculate the "collision line"
  float vp1 = velocity1.x * cosf(ang) + velocity1.y * sinf(ang);
  float vp2 = velocity2.x * cosf(ang) + velocity2.y * sinf(ang);
  // Calculate the rotated velocities along the "collision line" vector
  float vn1 = -velocity1.x * sinf(ang) + velocity1.y * cosf(ang);
  float vn2 = -velocity2.x * sinf(ang) + velocity2.y * cosf(ang);
  // Calculate the final velocities: 1 and 2
  float fv1 = ((mass1-cor*mass2)/(mass1+mass2))*vp1 + (((1+cor)*mass2)/(mass1+mass2))*vp2;
  float fv2 = (((1+cor)*mass1)/(mass1+mass2))*vp1 + ((mass2-cor*mass1)/(mass1+mass2))*vp2;
  // Calculate final x/y vector velocities 2, and rotate back
  SmyVertice<float> f2;
  f2.x = fv1 * cosf(ang) - vn1 * sinf(ang);
  f2.y = fv1 * sinf(ang) + vn1 * cosf(ang);
  // Calculate final x/y vector velocities 1, and rotate back
  SmyVertice<float> f1;
  f1.x = fv2 * cosf(ang) - vn2 * sinf(ang);
  f1.y = fv2 * sinf(ang) + vn2 * cosf(ang);
  SmyLineSeg<T> results( (T)f1.x, (T)f1.y, (T)f2.x, (T)f2.y );
  return results;

Texture Postion and Screen Resolution Problem

14 October 2008 - 05:44 PM

Hi, Im relatively new to DirectX and for some days now I have been making a 2D tile based game. My problem is when ever I draw sprites on the screen in windowed mode either the texture or the rectangle position(used to represent a boundary box) is off. Im pretty sure that the texture is drawn in the wrong location due to some weird scaling. This only happens when I set the directx device to windowed and everything works fine in fullscreen mode. I also noticed when in windowed mode the mouse y-value does not go beyound 575 and my screen resolution is 800x600 so I figured I setup something wrong. Does anyone have any idea why this is happening. This causes problems when ever the user attempts to click a button and cannot because either the texture or the boundary box is not where it is suppose to be. Thanks In Advance, Bradley