Sign in to follow this  
Assassian360

Texture is not showing

Recommended Posts

I have been looking over this code for a friend, and can't work out why the texture is not displaying. As far as I can see everything in the code is fine, and the program runs and displays a rotating 'square' made up of 2 triangles, but the texture is not displaying. I'm sure the answer is quite simple, so I hope someone will be able to point it out soon so I can relay the answer back to my friend. :) Thanks in Advance :)
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3dDev;
LPDIRECT3DVERTEXBUFFER9 tBuffer = NULL;
LPDIRECT3DTEXTURE9 texture = NULL;

void InitD3D(HWND hWnd);
void RenderFrame();
void CleanD3D(void);
void InitGraphics(void);

struct CUSTOMVERTEX { FLOAT X, Y, Z; DWORD COLOR; float U, V;};
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	HWND hWnd;
	WNDCLASSEX wc;

	ZeroMemory(&wc, sizeof(WNDCLASSEX));

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC)WindowProcedure;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.lpszClassName = L"WindowClass";
	

	RegisterClassEx(&wc);

	hWnd = CreateWindowEx(NULL, L"WindowClass", L"Direct3D Program", WS_OVERLAPPEDWINDOW, 300, 300, 640, 480, NULL, NULL, hInstance, NULL);

	ShowWindow(hWnd, nCmdShow);

	InitD3D(hWnd);

	MSG msg;

	while(1)
	{
		DWORD startingPoint = GetTickCount();

		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		RenderFrame();

		while((GetTickCount() - startingPoint) < 25);
	}
	CleanD3D();

	return msg.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		case WM_DESTROY:
			{
				PostQuitMessage(0);
				return 0;
			}
			break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

void InitD3D(HWND hWnd)
{
	d3d = Direct3DCreate9 (D3D_SDK_VERSION);
	
	D3DPRESENT_PARAMETERS d3dpp;

	ZeroMemory(&d3dpp, sizeof(d3dpp));

	d3dpp.Windowed = true;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow = hWnd;
	d3dpp.EnableAutoDepthStencil = true;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

	d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3dDev);

	InitGraphics();
	d3dDev->SetRenderState(D3DRS_LIGHTING, FALSE);
	d3dDev->SetRenderState(D3DRS_ZENABLE, TRUE);

	return;
}

static float index = 0.0f;

void RenderFrame()
{
	d3dDev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1, 0);
	d3dDev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1, 0);

	d3dDev->BeginScene();

	d3dDev->SetFVF(CUSTOMFVF);

	D3DXMATRIX matRotationY;
	
	index += 0.05f;

	D3DXMatrixRotationY(&matRotationY, index);

	d3dDev->SetTransform(D3DTS_WORLD, &matRotationY);

	D3DXMATRIX matView;
	D3DXMatrixLookAtLH(&matView,
                       &D3DXVECTOR3 (0.0f, 8.0f, 25.0f),
                       &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),
                       &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
	d3dDev->SetTransform(D3DTS_VIEW, &matView);

	D3DXMATRIX matProjection;
	D3DXMatrixPerspectiveFovLH(&matProjection, D3DXToRadian(45), 4/3, 1.0f, 100.0f);

	d3dDev->SetTransform(D3DTS_PROJECTION, &matProjection);

	d3dDev->SetStreamSource(0, tBuffer, 0, sizeof(CUSTOMVERTEX));

	d3dDev->SetTexture(0, texture);

	d3dDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

	d3dDev->EndScene();

	d3dDev->Present(NULL, NULL, NULL, NULL);

	return;
}

void InitGraphics(void)
{
	D3DXCreateTextureFromFile(d3dDev, L"Metal.png", &texture);

	CUSTOMVERTEX tVert[] =
	{
		{5, 0, -5, 0xffffffff, 1, 0,},
        {-5, 0, -5, 0xffffffff, 0, 0,},
        {5, 0, 5, 0xffffffff, 1, 1,},
        {-5, 0, 5, 0xffffffff, 0, 1,},
	};

	d3dDev->CreateVertexBuffer(4 * sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &tBuffer, NULL);

	VOID* pVoid;

	tBuffer->Lock(0, 0, (VOID**)&pVoid, 0);
	memcpy(pVoid, tVert, sizeof(tVert));
	tBuffer->Unlock();

	return;
}

void CleanD3D(void)
{
	tBuffer->Release();
	d3dDev->Release();
	d3d->Release();
	texture->Release();

	return;
}

Share this post


Link to post
Share on other sites
First step should always be checking the return value of D3DXCreateTextureFromFile, maybe this function fails to load the texture (corrupt file, missing file etc.)...

Share this post


Link to post
Share on other sites
A few points:
1. What do the Debug Runtimes say?
2. You're not checking any return values. If any function fails, at best you'll crash, at worst you'll get weird bugs like this.
3. You should just make one Clear() call, and bitwise OR the D3DCLEAR_TARGET and D3DCLEAR_ZBUFFER flags together
4. Your aspect ratio is wrong - 4/3 is 1 because you're using integers. You probably want 4.0f/3.0f.
5. This does nothing but waste CPU like crazy: while((GetTickCount() - startingPoint) < 25); You're not yielding to the OS at all, so it'll use up 100% of the CPU.
6. You should process all pending window messages, then render a frame - not process one then render a frame. If rendering causes a new message to be submitted, you'll never clear your message queue at best, and at worst you'll overflow it.
7. Try clearing the backbuffer to a non-black colour to see if something is being rendered, but in black for some reason.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
A few points:
1. What do the Debug Runtimes say?
2. You're not checking any return values. If any function fails, at best you'll crash, at worst you'll get weird bugs like this.
3. You should just make one Clear() call, and bitwise OR the D3DCLEAR_TARGET and D3DCLEAR_ZBUFFER flags together
4. Your aspect ratio is wrong - 4/3 is 1 because you're using integers. You probably want 4.0f/3.0f.
5. This does nothing but waste CPU like crazy: while((GetTickCount() - startingPoint) < 25); You're not yielding to the OS at all, so it'll use up 100% of the CPU.
6. You should process all pending window messages, then render a frame - not process one then render a frame. If rendering causes a new message to be submitted, you'll never clear your message queue at best, and at worst you'll overflow it.
7. Try clearing the backbuffer to a non-black colour to see if something is being rendered, but in black for some reason.


Thanks for that, as soon as I activated the directx debug it showed up that it was not finding the file and so I corrected it and now it works. Thanks for all of those other corrections as well. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this