Sign in to follow this  

Next step, add a texture

This topic is 2663 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

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 600

LPDIRECT3D9 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();
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!)

Share this post


Link to post
Share on other sites

This topic is 2663 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