Jump to content
  • Advertisement
Sign in to follow this  
Barrow Boy

Working with textures in Direct3D

This topic is 3592 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'm trying to load a texture into my program and have it appear on a rotating square, but my square appears plain white without the texture. I have the texture ("metal.png") in the resource section of my project, but when I first put it in there, some weird screen came up asking me about file types that I didn't really understand (this is in Visual C++ 08). Can anyone tell me what to do with a texture? (Here's my code:
// include the basic windows header files and the Direct3D header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>

// define the screen resolution and keyboard macros
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

// global declarations
LPDIRECT3D9 d3d;    // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 t_buffer = NULL;    // the pointer to the vertex buffer

// texture declarations
LPDIRECT3DTEXTURE9 texture_1;    // our first texture

// function prototypes
void initD3D(HWND hWnd);    // sets up and initializes Direct3D
void render_frame(void);    // renders a single frame
void cleanD3D(void);    // closes Direct3D and releases memory
void init_graphics(void);    // 3D declarations


// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
    HWND hWnd;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

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


    hWnd = CreateWindowEx(NULL, L"WindowClass", L"Our Direct3D Program",
                          WS_EX_TOPMOST | WS_POPUP, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
                          NULL, NULL, hInstance, NULL);

    ShowWindow(hWnd, nCmdShow);

    // set up and initialize Direct3D

    // enter the main loop:

    MSG msg;

        DWORD starting_point = GetTickCount();

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



        // check the 'escape' key
            PostMessage(hWnd, WM_DESTROY, 0, 0);

        while ((GetTickCount() - starting_point) < 25);

    // clean up DirectX and COM

    return msg.wParam;

// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
        case WM_DESTROY:
                return 0;
            } break;

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

// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
    d3d = Direct3DCreate9(D3D_SDK_VERSION);


    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = FALSE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferWidth = SCREEN_WIDTH;
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;

    // create a device class using this information and the info from the d3dpp stuct

    init_graphics();    // call the function to initialize the triangle

    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);    // turn off the 3D lighting


// this is the function used to render a single frame
void render_frame(void)
    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);


    // select which vertex format we are using

    // set the view transform
    D3DXMATRIX matView;    // the view transform matrix
    &D3DXVECTOR3 (0.0f, 8.0f, 25.0f),    // the camera position
    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),      // the look-at position
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
    d3ddev->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView 

    // set the projection transform
    D3DXMATRIX matProjection;    // the projection transform matrix
                               D3DXToRadian(45),    // the horizontal field of view
                               (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
                               1.0f,   // the near view-plane
                               100.0f);    // the far view-plane
    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection

    // set the world transform
    static float index = 0.0f; index+=0.03f; // an ever-increasing float value
    D3DXMATRIX matRotateY;    // a matrix to store the rotation for each triangle
    D3DXMatrixRotationY(&matRotateY, index);    // the rotation matrix
    d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY));    // set the world transform

    // select the vertex buffer to display
    d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));

    // set the texture
    d3ddev->SetTexture(0, texture_1);

    // draw the textured square
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);


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


// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
    t_buffer->Release();    // close and release the vertex buffer
    d3ddev->Release();    // close and release the 3D device
    d3d->Release();    // close and release Direct3D


// this is the function that puts the 3D models into video RAM
void init_graphics(void)
    // load the texture we will use

    // create the vertices using the CUSTOMVERTEX
    struct CUSTOMVERTEX t_vert[] =
        {5, 0, -5, 0xffffffff, 1, 0,},
        {-5, 0, -5, 0xffffffff, 0, 0,},
        {5, 0, 5, 0xffffffff, 1, 1,},
        {-5, 0, 5, 0xffffffff, 0, 1,},

    // create a vertex buffer interface called t_buffer

    VOID* pVoid;    // a void pointer

    // lock t_buffer and load the vertices into it
    t_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, t_vert, sizeof(t_vert));


Share this post

Link to post
Share on other sites
The texture should be in the same directory as the executable file (Unless you specify another path when loading it). I don't think that D3DX function looks for it in the executables resources.

You can check to see if this is the problem by using a debugger and checking the value of "texture_1" after calling the loading routine. If it has failed it will be an invalid pointer.

Share this post

Link to post
Share on other sites
Also, be aware that when run from the IDE, VS sets the working directory to the directory with all your source (by default) files in rather than the exe directory, since there are actually two exe directories (debug and release).

If the same exe is run from outside VS, it will treat the exe directory as the working directory.

So realistically, you need to put the image file in the directory with your source files in. When you distribute, you need to put the image in the same directory as the exe.

Certain things can change the working directory (using an open or save dialog for example).

I tend to use the following function to get the exe directory in a release build and use a fully qualified path to load resources:

std::string ExecutablePath()
char c[MAX_PATH];

std::string S=c;
return S.substr(0,S.find_last_of("\\/"))+"\\";

Another alternative is to build the image file into the exe as a resource but I've only done this with bitmaps, not pngs. I'd assume that a png could be embedded in an exe as a binary resource, but I'm not sure if there is D3DX functionality to load a png from a resource.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!