Joined: May 22, 2003 Posts: 23945 Location: NSW, Australia
Posted: Sun Nov 14, 2004 11:04 am Post subject: Prt3 - "Wow a triangle"
Finally something exciting - well if you understand it all up to now, you'll be a game programmer in a few days. 3D is based on vertices, e.g. points in space. X,Y,Z value represents were in space it is located. We just join up these points to create complex shapes, like those used in Doom, or Halo
A lot of the initialisation code is the same as before, you'll get used to it in time, and eventually just put it in a separate file where you can call it just once.
Code:
//Application entry point
void __cdecl main()
{
InitialiseD3D();
while(true)
{
//Clear the backbuffer to black
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0);
//Begin the scene
g_pD3DDevice->BeginScene();
//NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
DrawTriangle();
//End the scene
g_pD3DDevice->EndScene();
//Filp the back and front buffers so that whatever has been rendered on the back buffer
//will now be visible on screen (front buffer).
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
CleanUp();
}
And next comes the DirectX Initilisation, and De-Initilisation.
void InitialiseD3D()
{
//First of all, create the main D3D object. If it is created successfully we
//should get a pointer to an IDirect3D8 interface.
g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
//Create a structure to hold the settings for our device
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
//Fill the structure.
// Set fullscreen 640x480x32 mode
d3dpp.BackBufferWidth = 640;
d3dpp.BackBufferHeight = 480;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
// Create one backbuffer
d3dpp.BackBufferCount = 1;
// Set up how the backbuffer is "presented" to the frontbuffer each time
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
Finally the Code that draws our wonderful, wonderful triangle for us. As you can see, the DrawTriangle code has to be called within the ->BeginScene(), and ->EndScene()
member functions above in main(), I've commented the code with //NEW NEW.. so that you can see the main points I am trying to outline.
Code:
//NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
//NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
LPDIRECT3DVERTEXBUFFER8 g_pVertexBuffer = NULL; // Vertices Buffer
struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; // The transformed position for the vertex.
DWORD colour; // The vertex colour.
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
void DrawTriangle()
{
VOID* pVertices;
//Store each point of the triangle together with it's colour
CUSTOMVERTEX cvVertices[] =
{
{250.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red (250, 100)
{400.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 2 - Green (400, 350)
{100.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 3 - Blue (100, 350)
};
//Create the vertex buffer from our device
g_pD3DDevice->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVertexBuffer);
//Get a pointer to the vertex buffer vertices and lock the vertex buffer
g_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0);
//Copy our stored vertices values into the vertex buffer
memcpy(pVertices, cvVertices, sizeof(cvVertices));
//Unlock the vertex buffer
g_pVertexBuffer->Unlock();
// Every time we Create a vertex buffer, we must release one!.
g_pVertexBuffer->Release();
}
//NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
//NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
Yup, I can hear you say it from here, "Wow", a triangle, not just any triangle, a funky coloured textured one on a blue background. Well when you've just started out in the big bad world of xdk development I think it seems pretty rewarding.
You can see from the DrawTriangle() function, that we set some vertices up (e.g. x,y,z points) and just copy them into our directX buffer and render them to the screen. DirectX buffer? Whats that? Well your graphics card has memory onboard, and if we use the directX buffer it will put it in there so that we can obtain better performance. _________________ HQ Network:
www.xbox-hq.com | www.xboxone-hq.com | www.360-hq.com | www.c64-hq.com
|
All times are GMT |Page 1 of 1
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum