MeshNode.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "MeshNode.h"
  2. #include "Resource.h"
  3. #include "Mesh.h"
  4. #include "Renderer.h"
  5. #include "Material.h"
  6. #include "SkelNode.h"
  7. #include "Skeleton.h"
  8. #include "MeshSkelNodeCtrl.h"
  9. #include "SkelAnimCtrl.h"
  10. //=====================================================================================================================================
  11. // init =
  12. //=====================================================================================================================================
  13. void MeshNode::init( const char* filename )
  14. {
  15. mesh = Rsrc::meshes.load( filename );
  16. material = Rsrc::materials.load( mesh->materialName.c_str() );
  17. // sanity checks
  18. if( material->attribLocs.texCoords != -1 && mesh->vbos.texCoords.getGlId() == 0 )
  19. {
  20. ERROR( "The shader program needs information that the mesh do not have" );
  21. }
  22. }
  23. //=====================================================================================================================================
  24. // deinit =
  25. //=====================================================================================================================================
  26. void MeshNode::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 MeshNode::render( Material* mtl ) const
  36. {
  37. glPushMatrix();
  38. R::multMatrix( transformationWspace );
  39. // if we have skeleton controller
  40. if( meshSkelCtrl )
  41. {
  42. // first the uniforms
  43. glUniformMatrix3fv( mtl->uniLocs.skinningRotations, meshSkelCtrl->skelNode->skeleton->bones.size(), 1,
  44. &(meshSkelCtrl->skelNode->skelAnimCtrl->boneRotations[0])[0] );
  45. glUniform3fv( mtl->uniLocs.skinningTranslations, meshSkelCtrl->skelNode->skeleton->bones.size(),
  46. &(meshSkelCtrl->skelNode->skelAnimCtrl->boneTranslations[0])[0] );
  47. // then the attributes
  48. DEBUG_ERR( !mtl->hasHWSkinning() ); // it has skel controller but no skinning
  49. mesh->vbos.vertWeights.bind();
  50. glEnableVertexAttribArray( mtl->attribLocs.vertWeightBonesNum );
  51. glVertexAttribPointer( mtl->attribLocs.vertWeightBonesNum, 1, GL_FLOAT, GL_FALSE, sizeof(Mesh::VertexWeight), BUFFER_OFFSET(0) );
  52. glEnableVertexAttribArray( mtl->attribLocs.vertWeightBoneIds );
  53. glVertexAttribPointer( mtl->attribLocs.vertWeightBoneIds, 4, GL_FLOAT, GL_FALSE, sizeof(Mesh::VertexWeight), BUFFER_OFFSET(4) );
  54. glEnableVertexAttribArray( mtl->attribLocs.vertWeightWeights );
  55. glVertexAttribPointer( mtl->attribLocs.vertWeightWeights, 4, GL_FLOAT, GL_FALSE, sizeof(Mesh::VertexWeight), BUFFER_OFFSET(20) );
  56. }
  57. if( mtl->attribLocs.position != -1 )
  58. {
  59. mesh->vbos.vertCoords.bind();
  60. glVertexAttribPointer( mtl->attribLocs.position, 3, GL_FLOAT, false, 0, NULL );
  61. glEnableVertexAttribArray( mtl->attribLocs.position );
  62. }
  63. if( mtl->attribLocs.normal != -1 )
  64. {
  65. mesh->vbos.vertNormals.bind();
  66. glVertexAttribPointer( mtl->attribLocs.normal, 3, GL_FLOAT, false, 0, NULL );
  67. glEnableVertexAttribArray( mtl->attribLocs.normal );
  68. }
  69. if( mtl->attribLocs.texCoords != -1 )
  70. {
  71. mesh->vbos.texCoords.bind();
  72. glVertexAttribPointer( mtl->attribLocs.texCoords, 2, GL_FLOAT, false, 0, NULL );
  73. glEnableVertexAttribArray( mtl->attribLocs.texCoords );
  74. }
  75. if( mtl->attribLocs.tanget != -1 )
  76. {
  77. mesh->vbos.vertTangents.bind();
  78. glVertexAttribPointer( mtl->attribLocs.tanget, 4, GL_FLOAT, false, 0, NULL );
  79. glEnableVertexAttribArray( mtl->attribLocs.tanget );
  80. }
  81. mesh->vbos.vertIndeces.bind();
  82. glDrawElements( GL_TRIANGLES, mesh->vertIndeces.size(), GL_UNSIGNED_SHORT, 0 );
  83. // disable
  84. if( mtl->attribLocs.position != -1 ) glDisableVertexAttribArray( mtl->attribLocs.position );
  85. if( mtl->attribLocs.normal != -1 ) glDisableVertexAttribArray( mtl->attribLocs.normal );
  86. if( mtl->attribLocs.texCoords != -1 ) glDisableVertexAttribArray( mtl->attribLocs.texCoords );
  87. if( mtl->attribLocs.tanget != -1 ) glDisableVertexAttribArray( mtl->attribLocs.tanget );
  88. if( meshSkelCtrl )
  89. {
  90. glDisableVertexAttribArray( mtl->attribLocs.vertWeightBonesNum );
  91. glDisableVertexAttribArray( mtl->attribLocs.vertWeightBoneIds );
  92. glDisableVertexAttribArray( mtl->attribLocs.vertWeightWeights );
  93. }
  94. Vbo::unbindAllTargets();
  95. glPopMatrix();
  96. }