Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

tri-viewport.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <string.h>
  26. #include <stdlib.h>
  27. #include <GL/glut.h>
  28. GLenum doubleBuffer = 1;
  29. int win;
  30. static float tx = 0;
  31. static float ty = 0;
  32. static float tw = 0;
  33. static float th = 0;
  34. static float z = -5;
  35. static float win_width = 250;
  36. static float win_height = 250;
  37. static enum {
  38. ORTHO,
  39. FRUSTUM,
  40. MODE_MAX
  41. } mode = ORTHO;
  42. static void Init(void)
  43. {
  44. fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  45. fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  46. fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  47. fflush(stderr);
  48. glClearColor(0, 0, 0, 0.0);
  49. }
  50. static void Reshape(int width, int height)
  51. {
  52. win_width = width;
  53. win_height = height;
  54. glutPostRedisplay();
  55. }
  56. static void Key(unsigned char key, int x, int y)
  57. {
  58. switch (key) {
  59. case 27:
  60. exit(0);
  61. case 'w':
  62. tw += 1.0;
  63. break;
  64. case 'W':
  65. tw -= 1.0;
  66. break;
  67. case 'h':
  68. th += 1.0;
  69. break;
  70. case 'H':
  71. th -= 1.0;
  72. break;
  73. case 'z':
  74. z += 1.0;
  75. break;
  76. case 'Z':
  77. z -= 1.0;
  78. break;
  79. case 'm':
  80. mode++;
  81. mode %= MODE_MAX;
  82. break;
  83. case ' ':
  84. tw = th = tx = ty = 0;
  85. z = -5;
  86. mode = ORTHO;
  87. break;
  88. default:
  89. break;
  90. }
  91. glutPostRedisplay();
  92. }
  93. static void Draw(void)
  94. {
  95. int i;
  96. float w = tw + win_width;
  97. float h = th + win_height;
  98. fprintf(stderr, "glViewport(%f %f %f %f)\n", tx, ty, w, h);
  99. fprintf(stderr, "mode: %s\n", mode == FRUSTUM ? "FRUSTUM" : "ORTHO");
  100. fprintf(stderr, "z: %f\n", z);
  101. fflush(stderr);
  102. glMatrixMode(GL_PROJECTION);
  103. glLoadIdentity();
  104. switch (mode) {
  105. case FRUSTUM:
  106. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  107. break;
  108. case ORTHO:
  109. default:
  110. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  111. break;
  112. }
  113. glMatrixMode(GL_MODELVIEW);
  114. glLoadIdentity();
  115. glClear(GL_COLOR_BUFFER_BIT);
  116. /***********************************************************************
  117. * Should be clipped to be no larger than the triangles:
  118. */
  119. glViewport(tx, ty, w, h);
  120. glBegin(GL_POLYGON);
  121. glColor3f(1,1,0);
  122. glVertex3f(-100, -100, z);
  123. glVertex3f(-100, 100, z);
  124. glVertex3f(100, 100, z);
  125. glVertex3f(100, -100, z);
  126. glEnd();
  127. glBegin(GL_POLYGON);
  128. glColor3f(0,1,1);
  129. glVertex3f(-10, -10, z);
  130. glVertex3f(-10, 10, z);
  131. glVertex3f(10, 10, z);
  132. glVertex3f(10, -10, z);
  133. glEnd();
  134. glBegin(GL_POLYGON);
  135. glColor3f(1,0,0);
  136. glVertex3f(-2, -2, z);
  137. glVertex3f(-2, 2, z);
  138. glVertex3f(2, 2, z);
  139. glVertex3f(2, -2, z);
  140. glEnd();
  141. glBegin(GL_POLYGON);
  142. glColor3f(.5,.5,1);
  143. glVertex3f(-1, -1, z);
  144. glVertex3f(-1, 1, z);
  145. glVertex3f(1, 1, z);
  146. glVertex3f(1, -1, z);
  147. glEnd();
  148. /***********************************************************************
  149. */
  150. glViewport(0, 0, win_width, win_height);
  151. glBegin(GL_LINES);
  152. glColor3f(1,1,0);
  153. glVertex3f(-1, 0, z);
  154. glVertex3f(1, 0, z);
  155. glVertex3f(0, -1, z);
  156. glVertex3f(0, 1, z);
  157. glEnd();
  158. /***********************************************************************
  159. */
  160. glViewport(tx, ty, w, h);
  161. glBegin(GL_TRIANGLES);
  162. glColor3f(1,0,0);
  163. glVertex3f(-1, -1, z);
  164. glVertex3f(0, -1, z);
  165. glVertex3f(-.5, -.5, z);
  166. glColor3f(1,1,1);
  167. glVertex3f(0, -1, z);
  168. glVertex3f(1, -1, z);
  169. glVertex3f(.5, -.5, z);
  170. glVertex3f(-.5, -.5, z);
  171. glVertex3f(.5, -.5, z);
  172. glVertex3f(0, 0, z);
  173. glColor3f(0,1,0);
  174. glVertex3f(1, 1, z);
  175. glVertex3f(0, 1, z);
  176. glVertex3f(.5, .5, z);
  177. glColor3f(1,1,1);
  178. glVertex3f(0, 1, z);
  179. glVertex3f(-1, 1, z);
  180. glVertex3f(-.5, .5, z);
  181. glVertex3f(.5, .5, z);
  182. glVertex3f(-.5, .5, z);
  183. glVertex3f( 0, 0, z);
  184. glEnd();
  185. glViewport(0, 0, win_width, win_height);
  186. glBegin(GL_LINES);
  187. glColor3f(.5,.5,0);
  188. for (i = -10; i < 10; i++) {
  189. float f = i / 10.0;
  190. if (i == 0)
  191. continue;
  192. glVertex3f(-1, f, z);
  193. glVertex3f(1, f, z);
  194. glVertex3f(f, -1, z);
  195. glVertex3f(f, 1, z);
  196. }
  197. glEnd();
  198. glFlush();
  199. if (doubleBuffer) {
  200. glutSwapBuffers();
  201. }
  202. }
  203. static GLenum Args(int argc, char **argv)
  204. {
  205. GLint i;
  206. if (getenv("VPX"))
  207. tx = atof(getenv("VPX"));
  208. if (getenv("VPY"))
  209. ty = atof(getenv("VPY"));
  210. for (i = 1; i < argc; i++) {
  211. if (strcmp(argv[i], "-sb") == 0) {
  212. doubleBuffer = GL_FALSE;
  213. } else if (strcmp(argv[i], "-db") == 0) {
  214. doubleBuffer = GL_TRUE;
  215. } else {
  216. fprintf(stderr, "%s (Bad option).\n", argv[i]);
  217. return GL_FALSE;
  218. }
  219. }
  220. return GL_TRUE;
  221. }
  222. static void
  223. special(int k, int x, int y)
  224. {
  225. switch (k) {
  226. case GLUT_KEY_UP:
  227. ty += 1.0;
  228. break;
  229. case GLUT_KEY_DOWN:
  230. ty -= 1.0;
  231. break;
  232. case GLUT_KEY_LEFT:
  233. tx -= 1.0;
  234. break;
  235. case GLUT_KEY_RIGHT:
  236. tx += 1.0;
  237. break;
  238. default:
  239. break;
  240. }
  241. glutPostRedisplay();
  242. }
  243. int main(int argc, char **argv)
  244. {
  245. GLenum type;
  246. glutInit(&argc, argv);
  247. if (Args(argc, argv) == GL_FALSE) {
  248. exit(1);
  249. }
  250. glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
  251. type = GLUT_RGB | GLUT_ALPHA;
  252. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  253. glutInitDisplayMode(type);
  254. win = glutCreateWindow(*argv);
  255. if (!win) {
  256. exit(1);
  257. }
  258. Init();
  259. glutReshapeFunc(Reshape);
  260. glutKeyboardFunc(Key);
  261. glutSpecialFunc(special);
  262. glutDisplayFunc(Draw);
  263. glutMainLoop();
  264. return 0;
  265. }