spritemodel.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. Copyright (C) 2002 Dominic Clifton.
  3. This file is part of GtkRadiant.
  4. GtkRadiant is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. GtkRadiant is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GtkRadiant; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. //
  17. // Sprite Model Plugin
  18. //
  19. // Code by Hydra aka Dominic Clifton
  20. //
  21. // Based on MD3Model source code by SPoG
  22. //
  23. #include "spritemodel.h"
  24. void LoadSpriteModel(entity_interfaces_t *interfaces, const char *name)
  25. {
  26. IShader *pShader;
  27. pShader = QERApp_Shader_ForName(name);
  28. if (!pShader)
  29. {
  30. Sys_Printf("ERROR: can't find shader (or image) for: %s\n", name );
  31. return;// NULL;
  32. }
  33. CSpriteModel *model = new CSpriteModel();
  34. model->Construct(pShader);
  35. interfaces->pRender = (IRender*)model;
  36. interfaces->pRender->IncRef();
  37. //interfaces->pSelect = (ISelect*)model;
  38. //interfaces->pSelect->IncRef();
  39. interfaces->pSelect = NULL;
  40. interfaces->pEdit = NULL;
  41. model->DecRef();
  42. }
  43. void CSpriteModel::Construct(IShader *pShader)
  44. {
  45. m_pShader = pShader;
  46. aabb_clear(&m_BBox);
  47. /*
  48. md3Surface_t *pSurface = (md3Surface_t *)(((unsigned char *)pHeader) + pHeader->ofsSurfaces);
  49. m_nSurfaces = pHeader->numSurfaces;
  50. CMD3Surface* surfaces = new CMD3Surface[m_nSurfaces];
  51. for (int i = 0; i < m_nSurfaces; i++ )
  52. {
  53. surfaces[i].Construct(pSurface);
  54. pSurface = (md3Surface_t *) ((( char * ) pSurface) + pSurface->ofsEnd);
  55. }
  56. m_children = surfaces;
  57. AccumulateBBox();
  58. */
  59. }
  60. CSpriteModel::CSpriteModel()
  61. {
  62. refCount = 1;
  63. //m_nSurfaces = 0;
  64. //m_children = NULL;
  65. m_pShader = NULL;
  66. }
  67. CSpriteModel::~CSpriteModel()
  68. {
  69. // if(m_children) delete[] m_children;
  70. if (m_pShader)
  71. m_pShader->DecRef();
  72. }
  73. void CSpriteModel::Draw(int state, int rflags) const
  74. {
  75. /*
  76. // Draw a point in the middle of the bbox
  77. vec3_t middle = {0,0,0};
  78. g_QglTable.m_pfn_qglPointSize (4);
  79. g_QglTable.m_pfn_qglColor3f (0,1,0);
  80. g_QglTable.m_pfn_qglBegin (GL_POINTS);
  81. g_QglTable.m_pfn_qglVertex3fv (middle);
  82. g_QglTable.m_pfn_qglEnd ();
  83. */
  84. qtexture_t *q = m_pShader->getTexture();
  85. // convert pixels to units and divide in half again so we draw in the middle
  86. // of the bbox.
  87. int h = q->height / 8;
  88. int w = q->width / 8;
  89. // setup opengl stuff
  90. g_QglTable.m_pfn_qglPushAttrib (GL_ALL_ATTRIB_BITS); // GL_ENABLE_BIT
  91. //g_QglTable.m_pfn_qglColor3f (1,1,1); //testing
  92. //g_QglTable.m_pfn_qglColor4f (1,1,1,1); //testing
  93. g_QglTable.m_pfn_qglBindTexture (GL_TEXTURE_2D, q->texture_number);
  94. //g_QglTable.m_pfn_qglEnable (GL_TEXTURE_2D); // FIXME: ? this forces textures, even in wireframe mode, bad... ?
  95. g_QglTable.m_pfn_qglAlphaFunc (GL_LESS, 1);
  96. g_QglTable.m_pfn_qglEnable (GL_ALPHA_TEST);
  97. // get rid of this when sprite always faces camera
  98. g_QglTable.m_pfn_qglDisable(GL_CULL_FACE);
  99. g_QglTable.m_pfn_qglPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  100. // draw the sprite
  101. #if 0
  102. // using x/y axis, it appears FLAT without the proper transform and rotation.
  103. g_QglTable.m_pfn_qglBegin(GL_QUADS);
  104. g_QglTable.m_pfn_qglTexCoord2f (0,0);
  105. g_QglTable.m_pfn_qglVertex3f (0-w,0-h, 0);
  106. g_QglTable.m_pfn_qglTexCoord2f (1,0);
  107. g_QglTable.m_pfn_qglVertex3f ( w,0-h, 0);
  108. g_QglTable.m_pfn_qglTexCoord2f (1,1);
  109. g_QglTable.m_pfn_qglVertex3f ( w, h, 0);
  110. g_QglTable.m_pfn_qglTexCoord2f (0,1);
  111. g_QglTable.m_pfn_qglVertex3f (0-w, h, 0);
  112. g_QglTable.m_pfn_qglEnd ();
  113. #else
  114. // so draw it using y/z instead.
  115. g_QglTable.m_pfn_qglBegin(GL_QUADS);
  116. g_QglTable.m_pfn_qglTexCoord2f(0.0f, 0.0f);
  117. g_QglTable.m_pfn_qglVertex3f(0.0f, static_cast<float>(w), static_cast<float>(h));
  118. g_QglTable.m_pfn_qglTexCoord2f(1.0f, 0.0f);
  119. g_QglTable.m_pfn_qglVertex3f(0.0f, 0.0f - static_cast<float>(w), static_cast<float>(h));
  120. g_QglTable.m_pfn_qglTexCoord2f(1.0f, 1.0f);
  121. g_QglTable.m_pfn_qglVertex3f(0.0f, 0.0f - static_cast<float>(w), 0.0f - static_cast<float>(h));
  122. g_QglTable.m_pfn_qglTexCoord2f(0.0f, 0.0f);
  123. g_QglTable.m_pfn_qglVertex3f(0.0f, static_cast<float>(w), 0.0f - static_cast<float>(h));
  124. g_QglTable.m_pfn_qglEnd ();
  125. #endif
  126. g_QglTable.m_pfn_qglBindTexture (GL_TEXTURE_2D, 0);
  127. g_QglTable.m_pfn_qglPopAttrib();
  128. }
  129. /*
  130. bool CSpriteModel::TestRay(const ray_t *ray, vec_t *dist) const
  131. {
  132. vec_t depth_start = *dist;
  133. vec_t depth_local = *dist;
  134. if (aabb_test_ray(&m_BBox, ray) == 0)
  135. return false;
  136. for(int i=0; i<m_nSurfaces; i++)
  137. {
  138. if(m_children[i].TestRay(ray, &depth_local))
  139. {
  140. if (depth_local < *dist) *dist = depth_local;
  141. }
  142. }
  143. return *dist < depth_start;
  144. }
  145. */