You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

150 lines
3.7 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #ifdef WIN32
  14. # define _USE_MATH_DEFINES /* for M_PI */
  15. # define WIN32_LEAN_AND_MEAN
  16. # include <windows.h>
  17. #endif
  18. #if defined __APPLE__ && defined __MACH__
  19. # include <OpenGL/gl.h>
  20. #else
  21. # define GL_GLEXT_PROTOTYPES
  22. # include <GL/gl.h>
  23. #endif
  24. #include <cstdio>
  25. #include <cmath>
  26. #include "core.h"
  27. #include "debugsphere.h"
  28. /*
  29. * DebugSphere implementation class
  30. */
  31. class DebugSphereData
  32. {
  33. friend class DebugSphere;
  34. void DrawSphere(int ndiv, GLfloat r)
  35. {
  36. glBegin(GL_TRIANGLES);
  37. for (int i = 0; i < 20; i++)
  38. DrawTriangle(vdata[tindices[i][0]],
  39. vdata[tindices[i][2]],
  40. vdata[tindices[i][1]], ndiv, r);
  41. glEnd();
  42. }
  43. void DrawTriangle(GLfloat const *a, GLfloat const *b, GLfloat const *c,
  44. int div, GLfloat r)
  45. {
  46. if (div <= 0)
  47. {
  48. glNormal3fv(a); glVertex3f(a[0] * r, a[1] * r, a[2] * r);
  49. glNormal3fv(b); glVertex3f(b[0] * r, b[1] * r, b[2] * r);
  50. glNormal3fv(c); glVertex3f(c[0] * r, c[1] * r, c[2] * r);
  51. }
  52. else
  53. {
  54. GLfloat ab[3], ac[3], bc[3];
  55. for (int i = 0; i < 3; i++)
  56. {
  57. ab[i] = (a[i] + b[i]) * 0.5;
  58. ac[i] = (a[i] + c[i]) * 0.5;
  59. bc[i] = (b[i] + c[i]) * 0.5;
  60. }
  61. Normalize(ab); Normalize(ac); Normalize(bc);
  62. DrawTriangle(a, ab, ac, div - 1, r);
  63. DrawTriangle(b, bc, ab, div - 1, r);
  64. DrawTriangle(c, ac, bc, div - 1, r);
  65. DrawTriangle(ab, bc, ac, div - 1, r);
  66. }
  67. }
  68. void Normalize(GLfloat *a)
  69. {
  70. GLfloat d = 1.0f / sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
  71. a[0] *= d; a[1] *= d; a[2] *= d;
  72. }
  73. private:
  74. float time;
  75. static GLfloat const vdata[12][3];
  76. static GLuint const tindices[20][3];
  77. static GLfloat const X, Z;
  78. };
  79. GLfloat const DebugSphereData::X = .525731112119133606;
  80. GLfloat const DebugSphereData::Z = .850650808352039932;
  81. GLfloat const DebugSphereData::vdata[12][3] =
  82. {
  83. {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
  84. {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
  85. {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
  86. };
  87. GLuint const DebugSphereData::tindices[20][3] =
  88. {
  89. {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
  90. {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
  91. {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
  92. {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
  93. };
  94. /*
  95. * Public DebugSphere class
  96. */
  97. DebugSphere::DebugSphere()
  98. : data(new DebugSphereData())
  99. {
  100. data->time = 0.0f;
  101. }
  102. void DebugSphere::TickGame(float deltams)
  103. {
  104. Entity::TickGame(deltams);
  105. data->time += 0.003f * deltams;
  106. while (data->time > 6.0 * M_PI)
  107. data->time -= 6.0 * M_PI;
  108. }
  109. void DebugSphere::TickDraw(float deltams)
  110. {
  111. Entity::TickDraw(deltams);
  112. float a = sinf(data->time);
  113. float b = sinf(data->time * 0.5f);
  114. glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
  115. glBindTexture(GL_TEXTURE_2D, NULL);
  116. glColor4f(1.0f, b, a, 0.1f);
  117. glTranslatef(320.0f, 240.0f, 32.0f);
  118. for (float t = 0.01f; t < 1.0f; t *= 1.1f)
  119. data->DrawSphere(2, t * (60.0f + 40.0f * a));
  120. glTranslatef(-320.0f, -240.0f, -32.0f);
  121. glPopAttrib();
  122. }
  123. DebugSphere::~DebugSphere()
  124. {
  125. delete data;
  126. }