Skip to main content
gamedev.net
🔒 Locked

Texture mapping problem

Started by startreky498 3 replies 800+ views
Original Post
startreky498
startreky498
I've been working at this all day, and nothing's working for me (and for no apparent reason?). Here's a screenshot I've taken of a recent test creation: Image hosted by Photobucket.com As you can see, there are some definate row- and column-overlapping problems across the window. I loaded the picture as normal with D3DXCreateTextureFromFileEx and blitted it to a 640x480 quad (triangle fan). I just can't figure out why the picture isn't showing up perfectly. Can someone help me out here? Just for the record, here's my code:

#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#define RELEASE(x) { if(x) { (x)->Release(); (x)=NULL; } }

HINSTANCE hInst;
HWND wndHandle;

bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);


struct D3DVERTEX
{
	float x, y, z, rhw;
	float tu, tv;
};
const DWORD vertex_fvf = D3DFVF_XYZRHW | D3DFVF_TEX1;

LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;
LPDIRECT3DTEXTURE9 Background;

D3DVERTEX vBackground[] = {
	{0,0,0,1,0,0}, {640,0,0,1,1,0}, {640,480,0,1,1,1}, {0,480,0,1,0,1}
};


bool initD3D(int,int,D3DFORMAT,bool);
void Render();

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	if(!initWindow(hInstance))
		return false;
	if(!initD3D(640,480,D3DFMT_UNKNOWN,false))
		return false;

	D3DXCreateTextureFromFileEx(pd3dDevice,
								"background.bmp",
								640,
								480,
								1,
								0,
								D3DFMT_X8R8G8B8,
								D3DPOOL_MANAGED,
								D3DX_FILTER_NONE,
								D3DX_DEFAULT,
								NULL,
								NULL,
								NULL,
								&Background);
	
	pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);


	MSG msg;
	ZeroMemory(&msg, sizeof(msg));
	while(msg.message!=WM_QUIT)
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			Render();
		}
	}

	RELEASE(Background);
	RELEASE(pd3dDevice);
	RELEASE(pD3D);

	return (int)msg.wParam;
}

bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
	
	wcex.cbSize			= sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "DirectXExample";
	wcex.hIconSm		= 0;
	RegisterClassEx(&wcex);

	wndHandle = CreateWindow("DirectXExample", 
							 "DirectXExample", 
							 WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
							 CW_USEDEFAULT, 
							 CW_USEDEFAULT, 
							 640, 
							 480, 
							 NULL, 
							 NULL, 
							 hInstance, 
							 NULL);
	if(!wndHandle)
		return false;

	ShowWindow(wndHandle, SW_SHOW);
	UpdateWindow(wndHandle);
	return true;
}

bool initD3D(int bbWidth, int bbHeight, D3DFORMAT bbFormat, bool Fullscreen)
{
	pD3D = Direct3DCreate9( D3D_SDK_VERSION );
	D3DPRESENT_PARAMETERS d3dpp;
	{
		ZeroMemory( &d3dpp, sizeof(d3dpp) );
		d3dpp.Windowed = !Fullscreen;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.BackBufferFormat = bbFormat;
		d3dpp.BackBufferCount  = 1;
		d3dpp.BackBufferHeight = bbHeight;
		d3dpp.BackBufferWidth  = bbWidth;
		d3dpp.hDeviceWindow    = wndHandle;
	}

	if( FAILED (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice) ) )
		return false;

	pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
	pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);

	return true;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_KEYDOWN:
		switch(wParam)
		{
		case VK_ESCAPE:
			PostQuitMessage(0);
			break;
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}

void Render()
{
	pd3dDevice->SetFVF(vertex_fvf);
	pd3dDevice->BeginScene();
	pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0xFF0000FF, 1.0f, 0);

	pd3dDevice->SetTexture(0, Background);
	pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vBackground, sizeof(D3DVERTEX));

	pd3dDevice->EndScene();
	pd3dDevice->Present(NULL, NULL, NULL, NULL);
}


Thanks a lot.
DesignerX
DesignerX
Why are you using TRIANGLEFAN for rendering and not TRIANGLESTRIP ???
(I'm using DirectX8 so maybe it's different with DirectX9
But you can never know)

And if you really want make sure there is nothing wrong with the rendering
try to define your triangles with 3 vertices to each triangle and render
them with the TRIANGLELIST flag.

Also check the image size, maybe its being strecthed when applied. (Is it 640x480 ?)

There is nothing that can't be solved. Just people that can't solve it. :)
startreky498
startreky498
Hmm nope, neither TRIANGLESTRIP nor TRIANGLELIST (with the appropriate vertex corrections) managed to do the job. Looks exactly the same. There doesn't seem to be any reason for this that I can see... it all looks fine, doesn't it?

btw, the image is the correct size, 640x480. upon measuring the output, the destination was the same size. The only problem seems to be that DX9 is taking it upon itself to stretch a few columns a little farter than they need to go. Can't figure out why.

edit:

Here's the source picture to display what the image SHOULD look like.

Image hosted by Photobucket.com

Note that although the source image is only 320x240, data loss is 0% since each pixel on the large image is made up of 4 pixels anyway.

I've also tried this on an image of size 640x480 anyway, so the 2x scaling does not seem to be the problem.

[Edited by - startreky498 on April 3, 2005 6:05:05 AM]
Cambo_frog
Cambo_frog
Could be a sampling effect.
Search on "Directly Mapping Texels to Pixels" in the SDK help file, then try adjusting the vertex definitions in vBackground[] accordingly.

HTH,
Cambo_frog
For the love of god, please tell me that you've just omitted your error checking code for brevity, and you don't really assume that all those functions succeed.
startreky498
startreky498
Props to Cambo_frog. That's exactly what did it. Just set everything back 0.5 units and the images come out perfect. Thanks a lot for your help, everybody.

Topic Locked

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

Sign in to reply to this topic.