Skip to main content
GameDev.net gamedev.net
🔒 Locked

Strange Texture Render Problem

Started by JonW Jan 9, 2003 at 1:47 PM 2 replies 300+ views
Original Post
JonW
JonW
Say I have a 128x128 texture, and fill the entire thing with red pixels. Then I make another 128x128 texture and render the first texture to it. The 2nd texture is now red also. But then I use DrawPrimitive to fill texture 2 with white pixels. Now when I render the 2nd texture to the screen, it should be white, right? WRONG! I've been trying to figure this out for days. When I render texture 2, most of it is white, but there is a jagged tear in the upper right corner where some of the red is peeking through. Why isn't DrawPrimitive filling the entire thing? I have some code to show, I don't know the best way to do this because there are so many functions but I'll try to combine them all into one.
    
// Create a DirectDraw 7 interface
DirectDrawCreateEx (NULL, (void **)&m_lpdd, IID_IDirectDraw7, NULL);
//
// Set the cooperation level between DirectDraw and Windows 
m_lpdd->SetCooperativeLevel (pEngine->WindowHandle (), DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);
//
// Set the display mode.
m_lpdd->SetDisplayMode (640, 480, 16, 0, 0);
//
// Create the primary and backbuffer surfaces
m_pBackBuffer = new CSurface ();
//
ZeroMemory (&ddsd, sizeof (ddsd));
//
ddsd.dwSize = sizeof (ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
   ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_3DDEVICE;
ddsd.dwBackBufferCount = 1;
//
// Create the primary surface
m_lpdd->CreateSurface (ddsd, m_lpddsPrimary, NULL);
//
// Now retrieve the back buffer surface
LPDIRECTDRAWSURFACE7 lpddSurface;
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER | DDSCAPS_3DDEVICE;
//
m_lpddsPrimary->GetAttachedSurface (&ddsd.ddsCaps, &lpddSurface);
//
// Get pointer to Direct3D
m_lpdd->QueryInterface (IID_IDirect3D7, (void **)&m_lpd3d);
//
// Create the 3D device
//
// See if we can get a TnL device
if (FAILED (m_lpd3d->CreateDevice (IID_IDirect3DTnLHalDevice, lpddSurface,
	&m_lpd3dDevice)))
{
	// If not try HAL
	if (FAILED (m_lpd3d->CreateDevice (IID_IDirect3DHALDevice, lpddSurface,
		&m_lpd3dDevice)))
	{
		// Try MMX
		if (FAILED (m_lpd3d->CreateDevice (IID_IDirect3DMMXDevice, lpddSurface,
			&m_lpd3dDevice)))
		{
			// Worst case scenario - try RGB
			m_lpd3d->CreateDevice (IID_IDirect3DRGBDevice, lpddSurface,
					&m_lpd3dDevice);
		}
	}
}
//
// Turn off ZBuffer
m_lpd3dDevice->SetRenderState (D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
m_lpd3dDevice->SetRenderState (D3DRENDERSTATE_ZVISIBLE, FALSE);
m_lpd3dDevice->SetRenderState (D3DRENDERSTATE_ZWRITEENABLE, FALSE);
//
// Set alpha blending states
m_lpd3dDevice->SetRenderState (D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
m_lpd3dDevice->SetRenderState (D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
//
// Set texture rendering states
m_lpd3dDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
m_lpd3dDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_lpd3dDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
//
m_lpd3dDevice->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_lpd3dDevice->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
//
// Setup viewport
D3DVIEWPORT7 d3dViewport;
d3dViewport.dwX = 0;
d3dViewport.dwY = 0;
d3dViewport.dwWidth = 640;
d3dViewport.dwHeight = 480;
d3dViewport.dvMinZ = 0.0;
d3dViewport.dvMaxZ = 1.0;
//
// Set viewport for device
hError = m_lpd3dDevice->SetViewport (&d3dViewport);
//
D3DTLVERTEX d3dVertex[4];
//
d3dVertex[0].sx = 0;
d3dVertex[0].sy = 0;
d3dVertex[0].sz = 0.5;
d3dVertex[0].color = D3DRGB (1.0, 1.0, 1.0);
d3dVertex[0].specular = 0;
d3dVertex[0].rhw = 1.0;
d3dVertex[0].tu = 0.0;
d3dVertex[0].tv = 0.0;
//
d3dVertex[1].sx = 128;
d3dVertex[1].sy = 0;
d3dVertex[1].sz = 0.5;
d3dVertex[1].color = D3DRGB (1.0, 1.0, 1.0);
d3dVertex[1].specular = 0;
d3dVertex[1].rhw = 1.0;
d3dVertex[1].tu = 1.0;
d3dVertex[1].tv = 0.0;
//
d3dVertex[2].sx = 0.0;
d3dVertex[2].sy = 128;
d3dVertex[2].sz = 0.5;
d3dVertex[2].color = D3DRGB (1.0, 1.0, 1.0);
d3dVertex[2].specular = 0;
d3dVertex[2].rhw = 1.0;
d3dVertex[2].tu = 0.0;
d3dVertex[2].tv = 1.0;
//
d3dVertex[3].sx = 128;
d3dVertex[3].sy = 128;
d3dVertex[3].sz = 0.5;
d3dVertex[3].color = D3DRGB (1.0, 1.0, 1.0);
d3dVertex[3].specular = 0;
d3dVertex[3].rhw = 1.0;
d3dVertex[3].tu = 1.0;
d3dVertex[3].tv = 1.0;
//
LPDIRECTDRAWSURFACE7 lpddSurface1;
LPDIRECTDRAWSURFACE7 lpddSurface2;
//
ZeroMemory (&ddsd, sizeof (ddsd));
//
ddsd.dwSize = sizeof (ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.dwWidth = 128;
ddsd.dwHeight = 128;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
//
m_lpdd->CreateSurface (ddsd, &pSurface1, NULL);
//
// Set the new render target
m_lpd3dDevice->SetRenderTarget (&pSurface1, NULL);
//
// Set the viewport for texture
d3dViewport.dwX = 0;
d3dViewport.dwY = 0;
d3dViewport.dwWidth = 128;
d3dViewport.dwHeight = 128;
d3dViewport.dvMinZ = 0.0;
d3dViewport.dvMaxZ = 1.0;
//
m_lpd3dDevice->SetViewport (&d3dViewport);
//
// Start the new scene
m_lpd3dDevice->BeginScene ();
//
// Create pSurface2 like pSurface1...
// Fill pSurface2 with red pixels...
//
// Set pSurface2 as texture and render to pSurface1
m_lpd3dDevice->SetTexture (0, &pSurface2);
m_lpd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, D3DFVF_TLVERTEX, d3dVertex, 4, 0);
//
// Now fill pSurface1 with white
m_lpd3dDevice->SetTexture (0, NULL);
m_lpd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, D3DFVF_TLVERTEX, d3dVertex, 4, 0);
//
m_lpd3dDevice->EndScene ();
// Set backbuffer as render target and draw pSurface2...
    
See anything wrong? Thanks! [Edited by - JonW on November 25, 2008 12:09:33 PM]
JonW
JonW
The only thing I can think of is that the Z-buffer is screwing it up. But I disabled the z-buffer at the top of the code with SetRenderState. Do I have to disable it when I set the new render target each time?
Pseudo
Pseudo
You said you were using drawprimative to fill it with white, did you try extending the triangles so the go outside the bounds of the texture, like from -0.1 to 1.1 instead of 0 to 1. Its worth a shot. might have something to do with it not drawing the last pixel in a triangle (I think thats an option for d3d)
JonW
JonW
Thanks, but it didn't seem to work...

I'll show you the polygon so you can see what it looks like:



Strange, eh?


[Edited by - JonW on November 25, 2008 12:40:12 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.