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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| #include "stdafx.h"
#include "D3D9Device.h"
// TEMP
#include "EntityPlugin_Mesh.h"
EntityPluginMesh * plugMesh;
void D3D9Device::Initialize(HWND hWnd)
{
m_D3D = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface
D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information
ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
d3dpp.Windowed = TRUE; // program fullscreen, not windowed
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = SCREENWIDTH; // set the width of the buffer
d3dpp.BackBufferHeight = SCREENHEIGHT; // set the height of the buffer
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// create a device class using this information and information from the d3dpp stuct
m_D3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&m_Device);
m_Device->SetRenderState(D3DRS_LIGHTING, TRUE);
m_Device->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));
m_Device->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer
m_Device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
InitLight();
}
// this is the function that sets up the lights and materials
void D3D9Device::InitLight(void)
{
D3DLIGHT9 light; // create the light struct
ZeroMemory(&light, sizeof(light)); // clear out the light struct for use
light.Type = D3DLIGHT_POINT; // make the light type 'directional light'
light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set the light's color
// light.Direction = D3DXVECTOR3(-1.0f, -0.3f, -1.0f);
m_Device->SetLight(0, &light); // send the light struct properties to light #0
m_Device->LightEnable(0, TRUE); // turn on light #0
InitMaterial();
}
void D3D9Device::InitMaterial(void)
{
D3DMATERIAL9 material; // create the material struct
ZeroMemory(&material, sizeof(D3DMATERIAL9)); // clear out the struct for use
material.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set diffuse color to white
material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set ambient color to white
m_Device->SetMaterial(&material); // set the globably-used material to &material
}
// Preparation de la scene avant le rendu. Collecte les infos nécessaires au rendu
void D3D9Device::PrepareScene(void)
{
static bool done = false;
if(!done){
// TEMP
plugMesh = new EntityPluginMesh();
string s( "aegil.amf");
plugMesh->LoadFromFile( &s );
// create the vertices using the CUSTOMVERTEX struct
int vCount = plugMesh->GetVecteurs().size();
CUSTOMVERTEX * vertices = new CUSTOMVERTEX[ vCount ];
for( int i = 0; i < vCount; i++ )
{
vertices[i].X = plugMesh->GetVecteurs()[i].x;
vertices[i].Y = plugMesh->GetVecteurs()[i].y;
vertices[i].Z = plugMesh->GetVecteurs()[i].z;
vertices[i].Z = 1.0;
//vertices[i].COLOR = D3DCOLOR_COLORVALUE( 1.0, 1.0, 1.0, 1.0 );
vertices[i].NORMAL.x = plugMesh->GetNormales()[i].x;
vertices[i].NORMAL.y = plugMesh->GetNormales()[i].y;
vertices[i].NORMAL.z = plugMesh->GetNormales()[i].z;
}
// create a vertex buffer interface called m_VertexBuffer
if( FAILED( m_Device->CreateVertexBuffer( vCount * sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&m_VertexBuffer,
NULL) ) ){
return;
}
void* pVoid; // a void pointer
// lock m_VertexBuffer and load the vertices into it
m_VertexBuffer->Lock(0, 0, &pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
m_VertexBuffer->Unlock();
// create the indices using an int array
int fCount = plugMesh->GetFaces().size();
int * indices = new int[ fCount * 3 ];
int k = 0;
for(int i = 0; i < fCount; i++){
for(int j = 0; j < 3; j++){
indices[ k ] = plugMesh->GetFaces()[i][j];
k++;
}
}
// create an index buffer interface called m_IndexBuffer
m_Device->CreateIndexBuffer( 3 * fCount * sizeof( short ),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&m_IndexBuffer,
NULL);
// lock m_IndexBuffer and load the indices into it
void * pIndices;
m_IndexBuffer->Lock(0, 0, &pIndices, 0);
memcpy(pIndices, indices, 3 * fCount * sizeof( short ));
m_IndexBuffer->Unlock();
}
done = true;
}
// this is the function used to render a single frame
void D3D9Device::Render(void)
{
static float camDist = 20.0f;
camDist += 20.0f * Input::gInput->GetKey(VK_F3);
camDist -= 20.0f * Input::gInput->GetKey(VK_F4);
// clear the window to a deep blue
m_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
m_Device->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
m_Device->BeginScene(); // begins the 3D scene
// select which vertex format we are using
// VIEW
D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 5.0f, camDist), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
m_Device->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView
// PROJECTION
D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
(FLOAT)SCREENWIDTH / (FLOAT)SCREENHEIGHT, // aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane
m_Device->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
// set the world transform
static float index = 0.0f;
float rot = 0.0;
rot += Input::gInput->GetKey(VK_F1);
rot -= Input::gInput->GetKey(VK_F2);
index+=0.03f * rot ; // an ever-increasing float value
D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
m_Device->SetTransform(D3DTS_WORLD, &(matRotateY)); // set the world transform
// SET UP THE PIPELINE
// select the vertex and index buffers to use
m_Device->SetStreamSource(0, m_VertexBuffer, 0, sizeof(CUSTOMVERTEX));
m_Device->SetFVF(CUSTOMFVF);
m_Device->SetIndices(m_IndexBuffer);
int vCount = plugMesh->GetVecteurs().size();
int fCount = plugMesh->GetFaces().size();
//m_Device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, fCount);
m_Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,vCount,0,fCount);
m_Device->EndScene(); // ends the 3D scene
m_Device->Present(NULL, NULL, NULL, NULL); // displays the created frame
}
// this is the function that cleans up Direct3D and COM
void D3D9Device::CleanUp(void)
{
m_VertexBuffer->Release();
m_IndexBuffer->Release();
m_Device->Release(); // close and release the 3D device
m_D3D->Release(); // close and release Direct3D
}
D3D9Device::D3D9Device(HWND hWnd)
{
m_VertexBuffer = NULL;
m_IndexBuffer = NULL;
Initialize(hWnd);
}
D3D9Device::~D3D9Device(void)
{
CleanUp();
} |
Partager