mesh_node.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "mesh_node.h"
  2. #include "resource.h"
  3. #include "mesh.h"
  4. #include "renderer.h"
  5. #include "material.h"
  6. #include "skel_node.h"
  7. #include "skeleton.h"
  8. #include "skel_controller.h"
  9. #include "skel_anim_controller.h"
  10. //=====================================================================================================================================
  11. // Init =
  12. //=====================================================================================================================================
  13. void mesh_node_t::Init( const char* filename )
  14. {
  15. mesh = rsrc::meshes.Load( filename );
  16. material = rsrc::materials.Load( mesh->material_name.c_str() );
  17. }
  18. //=====================================================================================================================================
  19. // Deinit =
  20. //=====================================================================================================================================
  21. void mesh_node_t::Deinit()
  22. {
  23. rsrc::meshes.Unload( mesh );
  24. rsrc::materials.Unload( material );
  25. }
  26. //=====================================================================================================================================
  27. // Render =
  28. //=====================================================================================================================================
  29. void mesh_node_t::Render()
  30. {
  31. glPushMatrix();
  32. r::MultMatrix( transformation_wspace );
  33. // if we have skeleton controller
  34. if( skel_controller )
  35. {
  36. // first the uniforms
  37. glUniformMatrix3fv( material->uniform_locs.skinning_rotations, skel_controller->skel_node->skeleton->bones.size(), 1,
  38. &(skel_controller->skel_node->skel_anim_controller->bone_rotations[0])[0] );
  39. glUniform3fv( material->uniform_locs.skinning_translations, skel_controller->skel_node->skeleton->bones.size(),
  40. &(skel_controller->skel_node->skel_anim_controller->bone_translations[0])[0] );
  41. // then the attributes
  42. DEBUG_ERR( material->attribute_locs.vert_weight_bones_num == -1 );
  43. mesh->vbos.vert_weights.Bind();
  44. glEnableVertexAttribArray( material->attribute_locs.vert_weight_bones_num );
  45. glVertexAttribPointer( material->attribute_locs.vert_weight_bones_num, 1, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(0) );
  46. glEnableVertexAttribArray( material->attribute_locs.vert_weight_bone_ids );
  47. glVertexAttribPointer( material->attribute_locs.vert_weight_bone_ids, 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(4) );
  48. glEnableVertexAttribArray( material->attribute_locs.vert_weight_weights );
  49. glVertexAttribPointer( material->attribute_locs.vert_weight_weights, 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(20) );
  50. }
  51. if( material->attribute_locs.position != -1 )
  52. {
  53. mesh->vbos.vert_coords.Bind();
  54. glVertexAttribPointer( material->attribute_locs.position, 3, GL_FLOAT, false, 0, NULL );
  55. glEnableVertexAttribArray( material->attribute_locs.position );
  56. }
  57. if( material->attribute_locs.normal != -1 )
  58. {
  59. mesh->vbos.vert_normals.Bind();
  60. glVertexAttribPointer( material->attribute_locs.normal, 3, GL_FLOAT, false, 0, NULL );
  61. glEnableVertexAttribArray( material->attribute_locs.normal );
  62. }
  63. if( material->attribute_locs.tex_coords != -1 )
  64. {
  65. mesh->vbos.tex_coords.Bind();
  66. glVertexAttribPointer( material->attribute_locs.tex_coords, 2, GL_FLOAT, false, 0, NULL );
  67. glEnableVertexAttribArray( material->attribute_locs.tex_coords );
  68. }
  69. if( material->attribute_locs.tanget != -1 )
  70. {
  71. mesh->vbos.vert_tangents.Bind();
  72. glVertexAttribPointer( material->attribute_locs.tanget, 4, GL_FLOAT, false, 0, NULL );
  73. glEnableVertexAttribArray( material->attribute_locs.tanget );
  74. }
  75. mesh->vbos.vert_indeces.Bind();
  76. glDrawElements( GL_TRIANGLES, mesh->vert_indeces.size(), GL_UNSIGNED_SHORT, 0 );
  77. // disable
  78. if( material->attribute_locs.position != -1 ) glDisableVertexAttribArray( material->attribute_locs.position );
  79. if( material->attribute_locs.normal != -1 ) glDisableVertexAttribArray( material->attribute_locs.normal );
  80. if( material->attribute_locs.tex_coords != -1 ) glDisableVertexAttribArray( material->attribute_locs.tex_coords );
  81. if( material->attribute_locs.tanget != -1 ) glDisableVertexAttribArray( material->attribute_locs.tanget );
  82. if( skel_controller )
  83. {
  84. glDisableVertexAttribArray( material->attribute_locs.vert_weight_bones_num );
  85. glDisableVertexAttribArray( material->attribute_locs.vert_weight_bone_ids );
  86. glDisableVertexAttribArray( material->attribute_locs.vert_weight_weights );
  87. }
  88. vbo_t::UnbindAllTargets();
  89. glPopMatrix();
  90. }
  91. //=====================================================================================================================================
  92. // RenderDepth =
  93. //=====================================================================================================================================
  94. void mesh_node_t::RenderDepth()
  95. {
  96. glPushMatrix();
  97. r::MultMatrix( transformation_wspace );
  98. mesh->vbos.vert_coords.Bind();
  99. glVertexPointer( 3, GL_FLOAT, 0, NULL );
  100. if( material->grass_map )
  101. {
  102. mesh->vbos.tex_coords.Bind();
  103. glTexCoordPointer( 2, GL_FLOAT, 0, NULL );
  104. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  105. }
  106. // if we have skeleton controller
  107. if( skel_controller )
  108. {
  109. // first the uniforms
  110. glUniformMatrix3fv( material->uniform_locs.skinning_rotations, skel_controller->skel_node->skeleton->bones.size(), 1,
  111. &(skel_controller->skel_node->skel_anim_controller->bone_rotations[0])[0] );
  112. glUniform3fv( material->uniform_locs.skinning_translations, skel_controller->skel_node->skeleton->bones.size(),
  113. &(skel_controller->skel_node->skel_anim_controller->bone_translations[0])[0] );
  114. // then the attributes
  115. DEBUG_ERR( material->attribute_locs.vert_weight_bones_num == -1 );
  116. mesh->vbos.vert_weights.Bind();
  117. glEnableVertexAttribArray( r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(0) );
  118. glVertexAttribPointer( r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(0), 1, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(0) );
  119. glEnableVertexAttribArray(r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(1) );
  120. glVertexAttribPointer( r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(1), 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(4) );
  121. glEnableVertexAttribArray( r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(2) );
  122. glVertexAttribPointer( r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(2), 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(20) );
  123. }
  124. mesh->vbos.vert_indeces.Bind();
  125. glEnableClientState( GL_VERTEX_ARRAY );
  126. glDrawElements( GL_TRIANGLES, mesh->vert_indeces.size(), GL_UNSIGNED_SHORT, 0 );
  127. // disable
  128. glDisableClientState( GL_VERTEX_ARRAY );
  129. glDisableClientState( GL_TEXTURE_COORD_ARRAY );
  130. if( skel_controller )
  131. {
  132. glDisableVertexAttribArray( material->attribute_locs.vert_weight_bones_num );
  133. glDisableVertexAttribArray( material->attribute_locs.vert_weight_bone_ids );
  134. glDisableVertexAttribArray( material->attribute_locs.vert_weight_weights );
  135. }
  136. vbo_t::UnbindAllTargets();
  137. glPopMatrix();
  138. }