testgles2.c 20 KB

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