PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/SampleTOAPIA/Gears/Gears.cs

http://github.com/Wiladams/NewTOAPIA
C# | 212 lines | 156 code | 34 blank | 22 comment | 6 complexity | 5e3d4b7c5b57411c44fbb89fee5c0f3a MD5 | raw file
  1. using System;
  2. using TOAPI.OpenGL;
  3. using NewTOAPIA.Drawing;
  4. using NewTOAPIA.Graphics;
  5. using NewTOAPIA.GL;
  6. using NewTOAPIA;
  7. using NewTOAPIA.Modeling;
  8. /*
  9. * 2008: Converted to C# by William Adams (wiladams@microsoft.com)
  10. * 1996: "written" by Danny Sung <dannys@ucla.edu>
  11. * Based on 3-D gear wheels by Brian Paul which is in the public domain.
  12. */
  13. public class WireframeGear : GLRenderable
  14. {
  15. float fInnerRadius;
  16. float fOuterRadius;
  17. float fWidth;
  18. int fTeeth;
  19. float fToothDepth;
  20. float r0, r1, r2;
  21. float angle, da;
  22. ColorRGBA fColor;
  23. GraphicsInterface fGI;
  24. GLDisplayList fDisplayList;
  25. /*-
  26. * Draw a gear wheel. You'll probably want to call this function when
  27. * building a display list since we do a lot of trig here.
  28. *
  29. * Input: inner_radius - radius of hole at center
  30. * outer_radius - radius at center of teeth
  31. * width - width of gear
  32. * teeth - number of teeth
  33. * tooth_depth - depth of tooth
  34. * wire - true for wireframe mode
  35. */
  36. public WireframeGear(GraphicsInterface gi, float innerRadius, float outerRadius, float width, int teeth, float toothdepth)
  37. {
  38. fGI = gi;
  39. fDisplayList = new GLDisplayList(gi);
  40. fInnerRadius = innerRadius;
  41. fOuterRadius = outerRadius;
  42. fWidth = width;
  43. fTeeth = teeth;
  44. fToothDepth = toothdepth;
  45. r0 = fInnerRadius;
  46. r1 = fOuterRadius - fToothDepth / 2.0f;
  47. r2 = fOuterRadius + fToothDepth / 2.0f;
  48. da = 2.0f * (float)Math.PI / fTeeth / 4.0f;
  49. fDisplayList.BeginCompile();
  50. RenderContent(fGI);
  51. fDisplayList.End();
  52. }
  53. public ColorRGBA Color
  54. {
  55. get { return fColor; }
  56. set
  57. {
  58. fColor = value;
  59. }
  60. }
  61. void RenderFrontFacesOfTeeth(GraphicsInterface gi)
  62. {
  63. // draw front face
  64. for (int i = 0; i <= fTeeth; i++)
  65. {
  66. gi.Drawing.Lines.Begin();
  67. angle = i * 2.0f * (float)Math.PI / fTeeth;
  68. gi.Vertex(r0 * (float)Math.Cos(angle), r0 * (float)Math.Sin(angle), fWidth * 0.5f);
  69. gi.Vertex(r1 * (float)Math.Cos(angle), r1 * (float)Math.Sin(angle), fWidth * 0.5f);
  70. gi.Vertex(r1 * (float)Math.Cos(angle + 3 * da), r1 * (float)Math.Sin(angle + 3 * da), fWidth * 0.5f);
  71. gi.Vertex(r1 * (float)Math.Cos(angle + 4 * da), r1 * (float)Math.Sin(angle + 4 * da), fWidth * 0.5f);
  72. gi.Drawing.Lines.End();
  73. }
  74. }
  75. void RenderBackFacesOfTeeth(GraphicsInterface gi)
  76. {
  77. //// draw back face
  78. for (int i = 0; i <= fTeeth; i++)
  79. {
  80. angle = i * 2.0f * (float)Math.PI / fTeeth;
  81. gl.glBegin(gl.GL_LINES);
  82. gl.glVertex3f(r1 * (float)Math.Cos(angle), r1 * (float)Math.Sin(angle), -fWidth * 0.5f);
  83. gl.glVertex3f(r0 * (float)Math.Cos(angle), r0 * (float)Math.Sin(angle), -fWidth * 0.5f);
  84. gl.glVertex3f(r1 * (float)Math.Cos(angle + 3 * da), r1 * (float)Math.Sin(angle + 3 * da), -fWidth * 0.5f);
  85. gl.glVertex3f(r1 * (float)Math.Cos(angle + 4 * da), r1 * (float)Math.Sin(angle + 4 * da), -fWidth * 0.5f);
  86. gl.glEnd();
  87. }
  88. }
  89. void RenderFrontSidesOfTeeth(GraphicsInterface gi)
  90. {
  91. //// draw front sides of teeth
  92. for (int i = 0; i < fTeeth; i++)
  93. {
  94. angle = i * 2.0f * (float)Math.PI / fTeeth;
  95. gl.glBegin(gl.GL_LINE_LOOP);
  96. gl.glVertex3f(r1 * (float)Math.Cos(angle), r1 * (float)Math.Sin(angle), fWidth * 0.5f);
  97. gl.glVertex3f(r2 * (float)Math.Cos(angle + da), r2 * (float)Math.Sin(angle + da), fWidth * 0.5f);
  98. gl.glVertex3f(r2 * (float)Math.Cos(angle + 2 * da), r2 * (float)Math.Sin(angle + 2 * da), fWidth * 0.5f);
  99. gl.glVertex3f(r1 * (float)Math.Cos(angle + 3 * da), r1 * (float)Math.Sin(angle + 3 * da), fWidth * 0.5f);
  100. gl.glEnd();
  101. }
  102. }
  103. void RenderBackSidesOfTeeth(GraphicsInterface gi)
  104. {
  105. //// draw back sides of teeth
  106. for (int i = 0; i < fTeeth; i++)
  107. {
  108. angle = i * 2.0f * (float)Math.PI / fTeeth;
  109. gl.glBegin(gl.GL_LINE_LOOP);
  110. gl.glVertex3f(r1 * (float)Math.Cos(angle + 3 * da), r1 * (float)Math.Sin(angle + 3 * da), -fWidth * 0.5f);
  111. gl.glVertex3f(r2 * (float)Math.Cos(angle + 2 * da), r2 * (float)Math.Sin(angle + 2 * da), -fWidth * 0.5f);
  112. gl.glVertex3f(r2 * (float)Math.Cos(angle + da), r2 * (float)Math.Sin(angle + da), -fWidth * 0.5f);
  113. gl.glVertex3f(r1 * (float)Math.Cos(angle), r1 * (float)Math.Sin(angle), -fWidth * 0.5f);
  114. gl.glEnd();
  115. }
  116. }
  117. void RenderOutwardFacesOfTeeth(GraphicsInterface gi)
  118. {
  119. float u, v, len;
  120. //// draw outward faces of teeth
  121. for (int i = 0; i < fTeeth; i++)
  122. {
  123. angle = i * 2.0f * (float)Math.PI / fTeeth;
  124. gl.glBegin(gl.GL_LINES);
  125. gl.glVertex3f(r1 * (float)Math.Cos(angle), r1 * (float)Math.Sin(angle), fWidth * 0.5f);
  126. gl.glVertex3f(r1 * (float)Math.Cos(angle), r1 * (float)Math.Sin(angle), -fWidth * 0.5f);
  127. u = r2 * (float)Math.Cos(angle + da) - r1 * (float)Math.Cos(angle);
  128. v = r2 * (float)Math.Sin(angle + da) - r1 * (float)Math.Sin(angle);
  129. len = (float)Math.Sqrt(u * u + v * v);
  130. u /= len;
  131. v /= len;
  132. gl.glNormal3f(v, -u, 0.0f);
  133. gl.glVertex3f(r2 * (float)Math.Cos(angle + da), r2 * (float)Math.Sin(angle + da), fWidth * 0.5f);
  134. gl.glVertex3f(r2 * (float)Math.Cos(angle + da), r2 * (float)Math.Sin(angle + da), -fWidth * 0.5f);
  135. gl.glNormal3f((float)Math.Cos(angle), (float)Math.Sin(angle), 0.0f);
  136. gl.glVertex3f(r2 * (float)Math.Cos(angle + 2 * da), r2 * (float)Math.Sin(angle + 2 * da), fWidth * 0.5f);
  137. gl.glVertex3f(r2 * (float)Math.Cos(angle + 2 * da), r2 * (float)Math.Sin(angle + 2 * da), -fWidth * 0.5f);
  138. u = r1 * (float)Math.Cos(angle + 3 * da) - r2 * (float)Math.Cos(angle + 2 * da);
  139. v = r1 * (float)Math.Sin(angle + 3 * da) - r2 * (float)Math.Sin(angle + 2 * da);
  140. gl.glNormal3f(v, -u, 0.0f);
  141. gl.glVertex3f(r1 * (float)Math.Cos(angle + 3 * da), r1 * (float)Math.Sin(angle + 3 * da), fWidth * 0.5f);
  142. gl.glVertex3f(r1 * (float)Math.Cos(angle + 3 * da), r1 * (float)Math.Sin(angle + 3 * da), -fWidth * 0.5f);
  143. gl.glNormal3f((float)Math.Cos(angle), (float)Math.Sin(angle), 0.0f);
  144. gl.glEnd();
  145. }
  146. }
  147. void RenderInsideRadiusCylinder(GraphicsInterface gi)
  148. {
  149. float angle;
  150. // draw inside radius cylinder
  151. for (int i = 0; i <= fTeeth; i++)
  152. {
  153. angle = i * 2.0f * (float)Math.PI / fTeeth;
  154. gi.Drawing.Lines.Begin();
  155. gi.Normal(-(float)Math.Cos(angle), -(float)Math.Sin(angle), 0.0f);
  156. gi.Vertex(r0 * (float)Math.Cos(angle), r0 * (float)Math.Sin(angle), -fWidth * 0.5f);
  157. gi.Vertex(r0 * (float)Math.Cos(angle), r0 * (float)Math.Sin(angle), fWidth * 0.5f);
  158. gi.Vertex(r0 * (float)Math.Cos(angle), r0 * (float)Math.Sin(angle), -fWidth * 0.5f);
  159. gi.Vertex(r0 * (float)Math.Cos(angle + 4 * da), r0 * (float)Math.Sin(angle + 4 * da), -fWidth * 0.5f);
  160. gi.Vertex(r0 * (float)Math.Cos(angle), r0 * (float)Math.Sin(angle), fWidth * 0.5f);
  161. gi.Vertex(r0 * (float)Math.Cos(angle + 4 * da), r0 * (float)Math.Sin(angle + 4 * da), fWidth * 0.5f);
  162. gi.Drawing.Lines.End();
  163. }
  164. }
  165. protected override void RenderContent(GraphicsInterface gi)
  166. {
  167. RenderFrontFacesOfTeeth(gi);
  168. RenderFrontSidesOfTeeth(gi);
  169. RenderBackFacesOfTeeth(gi);
  170. RenderBackSidesOfTeeth(gi);
  171. RenderOutwardFacesOfTeeth(gi);
  172. RenderInsideRadiusCylinder(gi);
  173. }
  174. public override void Render(GraphicsInterface gi)
  175. {
  176. fDisplayList.Render(gi);
  177. }
  178. }