Jump to content
  • Advertisement
Sign in to follow this  
bushman_IL

Trouble with Texture Blending D3D9

This topic is 3184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have been having a problem getting a reflection texture for a floor in my application to blend correctly with the floor texture. Here is what I have tried so far... as a reference point, here is a screen shot of my application without attempting blending: Screen Shot Without Blending Now when I try to attempt blending my reflection texture with my floor texture I get the following result: Screen Shot With Blending The code I used is as follows:
device.SetTexture(0, floorTexture);
            device.SetTextureStageState(0, TextureStageStates.ColorOperation, 4);
            device.SetTextureStageState(0, TextureStageStates.ColorArgument1, 0x00000002);
            device.SetTextureStageState(0, TextureStageStates.ColorArgument2, 0x00000000);

            device.SetTexture(1, reflectionTexture);
            device.SetTextureStageState(1, TextureStageStates.ColorOperation, 4);
            device.SetTextureStageState(1, TextureStageStates.ColorArgument1, 0x00000002);
            device.SetTextureStageState(1, TextureStageStates.ColorArgument2, 0x00000001);
I am using .NET but essentially I am modulating Texture * Diffuse using the floor texture in the first stage and Texture * Result using the reflection texture in the second stage. As you can see, this looks pretty bad, even my floor texture looks completely distored. Just as a test, I used the reflectionTexture in the first stage and the floor texture in the second stage and this is what I got: Screen Shot reverse texture stages You can see that looks much better but my floor texture lost it's detail, I know this isn't right, but just thought I would throw that out there. Can anyone suggest what I might be doing wrong? Here is a little code snippet of my render function, hopefully this will be enough to determine what is wrong:
public void OnFrameRender(Device device, double appTime, float elapsedTime)
{

	RenderReflection( device );
    bool beginSceneCalled = false;

    // Clear the render target and the zbuffer 
    device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, 0x00FFFFFF, 1.0f, 0);
    try
    {
        device.BeginScene();
        beginSceneCalled = true;

		// setup texture projection
		device.TextureState[0].TextureCoordinateIndex = (int)TextureCoordinateIndex.CameraSpacePosition;
		device.TextureState[0].TextureTransform = TextureTransform.Count3 | TextureTransform.Projected;
		Matrix inverseView = device.Transform.View;
		inverseView.Invert();
		device.Transform.Texture0 = inverseView * reflectViewMatrix * device.Transform.Projection * remapMatrix;

		// draw water geometry
		device.Transform.World = Matrix.Translation(0, 0, 0);
		device.Material = waterMat;

        SetupTextureStages(device);
        floor.Render(device);
        
        device.Transform.Texture0 = Matrix.Identity;
		device.TextureState[0].TextureTransform = TextureTransform.Disable;
		device.Material = mat;
		device.SetTexture(0, null );
        device.RenderState.CullMode = Cull.CounterClockwise;

        // update the camera
        device.Transform.View = Matrix.LookAtLH(new Vector3(g_xpos, g_ypos, g_zpos), new Vector3(g_lookat_x + g_xpos, g_ypos, g_lookat_z + g_zpos), new Vector3(0, 1, 0));

        wall.Render(ref device);
        wall1.Render(ref device);
        wall2.Render(ref device);
        wall3.Render(ref device);
        teapot.Render(ref device);
		
    }
    finally
    {
        if (beginSceneCalled)
            device.EndScene();
    }
}

private void SetupTextureStages(Device device)
{
    device.SetTexture(0, wallTexture);
    device.SetTextureStageState(0, TextureStageStates.ColorOperation, 4);
    device.SetTextureStageState(0, TextureStageStates.ColorArgument1, 0x00000002);
    device.SetTextureStageState(0, TextureStageStates.ColorArgument2, 0x00000000);

    device.SetTexture(1, reflectionTexture);
    device.SetTextureStageState(1, TextureStageStates.ColorOperation, 4);
    device.SetTextureStageState(1, TextureStageStates.ColorArgument1, 0x00000002);
    device.SetTextureStageState(1, TextureStageStates.ColorArgument2, 0x00000001);
}

private void RenderReflection( Device device )
{
	using( Surface surface = reflectionTexture.GetSurfaceLevel(0) )
	{
		Viewport view = new Viewport();
		view.Width = refWidth;
		view.Height = refHeight;
		view.MaxZ = 1.0f;

		rts.BeginScene( surface, view );

		device.Clear( ClearFlags.Target | ClearFlags.ZBuffer, 0x00efefff, 1.0f, 0 );

		// Do magic reflection stuff
		reflectViewMatrix = Matrix.Identity;
		reflectViewMatrix.Reflect( reflectPlane );
		reflectViewMatrix = reflectViewMatrix * device.Transform.View;

		device.Transform.View = reflectViewMatrix;
		device.Transform.Projection = device.Transform.Projection;

		device.RenderState.CullMode = Cull.Clockwise;		

		device.ClipPlanes[0].Enabled = true;
		device.ClipPlanes[0].Plane = reflectPlane;
		device.RenderState.Clipping = true;

		// render scene                
		device.Material = mat;
		device.SetTexture(0, null );
	
        teapot.Render(ref device);

		// reset render states
		device.RenderState.Clipping = false;
		device.ClipPlanes[0].Enabled = false;

		// end RTS
		rts.EndScene( Filter.None );
	}
}

I am using D3D9, managed (not sure that makes a difference). Any suggestions or hints would greatly help... I have a handle on blending textures but still having trouble completely understanding what is going on under the hood.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!