/src/renderer/render.cpp

https://bitbucket.org/vivkin/gam3b00bs/ · C++ · 225 lines · 152 code · 37 blank · 36 comment · 22 complexity · eb3dc409e00a9815426d238d3ba37fa8 MD5 · raw file

  1. #include "app/app.h"
  2. #include "renderer/device.h"
  3. #include "renderer/mesh.h"
  4. #include "renderer/particles.h"
  5. #include "load_data.h"
  6. #include "visual.h"
  7. #include "app/app.h"
  8. #include "log.h"
  9. #include "load_data.h"
  10. //-----------------------------------------------------------------------------
  11. texture_h texture;
  12. mesh_h mesh;
  13. camera_t camera;
  14. //mesh_h text_mesh;
  15. //texture_h text_texture;
  16. render_op rop;
  17. environment_state_t env;
  18. D3DXMATRIX view;
  19. D3DXMATRIX proj;
  20. visual_h vis, boobs;
  21. particle_system_t psys;
  22. //-----------------------------------------------------------------------------
  23. float yaw = 0.0f;
  24. //-----------------------------------------------------------------------------
  25. bool test_init()
  26. {
  27. // some test data initialisation
  28. memset(&env, 0, sizeof(environment_state_t));
  29. env.light[0].Type = D3DLIGHT_POINT;
  30. env.light[0].Ambient = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
  31. env.light[0].Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  32. env.light[0].Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  33. env.light[0].Position = D3DXVECTOR3(0.0f, 100.0f, 0.0f);
  34. env.light[0].Range = 200.0f;
  35. env.light[0].Attenuation0 = 1.0f;
  36. env.light[0].Attenuation1 = 0.0f;
  37. env.light[0].Attenuation2 = 0.0001f;
  38. // env.light_enable[0] = true;
  39. env.light[1].Type = D3DLIGHT_SPOT;
  40. env.light[1].Ambient = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
  41. env.light[1].Diffuse = D3DXCOLOR(1.0f, 0.0f, 1.0f, 1.0f);
  42. env.light[1].Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  43. env.light[1].Position = D3DXVECTOR3(0.0f, 100.0f, 0.0f);
  44. env.light[1].Direction = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
  45. env.light[1].Range = 200.0f;
  46. env.light[1].Falloff = 1.0f;
  47. env.light[1].Phi = 3.14f / 2.0f;
  48. env.light[1].Theta = 3.14f / 3.0f;
  49. env.light[1].Attenuation0 = 1.0f;
  50. env.light[1].Attenuation1 = 0.0f;
  51. env.light[1].Attenuation2 = 0.0001f;
  52. // env.light_enable[1] = true;
  53. env.light[2].Type = D3DLIGHT_SPOT;
  54. env.light[2].Ambient = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
  55. env.light[2].Diffuse = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);
  56. env.light[2].Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  57. env.light[2].Position = D3DXVECTOR3(0.0f, 100.0f, 0.0f);
  58. env.light[2].Direction = D3DXVECTOR3(-1.0f, 0.0f, 0.0f);
  59. env.light[2].Range = 200.0f;
  60. env.light[2].Falloff = 1.0f;
  61. env.light[2].Phi = 3.14f / 3.0f;
  62. env.light[2].Theta = 3.14f / 4.0f;
  63. env.light[2].Attenuation0 = 1.0f;
  64. env.light[2].Attenuation1 = 0.0f;
  65. env.light[2].Attenuation2 = 0.0001f;
  66. // env.light_enable[2] = true;
  67. env.light[3].Type = D3DLIGHT_SPOT;
  68. env.light[3].Ambient = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
  69. env.light[3].Diffuse = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);
  70. env.light[3].Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  71. env.light[3].Position = D3DXVECTOR3(200.0f, 200.0f, 200.0f);
  72. env.light[3].Direction = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
  73. env.light[3].Range = 400.0f;
  74. env.light[3].Falloff = 1.0f;
  75. env.light[3].Phi = 3.14f / 3.0f;
  76. env.light[3].Theta = 3.14f / 4.0f;
  77. env.light[3].Attenuation0 = 1.0f;
  78. env.light[3].Attenuation1 = 0.01f;
  79. env.light[3].Attenuation2 = 0.0f;
  80. // env.light_enable[3] = true;
  81. env.light[4].Type = D3DLIGHT_DIRECTIONAL;
  82. env.light[4].Ambient = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
  83. env.light[4].Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  84. env.light[4].Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  85. env.light[4].Direction = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
  86. env.light_enable[4] = true;
  87. D3DXVec3Normalize((D3DXVECTOR3*)&env.light[1].Direction, (D3DXVECTOR3*)&env.light[1].Direction);
  88. D3DXVec3Normalize((D3DXVECTOR3*)&env.light[2].Direction, (D3DXVECTOR3*)&env.light[2].Direction);
  89. D3DXVec3Normalize((D3DXVECTOR3*)&env.light[3].Direction, (D3DXVECTOR3*)&env.light[3].Direction);
  90. D3DXVec3Normalize((D3DXVECTOR3*)&env.light[4].Direction, (D3DXVECTOR3*)&env.light[4].Direction);
  91. camera.angle = 0.0f;
  92. camera.aspect = 1.333f;
  93. camera.fov = 3.14f / 4.0f;
  94. camera.position = D3DXVECTOR3(200.0f, 200.0f, 200.0f);
  95. camera.target = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  96. camera.zfar = 1000.0f;
  97. camera.znear = 1.0f;
  98. camera.radius = 200.0f;
  99. camera.phi = 3.14f / 3.0f;
  100. // camera_look_at(camera, D3DXVECTOR3(200.0f, 200.0f, 200.0f), D3DXVECTOR3(0.0f, 80.0f, 0.0f), D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  101. // camera_perspective(camera, 3.14f / 4.0f, 1.333f, 1.0f, 1000.0f);
  102. D3DXVECTOR3 eye(200.0f, 200.0f, 200.0f);
  103. D3DXVECTOR3 at(0.0f, 80.0f, 0.0f);
  104. D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
  105. D3DXMatrixLookAtLH(&view, &eye, &at, &up);
  106. D3DXMatrixPerspectiveFovLH(&proj, 3.14f / 4.0f, 1.333f, 1.0f, 1000.0f);
  107. particle_system_create(&psys);
  108. byte* texture_data;
  109. uint32 texture_data_size;
  110. texture = 0;
  111. if (!load_data("..\\textures\\particle.dds", &texture_data, &texture_data_size))
  112. {
  113. texture = renderer::texture_create(texture_data, texture_data_size);
  114. free(texture_data);
  115. }
  116. particle_desc_t emitter_desc = {
  117. texture,
  118. D3DXVECTOR3(0.0f, 80.0f, 0.0f),
  119. D3DXVECTOR3(0.0f, 1.0f, 0.0f),
  120. {10, 12},
  121. {10, 12},
  122. {30, 50},
  123. {{1, 1, 0}, {1, 0, 0}},
  124. 20.0f,
  125. 100000.0f
  126. };
  127. particle_system_add(&psys, &emitter_desc);
  128. return true;
  129. }
  130. //-----------------------------------------------------------------------------
  131. namespace renderer
  132. {
  133. void tick()
  134. {
  135. if( !renderer::begin() )
  136. return;
  137. app::timings TM = app::time();
  138. particle_system_update(&psys, camera, TM.dt);
  139. renderer::clear(0xFF000000);
  140. set_environment_state(env);
  141. //set_view(view);
  142. //set_projection(proj);
  143. set_camera(camera);
  144. set_sampler(0, SAMPLER_DEFAULT);
  145. /*
  146. rop.vb = mesh->vb;
  147. rop.ib = mesh->ib;
  148. rop.index_count = mesh->header.index_count;
  149. rop.vertex_count = mesh->header.vertex_count;
  150. rop.material.texture[0] = texture;
  151. for( uint32 i=0; i<mesh->header.subset_count; i++ )
  152. {
  153. rop.index_count = mesh->subsets[i].index_count;
  154. rop.index_offset = mesh->subsets[i].index_offset;
  155. rop.vertex_offset = mesh->subsets[i].vertex_offset;
  156. renderer::render(rop);
  157. }
  158. */
  159. // visual_draw( vis );
  160. // visual_draw( boobs );
  161. visuals_render_all();
  162. particle_system_render(&psys, camera, renderer::device_get());
  163. // float dt = app::time().dt;
  164. // yaw += dt;
  165. // visual_set_rotation(boobs, 0.0f, yaw, 0.0f);
  166. renderer::end();
  167. int16 xr, yr;
  168. app::timings time = app::time();
  169. app::input::get_cursor_rel(&xr, &yr);
  170. if( app::input::get_button_state(2) == app::input::DOWN || app::input::get_button_state(2) == app::input::PRESSED )
  171. camera_rotate(camera, xr * time.dt);
  172. if( app::input::get_key_state(0x21) == app::input::DOWN || app::input::get_key_state(0x21) == app::input::PRESSED )
  173. camera.phi += time.dt;
  174. if( app::input::get_key_state('R') == app::input::DOWN || app::input::get_key_state('R') == app::input::PRESSED )
  175. camera.phi += time.dt;
  176. if( app::input::get_key_state(0x22) == app::input::DOWN || app::input::get_key_state(0x22) == app::input::PRESSED )
  177. camera.phi -= time.dt;
  178. if( app::input::get_key_state('F') == app::input::DOWN || app::input::get_key_state('F') == app::input::PRESSED )
  179. camera.phi -= time.dt;
  180. int wr = app::input::get_wheel_rel();
  181. camera_zoom(camera, -(float)wr * time.dt * 5.0f);
  182. float dx = (float)(app::input::get_key_state('A') > 0) - (float)(app::input::get_key_state('D') > 0);
  183. float dz = (float)(app::input::get_key_state('S') > 0) - (float)(app::input::get_key_state('W') > 0);
  184. camera_move(camera, dx * time.dt * 200.0f, dz * time.dt * 200.0f);
  185. }
  186. }
  187. //-----------------------------------------------------------------------------