Jump to content
  • Advertisement
Sign in to follow this  
TransformedBG

Trying to make a room... Need some help

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

Okay so I have been trying to build a basic room with textures right now im just trying to figure out how to texture a cube and be able to move throughout space.

unfortunately my cube isn't showing up and i'm not sure why... hoping maybe someone can shed some light on this:

main.cpp:
#include "base.h"
#include "camera.h"
#include "room.h"
// Globals
IDirect3DDevice9* Device = 0;
float intiTime = (float)timeGetTime();
LPDIRECT3DTEXTURE9 texture;
D3DXMATRIX World;
IDirect3DVertexBuffer9* Floor = 0; // vertex buffer to store
IDirect3DIndexBuffer9* floorBuff = 0;
//set screen
const int Width = 900;
const int Height = 600;
//Position tracking
Camera TheCamera;
float camPosX = 0.0f,camPosY = 0.0f, camPosZ = -5.0f;
bool drawFloor()
{
D3DXCreateTextureFromFile(Device, // the Direct3D device
"carpet.jpg", // the filename of the texture
&texture); // the address of the texture storage
if(!texture)
{
::MessageBox(0, "D3DXCreateTextureFromFile() - FAILED", 0, 0);
return false;
}
CUSTOMVERTEX vertices[] =
{
{ -3.0f, -3.0f, 3.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, }, // side 1
{ 3.0f, -3.0f, 3.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, },
{ -3.0f, 3.0f, 3.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, },
{ 3.0f, 3.0f, 3.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, },
{ -3.0f, -3.0f, -3.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, }, // side 2
{ -3.0f, 3.0f, -3.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, },
{ 3.0f, -3.0f, -3.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, },
{ 3.0f, 3.0f, -3.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, },
{ -3.0f, 3.0f, -3.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, }, // side 3
{ -3.0f, 3.0f, 3.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, },
{ 3.0f, 3.0f, -3.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, },
{ 3.0f, 3.0f, 3.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, },
{ -3.0f, -3.0f, -3.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, }, // side 4
{ 3.0f, -3.0f, -3.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, },
{ -3.0f, -3.0f, 3.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, },
{ 3.0f, -3.0f, 3.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, },
{ 3.0f, -3.0f, -3.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, }, // side 5
{ 3.0f, 3.0f, -3.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, },
{ 3.0f, -3.0f, 3.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, },
{ 3.0f, 3.0f, 3.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, },
{ -3.0f, -3.0f, -3.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, }, // side 6
{ -3.0f, -3.0f, 3.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, },
{ -3.0f, 3.0f, -3.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, },
{ -3.0f, 3.0f, 3.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, },
};
Device->CreateVertexBuffer(24*sizeof(CUSTOMVERTEX),0,CUSTOMFVF,D3DPOOL_MANAGED,&Floor,NULL);
VOID* pVoid; //void pointer
Floor->Lock(0,0,(void**)&pVoid,0);
memcpy(pVoid,vertices,sizeof(vertices));
Floor->Unlock();
// create the indices using an int array
short indices[] =
{
0, 1, 2, // side 1
2, 1, 3,
4, 5, 6, // side 2
6, 5, 7,
8, 9, 10, // side 3
10, 9, 11,
12, 13, 14, // side 4
14, 13, 15,
16, 17, 18, // side 5
18, 17, 19,
20, 21, 22, // side 6
22, 21, 23,
};
Device->CreateIndexBuffer(36*sizeof(short),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&floorBuff,NULL);
floorBuff->Lock(0,0,(void**)&pVoid,0);
memcpy(pVoid,indices,sizeof(indices));
floorBuff->Unlock();
return true;
}

//Lighting to see currObject
void renderLight()
{
//Set up Directional Light
D3DXVECTOR3 dir(1.0f, -0.0f, 0.25f);
D3DXCOLOR c = d3d::WHITE;
D3DLIGHT9 dirLight = d3d::InitDirectionalLight(&dir, &c);
//enable lights
Device->SetLight(0, &dirLight); // 0,1,2,3 etc for whatever light your on. / name of light
Device->LightEnable(0, true);
//set light render states
Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
Device->SetRenderState(D3DRS_SPECULARENABLE, false);
}

// Framework functions
bool Setup()
{
renderLight();
// Set camera.
D3DXVECTOR3 pos(camPosX, camPosY, camPosZ);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &pos, &target, &up);
Device->SetTransform(D3DTS_VIEW, &V);
// Set projection matrix.
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.25f, // 45 - degree
(float)Width / (float)Height,
0.01f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
return true;
}
void Cleanup()
{
d3d::Release<IDirect3DVertexBuffer9*>(Floor);
d3d::Release<IDirect3DIndexBuffer9*>(floorBuff);
//d3d::Release<ID3DXMesh*>;
}
//render the world
void render(float timeDelta)
{

// Render
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
Device->BeginScene();

//INSERT WHAT TO BE RENDERED
drawFloor();
Device->SetStreamSource(0,Floor,0,sizeof(CUSTOMVERTEX));
Device->SetIndices(floorBuff);
Device->SetFVF(CUSTOMFVF);
Device->SetTexture(0,texture);
Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,24,0,12);
Device->EndScene();
}

//uptates current state
bool Display(float timeDelta)
{
if( Device )
{
if( ::GetAsyncKeyState('W') & 0x8000f )
TheCamera.walk(4.0f * timeDelta);
if( ::GetAsyncKeyState('S') & 0x8000f )
TheCamera.walk(-4.0f * timeDelta);
if( ::GetAsyncKeyState('A') & 0x8000f )
TheCamera.strafe(-4.0f * timeDelta);
if( ::GetAsyncKeyState('D') & 0x8000f )
TheCamera.strafe(4.0f * timeDelta);
if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
TheCamera.pitch(0.3f * timeDelta);
if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
TheCamera.pitch(-.3f * timeDelta);
if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
TheCamera.yaw(-0.3f * timeDelta);
if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
TheCamera.yaw(0.3f * timeDelta);
// Update the view matrix representing the cameras
// new position/orientation.
D3DXMATRIX V;
TheCamera.getViewMatrix(&V);
Device->SetTransform(D3DTS_VIEW, &V);

render(timeDelta);
Device->Present(0, 0, 0, 0);
}
return true;
}
// WndProc
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_DESTROY:
::PostQuitMessage(0);
break;

case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
::DestroyWindow(hwnd);
break;
break;
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}

// WinMain
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE prevInstance, PSTR cmdLine,int showCmd)
{
if(!d3d::InitD3D(hinstance, Width, Height, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}

if(!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}
d3d::EnterMsgLoop( Display );
Cleanup();
Device->Release();
return 0;
}



attached is a zip with all files to run it.. but im just having one of those moments i could use some more eyes.

Share this post


Link to post
Share on other sites
Advertisement
Have you tried taking a look in PIX at your program while it is running? PIX lets you grab a frame of data, and then go back through all of the API calls for that frame to see what you did (or didn't) do during a rendering sequence. Using PIX will also help you to understand the pipeline in general, so it is a worth while time investment!

Share this post


Link to post
Share on other sites
maybe the way your index buffers tell to draw the cube is in a way that they can only be seen when you are outside the cube, not from inside

Share this post


Link to post
Share on other sites

maybe the way your index buffers tell to draw the cube is in a way that they can only be seen when you are outside the cube, not from inside


thats what happened.. then i was way to close to see it lol

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!