PageRenderTime 72ms CodeModel.GetById 46ms RepoModel.GetById 0ms app.codeStats 0ms

/limare/tests/cube_textured_spinning/egl.c

https://gitlab.com/lima-ncc1988/lima-ncc1988
C | 239 lines | 167 code | 47 blank | 25 comment | 7 complexity | b8bce8f5124aaf8704ca1723529b92dd MD5 | raw file
  1. /*
  2. * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be>
  3. * Copyright (c) 2012 Arvin Schnell <arvin.schnell@gmail.com>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial portions
  14. * of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <time.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <EGL/egl.h>
  31. #include <GLES2/gl2.h>
  32. #include "egl_common.h"
  33. #include "esUtil.h"
  34. #include "cube_mesh.h"
  35. #include "companion.h"
  36. int
  37. main(int argc, char *argv[])
  38. {
  39. EGLDisplay display;
  40. EGLSurface surface;
  41. GLuint vertex_shader;
  42. GLuint fragment_shader;
  43. GLuint program;
  44. GLint ret;
  45. GLint width, height;
  46. GLuint texture;
  47. const char *vertex_shader_source =
  48. "uniform mat4 modelviewMatrix;\n"
  49. "uniform mat4 modelviewprojectionMatrix;\n"
  50. "uniform mat3 normalMatrix;\n"
  51. "\n"
  52. "attribute vec4 in_position; \n"
  53. "attribute vec3 in_normal; \n"
  54. "attribute vec2 in_coord; \n"
  55. "\n"
  56. "vec4 lightSource = vec4(10.0, 20.0, 40.0, 0.0);\n"
  57. " \n"
  58. "varying vec4 vVaryingColor; \n"
  59. "varying vec2 coord; \n"
  60. " \n"
  61. "void main() \n"
  62. "{ \n"
  63. " gl_Position = modelviewprojectionMatrix * in_position;\n"
  64. " vec3 vEyeNormal = normalMatrix * in_normal;\n"
  65. " vec4 vPosition4 = modelviewMatrix * in_position;\n"
  66. " vec3 vPosition3 = vPosition4.xyz / vPosition4.w;\n"
  67. " vec3 vLightDir = normalize(lightSource.xyz - vPosition3);\n"
  68. " float diff = max(0.0, dot(vEyeNormal, vLightDir));\n"
  69. " vVaryingColor = vec4(diff * vec3(1.0, 1.0, 1.0), 1.0);\n"
  70. " coord = in_coord; \n"
  71. "} \n";
  72. const char *fragment_shader_source =
  73. "precision mediump float; \n"
  74. " \n"
  75. "varying vec4 vVaryingColor; \n"
  76. "varying vec2 coord; \n"
  77. " \n"
  78. "uniform sampler2D in_texture; \n"
  79. " \n"
  80. "void main() \n"
  81. "{ \n"
  82. " gl_FragColor = vVaryingColor * texture2D(in_texture, coord);\n"
  83. "} \n";
  84. buffer_size(&width, &height);
  85. printf("Buffer dimensions %dx%d\n", width, height);
  86. float aspect = (float) height / width;
  87. display = egl_display_init();
  88. surface = egl_surface_init(display, 2, width, height);
  89. glViewport(0, 0, width, height);
  90. glClearColor(0.5, 0.5, 0.5, 1.0);
  91. glClear(GL_COLOR_BUFFER_BIT);
  92. glEnable(GL_CULL_FACE);
  93. glEnable(GL_DEPTH_TEST);
  94. vertex_shader = vertex_shader_compile(vertex_shader_source);
  95. fragment_shader = fragment_shader_compile(fragment_shader_source);
  96. program = glCreateProgram();
  97. if (!program) {
  98. printf("Error: failed to create program!\n");
  99. return -1;
  100. }
  101. glAttachShader(program, vertex_shader);
  102. glAttachShader(program, fragment_shader);
  103. glBindAttribLocation(program, 0, "in_position");
  104. glBindAttribLocation(program, 1, "in_normal");
  105. glBindAttribLocation(program, 2, "in_coord");
  106. glLinkProgram(program);
  107. glGetProgramiv(program, GL_LINK_STATUS, &ret);
  108. if (!ret) {
  109. char *log;
  110. printf("Error: program linking failed!:\n");
  111. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &ret);
  112. if (ret > 1) {
  113. log = malloc(ret);
  114. glGetProgramInfoLog(program, ret, NULL, log);
  115. printf("%s", log);
  116. }
  117. return -1;
  118. }
  119. glUseProgram(program);
  120. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, cube_vertices);
  121. glEnableVertexAttribArray(0);
  122. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, cube_normals);
  123. glEnableVertexAttribArray(1);
  124. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0,
  125. cube_texture_coordinates);
  126. glEnableVertexAttribArray(2);
  127. glActiveTexture(GL_TEXTURE0);
  128. glGenTextures(1, &texture);
  129. glBindTexture(GL_TEXTURE_2D, texture);
  130. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  131. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  132. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  133. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  134. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  135. COMPANION_TEXTURE_WIDTH, COMPANION_TEXTURE_HEIGHT, 0,
  136. GL_RGB, GL_UNSIGNED_BYTE, companion_texture_flat);
  137. GLint texture_loc = glGetUniformLocation(program, "in_texture");
  138. glUniform1i(texture_loc, 0); // 0 -> GL_TEXTURE0 in glActiveTexture
  139. framerate_init();
  140. int i = 0;
  141. while (1) {
  142. i++;
  143. if (i == 0xFFFFFFF)
  144. i = 0;
  145. float angle = 0.5 * i;
  146. ESMatrix modelview;
  147. esMatrixLoadIdentity(&modelview);
  148. esTranslate(&modelview, 0.0, 0.0, -4.0);
  149. esRotate(&modelview, angle * 0.97, 1.0, 0.0, 0.0);
  150. esRotate(&modelview, angle * 1.13, 0.0, 1.0, 0.0);
  151. esRotate(&modelview, angle * 0.73, 0.0, 0.0, 1.0);
  152. ESMatrix projection;
  153. esMatrixLoadIdentity(&projection);
  154. esFrustum(&projection, -1.0, +1.0, -1.0 * aspect, +1.0 * aspect,
  155. 1.0, 10.0);
  156. ESMatrix modelviewprojection;
  157. esMatrixLoadIdentity(&modelviewprojection);
  158. esMatrixMultiply(&modelviewprojection, &modelview, &projection);
  159. float normal[9];
  160. normal[0] = modelview.m[0][0];
  161. normal[1] = modelview.m[0][1];
  162. normal[2] = modelview.m[0][2];
  163. normal[3] = modelview.m[1][0];
  164. normal[4] = modelview.m[1][1];
  165. normal[5] = modelview.m[1][2];
  166. normal[6] = modelview.m[2][0];
  167. normal[7] = modelview.m[2][1];
  168. normal[8] = modelview.m[2][2];
  169. GLint modelviewmatrix_handle =
  170. glGetUniformLocation(program, "modelviewMatrix");
  171. GLint modelviewprojectionmatrix_handle =
  172. glGetUniformLocation(program, "modelviewprojectionMatrix");
  173. GLint normalmatrix_handle =
  174. glGetUniformLocation(program, "normalMatrix");
  175. glUniformMatrix4fv(modelviewmatrix_handle,
  176. 1, GL_FALSE, &modelview.m[0][0]);
  177. glUniformMatrix4fv(modelviewprojectionmatrix_handle,
  178. 1, GL_FALSE, &modelviewprojection.m[0][0]);
  179. glUniformMatrix3fv(normalmatrix_handle, 1, GL_FALSE, normal);
  180. glDrawElements(GL_TRIANGLES, CUBE_INDEX_COUNT,
  181. GL_UNSIGNED_BYTE, cube_indices);
  182. eglSwapBuffers(display, surface);
  183. framerate_print(256, i);
  184. #if 1
  185. if (i >= 6400)
  186. break;
  187. #endif
  188. }
  189. usleep(1000000);
  190. fflush(stdout);
  191. return 0;
  192. }