Source code of Windows XP (NT5)
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.

398 lines
8.8 KiB

  1. /*
  2. * (c) Copyright 1993, Silicon Graphics, Inc.
  3. * ALL RIGHTS RESERVED
  4. * Permission to use, copy, modify, and distribute this software for
  5. * any purpose and without fee is hereby granted, provided that the above
  6. * copyright notice appear in all copies and that both the copyright notice
  7. * and this permission notice appear in supporting documentation, and that
  8. * the name of Silicon Graphics, Inc. not be used in advertising
  9. * or publicity pertaining to distribution of the software without specific,
  10. * written prior permission.
  11. *
  12. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  16. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  21. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * US Government Users Restricted Rights
  26. * Use, duplication, or disclosure by the Government is subject to
  27. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. * clause at DFARS 252.227-7013 and/or in similar or successor
  30. * clauses in the FAR or the DOD or NASA FAR Supplement.
  31. * Unpublished-- rights reserved under the copyright laws of the
  32. * United States. Contractor/manufacturer is Silicon Graphics,
  33. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  34. *
  35. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36. */
  37. #include <stdio.h>
  38. #include <string.h>
  39. //#include <unistd.h>
  40. #include <stdlib.h>
  41. #include <time.h>
  42. #include "tk.h"
  43. #ifdef __unix
  44. #include <sys/times.h>
  45. #include <sys/param.h>
  46. #else
  47. #include <sys\types.h>
  48. #include <sys\timeb.h>
  49. #endif
  50. #define GAP 10
  51. #define ROWS 1
  52. #define COLS 4
  53. GLenum rgb, doubleBuffer, directRender, windType;
  54. GLint windW, windH;
  55. GLint boxW, boxH;
  56. GLenum antialiasing = GL_FALSE;
  57. GLenum depthTesting = GL_FALSE;
  58. GLenum fogging = GL_FALSE, niceFogging = GL_FALSE;
  59. GLenum lighting = GL_FALSE;
  60. GLenum shading = GL_FALSE;
  61. GLenum texturing = GL_FALSE;
  62. // mf: change these to more reasonable numbers
  63. GLint repeatCount = 300;
  64. GLint loopCount = 10;
  65. GLubyte texture[4*3] = {
  66. 0xFF, 0, 0, 0, 0, 0,
  67. 0, 0, 0, 0, 0xFF, 0,
  68. };
  69. static void SetWindSize(int width, int height)
  70. {
  71. windW = (GLint)width;
  72. windH = (GLint)height;
  73. }
  74. static GLenum Key(int key, GLenum mask)
  75. {
  76. switch (key) {
  77. case TK_ESCAPE:
  78. tkQuit();
  79. case TK_a:
  80. antialiasing = !antialiasing;
  81. break;
  82. case TK_d:
  83. depthTesting = !depthTesting;
  84. break;
  85. case TK_f:
  86. fogging = !fogging;
  87. break;
  88. case TK_F:
  89. niceFogging = !niceFogging;
  90. break;
  91. case TK_l:
  92. lighting = !lighting;
  93. break;
  94. case TK_s:
  95. shading = !shading;
  96. break;
  97. case TK_t:
  98. texturing = !texturing;
  99. break;
  100. default:
  101. return GL_FALSE;
  102. }
  103. return GL_TRUE;
  104. }
  105. static void Viewport(GLint row, GLint column)
  106. {
  107. GLint x, y;
  108. boxW = (windW - (COLS + 1) * GAP) / COLS;
  109. boxH = (windH - (ROWS + 1) * GAP) / ROWS;
  110. x = GAP + column * (boxW + GAP);
  111. y = GAP + row * (boxH + GAP);
  112. glViewport(x, y, boxW, boxH);
  113. glMatrixMode(GL_PROJECTION);
  114. glLoadIdentity();
  115. gluOrtho2D(-boxW/2, boxW/2, -boxH/2, boxH/2);
  116. glMatrixMode(GL_MODELVIEW);
  117. glEnable(GL_SCISSOR_TEST);
  118. glScissor(x, y, boxW, boxH);
  119. }
  120. static double Now(void)
  121. {
  122. #ifdef __unix
  123. struct tms tm;
  124. clock_t clk;
  125. clk = times(&tm);
  126. return (double)clk / (double)HZ;
  127. #else
  128. struct _timeb tm;
  129. _ftime( &tm );
  130. return (tm.time + tm.millitm/1000.0);
  131. #endif
  132. }
  133. static void Report(const char *msg, float elapsed)
  134. {
  135. if (elapsed == 0.0) {
  136. printf("%s per second: Unknown, elapsed time is zero\n", msg);
  137. } else {
  138. printf("%s per second: %g\n", msg, repeatCount*loopCount/elapsed);
  139. }
  140. }
  141. static void Points(void)
  142. {
  143. GLint i, j;
  144. float v1[3];
  145. double start;
  146. start = Now();
  147. for (i = 0; i < repeatCount; i++) {
  148. v1[0] = 10;
  149. v1[1] = 10;
  150. v1[2] = 10;
  151. glBegin(GL_POINTS);
  152. for (j = 0; j < loopCount; j++) {
  153. glVertex2fv(v1);
  154. }
  155. glEnd();
  156. }
  157. glFinish();
  158. Report("Points", Now()-start);
  159. }
  160. static void Lines(void)
  161. {
  162. GLint i, j;
  163. float v1[3], v2[3];
  164. double start;
  165. start = Now();
  166. for (i = 0; i < repeatCount; i++) {
  167. v1[0] = 10;
  168. v1[1] = 10;
  169. v1[2] = 10;
  170. v2[0] = 20;
  171. v2[1] = 20;
  172. v2[2] = 10;
  173. glBegin(GL_LINES);
  174. for (j = 0; j < loopCount; j++) {
  175. glVertex2fv(v1);
  176. glVertex2fv(v2);
  177. }
  178. glEnd();
  179. }
  180. glFinish();
  181. Report("Lines", Now()-start);
  182. }
  183. static void Triangles(void)
  184. {
  185. GLint i, j;
  186. float v1[3], v2[3], v3[3], t1[2], t2[2], t3[2];
  187. double start;
  188. start = Now();
  189. v1[0] = 10;
  190. v1[1] = 10;
  191. v1[2] = 10;
  192. v2[0] = 20;
  193. v2[1] = 20;
  194. v2[2] = 10;
  195. v3[0] = 10;
  196. v3[1] = 20;
  197. v3[2] = 10;
  198. t1[0] = 0;
  199. t1[1] = 0;
  200. t2[0] = 1;
  201. t2[1] = 1;
  202. t3[0] = 0;
  203. t3[1] = 1;
  204. for (i = 0; i < repeatCount; i++) {
  205. glBegin(GL_TRIANGLES);
  206. for (j = 0; j < loopCount; j++) {
  207. if (texturing) {
  208. glTexCoord2fv(t1);
  209. }
  210. glVertex2fv(v1);
  211. if (texturing) {
  212. glTexCoord2fv(t2);
  213. }
  214. glVertex2fv(v2);
  215. if (texturing) {
  216. glTexCoord2fv(t3);
  217. }
  218. glVertex2fv(v3);
  219. }
  220. glEnd();
  221. }
  222. glFinish();
  223. Report("Triangles", Now()-start);
  224. }
  225. static void Rects(void)
  226. {
  227. GLint i, j;
  228. float v1[2], v2[2];
  229. double start;
  230. start = Now();
  231. for (i = 0; i < repeatCount; i++) {
  232. v1[0] = 10;
  233. v1[1] = 10;
  234. v2[0] = 20;
  235. v2[1] = 20;
  236. for (j = 0; j < loopCount; j++) {
  237. glRectfv(v1, v2);
  238. }
  239. }
  240. glFinish();
  241. Report("Rects", Now()-start);
  242. }
  243. static void Draw(void)
  244. {
  245. glDisable(GL_SCISSOR_TEST);
  246. glClearColor(0.0, 0.0, 0.0, 0.0);
  247. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  248. TK_SETCOLOR(windType, TK_YELLOW);
  249. if (antialiasing) {
  250. glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  251. glEnable(GL_BLEND);
  252. glEnable(GL_POINT_SMOOTH);
  253. glEnable(GL_LINE_SMOOTH);
  254. glEnable(GL_POLYGON_SMOOTH);
  255. }
  256. if (depthTesting) {
  257. glEnable(GL_DEPTH_TEST);
  258. }
  259. if (fogging) {
  260. glEnable(GL_FOG);
  261. glHint(GL_FOG_HINT, (niceFogging) ? GL_NICEST : GL_FASTEST);
  262. }
  263. if (lighting) {
  264. static GLfloat ambient[4] = {1, 0.5, 0.5, 0};
  265. glEnable(GL_NORMALIZE);
  266. glNormal3f(1.0, 1.0, 1.0);
  267. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
  268. glEnable(GL_LIGHTING);
  269. glEnable(GL_LIGHT0);
  270. }
  271. (shading) ? glShadeModel(GL_SMOOTH) : glShadeModel(GL_FLAT);
  272. if (texturing) {
  273. static GLfloat modulate[1] = {GL_DECAL};
  274. static GLfloat clamp[1] = {GL_CLAMP};
  275. static GLfloat linear[1] = {GL_LINEAR};
  276. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  277. glTexImage2D(GL_TEXTURE_2D, 0, 3, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE,
  278. (GLvoid *)texture);
  279. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modulate);
  280. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
  281. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
  282. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear);
  283. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear);
  284. glEnable(GL_TEXTURE_2D);
  285. }
  286. Viewport(0, 0); Points();
  287. Viewport(0, 1); Lines();
  288. Viewport(0, 2); Triangles();
  289. Viewport(0, 3); Rects();
  290. printf( "\n" );
  291. glFlush();
  292. if (doubleBuffer) {
  293. tkSwapBuffers();
  294. }
  295. }
  296. static GLenum Args(int argc, char **argv)
  297. {
  298. GLint i;
  299. rgb = GL_TRUE;
  300. doubleBuffer = GL_FALSE;
  301. directRender = GL_FALSE;
  302. for (i = 1; i < argc; i++) {
  303. if (strcmp(argv[i], "-ci") == 0) {
  304. rgb = GL_FALSE;
  305. } else if (strcmp(argv[i], "-rgb") == 0) {
  306. rgb = GL_TRUE;
  307. } else if (strcmp(argv[i], "-sb") == 0) {
  308. doubleBuffer = GL_FALSE;
  309. } else if (strcmp(argv[i], "-db") == 0) {
  310. doubleBuffer = GL_TRUE;
  311. } else if (strcmp(argv[i], "-dr") == 0) {
  312. directRender = GL_TRUE;
  313. } else if (strcmp(argv[i], "-ir") == 0) {
  314. directRender = GL_FALSE;
  315. } else {
  316. printf("%s (Bad option).\n", argv[i]);
  317. return GL_FALSE;
  318. }
  319. }
  320. return GL_TRUE;
  321. }
  322. void main(int argc, char **argv)
  323. {
  324. if (Args(argc, argv) == GL_FALSE) {
  325. tkQuit();
  326. }
  327. windW = 600;
  328. windH = 300;
  329. tkInitPosition(0, 0, windW, windH);
  330. windType = TK_DEPTH16;
  331. windType |= (rgb) ? TK_RGB : TK_INDEX;
  332. windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  333. windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  334. tkInitDisplayMode(windType);
  335. if (tkInitWindow("Speed Test") == GL_FALSE) {
  336. tkQuit();
  337. }
  338. tkExposeFunc(SetWindSize);
  339. tkReshapeFunc(SetWindSize);
  340. tkKeyDownFunc(Key);
  341. tkDisplayFunc(Draw);
  342. tkExec();
  343. }