Clone of mesa.
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and
  5. * its documentation for any purpose is hereby granted without fee, provided
  6. * that (i) the above copyright notices and this permission notice appear in
  7. * all copies of the software and related documentation, and (ii) the name of
  8. * Silicon Graphics may not be used in any advertising or
  9. * publicity relating to the software without the specific, prior written
  10. * permission of Silicon Graphics.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13. * ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include <GL/glut.h>
  30. #define MAXOBJS 10000
  31. #define MAXSELECT 100
  32. #define MAXFEED 300
  33. #define SOLID 1
  34. #define LINE 2
  35. #define POINT 3
  36. GLint windW, windH;
  37. GLuint selectBuf[MAXSELECT];
  38. GLfloat feedBuf[MAXFEED];
  39. GLint vp[4];
  40. float zRotation = 90.0;
  41. float zoom = 1.0;
  42. GLint objectCount;
  43. GLint numObjects;
  44. struct object {
  45. float v1[2];
  46. float v2[2];
  47. float v3[2];
  48. float color[3];
  49. } objects[MAXOBJS];
  50. GLenum linePoly = GL_FALSE;
  51. static void InitObjects(GLint num)
  52. {
  53. GLint i;
  54. float x, y;
  55. if (num > MAXOBJS) {
  56. num = MAXOBJS;
  57. }
  58. if (num < 1) {
  59. num = 1;
  60. }
  61. objectCount = num;
  62. srand((unsigned int)time(NULL));
  63. for (i = 0; i < num; i++) {
  64. x = (rand() % 300) - 150;
  65. y = (rand() % 300) - 150;
  66. objects[i].v1[0] = x + (rand() % 50) - 25;
  67. objects[i].v2[0] = x + (rand() % 50) - 25;
  68. objects[i].v3[0] = x + (rand() % 50) - 25;
  69. objects[i].v1[1] = y + (rand() % 50) - 25;
  70. objects[i].v2[1] = y + (rand() % 50) - 25;
  71. objects[i].v3[1] = y + (rand() % 50) - 25;
  72. objects[i].color[0] = ((rand() % 100) + 50) / 150.0;
  73. objects[i].color[1] = ((rand() % 100) + 50) / 150.0;
  74. objects[i].color[2] = ((rand() % 100) + 50) / 150.0;
  75. }
  76. }
  77. static void Init(void)
  78. {
  79. numObjects = 10;
  80. InitObjects(numObjects);
  81. glGetIntegerv(GL_VIEWPORT, vp);
  82. }
  83. static void Reshape(int width, int height)
  84. {
  85. windW = (GLint)width;
  86. windH = (GLint)height;
  87. }
  88. static void Render(GLenum mode)
  89. {
  90. GLint i;
  91. for (i = 0; i < objectCount; i++) {
  92. if (mode == GL_SELECT) {
  93. glLoadName(i);
  94. }
  95. glColor3fv(objects[i].color);
  96. glBegin(GL_POLYGON);
  97. glVertex2fv(objects[i].v1);
  98. glVertex2fv(objects[i].v2);
  99. glVertex2fv(objects[i].v3);
  100. glEnd();
  101. }
  102. }
  103. static GLint DoSelect(GLint x, GLint y)
  104. {
  105. GLint hits;
  106. glSelectBuffer(MAXSELECT, selectBuf);
  107. (void)glRenderMode(GL_SELECT);
  108. glInitNames();
  109. glPushName(~0);
  110. glPushMatrix();
  111. glViewport(0, 0, windW, windH);
  112. glGetIntegerv(GL_VIEWPORT, vp);
  113. glMatrixMode(GL_PROJECTION);
  114. glLoadIdentity();
  115. gluPickMatrix(x, windH-y, 4, 4, vp);
  116. gluOrtho2D(-175, 175, -175, 175);
  117. glMatrixMode(GL_MODELVIEW);
  118. glClearColor(0.0, 0.0, 0.0, 0.0);
  119. glClear(GL_COLOR_BUFFER_BIT);
  120. glScalef(zoom, zoom, zoom);
  121. glRotatef(zRotation, 0, 0, 1);
  122. Render(GL_SELECT);
  123. glPopMatrix();
  124. hits = glRenderMode(GL_RENDER);
  125. if (hits <= 0) {
  126. return -1;
  127. }
  128. return selectBuf[(hits-1)*4+3];
  129. }
  130. static void RecolorTri(GLint h)
  131. {
  132. objects[h].color[0] = ((rand() % 100) + 50) / 150.0;
  133. objects[h].color[1] = ((rand() % 100) + 50) / 150.0;
  134. objects[h].color[2] = ((rand() % 100) + 50) / 150.0;
  135. }
  136. static void DeleteTri(GLint h)
  137. {
  138. objects[h] = objects[objectCount-1];
  139. objectCount--;
  140. }
  141. static void GrowTri(GLint h)
  142. {
  143. float v[2];
  144. float *oldV;
  145. GLint i;
  146. v[0] = objects[h].v1[0] + objects[h].v2[0] + objects[h].v3[0];
  147. v[1] = objects[h].v1[1] + objects[h].v2[1] + objects[h].v3[1];
  148. v[0] /= 3;
  149. v[1] /= 3;
  150. for (i = 0; i < 3; i++) {
  151. switch (i) {
  152. case 0:
  153. oldV = objects[h].v1;
  154. break;
  155. case 1:
  156. oldV = objects[h].v2;
  157. break;
  158. case 2:
  159. oldV = objects[h].v3;
  160. break;
  161. }
  162. oldV[0] = 1.5 * (oldV[0] - v[0]) + v[0];
  163. oldV[1] = 1.5 * (oldV[1] - v[1]) + v[1];
  164. }
  165. }
  166. static void Mouse(int button, int state, int mouseX, int mouseY)
  167. {
  168. GLint hit;
  169. if (state != GLUT_DOWN)
  170. return;
  171. hit = DoSelect((GLint)mouseX, (GLint)mouseY);
  172. if (hit != -1) {
  173. if (button == GLUT_LEFT_BUTTON) {
  174. RecolorTri(hit);
  175. }
  176. if (button == GLUT_MIDDLE_BUTTON) {
  177. GrowTri(hit);
  178. }
  179. if (button == GLUT_RIGHT_BUTTON) {
  180. DeleteTri(hit);
  181. }
  182. }
  183. glutPostRedisplay();
  184. }
  185. static void Draw(void)
  186. {
  187. glPushMatrix();
  188. glViewport(0, 0, windW, windH);
  189. glGetIntegerv(GL_VIEWPORT, vp);
  190. glMatrixMode(GL_PROJECTION);
  191. glLoadIdentity();
  192. gluOrtho2D(-175, 175, -175, 175);
  193. glMatrixMode(GL_MODELVIEW);
  194. glClearColor(0.0, 0.0, 0.0, 0.0);
  195. glClear(GL_COLOR_BUFFER_BIT);
  196. glScalef(zoom, zoom, zoom);
  197. glRotatef(zRotation, 0, 0, 1);
  198. Render(GL_RENDER);
  199. glPopMatrix();
  200. glFlush();
  201. }
  202. static void DrawZoom(GLint x, GLint y)
  203. {
  204. glPushMatrix();
  205. glViewport(0, 0, windW, windH);
  206. glGetIntegerv(GL_VIEWPORT, vp);
  207. glMatrixMode(GL_PROJECTION);
  208. glLoadIdentity();
  209. gluPickMatrix(x, windH-y, 4, 4, vp);
  210. gluOrtho2D(-175, 175, -175, 175);
  211. glMatrixMode(GL_MODELVIEW);
  212. glClearColor(0.0, 0.0, 0.0, 0.0);
  213. glClear(GL_COLOR_BUFFER_BIT);
  214. glScalef(zoom, zoom, zoom);
  215. glRotatef(zRotation, 0, 0, 1);
  216. Render(GL_RENDER);
  217. glPopMatrix();
  218. }
  219. static void DumpFeedbackVert(GLint *i, GLint n)
  220. {
  221. GLint index;
  222. index = *i;
  223. if (index+7 > n) {
  224. *i = n;
  225. printf(" ???\n");
  226. return;
  227. }
  228. printf(" (%g %g %g), color = (%4.2f %4.2f %4.2f)\n",
  229. feedBuf[index],
  230. feedBuf[index+1],
  231. feedBuf[index+2],
  232. feedBuf[index+3],
  233. feedBuf[index+4],
  234. feedBuf[index+5]);
  235. index += 7;
  236. *i = index;
  237. }
  238. static void DrawFeedback(GLint n)
  239. {
  240. GLint i;
  241. GLint verts;
  242. printf("Feedback results (%d floats):\n", n);
  243. for (i = 0; i < n; i++) {
  244. switch ((GLint)feedBuf[i]) {
  245. case GL_POLYGON_TOKEN:
  246. printf("Polygon");
  247. i++;
  248. if (i < n) {
  249. verts = (GLint)feedBuf[i];
  250. i++;
  251. printf(": %d vertices", verts);
  252. } else {
  253. verts = 0;
  254. }
  255. printf("\n");
  256. while (verts) {
  257. DumpFeedbackVert(&i, n);
  258. verts--;
  259. }
  260. i--;
  261. break;
  262. case GL_LINE_TOKEN:
  263. printf("Line:\n");
  264. i++;
  265. DumpFeedbackVert(&i, n);
  266. DumpFeedbackVert(&i, n);
  267. i--;
  268. break;
  269. case GL_LINE_RESET_TOKEN:
  270. printf("Line Reset:\n");
  271. i++;
  272. DumpFeedbackVert(&i, n);
  273. DumpFeedbackVert(&i, n);
  274. i--;
  275. break;
  276. default:
  277. printf("%9.2f\n", feedBuf[i]);
  278. break;
  279. }
  280. }
  281. if (i == MAXFEED) {
  282. printf("...\n");
  283. }
  284. printf("\n");
  285. }
  286. static void DoFeedback(void)
  287. {
  288. GLint x;
  289. glFeedbackBuffer(MAXFEED, GL_3D_COLOR, feedBuf);
  290. (void)glRenderMode(GL_FEEDBACK);
  291. glPushMatrix();
  292. glViewport(0, 0, windW, windH);
  293. glGetIntegerv(GL_VIEWPORT, vp);
  294. glMatrixMode(GL_PROJECTION);
  295. glLoadIdentity();
  296. gluOrtho2D(-175, 175, -175, 175);
  297. glMatrixMode(GL_MODELVIEW);
  298. glClearColor(0.0, 0.0, 0.0, 0.0);
  299. glClear(GL_COLOR_BUFFER_BIT);
  300. glScalef(zoom, zoom, zoom);
  301. glRotatef(zRotation, 0, 0, 1);
  302. Render(GL_FEEDBACK);
  303. glPopMatrix();
  304. x = glRenderMode(GL_RENDER);
  305. if (x == -1) {
  306. x = MAXFEED;
  307. }
  308. DrawFeedback((GLint)x);
  309. }
  310. static void Key2(int key, int x, int y)
  311. {
  312. switch (key) {
  313. case GLUT_KEY_LEFT:
  314. zRotation += 0.5;
  315. break;
  316. case GLUT_KEY_RIGHT:
  317. zRotation -= 0.5;
  318. break;
  319. default:
  320. return;
  321. }
  322. glutPostRedisplay();
  323. }
  324. static void Key(unsigned char key, int x, int y)
  325. {
  326. switch (key) {
  327. case 27:
  328. exit(1);
  329. case 'Z':
  330. zoom /= 0.75;
  331. break;
  332. case 'z':
  333. zoom *= 0.75;
  334. break;
  335. case 'f':
  336. DoFeedback();
  337. break;
  338. case 'd':
  339. DrawZoom(x, y);
  340. break;
  341. case 'l':
  342. linePoly = !linePoly;
  343. if (linePoly) {
  344. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  345. } else {
  346. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  347. }
  348. break;
  349. default:
  350. return;
  351. }
  352. glutPostRedisplay();
  353. }
  354. int main(int argc, char **argv)
  355. {
  356. GLenum type;
  357. glutInit(&argc, argv);
  358. windW = 300;
  359. windH = 300;
  360. glutInitWindowPosition(0, 0); glutInitWindowSize( windW, windH);
  361. type = GLUT_RGB | GLUT_SINGLE;
  362. glutInitDisplayMode(type);
  363. if (glutCreateWindow("Select Test") == GL_FALSE) {
  364. exit(1);
  365. }
  366. Init();
  367. glutReshapeFunc(Reshape);
  368. glutKeyboardFunc(Key);
  369. glutSpecialFunc(Key2);
  370. glutMouseFunc(Mouse);
  371. glutDisplayFunc(Draw);
  372. glutMainLoop();
  373. return 0;
  374. }