1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
// d3dOnCreate.cpp ; 1/31/2011
// Rafael P. Santana (C) 2011
// MyDirect3D Framework.
#include "MyDirect3D.h"
// Definition for Flexible Vertex Format (FVF).
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
HRESULT MYDIRECT3D::d3dOnCreate()
// This is where your Direct3D objects are initialized.
{
InitViewport(); // setup the viewport.
// Create the vertex data for the Piramid.
CUSTOMVERTEX PiramidVertices[]=
{
// The Piramid will have yellow triangles.
{-2.0f,-2.0f, 0.0f, D3DCOLOR_ARGB(0,255,255,0)}, // 0
{ 0.0f, 3.0f, 0.0f, D3DCOLOR_ARGB(0,255,255,0)}, // 1
{ 2.0f,-2.0f, 0.0f, D3DCOLOR_ARGB(0,255,255,0)}, // 2
{ 0.0f,-2.0f,-2.0f, D3DCOLOR_ARGB(0,255,255,0)}, // 3
{ 0.0f,-2.0f, 2.0f, D3DCOLOR_ARGB(0,255,255,0)}, // 4
// Since I am drawing an indexed primitive, I only define the
// vertices I need. I only need 5 for the piramid. Also, I will
// not be rendering the bottom of the piramid, since we cannot
// see it anyway.
};
// Create the Piramid vertex buffer.
pd3dDevice->CreateVertexBuffer
(sizeof(PiramidVertices)*sizeof(CUSTOMVERTEX),
D3DUSAGE_WRITEONLY,
D3DFVF_CUSTOMVERTEX, // see the #define at top!
D3DPOOL_DEFAULT,
&PiramidVertexBuffer,
NULL);
// Now that we have our vertices for the piramid and have created the
// vertex buffer for it. I can copy the data to it, the data I will copy
// are the vertices I defined (PiramidVertices) and some data that tells
// Direct3D the vertex format and how to manage the memory for the buffer.
void*pVertices; // void Pointer, more on this later.
PiramidVertexBuffer->Lock(0,0,&pVertices,0); // Lock the vertex buffer.
// Copy the vertices to the vertex buffer.
memcpy(pVertices,PiramidVertices,sizeof(PiramidVertices)*sizeof(CUSTOMVERTEX));
PiramidVertexBuffer->Unlock(); // Unlock the vertex buffer.
// When I lock the vertex buffer, I tell Direct3D 'where' to store the data for
// the vertex buffer which, happens to be at the address of the void pointer.
// The memcpy() function is a standard C function; the first parameter is the
// destination, the second is the source, followed by the size in bytes of data.
// After the operation is complete, I unlock the vertex buffer.
// Create Piramid index data.
WORD PiramidIndexData[]=
{
0,1,3, // Triangle 1
3,1,2, // Triangle 2
2,1,4, // Triangle 3
4,1,0, // Triangle 4
// The index buffer is used to draw the faces of the piramid I am creating.
// The data is the x,y,z location of a vertex, I just combine the data together
// to create the required triangles for the Piramid.
};
// Create the Piramid index buffer.
pd3dDevice->CreateIndexBuffer
(sizeof(PiramidIndexData)*sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, // WORD is 16bit; WORD PiramidIndexData[]
D3DPOOL_DEFAULT,
&PiramidIndexBuffer,
NULL);
// Same as before but now for the index buffer. Also, CreateIndexBuffer()
// appears to be similar to CreateVertexBuffer() they are not!
void*pIndex; // Prepare to copy Piramid index data.
PiramidIndexBuffer->Lock(0,0,&pIndex,0); // Lock the index buffer.
// Copy data to index buffer.
memcpy(pIndex,PiramidIndexData,sizeof(PiramidIndexData)*sizeof(WORD));
PiramidIndexBuffer->Unlock(); // Unlock the index buffer.
// Since I am drawing an indexed primitive, I have to tell Direct3D
// where to find the index data. I use SetIndices() for this task.
pd3dDevice->SetIndices(PiramidIndexBuffer); // Set index buffer to use.
// Next, I tell Direct3D where to find the vertices for my Piramid and
// I also setup the Flexible Vertex Format for the shader. See #define.
// Setup the stream source for Direct3D.
pd3dDevice->SetStreamSource(0,PiramidVertexBuffer,0,sizeof(CUSTOMVERTEX));
pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); // Configure the vertex shader.
// Setup render state settings.
pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
// When I set the render state settings. I turned lighting off for this
// presentation, disabled culling so all the faces of the piramid are
// visible and set the fill mode to wireframe so the triangles of the
// piramid can be seen.
rotAngle=0.0f; // Rotation angle.
rotSpeed=0.001f; // Rotation speed rate.
return S_OK; // all OK.
}
|