mesh_node.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /// Called in material or blending stages
  30. void mesh_node_t::Render()
  31. {
  32. const material_t* mtl = material;
  33. glPushMatrix();
  34. r::MultMatrix( transformation_wspace );
  35. // if we have skeleton controller
  36. if( skel_controller )
  37. {
  38. // first the uniforms
  39. glUniformMatrix3fv( mtl->uni_locs.skinning_rotations, skel_controller->skel_node->skeleton->bones.size(), 1,
  40. &(skel_controller->skel_node->skel_anim_controller->bone_rotations[0])[0] );
  41. glUniform3fv( mtl->uni_locs.skinning_translations, skel_controller->skel_node->skeleton->bones.size(),
  42. &(skel_controller->skel_node->skel_anim_controller->bone_translations[0])[0] );
  43. // then the attributes
  44. DEBUG_ERR( !mtl->HasHWSkinning() );
  45. mesh->vbos.vert_weights.Bind();
  46. glEnableVertexAttribArray( mtl->attrib_locs.vert_weight_bones_num );
  47. glVertexAttribPointer( mtl->attrib_locs.vert_weight_bones_num, 1, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(0) );
  48. glEnableVertexAttribArray( mtl->attrib_locs.vert_weight_bone_ids );
  49. glVertexAttribPointer( mtl->attrib_locs.vert_weight_bone_ids, 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(4) );
  50. glEnableVertexAttribArray( mtl->attrib_locs.vert_weight_weights );
  51. glVertexAttribPointer( mtl->attrib_locs.vert_weight_weights, 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(20) );
  52. }
  53. if( mtl->attrib_locs.position != -1 )
  54. {
  55. mesh->vbos.vert_coords.Bind();
  56. glVertexAttribPointer( mtl->attrib_locs.position, 3, GL_FLOAT, false, 0, NULL );
  57. glEnableVertexAttribArray( mtl->attrib_locs.position );
  58. }
  59. if( mtl->attrib_locs.normal != -1 )
  60. {
  61. mesh->vbos.vert_normals.Bind();
  62. glVertexAttribPointer( mtl->attrib_locs.normal, 3, GL_FLOAT, false, 0, NULL );
  63. glEnableVertexAttribArray( mtl->attrib_locs.normal );
  64. }
  65. if( mtl->attrib_locs.tex_coords != -1 )
  66. {
  67. mesh->vbos.tex_coords.Bind();
  68. glVertexAttribPointer( mtl->attrib_locs.tex_coords, 2, GL_FLOAT, false, 0, NULL );
  69. glEnableVertexAttribArray( mtl->attrib_locs.tex_coords );
  70. }
  71. if( mtl->attrib_locs.tanget != -1 )
  72. {
  73. mesh->vbos.vert_tangents.Bind();
  74. glVertexAttribPointer( mtl->attrib_locs.tanget, 4, GL_FLOAT, false, 0, NULL );
  75. glEnableVertexAttribArray( mtl->attrib_locs.tanget );
  76. }
  77. mesh->vbos.vert_indeces.Bind();
  78. glDrawElements( GL_TRIANGLES, mesh->vert_indeces.size(), GL_UNSIGNED_SHORT, 0 );
  79. // disable
  80. if( mtl->attrib_locs.position != -1 ) glDisableVertexAttribArray( mtl->attrib_locs.position );
  81. if( mtl->attrib_locs.normal != -1 ) glDisableVertexAttribArray( mtl->attrib_locs.normal );
  82. if( mtl->attrib_locs.tex_coords != -1 ) glDisableVertexAttribArray( mtl->attrib_locs.tex_coords );
  83. if( mtl->attrib_locs.tanget != -1 ) glDisableVertexAttribArray( mtl->attrib_locs.tanget );
  84. if( skel_controller )
  85. {
  86. glDisableVertexAttribArray( mtl->attrib_locs.vert_weight_bones_num );
  87. glDisableVertexAttribArray( mtl->attrib_locs.vert_weight_bone_ids );
  88. glDisableVertexAttribArray( mtl->attrib_locs.vert_weight_weights );
  89. }
  90. vbo_t::UnbindAllTargets();
  91. glPopMatrix();
  92. }
  93. //=====================================================================================================================================
  94. // RenderDepth =
  95. //=====================================================================================================================================
  96. void mesh_node_t::RenderDepth()
  97. {
  98. glPushMatrix();
  99. r::MultMatrix( transformation_wspace );
  100. mesh->vbos.vert_coords.Bind();
  101. glVertexPointer( 3, GL_FLOAT, 0, NULL );
  102. if( material->grass_map )
  103. {
  104. mesh->vbos.tex_coords.Bind();
  105. glTexCoordPointer( 2, GL_FLOAT, 0, NULL );
  106. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  107. }
  108. // if we have skeleton controller
  109. if( skel_controller )
  110. {
  111. // first the uniforms
  112. glUniformMatrix3fv( material->uni_locs.skinning_rotations, skel_controller->skel_node->skeleton->bones.size(), 1,
  113. &(skel_controller->skel_node->skel_anim_controller->bone_rotations[0])[0] );
  114. glUniform3fv( material->uni_locs.skinning_translations, skel_controller->skel_node->skeleton->bones.size(),
  115. &(skel_controller->skel_node->skel_anim_controller->bone_translations[0])[0] );
  116. // then the attributes
  117. DEBUG_ERR( material->attrib_locs.vert_weight_bones_num == -1 );
  118. mesh->vbos.vert_weights.Bind();
  119. glEnableVertexAttribArray( r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(0) );
  120. glVertexAttribPointer( r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(0), 1, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(0) );
  121. glEnableVertexAttribArray(r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(1) );
  122. glVertexAttribPointer( r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(1), 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(4) );
  123. glEnableVertexAttribArray( r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(2) );
  124. glVertexAttribPointer( r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(2), 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(20) );
  125. }
  126. mesh->vbos.vert_indeces.Bind();
  127. glEnableClientState( GL_VERTEX_ARRAY );
  128. glDrawElements( GL_TRIANGLES, mesh->vert_indeces.size(), GL_UNSIGNED_SHORT, 0 );
  129. // disable
  130. glDisableClientState( GL_VERTEX_ARRAY );
  131. glDisableClientState( GL_TEXTURE_COORD_ARRAY );
  132. if( skel_controller )
  133. {
  134. glDisableVertexAttribArray( material->attrib_locs.vert_weight_bones_num );
  135. glDisableVertexAttribArray( material->attrib_locs.vert_weight_bone_ids );
  136. glDisableVertexAttribArray( material->attrib_locs.vert_weight_weights );
  137. }
  138. vbo_t::UnbindAllTargets();
  139. glPopMatrix();
  140. }