Sign in to follow this  
Madi

3D objects in a room

Recommended Posts

Madi    114
Hi I have made a room with four walls, a roof and a floor, using only vertices. Now I want to add some 3D objects created and exported from 3DS Max 9. But the problem is, that it's not loading the texture and mesh. I am definately sure I have done something wrong. Here's the source:
.
.
.
bool InitTableMesh();
void Shutdown();
.
.
.
// globals
LPDIRECT3D9          g_pDirect3D = NULL;
LPDIRECT3DDEVICE9    g_pDirect3D_Device = NULL;
IDirect3DTexture9    *g_pTableTexture;	/* The building's texture */
ID3DXMesh            *g_pTableMesh;		/* The building's mesh */
camera_t             g_Camera;
Object3D             g_table;

bool InitTableMesh()
{
	HRESULT hr = D3DXLoadMeshFromX("table.x", D3DXMESH_MANAGED, g_pDirect3D_Device,
                    0, 0, 0, 0, &g_pTableMesh);

    if (FAILED(hr))
        return false;
                
    D3DXVECTOR3 *pVertices = 0;
	    
	hr = g_pTableMesh->LockVertexBuffer(0, reinterpret_cast<LPVOID*>(&pVertices));

    if (SUCCEEDED(hr))
    {
		g_pTableMesh->UnlockVertexBuffer();
    }

    return true;
}

void Update(float elapsedTimeSec)
{
	g_table.update(elapsedTimeSec);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{
.
.
.
 g_pDirect3D_Device->Release();
   g_pDirect3D->Release();
   Shutdown();
.
.
.
}

LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   HRESULT hr = S_OK;
   
   switch(msg)
   {
      case WM_CREATE:
// init DirectInput
         hr = CreateKeyboardDirectInputDevice(hwnd);
         
         if(FAILED(hr))
         {
            ::MessageBox(hwnd, "Could not create DirectInput device for keyboard.", "Error", MB_OK);
            PostQuitMessage(0);
            break;
         }

		 hr = D3DXCreateTextureFromFile(g_pDirect3D_Device, "table.jpeg", &g_pTableTexture);

		 if (FAILED(hr))
		 {
			 ::MessageBox(hwnd, "Failed to load test texture!", "Error", MB_OK);
			 return false;
		 }

		 // Load the mesh for the "building" and if it can't, give a message to the user 
		 if (!InitTableMesh())
		 {
			 ::MessageBox(hwnd, "Failed to load the table mesh!", "Error", MB_OK);
			 return false;
		 }

		 g_table.constrainToWorldYAxis(true);
		 g_table.setPosition(0.0f, 0.0f, 0.0f);
.
.
.
}

void RenderFrame(void)
{
.
.
.
g_pDirect3D_Device->SetTransform(D3DTS_WORLD, &g_table.getWorldMatrix());
	g_pDirect3D_Device->SetTexture(0, g_pTableTexture);
	g_pTableMesh->DrawSubset(0);
	g_pDirect3D_Device->SetTexture(0, 0);
.
.
.
}
.
.
.
void Shutdown(void)
{
	if (g_pTableTexture)
    {
        g_pTableTexture->Release();
        g_pTableTexture = 0;
    }

    if (g_pTableMesh)
    {
        g_pTableMesh->Release();
        g_pTableMesh = 0;
    }
}

I am using Visual Studio C++ 2008 together with the DirectX 9 SDK (June 2007). Basically, I want the table to be positioned in the center of the room I've created. If you need more info, don't hesitate to ask. Thanks in advance.

Share this post


Link to post
Share on other sites
dmatter    4821
Quote:
Original post by Madi
the problem is, that it's not loading the texture and mesh.
If the texture loading fails then you're returning false (I'm not sure why, the return type of WndProc is LRESULT not bool, and false will resolve a 0L which indicates success rather than failure) so you never even attempt to load the .x file.

I don't see anything glaringly wrong with your texture loading, except maybe you're trying to load an image with a .jpeg extension rather than a .jpg extension.

As for mesh loading you're setting 0 to arguments that provide an output. This looks like it might be helpful.

Share this post


Link to post
Share on other sites
Madi    114
Thanks for the reply dmatter. I've been looking at the website you've provided, and I tried to implement it. Now the textures loads, but the scene doesn't render at all. A Just-In-Time Debugger interrupts, claiming that "An unhandled win32 exception occurred in the test.exe [32732]"

Here's the full source:


// main.cpp
#include <windows.h>
#include <math.h>
#include <d3d9.h>
#include <d3dx9.h> // DirectX Extensions Utility library
#include "object3D.h"

#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h> // DirectX/Direct Input

#define PI 3.141592654f

typedef struct
{
float x, y, z;
} point_t;

typedef struct
{
float Rotation; // rotation of the camera around the Y-axis (in radians)
point_t Location; // location of the camera in the world
} camera_t;

// globals
LPDIRECT3D9 g_pDirect3D = NULL;
LPDIRECT3DDEVICE9 g_pDirect3D_Device = NULL;
ID3DXMesh *g_pTableMesh;
IDirect3DTexture9 *g_pTableTexture; /* The building's texture */
LPD3DXBUFFER materialBuffer;
DWORD numMaterials;
DWORD m_numMaterials; // Note: DWORD is a typedef for unsigned long
//LPD3DXMESH *g_pTableMesh; /* The building's mesh */


LPDIRECTINPUT8 g_pDI = NULL; // The DirectInput object
LPDIRECTINPUTDEVICE8 g_pKeyboard = NULL; // The keyboard device
BYTE dx_keyboard_state[256]; // DirectInput keyboard state buffer
camera_t g_Camera;
Object3D g_table;

// prototypes
LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void CalcMatrices(void);
bool InitTableMesh();
void RenderFrame(void);
void Shutdown();
//=================================== For DirectInput ===================================
HRESULT CreateKeyboardDirectInputDevice(HWND hWnd);
void HandleKeys(void);
HRESULT ReadImmediateData(HWND hWnd);
VOID FreeDirectInput(void);
//=======================================================================================

bool InitTableMesh()
{
HRESULT hr = D3DXLoadMeshFromX("table.x", D3DXMESH_MANAGED, g_pDirect3D_Device,
0, 0, 0, 0, &g_pTableMesh);

if (FAILED(hr))
return false;

D3DXVECTOR3 *pVertices = 0;

hr = g_pTableMesh->LockVertexBuffer(0, reinterpret_cast<LPVOID*>(&pVertices));

if (SUCCEEDED(hr))
{
g_pTableMesh->UnlockVertexBuffer();
}

return true;
}

void Update(float elapsedTimeSec)
{
g_table.update(elapsedTimeSec);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{
MSG msg;

WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WndProc, 0, 0, hInstance,
NULL, NULL, NULL, NULL, "DX9_TUTORIAL3_CLASS", NULL};

RegisterClassEx(&wc);

HWND hMainWnd = CreateWindow("DX9_TUTORIAL3_CLASS", "DirectX 9 Tutorial 3 - Simple 3D Room", WS_OVERLAPPEDWINDOW,
0, 0, 1024, 768, NULL, NULL, hInstance, NULL);

g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS PresentParams;
memset(&PresentParams, 0, sizeof(D3DPRESENT_PARAMETERS));
PresentParams.Windowed = TRUE;
PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
PresentParams.EnableAutoDepthStencil = TRUE; // turn on z-buffering
PresentParams.AutoDepthStencilFormat = D3DFMT_D16;

g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hMainWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &PresentParams, &g_pDirect3D_Device);

// setup the projection matrix (this is done at init time because
// this matrix never changes)
D3DXMATRIXA16 ProjectionMatrix;
D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, PI/4, 1.0f, 1.0f, 5000.0f);
g_pDirect3D_Device->SetTransform(D3DTS_PROJECTION, &ProjectionMatrix);

// set the world transformation matrix (we set it here because we're not
// using it - that's why it's an identity matrix - so it doesn't need to
// be set per frame)
D3DXMATRIXA16 WorldTransformMatrix;
D3DXMatrixIdentity(&WorldTransformMatrix);
g_pDirect3D_Device->SetTransform(D3DTS_WORLD, &WorldTransformMatrix);

// turn off backface culling and lighting (this makes each polygon 2-sided and
// allows our polygons to be seen without any lights defined)
g_pDirect3D_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); // set to D3DCULL_CW to turn culling back on
g_pDirect3D_Device->SetRenderState(D3DRS_LIGHTING, FALSE);

ShowWindow(hMainWnd, nShow);
UpdateWindow(hMainWnd);



while(PeekMessage(&msg, NULL, 0, 0, TRUE))
{
DispatchMessage(&msg);
ReadImmediateData(hMainWnd); // get keyboard state
HandleKeys();
RenderFrame();
InvalidateRect(hMainWnd, NULL, FALSE);
}

g_pDirect3D_Device->Release();
g_pDirect3D->Release();
Shutdown();
FreeDirectInput();

return(0);
}

LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HRESULT hr = S_OK;
//D3DMATERIAL9 *meshMaterials = new D3DMATERIAL9[numMaterials];
//LPDIRECT3DTEXTURE9 *meshTextures = new LPDIRECT3DTEXTURE9[numMaterials];

switch(msg)
{
case WM_CREATE:
// init DirectInput
hr = CreateKeyboardDirectInputDevice(hwnd);

if(FAILED(hr))
{
::MessageBox(hwnd, "Could not create DirectInput device for keyboard.", "Error", MB_OK);
PostQuitMessage(0);
break;
}



/*hr = D3DXCreateTextureFromFile(g_pDirect3D_Device, "table.jpg", &g_pTableTexture);

if (FAILED(hr))
{
::MessageBox(hwnd, "Failed to load test texture!", "Error", MB_OK);
return false;
}

// Load the mesh for the "building" and if it can't, give a message to the user
if (!InitTableMesh())
{
::MessageBox(hwnd, "Failed to load the table mesh!", "Error", MB_OK);
return false;
}*/


g_table.constrainToWorldYAxis(true);
g_table.setPosition(0.0f, 0.0f, 0.0f);


// init camera position and orientation
g_Camera.Location.x = -64;
g_Camera.Location.y = 40;
g_Camera.Location.z = -550;
g_Camera.Rotation = PI/2.0f;

return(0);
case WM_DESTROY:
PostQuitMessage(0);
return(0);
case WM_ACTIVATE:
if(WA_INACTIVE != wParam && g_pKeyboard)
{
// Make sure the devices are acquired, if we are gaining focus.
hr = g_pKeyboard->Acquire();

#ifdef _DEBUG
if(!SUCCEEDED(hr))
MessageBox(hwnd, "Acquire() for DirectInput keyboard failed.", "Error", MB_OK);
#endif
}

return(0);
case WM_PAINT:


RenderFrame();
ValidateRect(hwnd, NULL);

return(0);
}

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

void RenderFrame(void)
{
// give DirectX the vertex information of the scene
#pragma region Setup Vertices
#define NUM_VERTICES 36
#define FRONT_WALL_COLOR 0xffdead;
#define RIGHT_WALL_COLOR 0xddbc8b;
#define LEFT_WALL_COLOR 0xddbc8b;
#define BACK_WALL_COLOR 0xffdead;
#define FLOOR_COLOR 0x858585;
#define CEILING_COLOR 0x555555;
#define FRONT_WALL_COLOR2 0xeecd9c;
struct D3DVERTEX {float x, y, z; DWORD color;} vertices[NUM_VERTICES];

D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer();
D3DMATERIAL9 *meshMaterials = new D3DMATERIAL9[numMaterials];
LPDIRECT3DTEXTURE9 *meshTextures = new LPDIRECT3DTEXTURE9[numMaterials];

//-------------------------------------------------
// front wall
//-------------------------------------------------
vertices[0].x = -64.0f*3;
vertices[0].y = -64.0f;
vertices[0].z = 0;
vertices[0].color = FRONT_WALL_COLOR2;

vertices[1].x = 64.0f;
vertices[1].y = -64.0f;
vertices[1].z = 0;
vertices[1].color = FRONT_WALL_COLOR2;

vertices[2].x = 64.0f;
vertices[2].y = 64.0f*2;
vertices[2].z = 0;
vertices[2].color = FRONT_WALL_COLOR2;

vertices[3].x = -64.0f*3;
vertices[3].y = -64.0f;
vertices[3].z = 0;
vertices[3].color = FRONT_WALL_COLOR2;

vertices[4].x = 64.0f;
vertices[4].y = 64.0f*2;
vertices[4].z = 0;
vertices[4].color = FRONT_WALL_COLOR2;

vertices[5].x = -64.0f*3;
vertices[5].y = 64.0f*2;
vertices[5].z = 0;
vertices[5].color = FRONT_WALL_COLOR2;

//-------------------------------------------------
// right wall
//-------------------------------------------------
vertices[6].x = 64.0f*1;
vertices[6].y = -64.0f;
vertices[6].z = 0;
vertices[6].color = FRONT_WALL_COLOR;

vertices[7].x = 64.0f*1;
vertices[7].y = -64.0f;
vertices[7].z = -128.0f*3;
vertices[7].color = RIGHT_WALL_COLOR;

vertices[8].x = 64.0f*1;
vertices[8].y = 64.0f*2;
vertices[8].z = -128.0f*3;
vertices[8].color = RIGHT_WALL_COLOR;

vertices[9].x = 64.0f*1;
vertices[9].y = -64.0f;
vertices[9].z = 0;
vertices[9].color = FRONT_WALL_COLOR;

vertices[10].x = 64.0f*1;
vertices[10].y = 64.0f*2;
vertices[10].z = -128.0f*3;
vertices[10].color = RIGHT_WALL_COLOR;

vertices[11].x = 64.0f*1;
vertices[11].y = 64.0f*2;
vertices[11].z = 0;
vertices[11].color = FRONT_WALL_COLOR;

//-------------------------------------------------
// left wall
//-------------------------------------------------
vertices[12].x = -64.0f*3;
vertices[12].y = -64.0f;
vertices[12].z = -128.0f*3;
vertices[12].color = LEFT_WALL_COLOR;

vertices[13].x = -64.0f*3;
vertices[13].y = -64.0f;
vertices[13].z = 0;
vertices[13].color = FRONT_WALL_COLOR;

vertices[14].x = -64.0f*3;
vertices[14].y = 64.0f*2;
vertices[14].z = 0;
vertices[14].color = FRONT_WALL_COLOR;

vertices[15].x = -64.0f*3;
vertices[15].y = -64.0f;
vertices[15].z = -128.0f*3;
vertices[15].color = LEFT_WALL_COLOR;

vertices[16].x = -64.0f*3;
vertices[16].y = 64.0f*2;
vertices[16].z = 0;
vertices[16].color = FRONT_WALL_COLOR;

vertices[17].x = -64.0f*3;
vertices[17].y = 64.0f*2;
vertices[17].z = -128.0f*3;
vertices[17].color = LEFT_WALL_COLOR;

//-------------------------------------------------
// back wall
//-------------------------------------------------
vertices[18].x = 64.0f;
vertices[18].y = -64.0f;
vertices[18].z = -128.0f*3;
vertices[18].color = BACK_WALL_COLOR;

vertices[19].x = -64.0f*3;
vertices[19].y = -64.0f;
vertices[19].z = -128.0f*3;
vertices[19].color = BACK_WALL_COLOR;

vertices[20].x = 64.0f;
vertices[20].y = 64.0f*2;
vertices[20].z = -128.0f*3;
vertices[20].color = BACK_WALL_COLOR;

vertices[21].x = 64.0f;
vertices[21].y = 64.0f*2;
vertices[21].z = -128.0f*3;
vertices[21].color = BACK_WALL_COLOR;

vertices[22].x = -64.0f*3;
vertices[22].y = -64.0f;
vertices[22].z = -128.0f*3;
vertices[22].color = BACK_WALL_COLOR;

vertices[23].x = -64.0f*3;
vertices[23].y = 64.0f*2;
vertices[23].z = -128.0f*3;
vertices[23].color = BACK_WALL_COLOR;

//-------------------------------------------------
// floor
//-------------------------------------------------
vertices[24].x = -64.0f*3;
vertices[24].y = -64.0f;
vertices[24].z = -128.0f*3;
vertices[24].color = CEILING_COLOR;

vertices[25].x = 64.0f;
vertices[25].y = -64.0f;
vertices[25].z = -128.0f*3;
vertices[25].color = CEILING_COLOR;

vertices[26].x = 64.0f;
vertices[26].y = -64.0f;
vertices[26].z = 0;
vertices[26].color = FLOOR_COLOR;

vertices[27].x = -64.0f*3;
vertices[27].y = -64.0f;
vertices[27].z = -128.0f*3;
vertices[27].color = CEILING_COLOR;

vertices[28].x = 64.0f;
vertices[28].y = -64.0f;
vertices[28].z = 0;
vertices[28].color = FLOOR_COLOR;

vertices[29].x = -64.0f*3;
vertices[29].y = -64.0f;
vertices[29].z = 0;
vertices[29].color = FLOOR_COLOR;

//-------------------------------------------------
// ceiling
//-------------------------------------------------
vertices[30].x = -64.0f*3;
vertices[30].y = 64.0f*2;
vertices[30].z = -128.0f*3;
vertices[30].color = CEILING_COLOR;

vertices[31].x = 64.0f;
vertices[31].y = 64.0f*2;
vertices[31].z = 0;
vertices[31].color = FLOOR_COLOR;

vertices[32].x = 64.0f;
vertices[32].y = 64.0f*2;
vertices[32].z = -128.0f*3;
vertices[32].color = CEILING_COLOR;

vertices[33].x = -64.0f*3;
vertices[33].y = 64.0f*2;
vertices[33].z = -128.0f*3;
vertices[33].color = CEILING_COLOR;

vertices[34].x = -64.0f*3;
vertices[34].y = 64.0f*2;
vertices[34].z = 0;
vertices[34].color = FLOOR_COLOR;

vertices[35].x = 64.0f;
vertices[35].y = 64.0f*2;
vertices[35].z = 0;
vertices[35].color = FLOOR_COLOR;

LPDIRECT3DVERTEXBUFFER9 pVertexObject = NULL;
void *pVertexBuffer = NULL;

if(FAILED(g_pDirect3D_Device->CreateVertexBuffer(NUM_VERTICES*sizeof(D3DVERTEX), 0,
D3DFVF_XYZ|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &pVertexObject, NULL)))
return;

if(FAILED(pVertexObject->Lock(0, NUM_VERTICES*sizeof(D3DVERTEX), &pVertexBuffer, 0)))
return;

memcpy(pVertexBuffer, vertices, NUM_VERTICES*sizeof(D3DVERTEX));
pVertexObject->Unlock();
#pragma endregion

// render the actual scene
#pragma region Render Scene
g_pDirect3D_Device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); // clear frame

for (DWORD i=0; i<m_numMaterials; i++)
{

// Copy the material
meshMaterials[i] = d3dxMaterials[i].MatD3D;

// Set the ambient color for the material (D3DX does not do this)
meshMaterials[i].Ambient = meshMaterials[i].Diffuse;

// Create the texture if it exists - it may not
meshTextures[i] = NULL;
if (d3dxMaterials[i].pTextureFilename)
D3DXCreateTextureFromFile(g_pDirect3D_Device, d3dxMaterials[i].pTextureFilename, &meshTextures[i]);
}


if(SUCCEEDED(g_pDirect3D_Device->BeginScene()))
{
CalcMatrices();
g_pDirect3D_Device->SetStreamSource(0, pVertexObject, 0, sizeof(D3DVERTEX));
g_pDirect3D_Device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
g_pDirect3D_Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, NUM_VERTICES/3);
g_pDirect3D_Device->EndScene();
}
/*
g_pDirect3D_Device->SetTransform(D3DTS_WORLD, &g_table.getWorldMatrix());
g_pDirect3D_Device->SetTexture(0, g_pTableTexture);
g_pTableMesh->DrawSubset(0);
g_pDirect3D_Device->SetTexture(0, 0);
*/


for (DWORD i=0; i<m_numMaterials; i++)
{
// Set the material and texture for this subset
g_pDirect3D_Device->SetMaterial(&meshMaterials[i]);
g_pDirect3D_Device->SetTexture(0,meshTextures[i]);

// Draw the mesh subset
g_pTableMesh->DrawSubset( i );
}



g_pDirect3D_Device->Present(NULL, NULL, NULL, NULL);
pVertexObject->Release();
#pragma endregion
}

void CalcMatrices(void)
{
D3DXMATRIXA16 ViewMatrix;

// set the view matrix
D3DXVECTOR3 EyePoint(g_Camera.Location.x, g_Camera.Location.y, g_Camera.Location.z);
D3DXVECTOR3 LookAt(g_Camera.Location.x+cos(g_Camera.Rotation), g_Camera.Location.y, g_Camera.Location.z+sin(g_Camera.Rotation));
D3DXVECTOR3 UpVector(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&ViewMatrix, &EyePoint, &LookAt, &UpVector);
g_pDirect3D_Device->SetTransform(D3DTS_VIEW, &ViewMatrix);

}

//===================================================================================================
// The following functions are for DirectInput so we can see what the user is doing with the keyboard
//===================================================================================================

HRESULT CreateKeyboardDirectInputDevice(HWND hWnd)
{
HRESULT hr;
BOOL bExclusive;
BOOL bForeground;
BOOL bImmediate;
BOOL bDisableWindowsKey;
DWORD dwCoopFlags;

// Cleanup any previous call first
FreeDirectInput();

bExclusive = FALSE;
bForeground = TRUE;
bImmediate = TRUE;
bDisableWindowsKey = FALSE;

if( bExclusive )
dwCoopFlags = DISCL_EXCLUSIVE;
else
dwCoopFlags = DISCL_NONEXCLUSIVE;

if( bForeground )
dwCoopFlags |= DISCL_FOREGROUND;
else
dwCoopFlags |= DISCL_BACKGROUND;

// Disabling the windows key is only allowed only if we are in foreground nonexclusive
if( bDisableWindowsKey && !bExclusive && bForeground )
dwCoopFlags |= DISCL_NOWINKEY;

// Create a DInput object
if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) )
return hr;

// Obtain an interface to the system keyboard device.
if( FAILED( hr = g_pDI->CreateDevice( GUID_SysKeyboard, &g_pKeyboard, NULL ) ) )
return hr;

// Set the data format to "keyboard format" - a predefined data format
//
// A data format specifies which controls on a device we
// are interested in, and how they should be reported.
//
// This tells DirectInput that we will be passing an array
// of 256 bytes to IDirectInputDevice::GetDeviceState.
if( FAILED( hr = g_pKeyboard->SetDataFormat( &c_dfDIKeyboard ) ) )
return hr;

// Set the cooperativity level to let DirectInput know how
// this device should interact with the system and with other
// DirectInput applications.
hr = g_pKeyboard->SetCooperativeLevel( hWnd, dwCoopFlags );
if( hr == DIERR_UNSUPPORTED && !bForeground && bExclusive )
{
FreeDirectInput();
MessageBox( hWnd, "SetCooperativeLevel() returned DIERR_UNSUPPORTED.\n"
"For security reasons, background exclusive keyboard\n"
"access is not allowed.", "Keyboard", MB_OK );
return S_OK;
}

if( FAILED(hr) )
return hr;

// Acquire the newly created keyboard device
hr = g_pKeyboard->Acquire(); // note: this sometimes returns E_ACCESSDENIED, but keyboard still works

return S_OK;
}

HRESULT ReadImmediateData(HWND hWnd)
{
HRESULT hr;

if( NULL == g_pKeyboard )
return S_OK;

// Get the input's device state, and put the state in dims
ZeroMemory( dx_keyboard_state, sizeof(dx_keyboard_state) );
hr = g_pKeyboard->GetDeviceState( sizeof(dx_keyboard_state), dx_keyboard_state );
if( FAILED(hr) )
{
// DirectInput may be telling us that the input stream has been
// interrupted. We aren't tracking any state between polls, so
// we don't have any special reset that needs to be done.
// We just re-acquire and try again.

// If input is lost then acquire and keep trying
hr = g_pKeyboard->Acquire();
while( hr == DIERR_INPUTLOST )
hr = g_pKeyboard->Acquire();

// hr may be DIERR_OTHERAPPHASPRIO or other errors. This
// may occur when the app is minimized or in the process of
// switching, so just try again later
return S_OK;
}

return S_OK;
}

void HandleKeys(void)
{
float RotationStep = PI/175.0f;
float WalkStep = 3.0f;

//-------------------------------------------
// adjust the camera position and orientation
//-------------------------------------------
if(dx_keyboard_state[DIK_UP]&0x80) // moving forward
{
g_Camera.Location.x += cos(g_Camera.Rotation)*WalkStep;
g_Camera.Location.z += sin(g_Camera.Rotation)*WalkStep;
}

if(dx_keyboard_state[DIK_DOWN]&0x80) // moving backward
{
g_Camera.Location.x -= cos(g_Camera.Rotation)*WalkStep;
g_Camera.Location.z -= sin(g_Camera.Rotation)*WalkStep;
}

if(dx_keyboard_state[DIK_LEFT]&0x80) // look left
{
g_Camera.Rotation += RotationStep;

if(g_Camera.Rotation > PI*2)
g_Camera.Rotation = g_Camera.Rotation-PI*2;
}

if(dx_keyboard_state[DIK_RIGHT]&0x80) // look right
{
g_Camera.Rotation -= RotationStep;

if(g_Camera.Rotation < 0)
g_Camera.Rotation = PI*2+g_Camera.Rotation;
}

if(dx_keyboard_state[DIK_W]&0x80) // strafe left
{
float SideStepAngle = g_Camera.Rotation+(PI/2.0f);

if(SideStepAngle > PI*2) // handle wrap-around
SideStepAngle = SideStepAngle-PI*2;

g_Camera.Location.x += cos(SideStepAngle)*WalkStep;
g_Camera.Location.z += sin(SideStepAngle)*WalkStep;
}

if(dx_keyboard_state[DIK_E]&0x80) // strafe right
{
float SideStepAngle = g_Camera.Rotation-(PI/2.0f);

if(SideStepAngle < 0) // handle wrap-around
SideStepAngle = PI*2+SideStepAngle;

g_Camera.Location.x += cos(SideStepAngle)*WalkStep;
g_Camera.Location.z += sin(SideStepAngle)*WalkStep;
}
}

#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }

void Shutdown(void)
{
if (g_pTableTexture)
{
g_pTableTexture->Release();
g_pTableTexture = 0;
}

if (g_pTableMesh)
{
g_pTableMesh->Release();
g_pTableMesh = 0;
}
}

VOID FreeDirectInput(void)
{
// Unacquire the device one last time just in case
// the app tried to exit while the device is still acquired.
if( g_pKeyboard )
g_pKeyboard->Unacquire();

// Release any DirectInput objects.
SAFE_RELEASE( g_pKeyboard );
SAFE_RELEASE( g_pDI );
}



As you can notice, I haven't added the source for the object3d.cpp and its header file, because I truly believe that the problem lies in the main.cpp file.

Any help is highly appreciated.

Share this post


Link to post
Share on other sites

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