PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/taoframework-2.1.0/source/examples/Redbook/Select.cs

#
C# | 294 lines | 160 code | 20 blank | 114 comment | 2 complexity | 36c993bdaaf23ef39fb957fac80503fc MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, GPL-2.0
  1. #region License
  2. /*
  3. MIT License
  4. Copyright Š2003-2005 Tao Framework Team
  5. http://www.taoframework.com
  6. All rights reserved.
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. */
  23. #endregion License
  24. #region Original Credits / License
  25. /*
  26. * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  27. * ALL RIGHTS RESERVED
  28. * Permission to use, copy, modify, and distribute this software for
  29. * any purpose and without fee is hereby granted, provided that the above
  30. * copyright notice appear in all copies and that both the copyright notice
  31. * and this permission notice appear in supporting documentation, and that
  32. * the name of Silicon Graphics, Inc. not be used in advertising
  33. * or publicity pertaining to distribution of the software without specific,
  34. * written prior permission.
  35. *
  36. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  37. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  38. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  39. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  40. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  41. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  42. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  43. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  44. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  45. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  46. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  47. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  48. *
  49. * US Government Users Restricted Rights
  50. * Use, duplication, or disclosure by the Government is subject to
  51. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  52. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  53. * clause at DFARS 252.227-7013 and/or in similar or successor
  54. * clauses in the FAR or the DOD or NASA FAR Supplement.
  55. * Unpublished-- rights reserved under the copyright laws of the
  56. * United States. Contractor/manufacturer is Silicon Graphics,
  57. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  58. *
  59. * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  60. */
  61. #endregion Original Credits / License
  62. using System;
  63. using Tao.FreeGlut;
  64. using Tao.OpenGl;
  65. namespace Redbook {
  66. #region Class Documentation
  67. /// <summary>
  68. /// This is an illustration of the selection mode and name stack, which detects
  69. /// whether objects which collide with a viewing volume. First, four triangles and
  70. /// a rectangular box representing a viewing volume are drawn (drawScene routine).
  71. /// The green triangle and yellow triangles appear to lie within the viewing volume,
  72. /// but the red triangle appears to lie outside it. Then the selection mode is
  73. /// entered (SelectObjects routine). Drawing to the screen ceases. To see if any
  74. /// collisions occur, the four triangles are called. In this example, the green
  75. /// triangle causes one hit with the name 1, and the yellow triangles cause one hit
  76. /// with the name 3.
  77. /// </summary>
  78. /// <remarks>
  79. /// <para>
  80. /// Original Author: Silicon Graphics, Inc.
  81. /// http://www.opengl.org/developers/code/examples/redbook/select.c
  82. /// </para>
  83. /// <para>
  84. /// C# Implementation: Randy Ridge
  85. /// http://www.taoframework.com
  86. /// </para>
  87. /// </remarks>
  88. #endregion Class Documentation
  89. public sealed class Select {
  90. // --- Fields ---
  91. #region Private Constants
  92. private const int BUFSIZE = 512;
  93. #endregion Private Constants
  94. // --- Entry Point ---
  95. #region Run()
  96. [STAThread]
  97. public static void Run() {
  98. Glut.glutInit();
  99. Glut.glutInitDisplayMode(Glut.GLUT_SINGLE | Glut.GLUT_RGB | Glut.GLUT_DEPTH);
  100. Glut.glutInitWindowSize(200, 200);
  101. Glut.glutInitWindowPosition(100, 100);
  102. Glut.glutCreateWindow("Select");
  103. Init();
  104. Glut.glutDisplayFunc(new Glut.DisplayCallback(Display));
  105. Glut.glutKeyboardFunc(new Glut.KeyboardCallback(Keyboard));
  106. Glut.glutMainLoop();
  107. }
  108. #endregion Run()
  109. // --- Application Methods ---
  110. #region Init()
  111. private static void Init() {
  112. Gl.glEnable(Gl.GL_DEPTH_TEST);
  113. Gl.glShadeModel(Gl.GL_FLAT);
  114. }
  115. #endregion Init()
  116. #region DrawScene()
  117. /// <summary>
  118. /// <para>
  119. /// Draws 4 triangles and a wire frame which represents the viewing volume.
  120. /// </para>
  121. /// </summary>
  122. private static void DrawScene() {
  123. Gl.glMatrixMode(Gl.GL_PROJECTION);
  124. Gl.glLoadIdentity();
  125. Glu.gluPerspective(40.0, 4.0 / 3.0, 1.0, 100.0);
  126. Gl.glMatrixMode(Gl.GL_MODELVIEW);
  127. Gl.glLoadIdentity();
  128. Glu.gluLookAt(7.5, 7.5, 12.5, 2.5, 2.5, -5.0, 0.0, 1.0, 0.0);
  129. Gl.glColor3f(0.0f, 1.0f, 0.0f); // green triangle
  130. DrawTriangle(2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, -5.0f);
  131. Gl.glColor3f(1.0f, 0.0f, 0.0f); // red triangle
  132. DrawTriangle(2.0f, 7.0f, 3.0f, 7.0f, 2.5f, 8.0f, -5.0f);
  133. Gl.glColor3f(1.0f, 1.0f, 0.0f); // yellow triangles
  134. DrawTriangle(2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, 0.0f);
  135. DrawTriangle(2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, -10.0f);
  136. DrawViewVolume(0.0f, 5.0f, 0.0f, 5.0f, 0.0f, 10.0f);
  137. }
  138. #endregion DrawScene()
  139. #region DrawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, float z)
  140. /// <summary>
  141. /// <para>
  142. /// Draw a triangle with vertices at (x1, y1), (x2, y2), and (x3, y3) at z units
  143. /// away from the origin
  144. /// </para>
  145. /// </summary>
  146. private static void DrawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, float z) {
  147. Gl.glBegin(Gl.GL_TRIANGLES);
  148. Gl.glVertex3f(x1, y1, z);
  149. Gl.glVertex3f(x2, y2, z);
  150. Gl.glVertex3f(x3, y3, z);
  151. Gl.glEnd();
  152. }
  153. #endregion DrawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, float z)
  154. #region DrawViewVolume(float x1, float x2, float y1, float y2, float z1, float z2)
  155. /// <summary>
  156. /// <para>
  157. /// Draws a rectangular box with these outer x, y, and z values.
  158. /// </para>
  159. /// </summary>
  160. private static void DrawViewVolume(float x1, float x2, float y1, float y2, float z1, float z2) {
  161. Gl.glColor3f(1.0f, 1.0f, 1.0f);
  162. Gl.glBegin(Gl.GL_LINE_LOOP);
  163. Gl.glVertex3f(x1, y1, -z1);
  164. Gl.glVertex3f(x2, y1, -z1);
  165. Gl.glVertex3f(x2, y2, -z1);
  166. Gl.glVertex3f(x1, y2, -z1);
  167. Gl.glEnd();
  168. Gl.glBegin(Gl.GL_LINE_LOOP);
  169. Gl.glVertex3f(x1, y1, -z2);
  170. Gl.glVertex3f(x2, y1, -z2);
  171. Gl.glVertex3f(x2, y2, -z2);
  172. Gl.glVertex3f(x1, y2, -z2);
  173. Gl.glEnd();
  174. Gl.glBegin(Gl.GL_LINES); // 4 lines
  175. Gl.glVertex3f(x1, y1, -z1);
  176. Gl.glVertex3f(x1, y1, -z2);
  177. Gl.glVertex3f(x1, y2, -z1);
  178. Gl.glVertex3f(x1, y2, -z2);
  179. Gl.glVertex3f(x2, y1, -z1);
  180. Gl.glVertex3f(x2, y1, -z2);
  181. Gl.glVertex3f(x2, y2, -z1);
  182. Gl.glVertex3f(x2, y2, -z2);
  183. Gl.glEnd();
  184. }
  185. #endregion DrawViewVolume(float x1, float x2, float y1, float y2, float z1, float z2)
  186. #region ProcessHits(int hits, int[] buffer)
  187. /// <summary>
  188. /// <para>
  189. /// ProcessHits prints out the contents of the selection array.
  190. /// </para>
  191. /// </summary>
  192. private static void ProcessHits(int hits, int[] buffer) {
  193. int i, j;
  194. int names;
  195. int[] ptr;
  196. Console.WriteLine("hits = {0}", hits);
  197. ptr = buffer;
  198. for(i = 0; i < hits; i++) { // for each hit
  199. names = ptr[i];
  200. Console.WriteLine(" number of names for hit = {0}", names);
  201. i++;
  202. Console.WriteLine(" z1 is {0}", (float) ptr[i] / 0x7fffffff);
  203. i++;
  204. Console.WriteLine(" z2 is {0}", (float) ptr[i] / 0x7fffffff);
  205. i++;
  206. Console.Write(" the name is ");
  207. for(j = 0; j < names; j++) { // for each name
  208. Console.Write("{0} ", ptr[i]);
  209. i++;
  210. }
  211. Console.WriteLine();
  212. }
  213. Console.WriteLine();
  214. }
  215. #endregion ProcessHits(int hits, int[] buffer)
  216. #region SelectObjects()
  217. /// <summary>
  218. /// <para>
  219. /// SelectObjects "draws" the triangles in selection mode, assigning names for
  220. /// the triangles. Note that the third and fourth triangles share one name, so
  221. /// that if either or both triangles intersects the viewing/clipping volume,
  222. /// only one hit will be registered.
  223. /// </para>
  224. /// </summary>
  225. private static void SelectObjects() {
  226. int[] selectBuffer = new int[BUFSIZE];
  227. int hits;
  228. Gl.glSelectBuffer(BUFSIZE, selectBuffer);
  229. Gl.glRenderMode(Gl.GL_SELECT);
  230. Gl.glInitNames();
  231. Gl.glPushName(0);
  232. Gl.glPushMatrix();
  233. Gl.glMatrixMode(Gl.GL_PROJECTION);
  234. Gl.glLoadIdentity();
  235. Gl.glOrtho(0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
  236. Gl.glMatrixMode(Gl.GL_MODELVIEW);
  237. Gl.glLoadIdentity();
  238. Gl.glLoadName(1);
  239. DrawTriangle(2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, -5.0f);
  240. Gl.glLoadName(2);
  241. DrawTriangle(2.0f, 7.0f, 3.0f, 7.0f, 2.5f, 8.0f, -5.0f);
  242. Gl.glLoadName(3);
  243. DrawTriangle(2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, 0.0f);
  244. DrawTriangle(2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, -10.0f);
  245. Gl.glPopMatrix();
  246. Gl.glFlush();
  247. hits = Gl.glRenderMode(Gl.GL_RENDER);
  248. ProcessHits(hits, selectBuffer);
  249. }
  250. #endregion SelectObjects()
  251. // --- Callbacks ---
  252. #region Display()
  253. private static void Display() {
  254. Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  255. Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
  256. DrawScene();
  257. SelectObjects();
  258. Gl.glFlush();
  259. }
  260. #endregion Display()
  261. #region Keyboard(byte key, int x, int y)
  262. private static void Keyboard(byte key, int x, int y) {
  263. switch(key) {
  264. case 27:
  265. Environment.Exit(0);
  266. break;
  267. }
  268. }
  269. #endregion Keyboard(byte key, int x, int y)
  270. }
  271. }