PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/limare/tests/cube_companion_spinning/limare.c

https://gitlab.com/lima-ncc1988/lima-ncc1988
C | 196 lines | 144 code | 34 blank | 18 comment | 8 complexity | dd41a7ca5ff672c3659893e5fdc7af69 MD5 | raw file
  1. /*
  2. * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <GLES2/gl2.h>
  22. #include "limare.h"
  23. #include "formats.h"
  24. #include "esUtil.h"
  25. #include "companion.h"
  26. int
  27. main(int argc, char *argv[])
  28. {
  29. struct limare_state *state;
  30. int ret;
  31. const char *vertex_shader_source =
  32. "uniform mat4 modelviewMatrix;\n"
  33. "uniform mat4 modelviewprojectionMatrix;\n"
  34. "uniform mat3 normalMatrix;\n"
  35. "\n"
  36. "attribute vec4 in_position; \n"
  37. "attribute vec3 in_normal; \n"
  38. "attribute vec2 in_coord; \n"
  39. "\n"
  40. "vec4 lightSource = vec4(10.0, 20.0, 40.0, 0.0);\n"
  41. " \n"
  42. "varying vec4 vVaryingColor; \n"
  43. "varying vec2 coord; \n"
  44. " \n"
  45. "void main() \n"
  46. "{ \n"
  47. " gl_Position = modelviewprojectionMatrix * in_position;\n"
  48. " vec3 vEyeNormal = normalMatrix * in_normal;\n"
  49. " vec4 vPosition4 = modelviewMatrix * in_position;\n"
  50. " vec3 vPosition3 = vPosition4.xyz / vPosition4.w;\n"
  51. " vec3 vLightDir = normalize(lightSource.xyz - vPosition3);\n"
  52. " float diff = max(0.0, dot(vEyeNormal, vLightDir));\n"
  53. " vVaryingColor = vec4(diff * vec3(1.0, 1.0, 1.0), 1.0);\n"
  54. " coord = in_coord; \n"
  55. "} \n";
  56. const char *fragment_shader_source =
  57. "precision mediump float; \n"
  58. " \n"
  59. "varying vec4 vVaryingColor; \n"
  60. "varying vec2 coord; \n"
  61. " \n"
  62. "uniform sampler2D in_texture; \n"
  63. " \n"
  64. "void main() \n"
  65. "{ \n"
  66. " gl_FragColor = vVaryingColor * texture2D(in_texture, coord);\n"
  67. "} \n";
  68. state = limare_init();
  69. if (!state)
  70. return -1;
  71. //limare_buffer_clear(state);
  72. ret = limare_state_setup(state, 0, 0, 0xFF505050);
  73. if (ret)
  74. return ret;
  75. int width, height;
  76. limare_buffer_size(state, &width, &height);
  77. float aspect = (float) height / width;
  78. limare_enable(state, GL_DEPTH_TEST);
  79. limare_enable(state, GL_CULL_FACE);
  80. limare_depth_mask(state, 1);
  81. int program = limare_program_new(state);
  82. vertex_shader_attach(state, program, vertex_shader_source);
  83. fragment_shader_attach(state, program, fragment_shader_source);
  84. limare_link(state);
  85. int vertices_buffer =
  86. limare_attribute_buffer_upload(state, LIMARE_ATTRIB_FLOAT, 3,
  87. 0, COMPANION_VERTEX_COUNT,
  88. companion_vertices);
  89. int texture_coordinates_buffer =
  90. limare_attribute_buffer_upload(state, LIMARE_ATTRIB_FLOAT, 2,
  91. 0, COMPANION_VERTEX_COUNT,
  92. companion_texture_coordinates);
  93. int normals_buffer =
  94. limare_attribute_buffer_upload(state, LIMARE_ATTRIB_FLOAT, 3,
  95. 0, COMPANION_VERTEX_COUNT,
  96. companion_normals);
  97. limare_attribute_buffer_attach(state, "in_position",
  98. vertices_buffer);
  99. limare_attribute_buffer_attach(state, "in_coord",
  100. texture_coordinates_buffer);
  101. limare_attribute_buffer_attach(state, "in_normal",
  102. normals_buffer);
  103. int elements_buffer =
  104. limare_elements_buffer_upload(state, GL_TRIANGLES,
  105. GL_UNSIGNED_SHORT,
  106. COMPANION_INDEX_COUNT,
  107. companion_triangles);
  108. int texture = limare_texture_upload(state, companion_texture,
  109. COMPANION_TEXTURE_WIDTH,
  110. COMPANION_TEXTURE_HEIGHT,
  111. COMPANION_TEXTURE_FORMAT, 0);
  112. limare_texture_attach(state, "in_texture", texture);
  113. int i = 0;
  114. while (1) {
  115. i++;
  116. if (i == 0xFFFFFFF)
  117. i = 0;
  118. float angle = 0.5 * i;
  119. ESMatrix modelview;
  120. esMatrixLoadIdentity(&modelview);
  121. esTranslate(&modelview, 0.0, 0.0, -4.0);
  122. esRotate(&modelview, angle * 0.97, 1.0, 0.0, 0.0);
  123. esRotate(&modelview, angle * 1.13, 0.0, 1.0, 0.0);
  124. esRotate(&modelview, angle * 0.73, 0.0, 0.0, 1.0);
  125. ESMatrix projection;
  126. esMatrixLoadIdentity(&projection);
  127. esFrustum(&projection, -1.0, +1.0, -1.0 * aspect, +1.0 * aspect,
  128. 1.0, 10.0);
  129. ESMatrix modelviewprojection;
  130. esMatrixLoadIdentity(&modelviewprojection);
  131. esMatrixMultiply(&modelviewprojection, &modelview, &projection);
  132. float normal[9];
  133. normal[0] = modelview.m[0][0];
  134. normal[1] = modelview.m[0][1];
  135. normal[2] = modelview.m[0][2];
  136. normal[3] = modelview.m[1][0];
  137. normal[4] = modelview.m[1][1];
  138. normal[5] = modelview.m[1][2];
  139. normal[6] = modelview.m[2][0];
  140. normal[7] = modelview.m[2][1];
  141. normal[8] = modelview.m[2][2];
  142. limare_uniform_attach(state, "modelviewMatrix", 16,
  143. &modelview.m[0][0]);
  144. limare_uniform_attach(state, "modelviewprojectionMatrix", 16,
  145. &modelviewprojection.m[0][0]);
  146. limare_uniform_attach(state, "normalMatrix", 9, normal);
  147. limare_frame_new(state);
  148. ret = limare_draw_elements_buffer(state, elements_buffer);
  149. if (ret)
  150. return ret;
  151. ret = limare_frame_flush(state);
  152. if (ret)
  153. return ret;
  154. limare_buffer_swap(state);
  155. #if 1
  156. if (i >= 6400)
  157. break;
  158. #endif
  159. }
  160. limare_finish(state);
  161. return 0;
  162. }