Jump to content
  • Advertisement
Sign in to follow this  

What's wrong with this code: Simple texture mapping

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

Argh, could someone tell me what's wrong with this code. I'm learning directx and this is frustrating. All I want is a ceiling and floor shown with textures.
// 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 640
#define SCREEN_HEIGHT 480
#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

// 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

{	FLOAT X, Y, Z;

// 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.hbrBackground = (HBRUSH)COLOR_WINDOW;    // not needed any more
    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;
    d3dpp.EnableAutoDepthStencil = TRUE;    // automatically run the z-buffer for us
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;    // 16-bit pixel format for the z-buffer

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

	//Creating the Textures


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

    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);    // turn off the 3D lighting
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-buffer


// 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(255, 255, 255), 1.0f, 0);


    // select which vertex format we are using

      // set the view transform
      D3DXMATRIX matView;    // the view transform matrix
                       &D3DXVECTOR3 (0.0f, 0.0f, -50.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
                               500.0f);    // the far view-plane
    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);     // set the projection

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

	//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
	D3DXMATRIX matWorld;
    d3ddev->SetTransform(D3DTS_WORLD, &matWorld);  // the reverse side

	d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);    // ceiling
	d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);    // left gun


    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)
    // create the vertices using the CUSTOMVERTEX struct
    CUSTOMVERTEX t_vert[] =
    // cieling
    { -100.0f, 10.0f, 0.0f, 0.0f, 0.0f },
    { 0.0f, 10.0f, 100.0f, 10.0f, 0.0f },
    { 100.0f, 10.0f, 0.0f, 10.0f, 10.0f },
	{ 0.0f, 10.0f, -100.0f, 0.0f, 10.0f }, 

    // floor
    { -100.0f, -10.0f, 0.0f, 0.0f, 0.0f },
    { 0.0f, -10.0f, 100.0f, 10.0f, 0.0f },
    { 100.0f, -10.0f, 0.0f, 10.0f, 10.0f },
	{ 0.0f, -10.0f, -100.0f, 0.0f, 10.0f }, 

    // 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));

Here is the image i get when I run this: [img=http://img91.imageshack.us/img91/8863/dxcc9.th.png] [Edited by - dhulli on July 20, 2008 2:14:38 AM]

Share this post

Link to post
Share on other sites

// cieling
{ -100.0f, 10.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 10.0f, 100.0f, 10.0f, 0.0f },
{ 100.0f, 10.0f, 0.0f, 10.0f, 10.0f },
{ 0.0f, 10.0f, -100.0f, 0.0f, 10.0f },

// floor
{ -100.0f, -10.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, -10.0f, 100.0f, 10.0f, 0.0f },
{ 100.0f, -10.0f, 0.0f, 10.0f, 10.0f },
{ 0.0f, -10.0f, -100.0f, 0.0f, 10.0f },

I am not a D3D guy, but maybe those 10.0f's should be 1.0f's since they are texture co-ordinates.

Share this post

Link to post
Share on other sites
Well, the 10 was meant to loop the texture ten times. I used 1 but it still didn't work.

Share this post

Link to post
Share on other sites
Your vertices are arranged incorrectly for a trianglestrip. For the floor, swap the 3rd and 4th vertices, but not the tex coords.

The ceiling has two problems: the vertices are arranged incorrectly for a trianglestrip, and you have them arranged so they would be CCW when viewed from below (which I think is how you want to be looking at the ceiling).

For the ceiling, if the current vertices are 0,1,2,3 - arrange them 0,3,1,2 (you can leave the tex coords as they are).

BTW, your pairs need to be lowercase.

Share this post

Link to post
Share on other sites
Thanks, now the floor and ceiling are drawn correctly, in white, they just don't have any texture on them. Any idea about that?

Share this post

Link to post
Share on other sites
You don't do error-checking anywhere in your code, dhulli. What that means: your code could compile correctly but there's no guarantee at all that it will run correctly (as you've already discovered).[smile]

Your code will work even if the textures don't load correctly. You're getting what you should expect if the textures are NULL.

In particular, for your question, it's probable that your textures didn't get created. Check the return codes from the D3DXCreateTexture calls. You may not have specified the correct path to the textures.

In general, though, I'd recommend you check return codes from just about every DirectX call you make. It's just good practice. Error-checking takes up just a fraction of your application's code and even smaller fraction of its execution time. It will force you to decide what you should do if there's an error.

Also, at some point you're going to revise your code to try something different and it won't work and you won't have a clue where the problem is. With error checking, you'll get that clue.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!