   Few problems with D3D plz help!
  Hi, im using c# for game programming with direct3d. So far ive made a breakout game so my experience with d3d is fair. Im having a few problems which i want to ask u bout (im making a sidescroller game similar to super mario):1- my rendering is done using transformed vertices and textured quads. Some textures need color key... so i use the textureloader.loadtexturefromfile() function and also put the color key. The problem is that when the texture renders on the quad, the areas that have been keyed out appear black. my color key is green (0,255,0) and the color keying makes it black and i cannot see whats rendered behind it if u know what i mean. basically instead of making it transparent its making it black.Any ideas???2- because of this i am going to try rendering using Sprite interface (plz suggest if this is better to above or not). I have experimented with the sprite interface and the color keying works along with the alpha channel transparency which i also tried out as an alternative to color keying.3- i also thought of using direct3d surfaces and using a stretchrect to do the rendering to the back buffer. This will work but how can i do color keying like this. Also can someone explain how i can access surface data and modify it on an offscreen plain surface.4- i did some experiments with surfaces. I made a function in my renderer class which returns a surface from a file. For some random reason, when i use Stretch rect using that surface, the call fails and i examined that the surface has Disposed = true. Is the garbage collector disposing my surface??? this never happens with textures. and im sure the surface was created successfully because i checked the Disposed flag right after creating and its false.Im kind of a beginner so ignore anything that may have sounded wierd.Any help would b appreciated.Thank you!   
  I'm not a MDX programmer myself, so I can't give you exact code - but I've a few comments anyway [smile]Theres a "Simple2D" sample for MDX in the SDK - have you checked that out? Strikes me as a good starting point.Quote:Original post by Diirewolf1- my rendering is done using transformed vertices and textured quads. Some textures need color key... so i use the textureloader.loadtexturefromfile() function and also put the color key. The problem is that when the texture renders on the quad, the areas that have been keyed out appear black. my color key is green (0,255,0) and the color keying makes it black and i cannot see whats rendered behind it if u know what i mean. basically instead of making it transparent its making it black.Any ideas???Three things:1. Does your texture format have an alpha channel? A format like A8R8G8B8 is okay, but the similar X8R8G8B8 will lose any alpha transparency.2. Have you enabled the alpha blending render states? Dont know the MDX equivalents (should be similar), but D3DRS_ALPHBLENDENABLE = TRUE, D3DRS_SRCBLEND = D3DBLEND_SRCALPHA and D3DRS_DESTBLEND = D3DBLEND_INVSRCALPHA.3. Are you rendering in the correct order? Blended output is "Draw Order Dependent" and you should really be rendering it *last*. If you clear your display to black and then render your alpha blended geometry you'll get black instead of transparent pixels.Quote:Original post by Diirewolf2- because of this i am going to try rendering using Sprite interface (plz suggest if this is better to above or not). I have experimented with the sprite interface and the color keying works along with the alpha channel transparency which i also tried out as an alternative to color keying.Use the sprite interface if you want - its a perfectly viable alternative. I'd only bother re-implementing it yourself if you need some custom enhancements or you have some desire to learn the low-level details.Quote:Original post by Diirewolf3- i also thought of using direct3d surfaces and using a stretchrect to do the rendering to the back buffer. This will work but how can i do color keying like this. Also can someone explain how i can access surface data and modify it on an offscreen plain surface.Dont do this. Performance will suck [smile]hthJack;  Not sure which version of C# you're using; but, in .NET 1.1 framework, you'd set up alpha blending like this:mDevice.RenderState.AlphaBlendState = true;mDevice.RenderState.SourceBlend = Blend.SourceAlpha;mDevice.RenderState.DestinationBlend = Blend.InvSourceAlpha;At least, that's how it looks like it should work. I'm only guessing because I don't use MDX. I just read the documentation on RenderStates under Managed DirectX.   ;  Ive done all 3 of those... it still has the problem. I implemented my sprite renderer class and its having some problems so im working on fixing that but the color keying works fine under sprite interface. Alpha also works :D.One question, with the sprite interface, how can i 'mirror' the sprite in the x axis. because with my previous renderer class i just reversed the Tu coordinates of the vertices and it worked but im not sure how to do it under this.Even though the Surfaces are useless and they will make my game slower, i still want to know about them... i want to know how u can do color keying and alpha blending with surfaces, how u can read and write the pixel data from an offscreenplain (please provide some code) and i also want to know why my surface gets disposed randomly... if anyone can explain these it will be greatly appreciatedThanks again!PS. if ur still using c++ with DirectX, consider moving to c#, i used to use c++ but now i found c# is 10000000 times better, more flexible and totally object orientated (which is supposed to be an advantage :P).[Edited by - Diirewolf on May 28, 2006 2:45:40 PM];  Just a thought, could it hav something to do with vertex buffer (dynamic static etc...). Maybe i set up the Vertex buffer wrongly or i am not writing my vertices to the VB properly... ill put some code in:My Initialization code:public Renderer(System.Windows.Forms.Control ctrlWindow)    {   Caps caps = Manager.GetDeviceCaps(Manager.Adapters.Default.Adapter, DeviceType.Hardware);   CreateFlags flags;   if (caps.DeviceCaps.SupportsHardwareTransformAndLight)   {  flags = CreateFlags.HardwareVertexProcessing;   }   else   {  flags = CreateFlags.SoftwareVertexProcessing;   }   PresentParameters m_PP = new PresentParameters();   m_PP.Windowed = true;   m_PP.BackBufferFormat = Format.A8R8G8B8;   m_PP.SwapEffect = SwapEffect.Discard;   m_PP.EnableAutoDepthStencil = true;   m_PP.AutoDepthStencilFormat = DepthFormat.D16;   m_PP.PresentationInterval = PresentInterval.One;   m_Device = new Device(0, DeviceType.Hardware, ctrlWindow, flags, m_PP);   m_Device.RenderState.AlphaBlendEnable = true;   m_Device.RenderState.AlphaSourceBlend = Blend.SourceAlpha;   m_Device.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha;   m_VB = new VertexBuffer(typeof(CustomVertex.TransformedColoredTextured), 4, m_Device, Usage.None, CustomVertex.TransformedColored.Format, Pool.Managed);   int i;   CustomVertex.TransformedColoredTextured[] m_Vertices = new CustomVertex.TransformedColoredTextured[4];   for (i = 0; i < 4; i++)   {  m_Vertices.Rhw = 1.0f;  m_Vertices.Z = 0.5f;  m_Vertices.Color = Color.FromArgb(255, 255, 255).ToArgb();  m_Vertices.X = 0.0f;  m_Vertices.Y = 0.0f;  m_Vertices.Tu = 0.0f;  m_Vertices.Tv = 0.0f;   }   m_VB.SetData(m_Vertices, 0, LockFlags.Discard);   m_Device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;   m_Device.SetStreamSource(0, m_VB, 0);   FontDescription FD = new FontDescription();   m_Font = new Microsoft.DirectX.Direct3D.Font(m_Device, FD);    }One of my drawing functions:public void DrawTexQuad(Rectangle destRect, Texture srcTexture)    {   CustomVertex.TransformedColoredTextured[] m_Vertices = new CustomVertex.TransformedColoredTextured[4];   m_Vertices[0].X = destRect.Left; m_Vertices[0].Y = destRect.Bottom; m_Vertices[0].Tu = 0.0f; m_Vertices[0].Tv = 1.0f;   m_Vertices[1].X = destRect.Left; m_Vertices[1].Y = destRect.Top; m_Vertices[1].Tu = 0.0f; m_Vertices[1].Tv = 0.0f;   m_Vertices[2].X = destRect.Right; m_Vertices[2].Y = destRect.Bottom; m_Vertices[2].Tu = 1.0f; m_Vertices[2].Tv = 1.0f;   m_Vertices[3].X = destRect.Right; m_Vertices[3].Y = destRect.Top; m_Vertices[3].Tu = 1.0f; m_Vertices[3].Tv = 0.0f;   int i;   for (i = 0; i < 4; i++)   {  m_Vertices.Rhw = 1.0f;  m_Vertices.Z = 0.0f;  m_Vertices.Color = Color.FromArgb(0, 255, 255, 255).ToArgb();   }   m_VB.SetData(m_Vertices, 0, LockFlags.Discard);   m_Device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;   m_Device.SetStreamSource(0, m_VB, 0);   m_Device.SetTexture(0, srcTexture);   m_Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);    }This is a bit out of topic but i heard that there are many ways to optimize this textured quads rendering style, can someone tell me how or link me... thxok so this is some of my code, i cant see the problem that why the images arent being made transparent. If this code is definately right, maybe its the images im using, although i have tried many many different formats etc. plz let me knowThanks;  I just realised something, i tried to run my game on my brothers computer (which has a much better graphics card than mine, mine is trash) and the alpha blending worked! Now i thought it had something to do with my graphics card, and my graphics card doesnt support alpha blending... but then i downloaded some c++ code that showed how to do alpha blending in Direct3D with textured quads, and i compiled and it worked (with the same image i used on my game). So what can be the problem? I'm thinking it is the way in which my device is set up:PresentParameters m_PP = new PresentParameters();  m_PP.Windowed = true;  m_PP.SwapEffect = SwapEffect.Flip;  m_PP.DeviceWindow = ctrlWindow;  m_PP.BackBufferCount = 1;  m_PP.BackBufferFormat = Format.A8R8G8B8;  m_PP.BackBufferWidth = ctrlWindow.Width;  m_PP.BackBufferHeight = ctrlWindow.Height;  m_PP.EnableAutoDepthStencil = true;  m_PP.AutoDepthStencilFormat = DepthFormat.D16;  m_Device = new Device(Manager.Adapters.Default.Adapter, DeviceType.Reference, ctrlWindow, CreateFlags.SoftwareVertexProcessing, m_PP);  m_Device.VertexShader = null;  m_Device.VertexFormat = CustomVertex.TransformedTextured.Format;  m_VB = new VertexBuffer(typeof(CustomVertex.TransformedTextured), 4, m_Device, 0, CustomVertex.TransformedTextured.Format, Pool.Managed);  m_Device.SetStreamSource(0, m_VB, 0);   m_Device.SetRenderState(RenderStates.Lighting, false);  m_Device.SetRenderState(RenderStates.AlphaBlendEnable, true);  m_Device.SetRenderState(RenderStates.SourceBlend, (int)Blend.SourceAlpha);  m_Device.SetRenderState(RenderStates.DestinationBlend, (int)Blend.InvSourceAlpha);   m_Device.SetTextureStageState(0, TextureStageStates.AlphaOperation, (int)TextureOperation.Modulate);Do you see any problems with this? I have tried many different combinations of PresentParameters settings... I'm really confused at this point. Can anyone help?tx!   ;  Problem Solved!ok... after spending about 20 hours in front of my computer trying to solve the problem, i found out that the problem had nothing to do with DirectX... at this point i was about to shoot myself. Here is the problem, dont get a shock!this.FormBorderstyle = FormBorderstyle.FixedToolWindow;Yes... it was the FormBorderstyle that was somehow messing up the alpha blending... (and all those hours trying to play with DX settings...). Now, before when i created a window, i had not specified a FormBorderstyle. So it took the default, and alpha blending did not work with the default FormBorderstyle. Now when i set it to FixedToolWindow, it works! Also one thing to note is that when i set the FormBorderstyle to SizeableToolWindow, the alpha blending works but when i resize the window, it stops working. Anyone can explain this? Thanks to all ppl who read this and tried to help!   