testgles2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. Copyright (r) 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. #if defined(__IPHONEOS__) || defined(__ANDROID__)
  16. #define HAVE_OPENGLES2
  17. #endif
  18. #ifdef HAVE_OPENGLES2
  19. #include "SDL_opengles2.h"
  20. typedef struct GLES2_Context
  21. {
  22. #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
  23. #include "../src/render/opengles2/SDL_gles2funcs.h"
  24. #undef SDL_PROC
  25. } GLES2_Context;
  26. static SDLTest_CommonState *state;
  27. static SDL_GLContext *context = NULL;
  28. static int depth = 16;
  29. static GLES2_Context ctx;
  30. static int LoadContext(GLES2_Context * data)
  31. {
  32. #if SDL_VIDEO_DRIVER_UIKIT
  33. #define __SDL_NOGETPROCADDR__
  34. #elif SDL_VIDEO_DRIVER_ANDROID
  35. #define __SDL_NOGETPROCADDR__
  36. #elif SDL_VIDEO_DRIVER_PANDORA
  37. #define __SDL_NOGETPROCADDR__
  38. #endif
  39. #if defined __SDL_NOGETPROCADDR__
  40. #define SDL_PROC(ret,func,params) data->func=func;
  41. #else
  42. #define SDL_PROC(ret,func,params) \
  43. do { \
  44. data->func = SDL_GL_GetProcAddress(#func); \
  45. if ( ! data->func ) { \
  46. return SDL_SetError("Couldn't load GLES2 function %s: %s\n", #func, SDL_GetError()); \
  47. } \
  48. } while ( 0 );
  49. #endif /* _SDL_NOGETPROCADDR_ */
  50. #include "../src/render/opengles2/SDL_gles2funcs.h"
  51. #undef SDL_PROC
  52. return 0;
  53. }
  54. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  55. static void
  56. quit(int rc)
  57. {
  58. int i;
  59. if (context != NULL) {
  60. for (i = 0; i < state->num_windows; i++) {
  61. if (context[i]) {
  62. SDL_GL_DeleteContext(context[i]);
  63. }
  64. }
  65. SDL_free(context);
  66. }
  67. SDLTest_CommonQuit(state);
  68. exit(rc);
  69. }
  70. #define GL_CHECK(x) \
  71. x; \
  72. { \
  73. GLenum glError = ctx.glGetError(); \
  74. if(glError != GL_NO_ERROR) { \
  75. SDL_Log("glGetError() = %i (0x%.8x) at line %i\n", glError, glError, __LINE__); \
  76. quit(1); \
  77. } \
  78. }
  79. /*
  80. * Simulates desktop's glRotatef. The matrix is returned in column-major
  81. * order.
  82. */
  83. static void
  84. rotate_matrix(double angle, double x, double y, double z, float *r)
  85. {
  86. double radians, c, s, c1, u[3], length;
  87. int i, j;
  88. radians = (angle * M_PI) / 180.0;
  89. c = cos(radians);
  90. s = sin(radians);
  91. c1 = 1.0 - cos(radians);
  92. length = sqrt(x * x + y * y + z * z);
  93. u[0] = x / length;
  94. u[1] = y / length;
  95. u[2] = z / length;
  96. for (i = 0; i < 16; i++) {
  97. r[i] = 0.0;
  98. }
  99. r[15] = 1.0;
  100. for (i = 0; i < 3; i++) {
  101. r[i * 4 + (i + 1) % 3] = u[(i + 2) % 3] * s;
  102. r[i * 4 + (i + 2) % 3] = -u[(i + 1) % 3] * s;
  103. }
  104. for (i = 0; i < 3; i++) {
  105. for (j = 0; j < 3; j++) {
  106. r[i * 4 + j] += c1 * u[i] * u[j] + (i == j ? c : 0.0);
  107. }
  108. }
  109. }
  110. /*
  111. * Simulates gluPerspectiveMatrix
  112. */
  113. static void
  114. perspective_matrix(double fovy, double aspect, double znear, double zfar, float *r)
  115. {
  116. int i;
  117. double f;
  118. f = 1.0/tan(fovy * 0.5);
  119. for (i = 0; i < 16; i++) {
  120. r[i] = 0.0;
  121. }
  122. r[0] = f / aspect;
  123. r[5] = f;
  124. r[10] = (znear + zfar) / (znear - zfar);
  125. r[11] = -1.0;
  126. r[14] = (2.0 * znear * zfar) / (znear - zfar);
  127. r[15] = 0.0;
  128. }
  129. /*
  130. * Multiplies lhs by rhs and writes out to r. All matrices are 4x4 and column
  131. * major. In-place multiplication is supported.
  132. */
  133. static void
  134. multiply_matrix(float *lhs, float *rhs, float *r)
  135. {
  136. int i, j, k;
  137. float tmp[16];
  138. for (i = 0; i < 4; i++) {
  139. for (j = 0; j < 4; j++) {
  140. tmp[j * 4 + i] = 0.0;
  141. for (k = 0; k < 4; k++) {
  142. tmp[j * 4 + i] += lhs[k * 4 + i] * rhs[j * 4 + k];
  143. }
  144. }
  145. }
  146. for (i = 0; i < 16; i++) {
  147. r[i] = tmp[i];
  148. }
  149. }
  150. /*
  151. * Create shader, load in source, compile, dump debug as necessary.
  152. *
  153. * shader: Pointer to return created shader ID.
  154. * source: Passed-in shader source code.
  155. * shader_type: Passed to GL, e.g. GL_VERTEX_SHADER.
  156. */
  157. void
  158. process_shader(GLuint *shader, const char * source, GLint shader_type)
  159. {
  160. GLint status = GL_FALSE;
  161. const char *shaders[1] = { NULL };
  162. /* Create shader and load into GL. */
  163. *shader = GL_CHECK(ctx.glCreateShader(shader_type));
  164. shaders[0] = source;
  165. GL_CHECK(ctx.glShaderSource(*shader, 1, shaders, NULL));
  166. /* Clean up shader source. */
  167. shaders[0] = NULL;
  168. /* Try compiling the shader. */
  169. GL_CHECK(ctx.glCompileShader(*shader));
  170. GL_CHECK(ctx.glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
  171. // Dump debug info (source and log) if compilation failed.
  172. if(status != GL_TRUE) {
  173. SDL_Log("Shader compilation failed");
  174. quit(-1);
  175. }
  176. }
  177. /* 3D data. Vertex range -0.5..0.5 in all axes.
  178. * Z -0.5 is near, 0.5 is far. */
  179. const float _vertices[] =
  180. {
  181. /* Front face. */
  182. /* Bottom left */
  183. -0.5, 0.5, -0.5,
  184. 0.5, -0.5, -0.5,
  185. -0.5, -0.5, -0.5,
  186. /* Top right */
  187. -0.5, 0.5, -0.5,
  188. 0.5, 0.5, -0.5,
  189. 0.5, -0.5, -0.5,
  190. /* Left face */
  191. /* Bottom left */
  192. -0.5, 0.5, 0.5,
  193. -0.5, -0.5, -0.5,
  194. -0.5, -0.5, 0.5,
  195. /* Top right */
  196. -0.5, 0.5, 0.5,
  197. -0.5, 0.5, -0.5,
  198. -0.5, -0.5, -0.5,
  199. /* Top face */
  200. /* Bottom left */
  201. -0.5, 0.5, 0.5,
  202. 0.5, 0.5, -0.5,
  203. -0.5, 0.5, -0.5,
  204. /* Top right */
  205. -0.5, 0.5, 0.5,
  206. 0.5, 0.5, 0.5,
  207. 0.5, 0.5, -0.5,
  208. /* Right face */
  209. /* Bottom left */
  210. 0.5, 0.5, -0.5,
  211. 0.5, -0.5, 0.5,
  212. 0.5, -0.5, -0.5,
  213. /* Top right */
  214. 0.5, 0.5, -0.5,
  215. 0.5, 0.5, 0.5,
  216. 0.5, -0.5, 0.5,
  217. /* Back face */
  218. /* Bottom left */
  219. 0.5, 0.5, 0.5,
  220. -0.5, -0.5, 0.5,
  221. 0.5, -0.5, 0.5,
  222. /* Top right */
  223. 0.5, 0.5, 0.5,
  224. -0.5, 0.5, 0.5,
  225. -0.5, -0.5, 0.5,
  226. /* Bottom face */
  227. /* Bottom left */
  228. -0.5, -0.5, -0.5,
  229. 0.5, -0.5, 0.5,
  230. -0.5, -0.5, 0.5,
  231. /* Top right */
  232. -0.5, -0.5, -0.5,
  233. 0.5, -0.5, -0.5,
  234. 0.5, -0.5, 0.5,
  235. };
  236. const float _colors[] =
  237. {
  238. /* Front face */
  239. /* Bottom left */
  240. 1.0, 0.0, 0.0, /* red */
  241. 0.0, 0.0, 1.0, /* blue */
  242. 0.0, 1.0, 0.0, /* green */
  243. /* Top right */
  244. 1.0, 0.0, 0.0, /* red */
  245. 1.0, 1.0, 0.0, /* yellow */
  246. 0.0, 0.0, 1.0, /* blue */
  247. /* Left face */
  248. /* Bottom left */
  249. 1.0, 1.0, 1.0, /* white */
  250. 0.0, 1.0, 0.0, /* green */
  251. 0.0, 1.0, 1.0, /* cyan */
  252. /* Top right */
  253. 1.0, 1.0, 1.0, /* white */
  254. 1.0, 0.0, 0.0, /* red */
  255. 0.0, 1.0, 0.0, /* green */
  256. /* Top face */
  257. /* Bottom left */
  258. 1.0, 1.0, 1.0, /* white */
  259. 1.0, 1.0, 0.0, /* yellow */
  260. 1.0, 0.0, 0.0, /* red */
  261. /* Top right */
  262. 1.0, 1.0, 1.0, /* white */
  263. 0.0, 0.0, 0.0, /* black */
  264. 1.0, 1.0, 0.0, /* yellow */
  265. /* Right face */
  266. /* Bottom left */
  267. 1.0, 1.0, 0.0, /* yellow */
  268. 1.0, 0.0, 1.0, /* magenta */
  269. 0.0, 0.0, 1.0, /* blue */
  270. /* Top right */
  271. 1.0, 1.0, 0.0, /* yellow */
  272. 0.0, 0.0, 0.0, /* black */
  273. 1.0, 0.0, 1.0, /* magenta */
  274. /* Back face */
  275. /* Bottom left */
  276. 0.0, 0.0, 0.0, /* black */
  277. 0.0, 1.0, 1.0, /* cyan */
  278. 1.0, 0.0, 1.0, /* magenta */
  279. /* Top right */
  280. 0.0, 0.0, 0.0, /* black */
  281. 1.0, 1.0, 1.0, /* white */
  282. 0.0, 1.0, 1.0, /* cyan */
  283. /* Bottom face */
  284. /* Bottom left */
  285. 0.0, 1.0, 0.0, /* green */
  286. 1.0, 0.0, 1.0, /* magenta */
  287. 0.0, 1.0, 1.0, /* cyan */
  288. /* Top right */
  289. 0.0, 1.0, 0.0, /* green */
  290. 0.0, 0.0, 1.0, /* blue */
  291. 1.0, 0.0, 1.0, /* magenta */
  292. };
  293. const char* _shader_vert_src =
  294. " attribute vec4 av4position; "
  295. " attribute vec3 av3color; "
  296. " uniform mat4 mvp; "
  297. " varying vec3 vv3color; "
  298. " void main() { "
  299. " vv3color = av3color; "
  300. " gl_Position = mvp * av4position; "
  301. " } ";
  302. const char* _shader_frag_src =
  303. " precision lowp float; "
  304. " varying vec3 vv3color; "
  305. " void main() { "
  306. " gl_FragColor = vec4(vv3color, 1.0); "
  307. " } ";
  308. typedef struct shader_data
  309. {
  310. GLuint shader_program, shader_frag, shader_vert;
  311. GLint attr_position;
  312. GLint attr_color, attr_mvp;
  313. int angle_x, angle_y, angle_z;
  314. } shader_data;
  315. static void
  316. Render(unsigned int width, unsigned int height, shader_data* data)
  317. {
  318. float matrix_rotate[16], matrix_modelview[16], matrix_perspective[16], matrix_mvp[16];
  319. /*
  320. * Do some rotation with Euler angles. It is not a fixed axis as
  321. * quaterions would be, but the effect is cool.
  322. */
  323. rotate_matrix(data->angle_x, 1.0, 0.0, 0.0, matrix_modelview);
  324. rotate_matrix(data->angle_y, 0.0, 1.0, 0.0, matrix_rotate);
  325. multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
  326. rotate_matrix(data->angle_z, 0.0, 1.0, 0.0, matrix_rotate);
  327. multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
  328. /* Pull the camera back from the cube */
  329. matrix_modelview[14] -= 2.5;
  330. perspective_matrix(45.0, (double)width/(double)height, 0.01, 100.0, matrix_perspective);
  331. multiply_matrix(matrix_perspective, matrix_modelview, matrix_mvp);
  332. GL_CHECK(ctx.glUniformMatrix4fv(data->attr_mvp, 1, GL_FALSE, matrix_mvp));
  333. data->angle_x += 3;
  334. data->angle_y += 2;
  335. data->angle_z += 1;
  336. if(data->angle_x >= 360) data->angle_x -= 360;
  337. if(data->angle_x < 0) data->angle_x += 360;
  338. if(data->angle_y >= 360) data->angle_y -= 360;
  339. if(data->angle_y < 0) data->angle_y += 360;
  340. if(data->angle_z >= 360) data->angle_z -= 360;
  341. if(data->angle_z < 0) data->angle_z += 360;
  342. GL_CHECK(ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
  343. GL_CHECK(ctx.glDrawArrays(GL_TRIANGLES, 0, 36));
  344. }
  345. int
  346. main(int argc, char *argv[])
  347. {
  348. int fsaa, accel;
  349. int value;
  350. int i, done;
  351. SDL_DisplayMode mode;
  352. SDL_Event event;
  353. Uint32 then, now, frames;
  354. int status;
  355. shader_data *datas, *data;
  356. /* Initialize parameters */
  357. fsaa = 0;
  358. accel = 0;
  359. /* Initialize test framework */
  360. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  361. if (!state) {
  362. return 1;
  363. }
  364. for (i = 1; i < argc;) {
  365. int consumed;
  366. consumed = SDLTest_CommonArg(state, i);
  367. if (consumed == 0) {
  368. if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
  369. ++fsaa;
  370. consumed = 1;
  371. } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
  372. ++accel;
  373. consumed = 1;
  374. } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
  375. i++;
  376. if (!argv[i]) {
  377. consumed = -1;
  378. } else {
  379. depth = SDL_atoi(argv[i]);
  380. consumed = 1;
  381. }
  382. } else {
  383. consumed = -1;
  384. }
  385. }
  386. if (consumed < 0) {
  387. SDL_Log ("Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
  388. SDLTest_CommonUsage(state));
  389. quit(1);
  390. }
  391. i += consumed;
  392. }
  393. /* Set OpenGL parameters */
  394. state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
  395. state->gl_red_size = 5;
  396. state->gl_green_size = 5;
  397. state->gl_blue_size = 5;
  398. state->gl_depth_size = depth;
  399. state->gl_major_version = 2;
  400. state->gl_minor_version = 0;
  401. state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
  402. if (fsaa) {
  403. state->gl_multisamplebuffers=1;
  404. state->gl_multisamplesamples=fsaa;
  405. }
  406. if (accel) {
  407. state->gl_accelerated=1;
  408. }
  409. if (!SDLTest_CommonInit(state)) {
  410. quit(2);
  411. return 0;
  412. }
  413. context = SDL_calloc(state->num_windows, sizeof(context));
  414. if (context == NULL) {
  415. SDL_Log("Out of memory!\n");
  416. quit(2);
  417. }
  418. /* Create OpenGL ES contexts */
  419. for (i = 0; i < state->num_windows; i++) {
  420. context[i] = SDL_GL_CreateContext(state->windows[i]);
  421. if (!context[i]) {
  422. SDL_Log("SDL_GL_CreateContext(): %s\n", SDL_GetError());
  423. quit(2);
  424. }
  425. }
  426. /* Important: call this *after* creating the context */
  427. if (LoadContext(&ctx) < 0) {
  428. SDL_Log("Could not load GLES2 functions\n");
  429. quit(2);
  430. return 0;
  431. }
  432. if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
  433. SDL_GL_SetSwapInterval(1);
  434. } else {
  435. SDL_GL_SetSwapInterval(0);
  436. }
  437. SDL_GetCurrentDisplayMode(0, &mode);
  438. SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
  439. SDL_Log("\n");
  440. SDL_Log("Vendor : %s\n", ctx.glGetString(GL_VENDOR));
  441. SDL_Log("Renderer : %s\n", ctx.glGetString(GL_RENDERER));
  442. SDL_Log("Version : %s\n", ctx.glGetString(GL_VERSION));
  443. SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
  444. SDL_Log("\n");
  445. status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
  446. if (!status) {
  447. SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
  448. } else {
  449. SDL_Log( "Failed to get SDL_GL_RED_SIZE: %s\n",
  450. SDL_GetError());
  451. }
  452. status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
  453. if (!status) {
  454. SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
  455. } else {
  456. SDL_Log( "Failed to get SDL_GL_GREEN_SIZE: %s\n",
  457. SDL_GetError());
  458. }
  459. status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
  460. if (!status) {
  461. SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
  462. } else {
  463. SDL_Log( "Failed to get SDL_GL_BLUE_SIZE: %s\n",
  464. SDL_GetError());
  465. }
  466. status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
  467. if (!status) {
  468. SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
  469. } else {
  470. SDL_Log( "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
  471. SDL_GetError());
  472. }
  473. if (fsaa) {
  474. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
  475. if (!status) {
  476. SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
  477. } else {
  478. SDL_Log( "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
  479. SDL_GetError());
  480. }
  481. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
  482. if (!status) {
  483. SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
  484. value);
  485. } else {
  486. SDL_Log( "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
  487. SDL_GetError());
  488. }
  489. }
  490. if (accel) {
  491. status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
  492. if (!status) {
  493. SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
  494. } else {
  495. SDL_Log( "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
  496. SDL_GetError());
  497. }
  498. }
  499. datas = SDL_calloc(state->num_windows, sizeof(shader_data));
  500. /* Set rendering settings for each context */
  501. for (i = 0; i < state->num_windows; ++i) {
  502. status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  503. if (status) {
  504. SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  505. /* Continue for next window */
  506. continue;
  507. }
  508. ctx.glViewport(0, 0, state->window_w, state->window_h);
  509. data = &datas[i];
  510. data->angle_x = 0; data->angle_y = 0; data->angle_z = 0;
  511. /* Shader Initialization */
  512. process_shader(&data->shader_vert, _shader_vert_src, GL_VERTEX_SHADER);
  513. process_shader(&data->shader_frag, _shader_frag_src, GL_FRAGMENT_SHADER);
  514. /* Create shader_program (ready to attach shaders) */
  515. data->shader_program = GL_CHECK(ctx.glCreateProgram());
  516. /* Attach shaders and link shader_program */
  517. GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_vert));
  518. GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_frag));
  519. GL_CHECK(ctx.glLinkProgram(data->shader_program));
  520. /* Get attribute locations of non-fixed attributes like color and texture coordinates. */
  521. data->attr_position = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av4position"));
  522. data->attr_color = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av3color"));
  523. /* Get uniform locations */
  524. data->attr_mvp = GL_CHECK(ctx.glGetUniformLocation(data->shader_program, "mvp"));
  525. GL_CHECK(ctx.glUseProgram(data->shader_program));
  526. /* Enable attributes for position, color and texture coordinates etc. */
  527. GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_position));
  528. GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_color));
  529. /* Populate attributes for position, color and texture coordinates etc. */
  530. GL_CHECK(ctx.glVertexAttribPointer(data->attr_position, 3, GL_FLOAT, GL_FALSE, 0, _vertices));
  531. GL_CHECK(ctx.glVertexAttribPointer(data->attr_color, 3, GL_FLOAT, GL_FALSE, 0, _colors));
  532. GL_CHECK(ctx.glEnable(GL_CULL_FACE));
  533. GL_CHECK(ctx.glEnable(GL_DEPTH_TEST));
  534. }
  535. /* Main render loop */
  536. frames = 0;
  537. then = SDL_GetTicks();
  538. done = 0;
  539. while (!done) {
  540. /* Check for events */
  541. ++frames;
  542. while (SDL_PollEvent(&event) && !done) {
  543. switch (event.type) {
  544. case SDL_WINDOWEVENT:
  545. switch (event.window.event) {
  546. case SDL_WINDOWEVENT_RESIZED:
  547. for (i = 0; i < state->num_windows; ++i) {
  548. if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
  549. status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  550. if (status) {
  551. SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  552. break;
  553. }
  554. /* Change view port to the new window dimensions */
  555. ctx.glViewport(0, 0, event.window.data1, event.window.data2);
  556. /* Update window content */
  557. Render(event.window.data1, event.window.data2, &datas[i]);
  558. SDL_GL_SwapWindow(state->windows[i]);
  559. break;
  560. }
  561. }
  562. break;
  563. }
  564. }
  565. SDLTest_CommonEvent(state, &event, &done);
  566. }
  567. if (!done) {
  568. for (i = 0; i < state->num_windows; ++i) {
  569. status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  570. if (status) {
  571. SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  572. /* Continue for next window */
  573. continue;
  574. }
  575. Render(state->window_w, state->window_h, &datas[i]);
  576. SDL_GL_SwapWindow(state->windows[i]);
  577. }
  578. }
  579. }
  580. /* Print out some timing information */
  581. now = SDL_GetTicks();
  582. if (now > then) {
  583. SDL_Log("%2.2f frames per second\n",
  584. ((double) frames * 1000) / (now - then));
  585. }
  586. #if !defined(__ANDROID__)
  587. quit(0);
  588. #endif
  589. return 0;
  590. }
  591. #else /* HAVE_OPENGLES2 */
  592. int
  593. main(int argc, char *argv[])
  594. {
  595. SDL_Log("No OpenGL ES support on this system\n");
  596. return 1;
  597. }
  598. #endif /* HAVE_OPENGLES2 */
  599. /* vi: set ts=4 sw=4 expandtab: */