/src/renderer/visual.cpp

https://bitbucket.org/vivkin/gam3b00bs/ · C++ · 236 lines · 175 code · 47 blank · 14 comment · 11 complexity · 7afde4d616d7bf4267102cb6292b1c0b MD5 · raw file

  1. #include "renderer/visual.h"
  2. #include "common.h"
  3. #include "log.h"
  4. #include "load_data.h"
  5. //-----------------------------------------------------------------------------
  6. #define MAX_VISUALS 1024
  7. //-----------------------------------------------------------------------------
  8. visual_t visuals[MAX_VISUALS] = {0};
  9. uint32 visuals_count = 0;
  10. mesh_h dummy_mesh = 0;
  11. texture_h dummy_texture = 0;
  12. render_op render_list[MAX_VISUALS];
  13. uint32 render_list_size = 0;
  14. //-----------------------------------------------------------------------------
  15. static const char meshes_dir[] = "..\\meshes";
  16. //-----------------------------------------------------------------------------
  17. visual_h visual_create( const char* mesh_name,
  18. const char* texture0_path,
  19. const char* texture1_path )
  20. {
  21. ASSERT(visuals_count < MAX_VISUALS);
  22. visual_h vis = &visuals[visuals_count];
  23. if( mesh_name )
  24. {
  25. vis->mesh = mesh_load(meshes_dir, mesh_name);
  26. if( !vis->mesh )
  27. {
  28. log_write("WARNING: unable to load mesh %s", mesh_name);
  29. vis->mesh = dummy_mesh;
  30. }
  31. vis->v_layout = renderer::get_vertex_layout( vis->mesh->header.vertex_type );
  32. }
  33. if( texture0_path )
  34. {
  35. byte* texture_data;
  36. uint32 texture_data_size;
  37. if( load_data(texture0_path, &texture_data, &texture_data_size) )
  38. {
  39. log_write("WARNING: unable to load texture %s", texture0_path);
  40. vis->texture[0] = dummy_texture;
  41. }
  42. else
  43. {
  44. vis->texture[0] = renderer::texture_create(texture_data, texture_data_size);
  45. if( !vis->texture[0] )
  46. {
  47. log_write("WARNING: unable to create texture %s", texture0_path);
  48. vis->texture[0] = dummy_texture;
  49. }
  50. free(texture_data);
  51. }
  52. }
  53. if( texture1_path )
  54. {
  55. byte* texture_data;
  56. uint32 texture_data_size;
  57. if( load_data(texture1_path, &texture_data, &texture_data_size) )
  58. {
  59. log_write("WARNING: unable to load texture %s", texture1_path);
  60. vis->texture[1] = dummy_texture;
  61. }
  62. else
  63. {
  64. vis->texture[1] = renderer::texture_create(texture_data, texture_data_size);
  65. if( !vis->texture[0] )
  66. {
  67. log_write("WARNING: unable to create texture %s", texture1_path);
  68. vis->texture[1] = dummy_texture;
  69. }
  70. free(texture_data);
  71. }
  72. }
  73. vis->material.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f);
  74. vis->material.Ambient = vis->material.Diffuse = vis->material.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  75. vis->material.Power = 1.0f;
  76. vis->scaleu = 1.0f;
  77. D3DXMatrixIdentity(&vis->transform);
  78. visuals_count++;
  79. return vis;
  80. }
  81. //-----------------------------------------------------------------------------
  82. visual_h visual_create( mesh_h mesh, texture_h tex0, texture_h tex1 )
  83. {
  84. ASSERT(visuals_count < MAX_VISUALS);
  85. visual_h vis = &visuals[visuals_count];
  86. vis->mesh = mesh;
  87. vis->v_layout = renderer::get_vertex_layout( mesh->header.vertex_type );
  88. vis->texture[0] = tex0;
  89. vis->texture[1] = tex1;
  90. vis->material.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f);
  91. vis->material.Ambient = vis->material.Diffuse = vis->material.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  92. vis->material.Power = 1.0f;
  93. vis->scaleu = 1.0f;
  94. D3DXMatrixIdentity(&vis->transform);
  95. visuals_count++;
  96. return vis;
  97. }
  98. //-----------------------------------------------------------------------------
  99. void visual_update_transform( visual_h vis )
  100. {
  101. ASSERT(vis);
  102. D3DXMATRIX translation, rotation, scale;
  103. D3DXMatrixTranslation(&translation, vis->position.x, vis->position.y, vis->position.z );
  104. D3DXMatrixRotationYawPitchRoll(&rotation, vis->yaw, vis->pitch, vis->roll);
  105. D3DXMatrixScaling(&scale, vis->scaleu, vis->scaleu, vis->scaleu);
  106. vis->transform = scale * rotation * translation;
  107. }
  108. //-----------------------------------------------------------------------------
  109. void visual_set_position( visual_h vis, const D3DXVECTOR3& pos )
  110. {
  111. ASSERT(vis);
  112. vis->position = pos;
  113. visual_update_transform(vis);
  114. }
  115. //-----------------------------------------------------------------------------
  116. void visual_set_rotation( visual_h vis, float pitch, float yaw, float roll )
  117. {
  118. ASSERT(vis);
  119. vis->pitch = pitch;
  120. vis->yaw = yaw;
  121. vis->roll = roll;
  122. visual_update_transform(vis);
  123. }
  124. //-----------------------------------------------------------------------------
  125. void visual_set_scaleu( visual_h vis, float scale )
  126. {
  127. ASSERT(vis);
  128. vis->scaleu = scale;
  129. visual_update_transform(vis);
  130. }
  131. //-----------------------------------------------------------------------------
  132. void visual_remove( visual_h vis )
  133. {
  134. ASSERT(vis);
  135. memset( vis, 0, sizeof(visual_t) );
  136. }
  137. //-----------------------------------------------------------------------------
  138. void visual_draw( visual_h vis )
  139. {
  140. ASSERT(vis);
  141. ASSERT(vis->mesh);
  142. render_op rop;
  143. rop.vb = vis->mesh->vb;
  144. rop.vertex_count = vis->mesh->header.vertex_count;
  145. rop.vertex_layout = vis->v_layout;
  146. rop.vertex_size = vis->mesh->header.vertex_size;
  147. rop.ib = vis->mesh->ib;
  148. rop.transformation = vis->transform;
  149. rop.material.stages[0].texture = vis->texture[0];
  150. rop.material.stages[1].texture = vis->texture[1];
  151. rop.material.material = vis->material;
  152. for( uint32 i=0; i<vis->mesh->header.subset_count; i++ )
  153. {
  154. rop.index_count = vis->mesh->subsets[i].index_count;
  155. rop.index_offset = vis->mesh->subsets[i].index_offset;
  156. rop.vertex_offset = vis->mesh->subsets[i].vertex_offset;
  157. renderer::render(rop);
  158. }
  159. }
  160. //-----------------------------------------------------------------------------
  161. void visual_render( visual_h vis )
  162. {
  163. ASSERT(vis);
  164. for( uint32 i=0; i<vis->mesh->header.subset_count; i++ )
  165. {
  166. render_op* rop = &render_list[render_list_size];
  167. rop->vb = vis->mesh->vb;
  168. rop->vertex_count = vis->mesh->header.vertex_count;
  169. rop->vertex_layout = vis->v_layout;
  170. rop->vertex_size = vis->mesh->header.vertex_size;
  171. rop->ib = vis->mesh->ib;
  172. rop->transformation = vis->transform;
  173. rop->material.stages[0].texture = vis->texture[0];
  174. rop->material.stages[1].texture = vis->texture[1];
  175. rop->material.material = vis->material;
  176. rop->index_count = vis->mesh->subsets[i].index_count;
  177. rop->index_offset = vis->mesh->subsets[i].index_offset;
  178. rop->vertex_offset = vis->mesh->subsets[i].vertex_offset;
  179. render_list_size++;
  180. }
  181. }
  182. //-----------------------------------------------------------------------------
  183. void visuals_render_all()
  184. {
  185. for( uint32 i=0; i<render_list_size; i++ )
  186. renderer::render( render_list[i] );
  187. render_list_size = 0;
  188. }
  189. //-----------------------------------------------------------------------------