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.

glmain.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * VMWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. /**
  22. * OpenGL/GLUT common code for perf programs.
  23. * Brian Paul
  24. * 15 Sep 2009
  25. */
  26. #include <stdio.h>
  27. #include "glmain.h"
  28. #include <GL/glut.h>
  29. static int Win;
  30. static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
  31. /** Return time in seconds */
  32. double
  33. PerfGetTime(void)
  34. {
  35. return glutGet(GLUT_ELAPSED_TIME) * 0.001;
  36. }
  37. void
  38. PerfSwapBuffers(void)
  39. {
  40. glutSwapBuffers();
  41. }
  42. /** make simple checkerboard texture object */
  43. GLuint
  44. PerfCheckerTexture(GLsizei width, GLsizei height)
  45. {
  46. const GLenum filter = GL_NEAREST;
  47. GLubyte *img = (GLubyte *) malloc(width * height * 4);
  48. GLint i, j, k;
  49. GLuint obj;
  50. k = 0;
  51. for (i = 0; i < height; i++) {
  52. for (j = 0; j < width; j++) {
  53. GLubyte color;
  54. if (((i / 8) ^ (j / 8)) & 1) {
  55. color = 0xff;
  56. }
  57. else {
  58. color = 0x0;
  59. }
  60. img[k++] = color;
  61. img[k++] = color;
  62. img[k++] = color;
  63. img[k++] = color;
  64. }
  65. }
  66. glGenTextures(1, &obj);
  67. glBindTexture(GL_TEXTURE_2D, obj);
  68. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
  69. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
  70. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
  71. GL_RGBA, GL_UNSIGNED_BYTE, img);
  72. free(img);
  73. return obj;
  74. }
  75. static GLuint
  76. CompileShader(GLenum type, const char *shader)
  77. {
  78. GLuint sh;
  79. GLint stat;
  80. sh = glCreateShader(type);
  81. glShaderSource(sh, 1, (const GLchar **) &shader, NULL);
  82. glCompileShader(sh);
  83. glGetShaderiv(sh, GL_COMPILE_STATUS, &stat);
  84. if (!stat) {
  85. GLchar log[1000];
  86. GLsizei len;
  87. glGetShaderInfoLog(sh, 1000, &len, log);
  88. fprintf(stderr, "Error: problem compiling shader: %s\n", log);
  89. exit(1);
  90. }
  91. return sh;
  92. }
  93. /** Make shader program from given vert/frag shader text */
  94. GLuint
  95. PerfShaderProgram(const char *vertShader, const char *fragShader)
  96. {
  97. GLuint prog;
  98. GLint stat;
  99. {
  100. const char *version = (const char *) glGetString(GL_VERSION);
  101. if ((version[0] != '2' &&
  102. version[0] != '3') || version[1] != '.') {
  103. fprintf(stderr, "Error: GL version 2.x or better required\n");
  104. exit(1);
  105. }
  106. }
  107. prog = glCreateProgram();
  108. if (vertShader) {
  109. GLuint vs = CompileShader(GL_VERTEX_SHADER, vertShader);
  110. glAttachShader(prog, vs);
  111. }
  112. if (fragShader) {
  113. GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fragShader);
  114. glAttachShader(prog, fs);
  115. }
  116. glLinkProgram(prog);
  117. glGetProgramiv(prog, GL_LINK_STATUS, &stat);
  118. if (!stat) {
  119. GLchar log[1000];
  120. GLsizei len;
  121. glGetProgramInfoLog(prog, 1000, &len, log);
  122. fprintf(stderr, "Shader link error:\n%s\n", log);
  123. exit(1);
  124. }
  125. return prog;
  126. }
  127. int
  128. PerfReshapeWindow( unsigned w, unsigned h )
  129. {
  130. if (glutGet(GLUT_SCREEN_WIDTH) < w ||
  131. glutGet(GLUT_SCREEN_HEIGHT) < h)
  132. return 0;
  133. glutReshapeWindow( w, h );
  134. glutPostRedisplay();
  135. return 1;
  136. }
  137. GLboolean
  138. PerfExtensionSupported(const char *ext)
  139. {
  140. return glutExtensionSupported(ext);
  141. }
  142. static void
  143. Idle(void)
  144. {
  145. PerfNextRound();
  146. }
  147. static void
  148. Draw(void)
  149. {
  150. PerfDraw();
  151. glutSwapBuffers();
  152. }
  153. static void
  154. Reshape(int width, int height)
  155. {
  156. WinWidth = width;
  157. WinHeight = height;
  158. glViewport(0, 0, width, height);
  159. glMatrixMode(GL_PROJECTION);
  160. glLoadIdentity();
  161. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  162. glMatrixMode(GL_MODELVIEW);
  163. glLoadIdentity();
  164. glTranslatef(0.0, 0.0, -15.0);
  165. }
  166. static void
  167. Key(unsigned char key, int x, int y)
  168. {
  169. const GLfloat step = 3.0;
  170. (void) x;
  171. (void) y;
  172. switch (key) {
  173. case 'z':
  174. Zrot -= step;
  175. break;
  176. case 'Z':
  177. Zrot += step;
  178. break;
  179. case 27:
  180. glutDestroyWindow(Win);
  181. exit(0);
  182. break;
  183. }
  184. glutPostRedisplay();
  185. }
  186. static void
  187. SpecialKey(int key, int x, int y)
  188. {
  189. const GLfloat step = 3.0;
  190. (void) x;
  191. (void) y;
  192. switch (key) {
  193. case GLUT_KEY_UP:
  194. Xrot -= step;
  195. break;
  196. case GLUT_KEY_DOWN:
  197. Xrot += step;
  198. break;
  199. case GLUT_KEY_LEFT:
  200. Yrot -= step;
  201. break;
  202. case GLUT_KEY_RIGHT:
  203. Yrot += step;
  204. break;
  205. }
  206. glutPostRedisplay();
  207. }
  208. int
  209. main(int argc, char *argv[])
  210. {
  211. glutInit(&argc, argv);
  212. glutInitWindowSize(WinWidth, WinHeight);
  213. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL);
  214. Win = glutCreateWindow(argv[0]);
  215. glewInit();
  216. glutReshapeFunc(Reshape);
  217. glutKeyboardFunc(Key);
  218. glutSpecialFunc(SpecialKey);
  219. glutDisplayFunc(Draw);
  220. glutIdleFunc(Idle);
  221. PerfInit();
  222. glutMainLoop();
  223. return 0;
  224. }