376 | 377 | ||
---|---|---|---|
32 | #include "font2.h" | 32 | #include "font2.h" |
33 | #include <XInput.h> // XInput API | 33 | #include <XInput.h> // XInput API |
34 | 34 | ||
35 | // Alien FOV - 115 | ||
36 | // Marine & Predator FOV - 77 | ||
37 | |||
35 | extern "C" | 38 | extern "C" |
36 | { | 39 | { |
37 | #include "AvP_UserProfile.h" | 40 | #include "AvP_UserProfile.h" |
... | ... | ||
48 | D3DXMATRIX matIdentity; | 51 | D3DXMATRIX matIdentity; |
49 | D3DXMATRIX matViewPort; | 52 | D3DXMATRIX matViewPort; |
50 | 53 | ||
54 | static D3DXPLANE m_frustum[6]; | ||
55 | |||
56 | extern "C" | ||
57 | { | ||
58 | BOOL CheckPointIsInFrustum(D3DXVECTOR3 *point) | ||
59 | { | ||
60 | // check if point is in front of each plane | ||
61 | for (int i = 0; i < 6; i++) | ||
62 | { | ||
63 | if (D3DXPlaneDotCoord(&m_frustum[i], point) < 0.0f) | ||
64 | { | ||
65 | // its outside | ||
66 | return FALSE; | ||
67 | } | ||
68 | } | ||
69 | |||
70 | // return here if the point is entirely within | ||
71 | return TRUE; | ||
72 | } | ||
73 | } | ||
74 | |||
75 | // call per frame after we've updated view and projection matrixes | ||
76 | void BuildFrustum() | ||
77 | { | ||
78 | D3DXMATRIX viewProjection; | ||
79 | D3DXMatrixMultiply(&viewProjection, &matView, &matProjection); | ||
80 | |||
81 | // Left plane | ||
82 | m_frustum[0].a = viewProjection._14 + viewProjection._11; | ||
83 | m_frustum[0].b = viewProjection._24 + viewProjection._21; | ||
84 | m_frustum[0].c = viewProjection._34 + viewProjection._31; | ||
85 | m_frustum[0].d = viewProjection._44 + viewProjection._41; | ||
86 | |||
87 | // Right plane | ||
88 | m_frustum[1].a = viewProjection._14 - viewProjection._11; | ||
89 | m_frustum[1].b = viewProjection._24 - viewProjection._21; | ||
90 | m_frustum[1].c = viewProjection._34 - viewProjection._31; | ||
91 | m_frustum[1].d = viewProjection._44 - viewProjection._41; | ||
92 | |||
93 | // Top plane | ||
94 | m_frustum[2].a = viewProjection._14 - viewProjection._12; | ||
95 | m_frustum[2].b = viewProjection._24 - viewProjection._22; | ||
96 | m_frustum[2].c = viewProjection._34 - viewProjection._32; | ||
97 | m_frustum[2].d = viewProjection._44 - viewProjection._42; | ||
98 | |||
99 | // Bottom plane | ||
100 | m_frustum[3].a = viewProjection._14 + viewProjection._12; | ||
101 | m_frustum[3].b = viewProjection._24 + viewProjection._22; | ||
102 | m_frustum[3].c = viewProjection._34 + viewProjection._32; | ||
103 | m_frustum[3].d = viewProjection._44 + viewProjection._42; | ||
104 | |||
105 | // Near plane | ||
106 | m_frustum[4].a = viewProjection._13; | ||
107 | m_frustum[4].b = viewProjection._23; | ||
108 | m_frustum[4].c = viewProjection._33; | ||
109 | m_frustum[4].d = viewProjection._43; | ||
110 | |||
111 | // Far plane | ||
112 | m_frustum[5].a = viewProjection._14 - viewProjection._13; | ||
113 | m_frustum[5].b = viewProjection._24 - viewProjection._23; | ||
114 | m_frustum[5].c = viewProjection._34 - viewProjection._33; | ||
115 | m_frustum[5].d = viewProjection._44 - viewProjection._43; | ||
116 | |||
117 | // Normalize planes | ||
118 | for (int i = 0; i < 6; i++) | ||
119 | { | ||
120 | D3DXPlaneNormalize(&m_frustum[i], &m_frustum[i]); | ||
121 | } | ||
122 | } | ||
123 | |||
51 | /* | 124 | /* |
52 | // vertex declarations | 125 | // vertex declarations |
53 | D3DVERTEXELEMENT9 declMain[] = | 126 | D3DVERTEXELEMENT9 declMain[] = |
... | ... | ||
559 | 632 | ||
560 | // test vertex buffer | 633 | // test vertex buffer |
561 | d3d.particleVB = new VertexBuffer; | 634 | d3d.particleVB = new VertexBuffer; |
562 | d3d.particleVB->Create(MAX_VERTEXES, FVF_LVERTEX, USAGE_DYNAMIC); | 635 | d3d.particleVB->Create(MAX_VERTEXES*6, FVF_LVERTEX, USAGE_DYNAMIC); |
563 | 636 | ||
564 | d3d.particleIB = new IndexBuffer; | 637 | d3d.particleIB = new IndexBuffer; |
565 | d3d.particleIB->Create(MAX_INDICES, USAGE_DYNAMIC); | 638 | d3d.particleIB->Create(MAX_INDICES*6, USAGE_DYNAMIC); |
566 | 639 | ||
567 | // test main | 640 | // test main |
568 | d3d.mainVB = new VertexBuffer; | 641 | d3d.mainVB = new VertexBuffer; |
... | ... | ||
689 | } | 762 | } |
690 | } | 763 | } |
691 | 764 | ||
765 | void R_SetFov(uint32_t fov) | ||
766 | { | ||
767 | d3d.fieldOfView = fov; | ||
768 | } | ||
769 | |||
692 | void FlushD3DZBuffer() | 770 | void FlushD3DZBuffer() |
693 | { | 771 | { |
694 | d3d.lpD3DDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0); | 772 | d3d.lpD3DDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0); |
... | ... | ||
999 | } | 1077 | } |
1000 | */ | 1078 | */ |
1001 | 1079 | ||
1080 | /* | ||
1002 | static bool CreateVertexShader(const std::string &fileName, r_VertexShader &vertexShader) | 1081 | static bool CreateVertexShader(const std::string &fileName, r_VertexShader &vertexShader) |
1003 | { | 1082 | { |
1004 | LPD3DXBUFFER pErrors = NULL; | 1083 | LPD3DXBUFFER pErrors = NULL; |
... | ... | ||
1046 | 1125 | ||
1047 | return true; | 1126 | return true; |
1048 | } | 1127 | } |
1128 | */ | ||
1049 | 1129 | ||
1050 | static bool CreatePixelShader(const std::string &fileName, r_PixelShader &pixelShader) | 1130 | static bool CreatePixelShader(const std::string &fileName, r_PixelShader &pixelShader) |
1051 | { | 1131 | { |
... | ... | ||
1180 | return d3dType; | 1260 | return d3dType; |
1181 | } | 1261 | } |
1182 | 1262 | ||
1183 | bool R_CreateVertexDeclaration(r_vertexDeclaration &declaration, std::vector<vertexElement> &elements) | 1263 | bool R_CreateVertexDeclaration(class VertexDeclaration *vertexDeclaration) |
1184 | { | 1264 | { |
1265 | // take a local copy of this to aid in keeping the code below a bit tidier.. | ||
1266 | size_t elementsSize = vertexDeclaration->elements.size(); | ||
1267 | |||
1185 | std::vector<D3DVERTEXELEMENT9> d3dElement; | 1268 | std::vector<D3DVERTEXELEMENT9> d3dElement; |
1186 | d3dElement.resize(elements.size() + 1); // +1 for DECL_END() | 1269 | d3dElement.resize(elementsSize+1); // +1 for DECL_END() |
1187 | 1270 | ||
1188 | for (uint32_t i = 0; i < elements.size(); i++) | 1271 | for (uint32_t i = 0; i < elementsSize; i++) |
1189 | { | 1272 | { |
1190 | d3dElement[i].Stream = elements[i].stream; | 1273 | d3dElement[i].Stream = vertexDeclaration->elements[i].stream; |
1191 | d3dElement[i].Offset = elements[i].offset; | 1274 | d3dElement[i].Offset = vertexDeclaration->elements[i].offset; |
1192 | d3dElement[i].Type = VD_TYPEtoD3DDECLTYPE(elements[i].type); | 1275 | d3dElement[i].Type = VD_TYPEtoD3DDECLTYPE(vertexDeclaration->elements[i].type); |
1193 | d3dElement[i].Method = D3DDECLMETHOD_DEFAULT; // TODO | 1276 | d3dElement[i].Method = D3DDECLMETHOD_DEFAULT; // TODO |
1194 | d3dElement[i].Usage = VD_USAGEtoD3DDECLUSAGE(elements[i].usage); | 1277 | d3dElement[i].Usage = VD_USAGEtoD3DDECLUSAGE(vertexDeclaration->elements[i].usage); |
1195 | d3dElement[i].UsageIndex = elements[i].usageIndex; | 1278 | d3dElement[i].UsageIndex = vertexDeclaration->elements[i].usageIndex; |
1196 | } | 1279 | } |
1197 | 1280 | ||
1198 | // add D3DDECL_END() which is {0xFF,0,D3DDECLTYPE_UNUSED,0,0,0} | 1281 | // add D3DDECL_END() which is {0xFF,0,D3DDECLTYPE_UNUSED,0,0,0} |
1199 | d3dElement[elements.size()].Stream = 0xFF; | 1282 | d3dElement[elementsSize].Stream = 0xFF; |
1200 | d3dElement[elements.size()].Offset = 0; | 1283 | d3dElement[elementsSize].Offset = 0; |
1201 | d3dElement[elements.size()].Type = D3DDECLTYPE_UNUSED; | 1284 | d3dElement[elementsSize].Type = D3DDECLTYPE_UNUSED; |
1202 | d3dElement[elements.size()].Method = 0; | 1285 | d3dElement[elementsSize].Method = 0; |
1203 | d3dElement[elements.size()].Usage = 0; | 1286 | d3dElement[elementsSize].Usage = 0; |
1204 | d3dElement[elements.size()].UsageIndex = 0; | 1287 | d3dElement[elementsSize].UsageIndex = 0; |
1205 | 1288 | ||
1206 | // try and create it now | 1289 | // try and create it now |
1207 | LastError = d3d.lpD3DDevice->CreateVertexDeclaration(&d3dElement[0], &declaration); | 1290 | LastError = d3d.lpD3DDevice->CreateVertexDeclaration(&d3dElement[0], &vertexDeclaration->declaration); |
1208 | if (FAILED(LastError)) | 1291 | if (FAILED(LastError)) |
1209 | { | 1292 | { |
1210 | Con_PrintError("Could not create vertex declaration"); | 1293 | Con_PrintError("Could not create vertex declaration"); |
... | ... | ||
1239 | return true; | 1322 | return true; |
1240 | } | 1323 | } |
1241 | 1324 | ||
1242 | bool R_CreateVertexShader(const std::string &fileName, r_VertexShader &vertexShader) | 1325 | bool R_CreateVertexShader(const std::string &fileName, r_VertexShader &vertexShader, VertexDeclaration *vertexDeclaration) |
1243 | { | 1326 | { |
1244 | return CreateVertexShader(fileName, vertexShader); | 1327 | LPD3DXBUFFER pErrors = NULL; |
1328 | LPD3DXBUFFER pCode = NULL; | ||
1329 | std::string actualPath = shaderPath + fileName; | ||
1330 | |||
1331 | // test that the path to the file is valid first (d3dx doesn't give a specific error message for this) | ||
1332 | std::ifstream fileOpenTest(actualPath.c_str(), std::ifstream::in | std::ifstream::binary); | ||
1333 | if (!fileOpenTest.good()) | ||
1334 | { | ||
1335 | LogErrorString("Can't open vertex shader file " + actualPath, __LINE__, __FILE__); | ||
1336 | return false; | ||
1337 | } | ||
1338 | // close the file | ||
1339 | fileOpenTest.close(); | ||
1340 | |||
1341 | // set up vertex shader | ||
1342 | LastError = D3DXCompileShaderFromFile(actualPath.c_str(), //filepath | ||
1343 | NULL, // macro's | ||
1344 | NULL, // includes | ||
1345 | "vs_main", // main function | ||
1346 | "vs_2_0", // shader profile | ||
1347 | 0, // flags | ||
1348 | &pCode, // compiled operations | ||
1349 | &pErrors, // errors | ||
1350 | &vertexShader.constantTable); // constants | ||
1351 | |||
1352 | if (FAILED(LastError)) | ||
1353 | { | ||
1354 | OutputDebugString(DXGetErrorString(LastError)); | ||
1355 | OutputDebugString(DXGetErrorDescription(LastError)); | ||
1356 | |||
1357 | if (pErrors) | ||
1358 | { | ||
1359 | // shader didn't compile for some reason | ||
1360 | OutputDebugString((const char*)pErrors->GetBufferPointer()); | ||
1361 | pErrors->Release(); | ||
1362 | } | ||
1363 | |||
1364 | return false; | ||
1365 | } | ||
1366 | |||
1367 | d3d.lpD3DDevice->CreateVertexShader((DWORD*)pCode->GetBufferPointer(), &vertexShader.shader); | ||
1368 | pCode->Release(); | ||
1369 | |||
1370 | return true; | ||
1371 | // return CreateVertexShader(fileName, vertexShader); | ||
1245 | } | 1372 | } |
1246 | 1373 | ||
1247 | bool R_CreatePixelShader(const std::string &fileName, r_PixelShader &pixelShader) | 1374 | bool R_CreatePixelShader(const std::string &fileName, r_PixelShader &pixelShader) |
... | ... | ||
2414 | return true; | 2541 | return true; |
2415 | } | 2542 | } |
2416 | 2543 | ||
2417 | void R_UpdateViewMatrix(float *viewMat) | ||
2418 | { | ||
2419 | D3DXVECTOR3 vecRight (viewMat[0], viewMat[1], viewMat[2]); | ||
2420 | D3DXVECTOR3 vecUp (viewMat[4], viewMat[5], viewMat[6]); | ||
2421 | D3DXVECTOR3 vecFront (viewMat[8], -viewMat[9], viewMat[10]); | ||
2422 | D3DXVECTOR3 vecPosition (viewMat[3], -viewMat[7], viewMat[11]); | ||
2423 | |||
2424 | D3DXVec3Normalize(&vecFront, &vecFront); | ||
2425 | |||
2426 | D3DXVec3Cross(&vecUp, &vecFront, &vecRight); | ||
2427 | D3DXVec3Normalize(&vecUp, &vecUp); | ||
2428 | |||
2429 | D3DXVec3Cross(&vecRight, &vecUp, &vecFront); | ||
2430 | D3DXVec3Normalize(&vecRight, &vecRight); | ||
2431 | |||
2432 | // right | ||
2433 | matView._11 = vecRight.x; | ||
2434 | matView._21 = vecRight.y; | ||
2435 | matView._31 = vecRight.z; | ||
2436 | |||
2437 | // up | ||
2438 | matView._12 = vecUp.x; | ||
2439 | matView._22 = vecUp.y; | ||
2440 | matView._32 = vecUp.z; | ||
2441 | |||
2442 | // front | ||
2443 | matView._13 = vecFront.x; | ||
2444 | matView._23 = vecFront.y; | ||
2445 | matView._33 = vecFront.z; | ||
2446 | |||
2447 | // 4th | ||
2448 | matView._14 = 0.0f; | ||
2449 | matView._24 = 0.0f; | ||
2450 | matView._34 = 0.0f; | ||
2451 | matView._44 = 1.0f; | ||
2452 | |||
2453 | matView._41 = -D3DXVec3Dot(&vecPosition, &vecRight); | ||
2454 | matView._42 = -D3DXVec3Dot(&vecPosition, &vecUp); | ||
2455 | matView._43 = -D3DXVec3Dot(&vecPosition, &vecFront); | ||
2456 | } | ||
2457 | |||
2458 | // we need this for zooming | 2544 | // we need this for zooming |
2459 | void R_CameraZoom(float zoomScale) | 2545 | void R_CameraZoom(float zoomScale) |
2460 | { | 2546 | { |