Direct3D background color

Started by
3 comments, last by Klaatu 20 years, 6 months ago
Like many before me, I have begun to delve into using DirectX (3D). Presently, I am converting some code that uses OpenGL and have run into the following problem: As a simple test, I render three lines on a window. I use the following code to accomplish this: // Render the vertex buffer contents pDirect3DDevice->BeginScene(); pDirect3DDevice->SetStreamSource(0, pDirect3DVb, 0, sizeof(CUSTOMVERTEX)); pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX); pDirect3DDevice->DrawPrimitive(d3dprim, 0, nPrims); pDirect3DDevice->EndScene(); The lines render ok. However, the background of the window suddenly changes to green. If I refresh the window, I can get it to change to another color and then back to green. I''ve searched this forum for similar problems and have come across a few, but no clear explanation as to the resolution. Has anyone come across this and if so, what was the resolution? TIA Gort...Klaatu, Barada Nikto!
Gort...Klaatu, Barada Nikto!
Advertisement
Use pDirect3DDevice->Clear() every frame to clear the target before you render to it.

The green (or magenta) is what the debug D3D runtime will clear your surface to with certatin swap modes to indicate that the contents of that surface are undefined. With the retail D3D runtime, "undefined" means that the surface could easily be full of garbage, or whatever the last app rendered to it etc...

--
Simon O''Connor
3D Game Programmer &
Microsoft DirectX MVP

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Simon,

Thanks for the quick reply. I am indeed using the Clear() method before rendering. I use the following code for that:

if (pDirect3DDevice)
{
// Get RGB components.
BYTE r = GetRValue(Wd_colors[gr_back_color]);
BYTE g = GetGValue(Wd_colors[gr_back_color]);
BYTE b = GetBValue(Wd_colors[gr_back_color]);
// clear back buffer with RGB.
pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(r,g,b), 1.0f, 0);
// move back buffer to screen.
pDirect3DDevice->Present(NULL, NULL, NULL, NULL);
}

In this case, Wd_colors yields "black".

Here''s a synopsis...

1. Fill the vertex buffer.
2. Clear the surface (using above).
3. Render the primitive.
4. Flush the buffer (using Present() method).

With the above scenario, should I expect a magenta of green background?


Gort...Klaatu, Barada Nikto!
Gort...Klaatu, Barada Nikto!
FYI:

With Simon''s help, I was able to resolve this problem. Here''s what I discovered:

As Simon said, the debug version does indeed fill the back buffer with green or magenta to indicate the buffer is undefined. This happens after the call to the Present() method. In my case, I was calling Present() twice. Once when I wanted to clear the buffer, and again when I rendered the scene. The result was a scene that rendered with a green or magenta background. I changed the code to call the Clear() method, render the scene, and call the Present() method. The code works fine. I no longer get a green or magenta background.
Gort...Klaatu, Barada Nikto!
Also, don''t forget to clear your z-buffer if you have one. Add D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER to your Clear call.

This topic is closed to new replies.

Advertisement