testgl2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #include "SDL_test_common.h"
  15. #ifdef __MACOS__
  16. #define HAVE_OPENGL
  17. #endif
  18. #ifdef HAVE_OPENGL
  19. #include "SDL_opengl.h"
  20. typedef struct GL_Context
  21. {
  22. #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
  23. #include "../src/render/opengl/SDL_glfuncs.h"
  24. #undef SDL_PROC
  25. } GL_Context;
  26. /* Undefine this if you want a flat cube instead of a rainbow cube */
  27. #define SHADED_CUBE
  28. static SDLTest_CommonState *state;
  29. static SDL_GLContext context;
  30. static GL_Context ctx;
  31. static int LoadContext(GL_Context * data)
  32. {
  33. #if SDL_VIDEO_DRIVER_UIKIT
  34. #define __SDL_NOGETPROCADDR__
  35. #elif SDL_VIDEO_DRIVER_ANDROID
  36. #define __SDL_NOGETPROCADDR__
  37. #elif SDL_VIDEO_DRIVER_PANDORA
  38. #define __SDL_NOGETPROCADDR__
  39. #endif
  40. #if defined __SDL_NOGETPROCADDR__
  41. #define SDL_PROC(ret,func,params) data->func=func;
  42. #else
  43. #define SDL_PROC(ret,func,params) \
  44. do { \
  45. data->func = SDL_GL_GetProcAddress(#func); \
  46. if ( ! data->func ) { \
  47. return SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
  48. } \
  49. } while ( 0 );
  50. #endif /* _SDL_NOGETPROCADDR_ */
  51. #include "../src/render/opengl/SDL_glfuncs.h"
  52. #undef SDL_PROC
  53. return 0;
  54. }
  55. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  56. static void
  57. quit(int rc)
  58. {
  59. if (context) {
  60. /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
  61. SDL_GL_DeleteContext(context);
  62. }
  63. SDLTest_CommonQuit(state);
  64. exit(rc);
  65. }
  66. static void
  67. Render()
  68. {
  69. static float color[8][3] = {
  70. {1.0, 1.0, 0.0},
  71. {1.0, 0.0, 0.0},
  72. {0.0, 0.0, 0.0},
  73. {0.0, 1.0, 0.0},
  74. {0.0, 1.0, 1.0},
  75. {1.0, 1.0, 1.0},
  76. {1.0, 0.0, 1.0},
  77. {0.0, 0.0, 1.0}
  78. };
  79. static float cube[8][3] = {
  80. {0.5, 0.5, -0.5},
  81. {0.5, -0.5, -0.5},
  82. {-0.5, -0.5, -0.5},
  83. {-0.5, 0.5, -0.5},
  84. {-0.5, 0.5, 0.5},
  85. {0.5, 0.5, 0.5},
  86. {0.5, -0.5, 0.5},
  87. {-0.5, -0.5, 0.5}
  88. };
  89. /* Do our drawing, too. */
  90. ctx.glClearColor(0.0, 0.0, 0.0, 1.0);
  91. ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  92. ctx.glBegin(GL_QUADS);
  93. #ifdef SHADED_CUBE
  94. ctx.glColor3fv(color[0]);
  95. ctx.glVertex3fv(cube[0]);
  96. ctx.glColor3fv(color[1]);
  97. ctx.glVertex3fv(cube[1]);
  98. ctx.glColor3fv(color[2]);
  99. ctx.glVertex3fv(cube[2]);
  100. ctx.glColor3fv(color[3]);
  101. ctx.glVertex3fv(cube[3]);
  102. ctx.glColor3fv(color[3]);
  103. ctx.glVertex3fv(cube[3]);
  104. ctx.glColor3fv(color[4]);
  105. ctx.glVertex3fv(cube[4]);
  106. ctx.glColor3fv(color[7]);
  107. ctx.glVertex3fv(cube[7]);
  108. ctx.glColor3fv(color[2]);
  109. ctx.glVertex3fv(cube[2]);
  110. ctx.glColor3fv(color[0]);
  111. ctx.glVertex3fv(cube[0]);
  112. ctx.glColor3fv(color[5]);
  113. ctx.glVertex3fv(cube[5]);
  114. ctx.glColor3fv(color[6]);
  115. ctx.glVertex3fv(cube[6]);
  116. ctx.glColor3fv(color[1]);
  117. ctx.glVertex3fv(cube[1]);
  118. ctx.glColor3fv(color[5]);
  119. ctx.glVertex3fv(cube[5]);
  120. ctx.glColor3fv(color[4]);
  121. ctx.glVertex3fv(cube[4]);
  122. ctx.glColor3fv(color[7]);
  123. ctx.glVertex3fv(cube[7]);
  124. ctx.glColor3fv(color[6]);
  125. ctx.glVertex3fv(cube[6]);
  126. ctx.glColor3fv(color[5]);
  127. ctx.glVertex3fv(cube[5]);
  128. ctx.glColor3fv(color[0]);
  129. ctx.glVertex3fv(cube[0]);
  130. ctx.glColor3fv(color[3]);
  131. ctx.glVertex3fv(cube[3]);
  132. ctx.glColor3fv(color[4]);
  133. ctx.glVertex3fv(cube[4]);
  134. ctx.glColor3fv(color[6]);
  135. ctx.glVertex3fv(cube[6]);
  136. ctx.glColor3fv(color[1]);
  137. ctx.glVertex3fv(cube[1]);
  138. ctx.glColor3fv(color[2]);
  139. ctx.glVertex3fv(cube[2]);
  140. ctx.glColor3fv(color[7]);
  141. ctx.glVertex3fv(cube[7]);
  142. #else /* flat cube */
  143. ctx.glColor3f(1.0, 0.0, 0.0);
  144. ctx.glVertex3fv(cube[0]);
  145. ctx.glVertex3fv(cube[1]);
  146. ctx.glVertex3fv(cube[2]);
  147. ctx.glVertex3fv(cube[3]);
  148. ctx.glColor3f(0.0, 1.0, 0.0);
  149. ctx.glVertex3fv(cube[3]);
  150. ctx.glVertex3fv(cube[4]);
  151. ctx.glVertex3fv(cube[7]);
  152. ctx.glVertex3fv(cube[2]);
  153. ctx.glColor3f(0.0, 0.0, 1.0);
  154. ctx.glVertex3fv(cube[0]);
  155. ctx.glVertex3fv(cube[5]);
  156. ctx.glVertex3fv(cube[6]);
  157. ctx.glVertex3fv(cube[1]);
  158. ctx.glColor3f(0.0, 1.0, 1.0);
  159. ctx.glVertex3fv(cube[5]);
  160. ctx.glVertex3fv(cube[4]);
  161. ctx.glVertex3fv(cube[7]);
  162. ctx.glVertex3fv(cube[6]);
  163. ctx.glColor3f(1.0, 1.0, 0.0);
  164. ctx.glVertex3fv(cube[5]);
  165. ctx.glVertex3fv(cube[0]);
  166. ctx.glVertex3fv(cube[3]);
  167. ctx.glVertex3fv(cube[4]);
  168. ctx.glColor3f(1.0, 0.0, 1.0);
  169. ctx.glVertex3fv(cube[6]);
  170. ctx.glVertex3fv(cube[1]);
  171. ctx.glVertex3fv(cube[2]);
  172. ctx.glVertex3fv(cube[7]);
  173. #endif /* SHADED_CUBE */
  174. ctx.glEnd();
  175. ctx.glMatrixMode(GL_MODELVIEW);
  176. ctx.glRotatef(5.0, 1.0, 1.0, 1.0);
  177. }
  178. int
  179. main(int argc, char *argv[])
  180. {
  181. int fsaa, accel;
  182. int value;
  183. int i, done;
  184. SDL_DisplayMode mode;
  185. SDL_Event event;
  186. Uint32 then, now, frames;
  187. int status;
  188. int dw, dh;
  189. /* Enable standard application logging */
  190. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  191. /* Initialize parameters */
  192. fsaa = 0;
  193. accel = -1;
  194. /* Initialize test framework */
  195. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  196. if (!state) {
  197. return 1;
  198. }
  199. for (i = 1; i < argc;) {
  200. int consumed;
  201. consumed = SDLTest_CommonArg(state, i);
  202. if (consumed == 0) {
  203. if (SDL_strcasecmp(argv[i], "--fsaa") == 0 && i+1 < argc) {
  204. fsaa = atoi(argv[i+1]);
  205. consumed = 2;
  206. } else if (SDL_strcasecmp(argv[i], "--accel") == 0 && i+1 < argc) {
  207. accel = atoi(argv[i+1]);
  208. consumed = 2;
  209. } else {
  210. consumed = -1;
  211. }
  212. }
  213. if (consumed < 0) {
  214. SDL_Log("Usage: %s %s [--fsaa n] [--accel n]\n", argv[0],
  215. SDLTest_CommonUsage(state));
  216. quit(1);
  217. }
  218. i += consumed;
  219. }
  220. /* Set OpenGL parameters */
  221. state->window_flags |= SDL_WINDOW_OPENGL;
  222. state->gl_red_size = 5;
  223. state->gl_green_size = 5;
  224. state->gl_blue_size = 5;
  225. state->gl_depth_size = 16;
  226. state->gl_double_buffer = 1;
  227. if (fsaa) {
  228. state->gl_multisamplebuffers = 1;
  229. state->gl_multisamplesamples = fsaa;
  230. }
  231. if (accel >= 0) {
  232. state->gl_accelerated = accel;
  233. }
  234. if (!SDLTest_CommonInit(state)) {
  235. quit(2);
  236. }
  237. /* Create OpenGL context */
  238. context = SDL_GL_CreateContext(state->windows[0]);
  239. if (!context) {
  240. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
  241. quit(2);
  242. }
  243. /* Important: call this *after* creating the context */
  244. if (LoadContext(&ctx) < 0) {
  245. SDL_Log("Could not load GL functions\n");
  246. quit(2);
  247. return 0;
  248. }
  249. if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
  250. /* try late-swap-tearing first. If not supported, try normal vsync. */
  251. if (SDL_GL_SetSwapInterval(-1) == -1) {
  252. SDL_GL_SetSwapInterval(1);
  253. }
  254. } else {
  255. SDL_GL_SetSwapInterval(0); /* disable vsync. */
  256. }
  257. SDL_GetCurrentDisplayMode(0, &mode);
  258. SDL_Log("Screen BPP : %d\n", SDL_BITSPERPIXEL(mode.format));
  259. SDL_Log("Swap Interval : %d\n", SDL_GL_GetSwapInterval());
  260. SDL_GetWindowSize(state->windows[0], &dw, &dh);
  261. SDL_Log("Window Size : %d,%d\n", dw, dh);
  262. SDL_GL_GetDrawableSize(state->windows[0], &dw, &dh);
  263. SDL_Log("Draw Size : %d,%d\n", dw, dh);
  264. SDL_Log("\n");
  265. SDL_Log("Vendor : %s\n", ctx.glGetString(GL_VENDOR));
  266. SDL_Log("Renderer : %s\n", ctx.glGetString(GL_RENDERER));
  267. SDL_Log("Version : %s\n", ctx.glGetString(GL_VERSION));
  268. SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
  269. SDL_Log("\n");
  270. status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
  271. if (!status) {
  272. SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
  273. } else {
  274. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError());
  275. }
  276. status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
  277. if (!status) {
  278. SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
  279. } else {
  280. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError());
  281. }
  282. status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
  283. if (!status) {
  284. SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
  285. } else {
  286. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError());
  287. }
  288. status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
  289. if (!status) {
  290. SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
  291. } else {
  292. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s\n", SDL_GetError());
  293. }
  294. if (fsaa) {
  295. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
  296. if (!status) {
  297. SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
  298. } else {
  299. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
  300. SDL_GetError());
  301. }
  302. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
  303. if (!status) {
  304. SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
  305. value);
  306. } else {
  307. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
  308. SDL_GetError());
  309. }
  310. }
  311. if (accel >= 0) {
  312. status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
  313. if (!status) {
  314. SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel,
  315. value);
  316. } else {
  317. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
  318. SDL_GetError());
  319. }
  320. }
  321. /* Set rendering settings */
  322. ctx.glMatrixMode(GL_PROJECTION);
  323. ctx.glLoadIdentity();
  324. ctx.glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
  325. ctx.glMatrixMode(GL_MODELVIEW);
  326. ctx.glLoadIdentity();
  327. ctx.glEnable(GL_DEPTH_TEST);
  328. ctx.glDepthFunc(GL_LESS);
  329. ctx.glShadeModel(GL_SMOOTH);
  330. /* Main render loop */
  331. frames = 0;
  332. then = SDL_GetTicks();
  333. done = 0;
  334. while (!done) {
  335. /* Check for events */
  336. ++frames;
  337. while (SDL_PollEvent(&event)) {
  338. SDLTest_CommonEvent(state, &event, &done);
  339. }
  340. for (i = 0; i < state->num_windows; ++i) {
  341. int w, h;
  342. if (state->windows[i] == NULL)
  343. continue;
  344. SDL_GL_MakeCurrent(state->windows[i], context);
  345. SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
  346. ctx.glViewport(0, 0, w, h);
  347. Render();
  348. SDL_GL_SwapWindow(state->windows[i]);
  349. }
  350. }
  351. /* Print out some timing information */
  352. now = SDL_GetTicks();
  353. if (now > then) {
  354. SDL_Log("%2.2f frames per second\n",
  355. ((double) frames * 1000) / (now - then));
  356. }
  357. quit(0);
  358. return 0;
  359. }
  360. #else /* HAVE_OPENGL */
  361. int
  362. main(int argc, char *argv[])
  363. {
  364. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL support on this system\n");
  365. return 1;
  366. }
  367. #endif /* HAVE_OPENGL */