zones.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (C) 2000 Sean Cavanaugh
  2. // This file is licensed under the terms of the Lesser GNU Public License
  3. // (see LPGL.txt, or http://www.gnu.org/copyleft/lesser.txt)
  4. #pragma warning(disable:4018) //amckern - 64bit - '<' Singed/Unsigned Mismatch
  5. #include "vis.h"
  6. void Zones::set(UINT32 zone, const BoundingBox& bounds)
  7. {
  8. if (zone < m_ZoneCount)
  9. {
  10. m_ZoneBounds[zone] = bounds;
  11. }
  12. }
  13. UINT32 Zones::getZoneFromBounds(const BoundingBox& bounds)
  14. {
  15. UINT32 x;
  16. for (x=0; x<m_ZoneCount; x++)
  17. {
  18. if (m_ZoneBounds[x].testSuperset(bounds))
  19. {
  20. return x;
  21. }
  22. }
  23. return 0;
  24. }
  25. UINT32 Zones::getZoneFromWinding(const Winding& winding)
  26. {
  27. UINT32 x;
  28. BoundingBox bounds;
  29. for (x=0; x<winding.m_NumPoints; x++)
  30. {
  31. bounds.add(winding.m_Points[x]);
  32. }
  33. return getZoneFromBounds(bounds);
  34. }
  35. // BORROWED FROM HLRAD
  36. // TODO: Consolite into common sometime
  37. static Winding* WindingFromFace(const dface_t* f)
  38. {
  39. int i;
  40. int se;
  41. dvertex_t* dv;
  42. int v;
  43. Winding* w = new Winding(f->numedges);
  44. for (i = 0; i < f->numedges; i++)
  45. {
  46. se = g_dsurfedges[f->firstedge + i];
  47. if (se < 0)
  48. {
  49. v = g_dedges[-se].v[1];
  50. }
  51. else
  52. {
  53. v = g_dedges[se].v[0];
  54. }
  55. dv = &g_dvertexes[v];
  56. VectorCopy(dv->point, w->m_Points[i]);
  57. }
  58. return w;
  59. }
  60. Zones* MakeZones(void)
  61. {
  62. UINT32 x;
  63. UINT32 func_vis_count = 0;
  64. ParseEntities();
  65. // Note: we arent looping through entities because we only care if it has a winding/bounding box
  66. // First count the number of func_vis's
  67. for (x=0; x<g_nummodels; x++)
  68. {
  69. entity_t* ent = EntityForModel(x);
  70. if (!strcasecmp(ValueForKey(ent, "classname"), "func_vis"))
  71. {
  72. UINT32 value = atoi(ValueForKey(ent, "node"));
  73. if (value)
  74. {
  75. func_vis_count++;
  76. }
  77. else
  78. {
  79. Error("func_vis with no \"node\" id\n");
  80. }
  81. }
  82. }
  83. if (!func_vis_count)
  84. {
  85. return NULL;
  86. }
  87. Zones* zones = new Zones(func_vis_count);
  88. for (x=0; x<g_nummodels; x++)
  89. {
  90. dmodel_t* mod = g_dmodels + x;
  91. entity_t* ent = EntityForModel(x);
  92. if (!strcasecmp(ValueForKey(ent, "classname"), "func_vis"))
  93. {
  94. UINT32 func_vis_id = atoi(ValueForKey(ent, "node"));
  95. {
  96. epair_t* keyvalue;
  97. for (keyvalue = ent->epairs; keyvalue; keyvalue = keyvalue->next)
  98. {
  99. UINT32 other_id = atoi(keyvalue->key);
  100. if (other_id)
  101. {
  102. zones->flag(func_vis_id, other_id);
  103. }
  104. }
  105. }
  106. {
  107. UINT32 j;
  108. BoundingBox bounds;
  109. dface_t* f = g_dfaces + mod->firstface;
  110. for (j = 0; j < mod->numfaces; j++, f++)
  111. {
  112. Winding* w = WindingFromFace(f);
  113. UINT32 k;
  114. for (k = 0; k < w->m_NumPoints; k++)
  115. {
  116. bounds.add(w->m_Points[k]);
  117. }
  118. delete w;
  119. }
  120. zones->set(func_vis_id, bounds);
  121. Log("Adding zone %u : mins(%4.3f %4.3f %4.3f) maxs(%4.3f %4.3f %4.3f)\n", func_vis_id,
  122. bounds.m_Mins[0],bounds.m_Mins[1],bounds.m_Mins[2],
  123. bounds.m_Maxs[0],bounds.m_Maxs[1],bounds.m_Maxs[2]);
  124. }
  125. }
  126. }
  127. return zones;
  128. }