mesh_node.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "mesh_skel_ctrl.h"
  9. #include "skel_anim_ctrl.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. // sanity checks
  18. if( material->attrib_locs.tex_coords != -1 && mesh->vbos.tex_coords.GetGLID() == 0 )
  19. {
  20. ERROR( "The shader program needs information that the mesh do not have" );
  21. }
  22. }
  23. //=====================================================================================================================================
  24. // Deinit =
  25. //=====================================================================================================================================
  26. void mesh_node_t::Deinit()
  27. {
  28. rsrc::meshes.Unload( mesh );
  29. rsrc::materials.Unload( material );
  30. }
  31. //=====================================================================================================================================
  32. // Render =
  33. //=====================================================================================================================================
  34. /// Called in material or blending stages
  35. void mesh_node_t::Render( material_t* mtl ) const
  36. {
  37. glPushMatrix();
  38. r::MultMatrix( transformation_wspace );
  39. // if we have skeleton controller
  40. if( mesh_skel_ctrl )
  41. {
  42. // first the uniforms
  43. glUniformMatrix3fv( mtl->uni_locs.skinning_rotations, mesh_skel_ctrl->skel_node->skeleton->bones.size(), 1,
  44. &(mesh_skel_ctrl->skel_node->skel_anim_ctrl->bone_rotations[0])[0] );
  45. glUniform3fv( mtl->uni_locs.skinning_translations, mesh_skel_ctrl->skel_node->skeleton->bones.size(),
  46. &(mesh_skel_ctrl->skel_node->skel_anim_ctrl->bone_translations[0])[0] );
  47. // then the attributes
  48. DEBUG_ERR( !mtl->HasHWSkinning() );
  49. mesh->vbos.vert_weights.Bind();
  50. glEnableVertexAttribArray( mtl->attrib_locs.vert_weight_bones_num );
  51. glVertexAttribPointer( mtl->attrib_locs.vert_weight_bones_num, 1, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(0) );
  52. glEnableVertexAttribArray( mtl->attrib_locs.vert_weight_bone_ids );
  53. glVertexAttribPointer( mtl->attrib_locs.vert_weight_bone_ids, 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(4) );
  54. glEnableVertexAttribArray( mtl->attrib_locs.vert_weight_weights );
  55. glVertexAttribPointer( mtl->attrib_locs.vert_weight_weights, 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(20) );
  56. }
  57. if( mtl->attrib_locs.position != -1 )
  58. {
  59. mesh->vbos.vert_coords.Bind();
  60. glVertexAttribPointer( mtl->attrib_locs.position, 3, GL_FLOAT, false, 0, NULL );
  61. glEnableVertexAttribArray( mtl->attrib_locs.position );
  62. }
  63. if( mtl->attrib_locs.normal != -1 )
  64. {
  65. mesh->vbos.vert_normals.Bind();
  66. glVertexAttribPointer( mtl->attrib_locs.normal, 3, GL_FLOAT, false, 0, NULL );
  67. glEnableVertexAttribArray( mtl->attrib_locs.normal );
  68. }
  69. if( mtl->attrib_locs.tex_coords != -1 )
  70. {
  71. mesh->vbos.tex_coords.Bind();
  72. glVertexAttribPointer( mtl->attrib_locs.tex_coords, 2, GL_FLOAT, false, 0, NULL );
  73. glEnableVertexAttribArray( mtl->attrib_locs.tex_coords );
  74. }
  75. if( mtl->attrib_locs.tanget != -1 )
  76. {
  77. mesh->vbos.vert_tangents.Bind();
  78. glVertexAttribPointer( mtl->attrib_locs.tanget, 4, GL_FLOAT, false, 0, NULL );
  79. glEnableVertexAttribArray( mtl->attrib_locs.tanget );
  80. }
  81. mesh->vbos.vert_indeces.Bind();
  82. glDrawElements( GL_TRIANGLES, mesh->vert_indeces.size(), GL_UNSIGNED_SHORT, 0 );
  83. // disable
  84. if( mtl->attrib_locs.position != -1 ) glDisableVertexAttribArray( mtl->attrib_locs.position );
  85. if( mtl->attrib_locs.normal != -1 ) glDisableVertexAttribArray( mtl->attrib_locs.normal );
  86. if( mtl->attrib_locs.tex_coords != -1 ) glDisableVertexAttribArray( mtl->attrib_locs.tex_coords );
  87. if( mtl->attrib_locs.tanget != -1 ) glDisableVertexAttribArray( mtl->attrib_locs.tanget );
  88. if( mesh_skel_ctrl )
  89. {
  90. glDisableVertexAttribArray( mtl->attrib_locs.vert_weight_bones_num );
  91. glDisableVertexAttribArray( mtl->attrib_locs.vert_weight_bone_ids );
  92. glDisableVertexAttribArray( mtl->attrib_locs.vert_weight_weights );
  93. }
  94. vbo_t::UnbindAllTargets();
  95. glPopMatrix();
  96. }