🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

765 lines
21 KiB

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