[MDX][VB.NET 2003] - Set a gradient background?

Started by
8 comments, last by MisterHeadache 18 years, 3 months ago
In my application I am using a device assigned to a Panel control. Presently I am setting the background color via the "Device.Clear() method, ie:
device.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or ClearFlags.ZBuffer, System.Drawing.Color.Black, 1.0F, 0)
What I would really like to have is a gradient color background, such as light blue at the top and dark blue at the bottom. How could I obtain this?
Advertisement
Hello MisterHeadache,

Oke an easy way to do this would be the following:

-Create a bitmap with the same dimensions your render-target has. You could do this with any kind of paint program.
-Your program needs to load the bitmap and store it into an IDirect3DSurface (you can use D3DXLoadSurfaceFromFile method for this).
-On the beginning of every frame you copy the data of the surface you loaded from the file and store it into the backbuffer of your IDirect3DDevice (Use the function IDirect3DDevice8.GetBackBuffer to retrieve the backbuffer).

The last step, the copying of data replaces the clear-function, you musn't forget to clear the ZBuffer though, you now don't have clear the Target anymore.

Hope this helps, if not feel free to ask.

Regards,

Xeile

You can still call clear, but only set the ClearFlags.ZBuffer flag.

Render a RHW (pretransformed vertex) quad with the vertex diffuse colors set the way you want and set the color argument to diffuse.
Set the vertex z value to 1.0 (sets to the deepest possible z position).
Have gouraud shading enabled to get a nice gradient.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Endurion's approach is probably the easiest and most flexible way to do it, flexible in that you don't have to worry about your backbuffer size/resolution. I've made a quick sample project for this which you can download by clicking here. Unfortunately it's in C# and not in VB.NET (which I guess you're using), but hopefully it'll still clear things up a bit.

I have one question of my own though... I used indexed TransformedColored vertices to render the background quad, but somehow I had to use a clockwise winding order for these pretransformed vertices to get them to show with a counter clockwise cull mode... I'll admit that I'm a bit of a mess with graphics theory, but isn't this kinda backwards? Or am I fundamentally missing something here? [smile]
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
Quote:DX SDK:
D3DCULL_CCW
Cull back faces with counterclockwise vertices.


I'm not sure what the problem is. If you were to use a counterclockwise order, they would have been culled.
Sirob Yes.» - status: Work-O-Rama.
Quote:Original post by sirob
I'm not sure what the problem is. If you were to use a counterclockwise order, they would have been culled.


Me neither... Intuitively I'd say the right thing is happening, setting the cullmode should specify which winding order to cull... So a CCW cullmode culls faces with CCW winding order and the CW cullmode culls CW wound faces, right? To be sure, 'cull' meaning 'not drawing' here.

If so, then the info in Managed DirectX Kickstart by Tom Miller is rather wrong it seems... Here's the quote from page 20 of that book:

Quote:The three culling options are none, clockwise and counterclockwise. In case of the clockwise or counterclockwise options, primitives whose vertices are wound in the opposite order of the cull mode are not drawn.

Though my results and your reply suggest exactly the opposite... Or am I just being dumb? [wink]


Edit: I've talked this through with Pieter/Armadon and it seems the Kickstart book just is wrong on this. My apologies for the confusion, but I learned MDX from that book, which would explain why backface culling eluded me for so long [smile]

[Edited by - remigius on January 26, 2006 6:19:22 AM]
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
I always check the unmanaged documentation for this stuff. That's also where I got the quote from. :)

Although, in this case, the MDX documentation is accurate, just very unclear :).
Sirob Yes.» - status: Work-O-Rama.
Quote:Original post by remigius
... I've made a quick sample project for this...


Wow, a 270k project just for me??? THANKS!!!

Anyway, I got the code portions ported to VB .NET and get a beautiful gradient background, but any triangles I draw after it are invisible:

Dim description As SurfaceDescription = device.GetBackBuffer(0, 0, BackBufferType.Mono).Description()        Dim backWidth As Integer = description.Width        Dim backHeight As Integer = description.Height        Me.m_BackgroundVertices(0) = New CustomVertex.TransformedColored(New Vector4(0, 0, 0.5F, 1.0F), Color.LightSkyBlue.ToArgb)        Me.m_BackgroundVertices(1) = New CustomVertex.TransformedColored(New Vector4(backWidth, 0, 0.5F, 1.0F), Color.LightSkyBlue.ToArgb)        Me.m_BackgroundVertices(2) = New CustomVertex.TransformedColored(New Vector4(backWidth, backHeight, 1.0F, 1.0F), Color.DarkBlue.ToArgb)        Me.m_BackgroundVertices(3) = New CustomVertex.TransformedColored(New Vector4(0, backHeight, 1.0F, 1.0F), Color.DarkBlue.ToArgb)        device.VertexFormat = CustomVertex.TransformedColored.Format        device.DrawUserPrimitives(PrimitiveType.TriangleFan, 2, Me.m_BackgroundVertices)


It almost seems like the gradient quad is being drawn at the FRONT clipping plane instead of the back???



Try clearing the ZBuffer after rendering the gradient quad.
Awesome - that did it!

Thanks for the help everyone!

This topic is closed to new replies.

Advertisement