Next step, add a texture

Started by
1 comment, last by LevyDee 13 years, 7 months ago
Hi guys, I am learning D3D right now, and have gone through the free tutorials on directxtutorial.com(Ive seen a lot of people frown on this site). Anyways, Ive learned everything I can from the free lessons(other then error handling, but thats not to hard), so now I need a hand in applying a texture if you guys don't mind.

Heres what I got. So I assume I need to change or add something to my FV Format right? And in my init_graphics function, I need to load in a bitmap? Any help is appreciated. Thanks!

#include <windows.h>#include <windowsx.h>#include <d3d9.h>#include <d3dx9.h>#pragma comment(lib, "d3d9.lib")#pragma comment(lib, "d3dx9.lib")#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)struct CUSTOMVERTEX{	FLOAT x, y, z;	DWORD color;};#define SCREEN_WIDTH 800#define SCREEN_HEIGHT 600LPDIRECT3D9 d3d;LPDIRECT3DDEVICE9 d3ddev;LPDIRECT3DVERTEXBUFFER9 v_buffer;LPDIRECT3DINDEXBUFFER9 i_buffer;void init_d3d(HWND hWnd);void init_graphics();void render_frame();void clean_d3d();LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){	WNDCLASSEX wc;	ZeroMemory(&wc, sizeof(WNDCLASSEX));	wc.cbSize			= sizeof(WNDCLASSEX);	wc.hbrBackground	= HBRUSH(COLOR_WINDOW);	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);	wc.hInstance		= hInstance;	wc.lpfnWndProc		= WndProc;	wc.lpszClassName	= "myWnd";	wc.style			= CS_HREDRAW | CS_VREDRAW;	RegisterClassEx(&wc);	HWND hWnd = CreateWindowEx(NULL, "myWnd", "My Program", WS_OVERLAPPEDWINDOW, 200, 200, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);	ShowWindow(hWnd, nCmdShow);	init_d3d(hWnd);	MSG msg;	while(TRUE)	{		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))		{			TranslateMessage(&msg);			DispatchMessage(&msg);		}		if(msg.message == WM_QUIT)			break;		render_frame();	}	return 0;}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){	switch(message)	{		case WM_DESTROY:			{				PostQuitMessage(0);				return 0;			}					case WM_KEYDOWN:			{				switch(wParam)				{			 				}			}			break;	}	return DefWindowProc(hWnd, message, wParam, lParam);}void init_d3d(HWND hWnd){	d3d = Direct3DCreate9(D3D_SDK_VERSION);	D3DPRESENT_PARAMETERS d3dpp;	ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));	d3dpp.Windowed					= TRUE;	d3dpp.SwapEffect				= D3DSWAPEFFECT_DISCARD;	d3dpp.hDeviceWindow				= hWnd;	d3dpp.BackBufferFormat			= D3DFMT_X8R8G8B8;	d3dpp.BackBufferHeight			= SCREEN_HEIGHT;	d3dpp.BackBufferWidth			= SCREEN_WIDTH;	d3dpp.EnableAutoDepthStencil	= TRUE;	d3dpp.AutoDepthStencilFormat	= D3DFMT_D16;	d3d->CreateDevice(		D3DADAPTER_DEFAULT, 		D3DDEVTYPE_HAL, 		hWnd, 		D3DCREATE_SOFTWARE_VERTEXPROCESSING, 		&d3dpp, 		&d3ddev);	init_graphics();	d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);	d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);	d3ddev->SetRenderState(D3DRS_CULLMODE, TRUE);}void init_graphics() //I assume I need to do something in here to load a bitmap in for my texture?{	CUSTOMVERTEX vertices[] =	{ //CUBE		{-3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255)},		{3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 0)},		{-3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255)},		{3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 255)},		{-3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 255)},		{3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 0)},		{-3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(255, 0, 0)},		{3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(255, 255, 0)},	};	d3ddev->CreateVertexBuffer(		8 * sizeof(CUSTOMVERTEX),		0, 		CUSTOMFVF, 		D3DPOOL_MANAGED, 		&v_buffer, 		NULL);	VOID *pVoid;	v_buffer->Lock(0, 0, (void**)&pVoid, 0);	memcpy(pVoid, vertices, sizeof(vertices));	v_buffer->Unlock();	short indices[] =	{		0, 1, 2,	// side 1		2, 1, 3,			4, 0, 6,	// side 2		6, 0, 2,		7, 5, 6,    // side 3		6, 5, 4,		3, 1, 7,    // side 4		7, 1, 5,		4, 5, 0,    // side 5		0, 5, 1,		3, 7, 2,    // side 6		2, 7, 6,	};	d3ddev->CreateIndexBuffer(		36 * sizeof(short),		0,		D3DFMT_INDEX16,		D3DPOOL_MANAGED,		&i_buffer,		NULL);	i_buffer->Lock(0, 0, (void**)&pVoid, 0);	memcpy(pVoid, indices, sizeof(indices));	i_buffer->Unlock();}void render_frame(){	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);	d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);	d3ddev->BeginScene();		d3ddev->SetFVF(CUSTOMFVF);		D3DXMATRIX matView;		D3DXMatrixLookAtLH(			&matView, 			&D3DXVECTOR3 (0.0f, 10.0f, 25.0f), //Position			&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), //Look at pos			&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));		d3ddev->SetTransform(D3DTS_VIEW, &matView);		D3DXMATRIX matProjection;		D3DXMatrixPerspectiveFovLH(			&matProjection,			D3DXToRadian(45),			(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT,			1.0f,			100.0f);		d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);		d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));		d3ddev->SetIndices(i_buffer);		D3DXMATRIX matTranslate1;		D3DXMATRIX matTranslate2;		D3DXMatrixTranslation(&matTranslate1, 0.0f, 0.0f, 5.0f);		D3DXMatrixTranslation(&matTranslate2, 0.0f, 0.0f, -5.0f);		d3ddev->SetTransform(D3DTS_WORLD, &matTranslate1);		d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);		d3ddev->SetTransform(D3DTS_WORLD, &matTranslate2);		d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);//Just creating two cubes next to eachother	d3ddev->EndScene();	d3ddev->Present(NULL, NULL, NULL, NULL);}void clean_d3d(){	v_buffer->Release();	i_buffer->Release();	d3ddev->Release();	d3d->Release();}
Advertisement
There are several new steps for you to start using textures. The DirectX SDK has a relatively simple example for drawing texured triangles. Your first step should be to study that a bit, look at the documenation for the various function calls and get that example working.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

All of the samples are built using DXUT right? How easily does that translate to D3D? Ill give it a look over, but in the mean time, if anyone wants to look my code over and tell me what they think, please do! (I KNOW THERES NO ERROR HANDLING!)

This topic is closed to new replies.

Advertisement