skeleton.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. //
  19. // skeleton.cpp : Defines the entry point for the application.
  20. //
  21. // Skeleton WW3D code, Hector Yee, 8/31/00
  22. #include "resource.h"
  23. #include "wwmath.h"
  24. #include "ww3d.h"
  25. #include "scene.h"
  26. #include "rendobj.h"
  27. #include "camera.h"
  28. #include "AssetMgr.H"
  29. #include "msgloop.h"
  30. #include "part_ldr.h"
  31. #include "rendobj.h"
  32. #include "hanim.h"
  33. #include "dx8wrapper.h"
  34. #include "dx8indexbuffer.h"
  35. #include "dx8vertexbuffer.h"
  36. #include "dx8fvf.h"
  37. #include "vertmaterial.h"
  38. #include "font3d.h"
  39. #include "render2d.h"
  40. #include "textdraw.h"
  41. #include "rect.h"
  42. #include "mesh.h"
  43. #include "meshmdl.h"
  44. #include "line3d.h"
  45. #include "dynamesh.h"
  46. #include "sphereobj.h"
  47. #include "ringobj.h"
  48. #include "surfaceclass.h"
  49. #include "vector2i.h"
  50. #include "bmp2d.h"
  51. #include "decalsys.h"
  52. #include "shattersystem.h"
  53. #include "light.h"
  54. #include "texproject.h"
  55. #include "keyboard.h"
  56. #include "wwmouse.h"
  57. #include "predlod.h"
  58. #include "segline.h"
  59. #include <stdio.h>
  60. #include "dx8renderer.h"
  61. #include "textureloader.h"
  62. #define MAX_LOADSTRING 100
  63. // Select between drawing a bitmap or the interface screen in the 2d scene
  64. //#define DRAWBITMAP
  65. const Vector3 CAMERA_POSITION(3.0f,0.0f,1.0f);
  66. const Vector3 CAMERA_TARGET(0.0f,0.0f,0.90f);
  67. const Vector3 OBJECT_POSITION(1000.0f,0.0f,0.0f);
  68. static void Log_Statistics();
  69. static void Init_Assets();
  70. static void Init_3D_Scene();
  71. static void Init_2D_Scene();
  72. class SkeletonSceneClass;
  73. // Global Variables:
  74. HINSTANCE hInst; // current instance
  75. TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  76. TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
  77. WW3DAssetManager * AssetManager=NULL;
  78. SkeletonSceneClass * my_scene = NULL;
  79. SimpleSceneClass * my_2d_scene = NULL;
  80. CameraClass * my_camera = NULL;
  81. CameraClass * my_2d_camera = NULL;
  82. Render2DTextClass * mytext = NULL;
  83. RenderObjClass * my_object = NULL;
  84. HAnimClass * my_anim = NULL;
  85. TexProjectClass * my_texture_projector = NULL;
  86. MaterialPassClass * my_material_pass = NULL;
  87. LightClass * my_lights[3];
  88. Font3DInstanceClass *my_font_a=NULL;
  89. Font3DInstanceClass *my_font_b=NULL;
  90. DecalSystemClass TheDecalSystem;
  91. bool running=true;
  92. bool rotate=false;
  93. bool randmat=false;
  94. bool randtex=false;
  95. bool staticsort=true;
  96. bool using_mat_pass=false;
  97. int shatter=0;
  98. // Foward declarations of functions included in this code module:
  99. ATOM MyRegisterClass(HINSTANCE hInstance);
  100. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  101. LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  102. void Enable_Alternate_Materials(RenderObjClass * model,bool onoff);
  103. void Render();
  104. void Render2();
  105. RenderObjClass * Create_Dynamesh(void);
  106. RenderObjClass * Create_Sphere(void);
  107. RenderObjClass * Create_Ring(void);
  108. RenderObjClass * Create_bitmap2d(void);
  109. RenderObjClass * Create_segline(void);
  110. void WWDebug_Message_Callback(DebugType type, const char * message);
  111. void WWAssert_Callback(const char * message);
  112. void Debug_Refs(void);
  113. void Create_Decal(RenderObjClass * obj,Matrix3D & tm,float radius,char * texture);
  114. MaterialPassClass * Create_Material_Pass(void);
  115. TexProjectClass * Create_Texture_Projector(void);
  116. // ----------------------------------------------------------------------------
  117. //
  118. // FPS counter class counts how many times Update() has been called during the
  119. // last second. The fps counter caps at MAX_FPS which is the maximum fps count
  120. // that can be measured.
  121. //
  122. // ----------------------------------------------------------------------------
  123. const unsigned MAX_FPS=2000;
  124. class FPSCounterClass
  125. {
  126. unsigned frame_times[MAX_FPS];
  127. unsigned frame_time_count;
  128. public:
  129. FPSCounterClass();
  130. void Update();
  131. unsigned Get_FPS();
  132. };
  133. static FPSCounterClass fps_counter;
  134. // ----------------------------------------------------------------------------
  135. FPSCounterClass::FPSCounterClass()
  136. :
  137. frame_time_count(0)
  138. {
  139. }
  140. // ----------------------------------------------------------------------------
  141. void FPSCounterClass::Update()
  142. {
  143. unsigned frame_time=timeGetTime();
  144. unsigned limit=frame_time-1000;
  145. for (unsigned i=0;i<frame_time_count;++i) {
  146. if (frame_times[i]<limit) {
  147. frame_times[i]=frame_times[frame_time_count-1];
  148. frame_time_count--;
  149. }
  150. }
  151. if (frame_time_count<MAX_FPS) {
  152. frame_times[frame_time_count++]=frame_time;
  153. }
  154. }
  155. // ----------------------------------------------------------------------------
  156. unsigned FPSCounterClass::Get_FPS()
  157. {
  158. return frame_time_count;
  159. }
  160. // ----------------------------------------------------------------------------
  161. //
  162. // Cusomized scene for testing material passes.
  163. //
  164. // ----------------------------------------------------------------------------
  165. class SkeletonSceneClass : public SimpleSceneClass
  166. {
  167. public:
  168. SkeletonSceneClass(void) : TestPass(NULL) { }
  169. ~SkeletonSceneClass(void) { REF_PTR_RELEASE(TestPass); }
  170. virtual void Customized_Render(RenderInfoClass & rinfo);
  171. void Set_Material_Pass(MaterialPassClass * pass) { REF_PTR_SET(TestPass,pass); }
  172. protected:
  173. MaterialPassClass * TestPass;
  174. };
  175. // ----------------------------------------------------------------------------
  176. void SkeletonSceneClass::Customized_Render(RenderInfoClass & rinfo)
  177. {
  178. if (TestPass) {
  179. rinfo.Push_Material_Pass(TestPass);
  180. }
  181. SimpleSceneClass::Customized_Render(rinfo);
  182. if (TestPass) {
  183. rinfo.Pop_Material_Pass();
  184. }
  185. }
  186. // ----------------------------------------------------------------------------
  187. //
  188. //
  189. //
  190. // ----------------------------------------------------------------------------
  191. int APIENTRY WinMain(HINSTANCE hInstance,
  192. HINSTANCE hPrevInstance,
  193. LPSTR lpCmdLine,
  194. int nCmdShow)
  195. {
  196. HACCEL hAccelTable;
  197. hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SKELETON);
  198. // install debug callbacks
  199. WWDebug_Install_Message_Handler(WWDebug_Message_Callback);
  200. WWDebug_Install_Assert_Handler(WWAssert_Callback);
  201. // Initialize global strings
  202. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  203. LoadString(hInstance, IDC_SKELETON, szWindowClass, MAX_LOADSTRING);
  204. MyRegisterClass(hInstance);
  205. // Perform application initialization:
  206. hInst = hInstance; // Store instance handle in our global variable
  207. HWND hWnd = CreateWindow(
  208. szWindowClass,
  209. szTitle,
  210. WS_OVERLAPPEDWINDOW,
  211. CW_USEDEFAULT,
  212. 0,
  213. CW_USEDEFAULT,
  214. 0,
  215. NULL,
  216. NULL,
  217. hInstance,
  218. NULL);
  219. ShowWindow(hWnd, nCmdShow);
  220. UpdateWindow(hWnd);
  221. // WW Inits
  222. WWMath::Init ();
  223. AssetManager=new WW3DAssetManager;
  224. AssetManager->Register_Prototype_Loader(&_ParticleEmitterLoader);
  225. AssetManager->Load_3D_Assets("ShatterPlanes0.w3d"); // Shatter planes
  226. WW3D::Init(hWnd);
  227. // WW3D::Set_Prelit_Mode(WW3D::PRELIT_MODE_VERTEX);
  228. WW3D::Set_Prelit_Mode(WW3D::PRELIT_MODE_LIGHTMAP_MULTI_PASS);
  229. // WW3D::Set_Prelit_Mode(WW3D::PRELIT_MODE_LIGHTMAP_MULTI_TEXTURE);
  230. // WW3D::Set_Collision_Box_Display_Mask(0xFF);
  231. if (WW3D::Set_Render_Device(-1,1024,768,16,1,true)!=WW3D_ERROR_OK) {
  232. // if (WW3D::Set_Render_Device(-1,320,240,32,1,true)!=WW3D_ERROR_OK) {
  233. if (WW3D::Set_Any_Render_Device()!=WW3D_ERROR_OK) {
  234. WW3D::Shutdown();
  235. WWMath::Shutdown ();
  236. Debug_Refs();
  237. return 0;
  238. }
  239. }
  240. Init_Assets();
  241. Init_2D_Scene();
  242. Init_3D_Scene();
  243. // main loop
  244. int time=timeGetTime();
  245. float theta = 0.0f;
  246. while (running)
  247. {
  248. if (my_object && rotate) {
  249. theta += DEG_TO_RADF(0.1f);
  250. Matrix3D tm(1);
  251. tm.Rotate_Z(theta);
  252. //tm.Rotate_X(theta*1.37f);
  253. //tm.Rotate_Y(theta*1.09f);
  254. tm.Set_Translation(OBJECT_POSITION);
  255. my_object->Set_Transform(tm);
  256. }
  257. if (my_object && my_texture_projector) {
  258. my_texture_projector->Set_Transform(my_object->Get_Bone_Transform("ProjectorBone"));
  259. }
  260. if (shatter==1)
  261. {
  262. if (my_object->Class_ID()==RenderObjClass::CLASSID_MESH)
  263. {
  264. my_scene->Remove_Render_Object(my_object);
  265. ShatterSystem::Shatter_Mesh((MeshClass*)my_object,Vector3(0,0,0),Vector3(1,0,0));
  266. REF_PTR_RELEASE(my_object);
  267. int count=ShatterSystem::Get_Fragment_Count();
  268. int i;
  269. for (i=0; i<count; i++)
  270. {
  271. my_object=ShatterSystem::Get_Fragment(i);
  272. my_object->Set_Position(my_object->Get_Position()+Vector3(rand()%15,rand()%15,rand()%15));
  273. my_scene->Add_Render_Object(my_object);
  274. REF_PTR_RELEASE(my_object);
  275. }
  276. }
  277. shatter++;
  278. }
  279. if (randmat)
  280. {
  281. if (my_object->Class_ID()==RenderObjClass::CLASSID_MESH)
  282. {
  283. MeshClass *mesh=(MeshClass*) my_object;
  284. MaterialInfoClass *mi=mesh->Get_Material_Info();
  285. VertexMaterialClass *vm=mi->Get_Vertex_Material(0);
  286. VertexMaterialClass *newvm=NEW_REF(VertexMaterialClass,(*vm));
  287. newvm->Set_Emissive(rand()%256/256.0,rand()%256/256.0,rand()%256/256.0);
  288. mesh->Replace_VertexMaterial(vm,newvm);
  289. mi->Replace_Material(0,newvm);
  290. REF_PTR_RELEASE(vm);
  291. REF_PTR_RELEASE(newvm);
  292. }
  293. randmat=!randmat;
  294. }
  295. if (randtex)
  296. {
  297. if (my_object->Class_ID()==RenderObjClass::CLASSID_MESH)
  298. {
  299. MeshClass *mesh=(MeshClass*) my_object;
  300. MaterialInfoClass *mi=mesh->Get_Material_Info();
  301. TextureClass *tex=mi->Peek_Texture(0);
  302. int ran=rand() % 3;
  303. char name[100];
  304. switch (ran)
  305. {
  306. case 0: sprintf(name,"catwalk.tga");
  307. break;
  308. case 1: sprintf(name,"chickonmap.tga");
  309. break;
  310. case 2: sprintf(name,"fullmoon.tga");
  311. break;
  312. }
  313. TextureClass *newtex=AssetManager->Get_Texture(name);
  314. mesh->Replace_Texture(tex,newtex);
  315. mi->Replace_Texture(0,newtex);
  316. REF_PTR_RELEASE(newtex);
  317. }
  318. randtex=!randtex;
  319. }
  320. if (my_texture_projector != NULL) {
  321. my_texture_projector->Pre_Render_Update(my_camera->Get_Transform());
  322. }
  323. WW3D::Enable_Static_Sort_Lists(staticsort);
  324. Render();
  325. Windows_Message_Handler();
  326. WW3D::Sync(timeGetTime()-time);
  327. Log_Statistics();
  328. }
  329. REF_PTR_RELEASE(my_scene);
  330. REF_PTR_RELEASE(my_camera);
  331. REF_PTR_RELEASE(my_2d_scene);
  332. REF_PTR_RELEASE(my_2d_camera);
  333. REF_PTR_RELEASE(my_lights[0]);
  334. REF_PTR_RELEASE(my_lights[1]);
  335. REF_PTR_RELEASE(my_lights[2]);
  336. delete mytext;
  337. REF_PTR_RELEASE(my_font_a);
  338. REF_PTR_RELEASE(my_font_b);
  339. REF_PTR_RELEASE(my_object);
  340. REF_PTR_RELEASE(my_anim);
  341. REF_PTR_RELEASE(my_material_pass);
  342. REF_PTR_RELEASE(my_texture_projector);
  343. PredictiveLODOptimizerClass::Free();
  344. // shutdown
  345. AssetManager->Free_Assets ();
  346. delete AssetManager;
  347. WW3D::Shutdown ();
  348. WWMath::Shutdown ();
  349. Debug_Refs();
  350. return 0;
  351. }
  352. // ----------------------------------------------------------------------------
  353. //
  354. // Render everything. Rendering stars with Begin_Scene() and ends to End_Scene().
  355. //
  356. // ----------------------------------------------------------------------------
  357. void Render()
  358. {
  359. // Predictive LOD optimizer optimizes the mesh LOD levels to match the given polygon budget
  360. PredictiveLODOptimizerClass::Optimize_LODs(5000);
  361. WW3D::Begin_Render(true,true,Vector3(0.0f,0.0f,0.0f));
  362. // Render 3D scene
  363. WW3D::Render(my_scene,my_camera);
  364. // Render 2D scene
  365. WW3D::Render(my_2d_scene,my_2d_camera);
  366. if (mytext) mytext->Render();
  367. WW3D::End_Render();
  368. fps_counter.Update();
  369. }
  370. // ----------------------------------------------------------------------------
  371. //
  372. // FUNCTION: MyRegisterClass()
  373. //
  374. // PURPOSE: Registers the window class.
  375. //
  376. // COMMENTS:
  377. //
  378. // This function and its usage is only necessary if you want this code
  379. // to be compatible with Win32 systems prior to the 'RegisterClassEx'
  380. // function that was added to Windows 95. It is important to call this function
  381. // so that the application will get 'well formed' small icons associated
  382. // with it.
  383. //
  384. // ----------------------------------------------------------------------------
  385. ATOM MyRegisterClass(HINSTANCE hInstance)
  386. {
  387. WNDCLASSEX wcex;
  388. wcex.cbSize = sizeof(WNDCLASSEX);
  389. wcex.style = CS_HREDRAW | CS_VREDRAW;
  390. wcex.lpfnWndProc = (WNDPROC)WndProc;
  391. wcex.cbClsExtra = 0;
  392. wcex.cbWndExtra = 0;
  393. wcex.hInstance = hInstance;
  394. wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_SKELETON);
  395. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  396. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  397. wcex.lpszMenuName = (LPCSTR)IDC_SKELETON;
  398. wcex.lpszClassName = szWindowClass;
  399. wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  400. return RegisterClassEx(&wcex);
  401. }
  402. // ----------------------------------------------------------------------------
  403. //
  404. // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  405. //
  406. // PURPOSE: Processes messages for the main window.
  407. //
  408. // WM_COMMAND - process the application menu
  409. // WM_PAINT - Paint the main window
  410. // WM_DESTROY - post a quit message and return
  411. //
  412. //
  413. // ----------------------------------------------------------------------------
  414. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  415. {
  416. int wmId, wmEvent;
  417. PAINTSTRUCT ps;
  418. HDC hdc;
  419. switch (message)
  420. {
  421. case WM_COMMAND:
  422. wmId = LOWORD(wParam);
  423. wmEvent = HIWORD(wParam);
  424. // Parse the menu selections:
  425. switch (wmId)
  426. {
  427. case IDM_ABOUT:
  428. DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  429. break;
  430. case IDM_EXIT:
  431. DestroyWindow(hWnd);
  432. break;
  433. default:
  434. return DefWindowProc(hWnd, message, wParam, lParam);
  435. }
  436. break;
  437. case WM_ACTIVATEAPP:
  438. if (wParam) WW3D::On_Activate_App();
  439. else WW3D::On_Deactivate_App();
  440. return DefWindowProc(hWnd, message, wParam, lParam);
  441. case WM_PAINT:
  442. hdc = BeginPaint(hWnd, &ps);
  443. // TODO: Add any drawing code here...
  444. EndPaint(hWnd, &ps);
  445. break;
  446. case WM_DESTROY:
  447. running=false;
  448. PostQuitMessage(0);
  449. break;
  450. case WM_KEYUP:
  451. if ((wParam&0xff)==VK_ESCAPE) {
  452. DestroyWindow(hWnd);
  453. return 0;
  454. }
  455. return DefWindowProc(hWnd, message, wParam, lParam);
  456. case WM_CHAR:
  457. {
  458. char key=LOWORD(wParam);
  459. switch (key)
  460. {
  461. case ' ':
  462. shatter++;
  463. break;
  464. case '-':
  465. WW3D::Make_Screen_Shot("screen");
  466. break;
  467. case '+':
  468. WW3D::Toggle_Movie_Capture();
  469. break;
  470. case 'r':
  471. rotate=!rotate;
  472. break;
  473. case '1':
  474. WW3D::Set_NPatches_Level(1);
  475. break;
  476. case '2':
  477. WW3D::Set_NPatches_Level(2);
  478. break;
  479. case '3':
  480. WW3D::Set_NPatches_Level(3);
  481. break;
  482. case '4':
  483. WW3D::Set_NPatches_Level(4);
  484. break;
  485. case '5':
  486. WW3D::Set_NPatches_Level(5);
  487. break;
  488. case '6':
  489. WW3D::Set_NPatches_Level(6);
  490. break;
  491. case '7':
  492. WW3D::Set_NPatches_Level(7);
  493. break;
  494. case '8':
  495. WW3D::Set_NPatches_Level(8);
  496. break;
  497. case 'q':
  498. randmat=!randmat;
  499. break;
  500. case 'w':
  501. randtex=!randtex;
  502. break;
  503. case 's':
  504. staticsort=!staticsort;
  505. break;
  506. case 'b':
  507. {
  508. using_mat_pass=!using_mat_pass;
  509. if (!using_mat_pass) {
  510. my_scene->Set_Material_Pass(0);
  511. }
  512. else {
  513. my_scene->Set_Material_Pass(my_material_pass);
  514. }
  515. }
  516. break;
  517. case 'g':
  518. {
  519. if (WW3D::Get_NPatches_Gap_Filling_Mode()==WW3D::NPATCHES_GAP_FILLING_DISABLED) {
  520. WW3D::Set_NPatches_Gap_Filling_Mode(WW3D::NPATCHES_GAP_FILLING_ENABLED);
  521. }
  522. else {
  523. WW3D::Set_NPatches_Gap_Filling_Mode(WW3D::NPATCHES_GAP_FILLING_DISABLED);
  524. }
  525. }
  526. break;
  527. case 'm':
  528. {
  529. SceneClass::PolyRenderType type = my_scene->Get_Polygon_Mode();
  530. type = (type == SceneClass::POINT) ? SceneClass::LINE : ((type == SceneClass::LINE) ? SceneClass::FILL : SceneClass::POINT);
  531. my_scene->Set_Polygon_Mode(type);
  532. }
  533. break;
  534. }
  535. }
  536. default:
  537. return DefWindowProc(hWnd, message, wParam, lParam);
  538. }
  539. return 0;
  540. }
  541. // Mesage handler for about box.
  542. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  543. {
  544. switch (message)
  545. {
  546. case WM_INITDIALOG:
  547. return TRUE;
  548. case WM_COMMAND:
  549. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  550. {
  551. EndDialog(hDlg, LOWORD(wParam));
  552. return TRUE;
  553. }
  554. break;
  555. }
  556. return FALSE;
  557. }
  558. void Enable_Alternate_Materials(RenderObjClass * model,bool onoff)
  559. {
  560. if (model == NULL) return;
  561. for (int i=0; i<model->Get_Num_Sub_Objects(); i++) {
  562. RenderObjClass * subobj = model->Get_Sub_Object(i);
  563. Enable_Alternate_Materials(subobj,onoff);
  564. REF_PTR_RELEASE(subobj);
  565. }
  566. if (model->Class_ID() == RenderObjClass::CLASSID_MESH) {
  567. MeshModelClass * mesh_model = ((MeshClass *)model)->Get_Model();
  568. mesh_model->Enable_Alternate_Material_Description(onoff);
  569. REF_PTR_RELEASE(mesh_model);
  570. }
  571. }
  572. // ----------------------------------------------------------------------------
  573. //
  574. // WWDebug message callback defines the behavior of WWDEBUG_SAY().
  575. //
  576. // ----------------------------------------------------------------------------
  577. void WWDebug_Message_Callback(DebugType type, const char * message)
  578. {
  579. OutputDebugString(message);
  580. }
  581. // ----------------------------------------------------------------------------
  582. //
  583. // WWAssert callback defines the behavior of WWASSERT().
  584. //
  585. // ----------------------------------------------------------------------------
  586. void WWAssert_Callback(const char * message)
  587. {
  588. OutputDebugString(message);
  589. _asm int 0x03
  590. }
  591. // ----------------------------------------------------------------------------
  592. RenderObjClass * Create_Dynamesh(void)
  593. {
  594. // Create a tetrahedron
  595. DynamicMeshClass *dynamesh = NEW_REF(DynamicMeshClass, (4, 6));
  596. dynamesh->Set_Shader(ShaderClass::_PresetOpaqueShader);
  597. VertexMaterialClass *vm = VertexMaterialClass::Get_Preset(VertexMaterialClass::PRELIT_NODIFFUSE);
  598. dynamesh->Set_Vertex_Material(vm);
  599. vm->Release_Ref();
  600. TextureClass *texture = AssetManager->Get_Texture("chickonmap.tga");
  601. dynamesh->Set_Texture(texture);
  602. texture->Release_Ref();
  603. dynamesh->Vertex(0.0f, 100.0f, 0.0f, 1.0f, 0.0f);
  604. dynamesh->Vertex(100.0f, -100.0f, 0.0f ,1.0f, -1.0f);
  605. dynamesh->Vertex(-100.0f, -100.0f, 0.0f, -1.0f, -1.0f);
  606. dynamesh->Vertex(0.0f, -100.0f, -100.0f, 0.0f, 0.0f);
  607. dynamesh->Vertex(0.0f, 100.0f, 0.0f, 1.0f, 0.0f);
  608. dynamesh->Vertex(100.0f, -100.0f, 0.0f ,1.0f, -1.0f);
  609. return dynamesh;
  610. }
  611. RenderObjClass *Create_segline(void)
  612. {
  613. SegmentedLineClass *segline = NEW_REF(SegmentedLineClass,());
  614. Vector3 locations[4];
  615. locations[0].Set(0,0,0);
  616. locations[1].Set(50,50,50);
  617. locations[2].Set(50,-50,10);
  618. locations[3].Set(-50,50,10);
  619. segline->Set_Points(4, locations);
  620. //segline->Set_Shader(ShaderClass::_PresetOpaqueShader);
  621. segline->Set_Shader(ShaderClass::_PresetAlphaShader);
  622. segline->Set_Color(Vector3(1,0,1));
  623. segline->Set_Opacity(0.5);
  624. segline->Set_Freeze_Random(1);
  625. segline->Set_Visible(1);
  626. segline->Set_Width(10);
  627. TextureClass *tex=NEW_REF(TextureClass,("cloudlayer.tga"));
  628. segline->Set_Texture(tex);
  629. REF_PTR_RELEASE(tex);
  630. // segline->Set_Texture_Mapping_Mode(SegmentedLineClass::TILED_TEXTURE_MAP);
  631. segline->Set_Texture_Tile_Factor(5);
  632. segline->Set_Noise_Amplitude(5);
  633. segline->Set_Merge_Intersections(1);
  634. return segline;
  635. }
  636. // ----------------------------------------------------------------------------
  637. RenderObjClass * Create_Sphere(void)
  638. {
  639. SphereRenderObjClass *sph=NEW_REF(SphereRenderObjClass,());
  640. sph->Set_Color(Vector3(1,0,1));
  641. sph->Set_Scale(Vector3(100,100,100));
  642. return sph;
  643. }
  644. // ----------------------------------------------------------------------------
  645. RenderObjClass * Create_bitmap2d(void)
  646. {
  647. Bitmap2DObjClass *bmp=NEW_REF(Bitmap2DObjClass,("chickonmap.tga",0.0,0.0,false,false,false));
  648. return bmp;
  649. }
  650. // ----------------------------------------------------------------------------
  651. RenderObjClass * Create_Ring(void)
  652. {
  653. RingRenderObjClass *rng=NEW_REF(RingRenderObjClass,());
  654. rng->Set_Color(Vector3(1,0,1));
  655. rng->Set_Inner_Scale(Vector2(10,10));
  656. rng->Set_Outer_Scale(Vector2(50,50));
  657. return rng;
  658. }
  659. // ----------------------------------------------------------------------------
  660. void Create_Decal(RenderObjClass * robj,Matrix3D & tm,float radius,char * texture_name)
  661. {
  662. /*
  663. ** Allocate the decal generator
  664. */
  665. DecalGeneratorClass * gen = TheDecalSystem.Lock_Decal_Generator();
  666. WWASSERT(gen != NULL);
  667. /*
  668. ** Set up the transform, projection, and bounding volume parameters
  669. */
  670. const float DECAL_HALF_SLAB_THICKNESS = 50.0f;
  671. const float DECAL_BACKFACE_THRESHHOLD = 0.0f;
  672. float backup_dist = DECAL_HALF_SLAB_THICKNESS + 0.01f;
  673. Matrix3D transform = tm;
  674. transform.Translate_Z(backup_dist);
  675. gen->Set_Transform(transform);
  676. gen->Set_Ortho_Projection(-radius,radius,-radius,radius,0.01f,backup_dist + 2.0f * DECAL_HALF_SLAB_THICKNESS);
  677. gen->Set_Backface_Threshhold(DECAL_BACKFACE_THRESHHOLD);
  678. /*
  679. ** Set up the material settings. Just plug in the standard alpha shader and the
  680. ** vertex material which all decals use. Then grab the texture which the user
  681. ** specified...
  682. */
  683. MaterialPassClass * material = gen->Get_Material();
  684. VertexMaterialClass * decal_mtl = NEW_REF(VertexMaterialClass,());
  685. decal_mtl->Set_Ambient(0,0,0);
  686. decal_mtl->Set_Diffuse(0,0,0);
  687. decal_mtl->Set_Specular(0,0,0);
  688. decal_mtl->Set_Emissive(1,1,1);
  689. decal_mtl->Set_Opacity(1.0f);
  690. decal_mtl->Set_Shininess(0.0f);
  691. TextureClass * tex = WW3DAssetManager::Get_Instance()->Get_Texture(texture_name);
  692. tex->Set_U_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  693. tex->Set_V_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  694. material->Set_Shader(ShaderClass::_PresetAlphaShader);
  695. material->Set_Material(decal_mtl);
  696. material->Set_Texture(tex);
  697. REF_PTR_RELEASE(decal_mtl);
  698. REF_PTR_RELEASE(tex);
  699. REF_PTR_RELEASE(material);
  700. /*
  701. ** Apply!
  702. */
  703. robj->Create_Decal(gen);
  704. TheDecalSystem.Unlock_Decal_Generator(gen);
  705. }
  706. // ----------------------------------------------------------------------------
  707. MaterialPassClass * Create_Material_Pass(void)
  708. {
  709. TextureClass * texture = WW3DAssetManager::Get_Instance()->Get_Texture("decal.tga");
  710. VertexMaterialClass * mtl = NEW_REF(VertexMaterialClass,());
  711. mtl->Set_Diffuse(0.5f,0.5f,0.5f);
  712. mtl->Set_Specular(0,0,0);
  713. mtl->Set_Ambient(0,0,0);
  714. mtl->Set_Emissive(0.5f,0.5f,0.5f);
  715. mtl->Set_Lighting(false);
  716. EnvironmentMapperClass * mapper = NEW_REF(EnvironmentMapperClass,(0));
  717. mtl->Set_Mapper(mapper,0);
  718. MaterialPassClass * test_pass = NEW_REF(MaterialPassClass,());
  719. test_pass->Set_Shader(ShaderClass::_PresetAdditiveShader);
  720. test_pass->Set_Material(mtl);
  721. test_pass->Set_Texture(texture);
  722. REF_PTR_RELEASE(mtl);
  723. REF_PTR_RELEASE(texture);
  724. REF_PTR_RELEASE(mapper);
  725. return test_pass;
  726. }
  727. // ----------------------------------------------------------------------------
  728. MaterialPassClass * Create_Bump_Material_Pass(void)
  729. {
  730. TextureClass * texture = WW3DAssetManager::Get_Instance()->Get_Texture("earthbump.tga",TextureClass::MIP_LEVELS_1);
  731. TextureClass* bump_texture=WW3DAssetManager::Get_Instance()->Get_Bumpmap_Based_On_Texture(texture);
  732. REF_PTR_RELEASE(texture);
  733. TextureClass * env_texture = WW3DAssetManager::Get_Instance()->Get_Texture("catwalk.tga",TextureClass::MIP_LEVELS_1);
  734. VertexMaterialClass * mtl = NEW_REF(VertexMaterialClass,());
  735. TextureMapperClass* mapper= NEW_REF(EnvironmentMapperClass,(1));
  736. mtl->Set_Mapper(mapper,1);
  737. REF_PTR_RELEASE(mapper);
  738. mtl->Set_Diffuse(0.0f,0.0f,0.0f);
  739. // mtl->Set_Emissive(0.0f,0.0f,0.0f);
  740. // mtl->Set_Specular(0.0f,0.0f,0.0f);
  741. mtl->Set_Ambient(0.0f,0.0f,0.0f);
  742. mtl->Set_Lighting(true);
  743. MaterialPassClass * test_pass = NEW_REF(MaterialPassClass,());
  744. test_pass->Set_Shader(ShaderClass::_PresetBumpenvmapShader);
  745. test_pass->Set_Material(mtl);
  746. REF_PTR_RELEASE(mtl);
  747. test_pass->Set_Texture(bump_texture,0);
  748. test_pass->Set_Texture(env_texture,1);
  749. /*
  750. Vector3 light_vector(0.0f,0.0f,1.0f);
  751. // Store the light vector, so it can be referenced in D3DTA_TFACTOR
  752. DWORD dwFactor = VectortoRGBA( light_vector, 0.0f );
  753. DX8Wrapper::Set_DX8_Render_State( D3DRS_TEXTUREFACTOR, dwFactor );
  754. TextureClass * texture = WW3DAssetManager::Get_Instance()->Get_Texture(
  755. "earthbump.tga",//"19_AmmoC.tga"
  756. TextureClass::MIP_LEVELS_1);
  757. TextureClass* normal_map=WW3DAssetManager::Get_Instance()->Get_Bumpmap_Based_On_Texture(texture);
  758. VertexMaterialClass * mtl = NEW_REF(VertexMaterialClass,());
  759. mtl->Set_Diffuse(0.1f,0.1f,0.1f);
  760. mtl->Set_Specular(0.0f,0,0);
  761. mtl->Set_Ambient(0,0,0.0f);
  762. mtl->Set_Emissive(0.2f,0.2f,0.2f);
  763. mtl->Set_Lighting(true);
  764. mtl->Set_UV_Source(0,0);
  765. mtl->Set_Diffuse_Color_Source(VertexMaterialClass::MATERIAL);
  766. mtl->Set_Emissive_Color_Source(VertexMaterialClass::MATERIAL);
  767. mtl->Set_Ambient_Color_Source(VertexMaterialClass::MATERIAL);
  768. MaterialPassClass * test_pass = NEW_REF(MaterialPassClass,());
  769. ShaderClass shader=ShaderClass::_PresetAdditiveShader;
  770. shader.Set_Primary_Gradient(ShaderClass::GRADIENT_DOTPRODUCT3);
  771. // shader.Set_Primary_Gradient(ShaderClass::GRADIENT_DISABLE);//MODULATE);
  772. // shader.Set_Depth_Compare(ShaderClass::PASS_ALWAYS);
  773. // shader.Set_Post_Detail_Color_Func(ShaderClass::DETAILCOLOR_SCALE);
  774. test_pass->Set_Shader(shader);
  775. test_pass->Set_Material(mtl);
  776. test_pass->Set_Texture(normal_map,0);
  777. */
  778. return test_pass;
  779. }
  780. // ----------------------------------------------------------------------------
  781. TexProjectClass * Create_Texture_Projector(void)
  782. {
  783. const float SIZE = 10.0f;
  784. const float ZNEAR = 0.1f;
  785. const float ZFAR = 300.0f;
  786. TexProjectClass * new_projector = NEW_REF(TexProjectClass,());
  787. new_projector->Init_Additive();
  788. new_projector->Set_Intensity(1.0,true);
  789. new_projector->Set_Ortho_Projection(-SIZE,SIZE,-SIZE,SIZE,ZNEAR,ZFAR);
  790. Matrix3D tm;
  791. tm.Look_At(Vector3(200,200,200),Vector3(0,0,0),0.0f);
  792. new_projector->Set_Transform(tm);
  793. TextureClass * texture = WW3DAssetManager::Get_Instance()->Get_Texture("GreenSpot.tga");
  794. new_projector->Set_Texture(texture);
  795. REF_PTR_RELEASE(texture);
  796. return new_projector;
  797. }
  798. // ----------------------------------------------------------------------------
  799. void Debug_Refs(void)
  800. {
  801. #ifdef _DEBUG
  802. WWDEBUG_SAY(("Dumping Un-Released Ref-Counted objects...\r\n"));
  803. RefCountNodeClass * first = RefCountClass::ActiveRefList.First();
  804. RefCountNodeClass * node = first;
  805. while (node->Is_Valid())
  806. {
  807. RefCountClass * obj = node->Get();
  808. ActiveRefStruct * ref = &(obj->ActiveRefInfo);
  809. bool display = true;
  810. int count = 0;
  811. RefCountNodeClass * search = first;
  812. while (search->Is_Valid()) {
  813. if (search == node) { // if this is not the first one
  814. if (count != 0) {
  815. display = false;
  816. break;
  817. }
  818. }
  819. RefCountClass * search_obj = search->Get();
  820. ActiveRefStruct * search_ref = &(search_obj->ActiveRefInfo);
  821. if ( ref->File && search_ref->File &&
  822. !strcmp(search_ref->File, ref->File) &&
  823. (search_ref->Line == ref->Line) ) {
  824. count++;
  825. } else if ( (ref->File == NULL) && (search_ref->File == NULL) ) {
  826. count++;
  827. }
  828. search = search->Next();
  829. }
  830. if ( display ) {
  831. WWDEBUG_SAY(( "%d Active Ref: %s %d %p\n", count, ref->File,ref->Line,obj));
  832. static int num_printed = 0;
  833. if (++num_printed > 20) {
  834. WWDEBUG_SAY(( "And Many More......\n"));
  835. break;
  836. }
  837. }
  838. node = node->Next();
  839. }
  840. WWDEBUG_SAY(("Done.\r\n"));
  841. #endif
  842. }
  843. // ----------------------------------------------------------------------
  844. //
  845. // Ugly statistics block!!!
  846. //
  847. // ----------------------------------------------------------------------
  848. void Log_Statistics()
  849. {
  850. StringClass format(255,true);
  851. StringClass status_text(500,true);
  852. // static unsigned last_frame_time=0;
  853. // unsigned frame_time=timeGetTime();
  854. // unsigned time=frame_time-last_frame_time;
  855. // last_frame_time=frame_time;
  856. // if (time>1000) time=1000;
  857. // float fps=1000.0f/float(time);
  858. // static float time_fps;
  859. // static float current_fps;
  860. // static unsigned fps_count;
  861. // time_fps+=fps;
  862. // fps_count++;
  863. // if (fps_count==20) {
  864. // fps_count=0;
  865. // current_fps=time_fps/20.0f;
  866. // time_fps=0.0f;
  867. // }
  868. unsigned current_fps=fps_counter.Get_FPS();
  869. static unsigned stats_mode;
  870. static bool tab_pressed;
  871. if (GetAsyncKeyState(VK_TAB)) {
  872. if (!tab_pressed) {
  873. stats_mode++;
  874. stats_mode%=3;
  875. }
  876. tab_pressed=true;
  877. }
  878. else {
  879. tab_pressed=false;
  880. }
  881. switch (stats_mode) {
  882. case 0:
  883. Debug_Statistics::Record_Texture_Mode(Debug_Statistics::RECORD_TEXTURE_NONE);
  884. break;
  885. case 1:
  886. Debug_Statistics::Record_Texture_Mode(Debug_Statistics::RECORD_TEXTURE_NONE);
  887. format.Format("%d FPS\n",current_fps);
  888. status_text+=format;
  889. format.Format("%d Polys/frame (%dk pps)\n",Debug_Statistics::Get_DX8_Polygons(),unsigned(Debug_Statistics::Get_DX8_Polygons()*float(current_fps))/1000);
  890. status_text+=format;
  891. break;
  892. case 2:
  893. Debug_Statistics::Record_Texture_Mode(Debug_Statistics::RECORD_TEXTURE_NONE);
  894. format.Format("%d FPS\n",current_fps);
  895. status_text+=format;
  896. format.Format("%d Polys/frame (%dk pps)\n",Debug_Statistics::Get_DX8_Polygons(),unsigned(Debug_Statistics::Get_DX8_Polygons()*float(current_fps))/1000);
  897. status_text+=format;
  898. format.Format("%d Verts/frame (%dk vps)\n",Debug_Statistics::Get_DX8_Vertices(),unsigned(Debug_Statistics::Get_DX8_Vertices()*float(current_fps))/1000);
  899. status_text+=format;
  900. format.Format("%d DX8 calls\n",DX8Wrapper::Get_Last_Frame_DX8_Calls());
  901. status_text+=format;
  902. format.Format("%d VB changes\n",DX8Wrapper::Get_Last_Frame_Vertex_Buffer_Changes());
  903. status_text+=format;
  904. format.Format("%d IB changes\n",DX8Wrapper::Get_Last_Frame_Index_Buffer_Changes());
  905. status_text+=format;
  906. format.Format("%d texture changes\n",DX8Wrapper::Get_Last_Frame_Texture_Changes());
  907. status_text+=format;
  908. format.Format("%d material changes\n",DX8Wrapper::Get_Last_Frame_Material_Changes());
  909. status_text+=format;
  910. format.Format("%d light changes\n",DX8Wrapper::Get_Last_Frame_Light_Changes());
  911. status_text+=format;
  912. format.Format("%d RS changes\n",DX8Wrapper::Get_Last_Frame_Render_State_Changes());
  913. status_text+=format;
  914. format.Format("%d TSS changes\n",DX8Wrapper::Get_Last_Frame_Texture_Stage_State_Changes());
  915. status_text+=format;
  916. format.Format("%d Mtx changes\n",DX8Wrapper::Get_Last_Frame_Matrix_Changes());
  917. status_text+=format;
  918. break;
  919. }
  920. mytext->Reset();
  921. mytext->Set_Location(Vector2(0.0,0.0));
  922. mytext->Draw_Text(status_text,0xffff0000);
  923. }
  924. // ----------------------------------------------------------------------------
  925. void Init_Assets()
  926. {
  927. TextureClass *tex=AssetManager->Get_Texture("PPVF0102.TGA");
  928. REF_PTR_RELEASE(tex);
  929. // Preload test assets. Note that not all of these will actually be used.
  930. /* AssetManager->Load_3D_Assets("GreenSphere.w3d"); // Tests regular meshes
  931. AssetManager->Load_3D_Assets("TexSphere.w3d"); // Tests texture mapping
  932. AssetManager->Load_3D_Assets("TexScrlSphere.w3d"); // Tests Linear Offset mapper
  933. AssetManager->Load_3D_Assets("SphereAnim.w3d"); // Tests hierarchical animation (if you install its anim)
  934. AssetManager->Load_3D_Assets("SkinTest.w3d"); // Tests skinning and environment mapping
  935. AssetManager->Load_3D_Assets("sorting_test.w3d"); // Tests skinning and environment mapping
  936. AssetManager->Load_3D_Assets("boxtest.w3d"); // Tests BoxRenderObjClass (turn on the box display mask)
  937. AssetManager->Load_3D_Assets("Test1.w3d"); // Test yellow particles
  938. AssetManager->Load_3D_Assets("Test2.w3d"); // Tests alpha particles
  939. AssetManager->Load_3D_Assets("MPassSphere.w3d"); // Tests more than 2 passes on a mesh
  940. AssetManager->Load_3D_Assets("MPassTeapot.w3d"); // Tests more that 2 passes
  941. AssetManager->Load_3D_Assets("DupSphere.w3d"); // Tests 3 passes which all share the same uv coords
  942. AssetManager->Load_3D_Assets("vcolortest.w3d"); // Tests vertex color
  943. AssetManager->Load_3D_Assets("valphatest.w3d"); // Tests vertex alpha
  944. AssetManager->Load_3D_Assets("LightmapTest0.w3d"); // Tests pre-lighting, 3 solves: vertex, multi-pass, multi-stage
  945. AssetManager->Load_3D_Assets("LightmapTest1.w3d"); // Tests pre-lighting with alternate solves! (not currently runtime switchable)
  946. AssetManager->Load_3D_Assets("CubeSkin.w3d"); // Tests a simple skin, also can be used to test decals on skin meshes
  947. AssetManager->Load_3D_Assets("ProjectorTest.w3d"); // This model contains a mesh and an animated "ProjectorBone" for testing texture projections
  948. AssetManager->Load_3D_Assets("PolyRateTest.w3d"); // This model has lots of polys!
  949. AssetManager->Load_3D_Assets("PPVF01A.w3d"); // Game asset
  950. AssetManager->Load_3D_Assets("SPHERE_TEST.w3d"); // Tests multitexturing
  951. AssetManager->Load_3D_Assets("SCREEN_ALIGN.w3d"); // Tests camera aligned meshes
  952. AssetManager->Load_3D_Assets("halfsphere.w3d"); // Tests two-sided flag, camera aligned, and camera oriented meshes
  953. AssetManager->Load_3D_Assets("tsi01a.w3d"); // Tests static sort level
  954. AssetManager->Load_3D_Assets("tsi08a.w3d"); // Tests static sort level
  955. */
  956. AssetManager->Load_3D_Assets("S_A_Human.W3D");
  957. AssetManager->Load_3D_Assets("c_nod_rsold_L0.W3D");
  958. AssetManager->Load_3D_Assets("h_a_a0a1.W3D");
  959. my_lights[0]=my_lights[1]=my_lights[2]=NULL;
  960. my_lights[0]=NEW_REF(LightClass,(LightClass::DIRECTIONAL));
  961. my_lights[0]->Set_Ambient(Vector3(0.1f,0.1f,0.1f));
  962. my_lights[0]->Set_Diffuse(Vector3(1.0f,1.0f,1.0f));
  963. my_lights[0]->Set_Spot_Direction(Vector3(0,1,0));
  964. }
  965. // ----------------------------------------------------------------------------
  966. //
  967. // Create a scene for 2D-display, such as text and rectangles.
  968. //
  969. // ----------------------------------------------------------------------------
  970. void Init_2D_Scene()
  971. {
  972. // Create a scene for the 2D stuff -----------------------------------------
  973. my_2d_scene=NEW_REF(SimpleSceneClass,());
  974. my_2d_scene->Set_Ambient_Light(Vector3(1,1,1));
  975. // Create fonts to be used for text rendering ------------------------------
  976. my_font_a = AssetManager->Get_Font3DInstance("font12x16.tga");
  977. my_font_b = AssetManager->Get_Font3DInstance("fontnew4.tga");
  978. mytext=new Render2DTextClass(my_font_b);
  979. mytext->Set_Coordinate_Range( Render2DClass::Get_Screen_Resolution() );
  980. #if 0
  981. // Display a 2D bitmap on the screen ---------------------------------------
  982. #ifdef DRAWBITMAP
  983. RenderObjClass *bm = Create_bitmap2d();
  984. my_2d_scene->Add_Render_Object(bm);
  985. REF_PTR_RELEASE(bm);
  986. #else
  987. RenderObjClass *my_object = AssetManager->Create_Render_Obj("TSI08A");
  988. if (my_object != NULL) {
  989. my_2d_scene->Add_Render_Object(my_object);
  990. REF_PTR_RELEASE(my_object);
  991. }
  992. my_object = AssetManager->Create_Render_Obj("TSI01A");
  993. if (my_object != NULL) {
  994. my_2d_scene->Add_Render_Object(my_object);
  995. REF_PTR_RELEASE(my_object);
  996. }
  997. #endif
  998. #endif
  999. // Scene needs camera to be rendered with ----------------------------------
  1000. my_2d_camera = NEW_REF(CameraClass,());
  1001. #ifdef DRAWBITMAP
  1002. my_2d_camera->Set_Position(Vector3(0,0,1));
  1003. my_2d_camera->Set_Clip_Planes(0.995,2);
  1004. #else
  1005. my_2d_camera->Set_Position(Vector3(0,0,320));
  1006. my_2d_camera->Set_Clip_Planes(300,1000);
  1007. #endif
  1008. Vector2 min = Vector2(-1, -0.75f);
  1009. Vector2 max = Vector2(+1, +0.75f);
  1010. my_2d_camera->Set_View_Plane(min, max);
  1011. }
  1012. // ----------------------------------------------------------------------------
  1013. void add_duplicate()
  1014. {
  1015. REF_PTR_RELEASE(my_object);
  1016. my_object = AssetManager->Create_Render_Obj("TexScrlSphere");
  1017. my_object->Set_Position(Vector3(0,50,0));
  1018. PredictiveLODOptimizerClass::Add_Object(my_object);
  1019. my_scene->Add_Render_Object(my_object);
  1020. ((MeshClass*)my_object)->Make_Unique();
  1021. if (my_object->Class_ID()==RenderObjClass::CLASSID_MESH)
  1022. {
  1023. MeshClass *mesh=(MeshClass*) my_object;
  1024. MaterialInfoClass *mi=mesh->Get_Material_Info();
  1025. VertexMaterialClass *vm=mi->Get_Vertex_Material(0);
  1026. VertexMaterialClass *newvm=NEW_REF(VertexMaterialClass,(*vm));
  1027. newvm->Set_Emissive(rand()%256/256.0,rand()%256/256.0,rand()%256/256.0);
  1028. mesh->Replace_VertexMaterial(vm,newvm);
  1029. mi->Replace_Material(0,newvm);
  1030. REF_PTR_RELEASE(vm);
  1031. REF_PTR_RELEASE(newvm);
  1032. }
  1033. if (my_object->Class_ID()==RenderObjClass::CLASSID_MESH)
  1034. {
  1035. MeshClass *mesh=(MeshClass*) my_object;
  1036. MaterialInfoClass *mi=mesh->Get_Material_Info();
  1037. TextureClass *tex=mi->Peek_Texture(0);
  1038. TextureClass *newtex=AssetManager->Get_Texture("catwalk.tga");
  1039. mesh->Replace_Texture(tex,newtex);
  1040. mi->Replace_Texture(0,newtex);
  1041. REF_PTR_RELEASE(newtex);
  1042. }
  1043. REF_PTR_RELEASE(my_object);
  1044. my_object = AssetManager->Create_Render_Obj("GreenSphere");
  1045. my_object->Set_Position(Vector3(-50,0,0));
  1046. PredictiveLODOptimizerClass::Add_Object(my_object);
  1047. my_scene->Add_Render_Object(my_object);
  1048. if (my_object->Class_ID()==RenderObjClass::CLASSID_MESH)
  1049. {
  1050. MeshClass *mesh=(MeshClass*) my_object;
  1051. MaterialInfoClass *mi=mesh->Get_Material_Info();
  1052. VertexMaterialClass *vm=mi->Get_Vertex_Material(0);
  1053. VertexMaterialClass *newvm=NEW_REF(VertexMaterialClass,(*vm));
  1054. newvm->Set_Emissive(rand()%256/256.0,rand()%256/256.0,rand()%256/256.0);
  1055. mesh->Replace_VertexMaterial(vm,newvm);
  1056. mi->Replace_Material(0,newvm);
  1057. REF_PTR_RELEASE(vm);
  1058. REF_PTR_RELEASE(newvm);
  1059. }
  1060. REF_PTR_RELEASE(my_object);
  1061. my_object = AssetManager->Create_Render_Obj("TexScrlSphere");
  1062. my_object->Set_Position(Vector3(50,0,0));
  1063. PredictiveLODOptimizerClass::Add_Object(my_object);
  1064. my_scene->Add_Render_Object(my_object);
  1065. ((MeshClass*)my_object)->Make_Unique();
  1066. }
  1067. void Init_3D_Scene()
  1068. {
  1069. WW3D::Enable_Sorting(false);
  1070. WW3D::Set_NPatches_Level(1);
  1071. // build scene
  1072. my_scene=NEW_REF(SkeletonSceneClass,());
  1073. my_scene->Set_Ambient_Light(Vector3(0.5f,0.5f,0.5f));
  1074. my_scene->Add_Render_Object(my_lights[0]);
  1075. // Scene needs camera to be rendered with ----------------------------------
  1076. my_camera=NEW_REF(CameraClass,());
  1077. Matrix3D camtransform(1);
  1078. camtransform.Look_At(CAMERA_POSITION,CAMERA_TARGET,0);
  1079. my_camera->Set_Transform(camtransform);
  1080. // Here are some test objects to choose from. Main loop set the rotation
  1081. // of my_object so even though the scene can have multiple objects, this
  1082. // application only applies movement to this single one.
  1083. // my_object = AssetManager->Create_Render_Obj("GreenSphere");
  1084. // my_object = AssetManager->Create_Render_Obj("TexSphere");
  1085. // my_object = AssetManager->Create_Render_Obj("TexScrlSphere");
  1086. // my_object = AssetManager->Create_Render_Obj("SphereAnim");
  1087. // my_object = AssetManager->Create_Render_Obj("SkinTest");
  1088. // my_object = AssetManager->Create_Render_Obj("sorting_test");
  1089. // my_object = AssetManager->Create_Render_Obj("CubeSkin");
  1090. // my_object = AssetManager->Create_Render_Obj("boxtest");
  1091. // my_object = AssetManager->Create_Render_Obj("Test1");
  1092. // my_object = AssetManager->Create_Render_Obj("Test2");
  1093. // my_object = AssetManager->Create_Render_Obj("LightmapTest1");
  1094. // my_object = AssetManager->Create_Render_Obj("ProjectorTest");
  1095. // my_object = AssetManager->Create_Render_Obj("PolyRateTest");
  1096. // my_object = AssetManager->Create_Render_Obj("PPVF01A");
  1097. // my_object = AssetManager->Create_Render_Obj("SPHERE_TEST");
  1098. // my_object = AssetManager->Create_Render_Obj("SCREEN_ALIGN");
  1099. // my_object = AssetManager->Create_Render_Obj(/*"A_CRATE-LOITER.*/"P_AMMOCRATE");
  1100. // my_object = Create_Dynamesh();
  1101. // my_object = Create_Sphere();
  1102. // my_object = Create_Ring();
  1103. // my_object = Create_segline();
  1104. // my_object = AssetManager->Create_Render_Obj("HALFSPHERE");
  1105. my_object = AssetManager->Create_Render_Obj("c_nod_rsold_L0");
  1106. // my_anim = AssetManager->Get_HAnim("SkinTest.SkinTest");
  1107. // my_anim = AssetManager->Get_HAnim("boxtest.boxtest");
  1108. // my_anim = AssetManager->Get_HAnim("CubeSkin.CubeSkin");
  1109. // my_anim = AssetManager->Get_HAnim("ProjectorTest.ProjectorTest");
  1110. // my_anim = AssetManager->Get_HAnim("PolyRateTest.PolyRateTest");
  1111. // my_anim = AssetManager->Get_HAnim("P_AMMOCRATE.A_CRATE-LOITER");
  1112. my_anim = AssetManager->Get_HAnim("S_A_HUMAN.H_A_A0A1");
  1113. if (my_object != NULL) {
  1114. #if 0
  1115. Enable_Alternate_Materials(my_object,true);
  1116. #endif
  1117. // Decal System
  1118. #if 0
  1119. Matrix3D decal_tm;
  1120. decal_tm.Look_At(Vector3(30,5,10),Vector3(0,0,10),DEG_TO_RADF(65.5f));
  1121. Create_Decal(my_object,decal_tm,13.7f,"decal.tga");
  1122. #endif
  1123. // PredictiveLODOptimizerClass::Add_Object(my_object);
  1124. my_object->Set_Position(Vector3(0,0,0));
  1125. if (my_anim != NULL) {
  1126. my_object->Set_Animation(my_anim,0,RenderObjClass::ANIM_MODE_LOOP);
  1127. }
  1128. my_scene->Add_Render_Object(my_object);
  1129. if (my_object->Class_ID()==RenderObjClass::CLASSID_PARTICLEEMITTER) {
  1130. ((ParticleEmitterClass*) my_object)->Start();
  1131. }
  1132. }
  1133. #if 0
  1134. add_duplicate();
  1135. #endif
  1136. // 3D line object ----------------------------------------------------------
  1137. #if 0
  1138. Line3DClass *line = NULL;
  1139. line = NEW_REF(Line3DClass,(Vector3(0,0,0),Vector3(100,100,0), 10,1,1,1,1));
  1140. my_scene->Add_Render_Object(line);
  1141. REF_PTR_RELEASE(line); // Obejct can be freed after adding scene so that it gets killed when scene dies
  1142. #endif
  1143. // Create texture projector material pass ----------------------------------
  1144. #if 0
  1145. my_texture_projector = Create_Texture_Projector();
  1146. my_scene->Set_Material_Pass(my_texture_projector->Peek_Material_Pass());
  1147. // REF_PTR_RELEASE(my_texture_projector);
  1148. #endif
  1149. // Create material pass ----------------------------------------------------
  1150. #if 0
  1151. my_material_pass = Create_Material_Pass();
  1152. my_scene->Set_Material_Pass(my_material_pass);
  1153. REF_PTR_RELEASE(my_material_pass);
  1154. #endif
  1155. #if 0
  1156. my_material_pass = Create_Bump_Material_Pass();
  1157. my_scene->Set_Material_Pass(my_material_pass);
  1158. using_mat_pass=true;
  1159. // REF_PTR_RELEASE(my_material_pass);
  1160. #endif
  1161. }