Sign in to follow this  

Help With DirectX 8.0

This topic is 4105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just started programming in DirectX 8.0, but I'm a bit confused. I have the following code below, but not everything works as planned. The textures don't show, the figure is always a triangle, and the shape and size and color of it never changes, and, I can't render the textures for some reason. I know my code is correct, but I don't know what's wrong with it.
#include <windows.h>
#include <directx/d3d8.h>
#include <d3dx8.h>
#include <assert.h>

#define VertexFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1) 

const char g_szClassName [ ] = "DirectX 8.0";  

typedef struct 
{
	FLOAT x, y, z, rhw; 
	FLOAT u, v;            //The texture coordinates
	D3DCOLOR color; 
} sVertex; 

IDirect3D8 *g_pDX = NULL; 
IDirect3DDevice8 *g_pDXDevice = NULL;  
IDirect3DVertexBuffer8 *g_pVB = NULL; 
IDirect3DTexture8 *g_pTexture = NULL; 

BOOL Init(HWND hwnd)
{
	//Initialize the DirectX object
	if ( (g_pDX = Direct3DCreate8(D3D_SDK_VERSION) ) == NULL)
	{
		return FALSE; 
	}

	D3DDISPLAYMODE display; 
	D3DPRESENT_PARAMETERS pParams; 

	display.Width = 600; 
	display.Height = 400; 
	display.RefreshRate = 0;   //Use default refresh rate
	display.Format = D3DFMT_R5G6B5; 

	//Check to see if the display mode exists
	if (FAILED(g_pDX->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &display) ) )
	{
		return FALSE; 
	}

	//Clear out the presentation parameters structure
	ZeroMemory(&pParams, sizeof (pParams) ); 

	pParams.Windowed = TRUE; 
	pParams.SwapEffect = D3DSWAPEFFECT_DISCARD; 
	pParams.BackBufferFormat = display.Format; 

	//Create the DirectX device
	if (FAILED(g_pDX->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, 
		D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pParams, &g_pDXDevice) ) )
	{
		return FALSE; 
	}

	//Create the vertex buffer
	if (FAILED(g_pDXDevice->CreateVertexBuffer(sizeof (sVertex) * 4, 0, 
		VertexFVF, D3DPOOL_MANAGED, &g_pVB) ) )
	{
		return FALSE; 
	}

	BYTE *ptr = NULL;  

	//Lock the vertex buffer, so we can now start adding vertices to it
	g_pVB->Lock(0, 0, &ptr, 0); 

	sVertex vertexes[4] = 
	{
      {  50.0f, 350.0f, 1.0f, 1.0f, 50.0f, 1.0f, D3DCOLOR_COLORVALUE(0, 0, 0, 0) },
      { 350.0f, 50.0f, 1.0f, 1.0f,  50.0f, 1.0f, D3DCOLOR_COLORVALUE(0, 0, 0, 0) },
      {  50.0f, 50.0f, 1.0f, 1.0f,  50.0f, 1.0f, D3DCOLOR_COLORVALUE(0, 0, 0, 0) },
      { 350.0f, 350.0f, 1.0f, 1.0f, 50.0f, 1.0f, D3DCOLOR_COLORVALUE(0, 0, 0, 0) }
    };  

	//Copy the local vertices, into the vertex buffer
	memcpy(ptr, vertexes, sizeof (vertexes) ); 

	//Now, unlock the vertex buffer
	g_pVB->Unlock();
	
	//Set the stream source
	if (FAILED(g_pDXDevice->SetStreamSource(0, g_pVB, sizeof (sVertex) ) ) )
	{
		return FALSE; 
	}

	//Set the vertex shader
	if (FAILED(g_pDXDevice->SetVertexShader(VertexFVF) ) )
	{
		return FALSE; 
	}

	D3DMATERIAL8 materials;

	//Set the materials attributes
	ZeroMemory(&materials, sizeof (D3DMATERIAL8) ); 

	materials.Diffuse.r = materials.Ambient.r = 1.0f; 
	materials.Diffuse.g = materials.Ambient.g = 1.0f; 
	materials.Diffuse.b = materials.Ambient.b = 1.0f; 
	materials.Diffuse.a = materials.Ambient.a = 1.0f; 

	//Set the material
	if (FAILED(g_pDXDevice->SetMaterial(&materials) ) )
	{
		return FALSE; 
	}

	//Clear the backbuffer, to prepare it for drawing
	if (FAILED(g_pDXDevice->Clear(0, NULL, D3DCLEAR_TARGET, 
		D3DCOLOR_COLORVALUE(0, 5, 100, 0), 1.0f, 0) ) ) 
	{
		return FALSE; 
	}

	//Load the textures
	D3DXCreateTextureFromFile(g_pDXDevice, "Texture.bmp", &g_pTexture); 

	//Set the textures
	g_pDXDevice->SetTexture(0, g_pTexture); 

	return TRUE; 
}

BOOL DoFrame()
{
	//Do the drawing
	if (SUCCEEDED(g_pDXDevice->BeginScene() ) )
	{
		if (SUCCEEDED(g_pDXDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2) ) )
		{
			//Present the graphics
			if (FAILED(g_pDXDevice->Present(NULL, NULL, NULL, NULL) ) ) 
			{
				return FALSE; 
			}
		}
	}

	return TRUE; 
}

BOOL CleanUp()
{
	//Release the textures
	g_pDXDevice->SetTexture(0, NULL); 

	g_pDX->Release(); 
	g_pDXDevice->Release(); 
	g_pVB->Release(); 
	
	if (g_pTexture != NULL)
	{
		g_pTexture->Release(); 
	}
	
	return TRUE; 
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_CLOSE: 
		DestroyWindow(hwnd);
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;


	default:
		return DefWindowProc(hwnd, msg, wParam, lParam); 
	
	}

	return 0; 
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc; 
	HWND hwnd; 
	MSG msg; 

	wc.cbSize = sizeof (WNDCLASSEX); 
	wc.style = CS_CLASSDC; 
	wc.lpfnWndProc = WndProc; 
	wc.cbClsExtra = 0; 
	wc.cbWndExtra = 0; 
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 
	wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
	wc.hInstance = hInst; 
	wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 3); 
	wc.lpszClassName = g_szClassName; 
	wc.lpszMenuName = NULL; 

	if (!RegisterClassEx(&wc) )
	{
		return 1; 
	}

	hwnd = CreateWindowEx (
		0,  
		g_szClassName, 
		"DirectX 8.0", 
		WS_BORDER | WS_SYSMENU, 
		200, 
		200, 
		600, 
		400, 
		NULL, NULL, hInst, NULL); 

	if (hwnd == NULL)
	{
		return 1; 
	}

	ShowWindow(hwnd, nCmdShow); 
	UpdateWindow(hwnd);

	if (!Init(hwnd) )
	{
		MessageBox(hwnd, "Unable to initialize DirectX 8.0. ", "Error", MB_OK | MB_ICONSTOP); 

		return 1; 
	}
	
	DoFrame(); 

	//While the user has not quit the program yet
	while (msg.message != WM_QUIT)
	{
		//If there is a message to handle
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
		{
			//Then handle that message
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		}
	}

	CleanUp(); 

	//Unregister the class
	UnregisterClass(g_szClassName, hInst); 

	return msg.wParam;
}

Oh, and my compiler is Microsoft Visual C++ 6.0. Anyone can help me? Thanks -Ryan EDIT: Please remember to use 'source' tags in future [Edited by - RSL on September 16, 2006 12:57:29 PM]

Share this post


Link to post
Share on other sites
I see your problem [smile]

Having D3DFVF_XYZRHW in your FVF disables the hardware T&L pipeline thus your various SetMaterial() calls will be ignored. It'll pick up the colour from your diffuse component which you're setting to be black. Multiply any texture/material by zero and you'll get zero back [oh]

Also, when you're creating your vertex data (between the lock/unlock call) you're setting each vertex to have the same coordinate - (50.0f,1.0f) - which doesn't make any sense. Not only do they need to be different, but a value of 50.0 is (provided the hardware doesn't burp on it) will tile your texture 50x in the horizontal axis!

Change the diffuse parts to D3DCOLOR_COLORVALUE(1, 1, 1, 1) and then modify the texture coordinates so that they map from (0,0) to (1,1).

hth
Jack

Share this post


Link to post
Share on other sites

This topic is 4105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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