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.
 
 
 

256 lines
5.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cmath>
  14. #include <cstdio>
  15. #ifdef WIN32
  16. # define WIN32_LEAN_AND_MEAN
  17. # include <windows.h>
  18. #endif
  19. #include "core.h"
  20. #include "lolgl.h"
  21. namespace lol
  22. {
  23. #if defined ANDROID_NDK
  24. vec2i saved_viewport;
  25. #endif
  26. #if defined HAVE_GL_2X || defined HAVE_GLES_2X
  27. Shader *stdshader;
  28. #endif
  29. mat4 proj_matrix, view_matrix, model_matrix;
  30. #if defined HAVE_GL_2X || defined HAVE_GLES_2X
  31. static char const *vertexshader =
  32. #if !defined HAVE_GLES_2X
  33. "#version 130\n"
  34. #endif
  35. "\n"
  36. #if defined HAVE_GLES_2X
  37. "attribute vec3 in_Position;\n"
  38. "attribute vec2 in_TexCoord;\n"
  39. "varying vec2 pass_TexCoord;\n"
  40. #else
  41. "in vec3 in_Position;\n"
  42. "in vec2 in_TexCoord;\n"
  43. #endif
  44. //"in vec3 in_Color;\n"
  45. //"out vec3 pass_Color;\n"
  46. "uniform mat4 proj_matrix;\n"
  47. "uniform mat4 view_matrix;\n"
  48. "uniform mat4 model_matrix;\n"
  49. "\n"
  50. "void main()\n"
  51. "{\n"
  52. " gl_Position = proj_matrix * view_matrix * model_matrix"
  53. " * vec4(in_Position, 1.0f);\n"
  54. //" pass_Color = in_Color;\n"
  55. #if defined HAVE_GLES_2X
  56. " pass_TexCoord = in_TexCoord;\n"
  57. #else
  58. " gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
  59. #endif
  60. "}\n";
  61. static char const *fragmentshader =
  62. #if !defined HAVE_GLES_2X
  63. "#version 130\n"
  64. #endif
  65. "\n"
  66. "uniform sampler2D in_Texture;\n"
  67. //"in vec3 pass_Color;\n"
  68. //"out vec4 out_Color;\n"
  69. #if defined HAVE_GLES_2X
  70. "varying vec2 pass_TexCoord;\n"
  71. #endif
  72. "\n"
  73. "void main()\n"
  74. "{\n"
  75. #if defined HAVE_GLES_2X
  76. " gl_FragColor = texture2D(in_Texture, pass_TexCoord);\n"
  77. //" gl_FragColor = vec4(0.5, 1.0, 0.0, 0.5);\n"
  78. //" gl_FragColor = vec4(pass_TexCoord * 4.0, 0.0, 0.25);\n"
  79. #else
  80. " gl_FragColor = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
  81. #endif
  82. "}\n";
  83. #endif
  84. /*
  85. * Public Video class
  86. */
  87. void Video::Setup(int width, int height)
  88. {
  89. /* Initialise OpenGL */
  90. glViewport(0, 0, width, height);
  91. #if defined ANDROID_NDK
  92. saved_viewport = vec2i(width, height);
  93. #endif
  94. glClearColor(0.1f, 0.2f, 0.3f, 0.0f);
  95. glClearDepthf(1.0);
  96. #if defined HAVE_GL_2X || defined HAVE_GLES_1X
  97. glShadeModel(GL_SMOOTH);
  98. #endif
  99. #if defined HAVE_GL_2X || defined HAVE_GLES_1X
  100. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  101. #endif
  102. #if defined HAVE_GL_2X || defined HAVE_GLES_2X
  103. stdshader = Shader::Create(vertexshader, fragmentshader);
  104. #endif
  105. }
  106. void Video::SetFov(float theta)
  107. {
  108. #undef near /* Fuck Microsoft */
  109. #undef far /* Fuck Microsoft again */
  110. mat4 proj;
  111. float width = GetWidth();
  112. float height = GetHeight();
  113. float near = -width - height;
  114. float far = width + height;
  115. /* Set the projection matrix */
  116. if (theta < 1e-4f)
  117. {
  118. /* The easy way: purely orthogonal projection. */
  119. proj_matrix = mat4::ortho(0, width, 0, height, near, far);
  120. }
  121. else
  122. {
  123. /* Compute a view that approximates the glOrtho view when theta
  124. * approaches zero. This view ensures that the z=0 plane fills
  125. * the screen. */
  126. float t1 = tanf(theta / 2);
  127. float t2 = t1 * height / width;
  128. float dist = (float)width / (2.0f * t1);
  129. near += dist;
  130. far += dist;
  131. if (near <= 0.0f)
  132. {
  133. far -= (near - 1.0f);
  134. near = 1.0f;
  135. }
  136. proj_matrix = mat4::frustum(-near * t1, near * t1,
  137. -near * t2, near * t2, near, far)
  138. * mat4::translate(-0.5f * width, -0.5f * height, -dist);
  139. }
  140. view_matrix = mat4(1.0f);
  141. #if defined HAVE_GL_2X || defined HAVE_GLES_2X
  142. stdshader->Bind(); /* Required on GLES 2.x? */
  143. GLuint uni;
  144. uni = stdshader->GetUniformLocation("proj_matrix");
  145. glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]);
  146. uni = stdshader->GetUniformLocation("view_matrix");
  147. glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
  148. #else
  149. glMatrixMode(GL_PROJECTION);
  150. glLoadIdentity();
  151. glMultMatrixf(&proj_matrix[0][0]);
  152. /* Reset the model view matrix, just in case */
  153. glMatrixMode(GL_MODELVIEW);
  154. glLoadIdentity();
  155. glMultMatrixf(&view_matrix[0][0]);
  156. #endif
  157. }
  158. void Video::SetDepth(bool set)
  159. {
  160. if (set)
  161. glEnable(GL_DEPTH_TEST);
  162. else
  163. glDisable(GL_DEPTH_TEST);
  164. }
  165. void Video::Clear()
  166. {
  167. glViewport(0, 0, GetWidth(), GetHeight());
  168. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  169. SetFov(0.0f);
  170. }
  171. void Video::Destroy()
  172. {
  173. #if defined HAVE_GL_2X || defined HAVE_GLES_2X
  174. Shader::Destroy(stdshader);
  175. #endif
  176. }
  177. void Video::Capture(uint32_t *buffer)
  178. {
  179. GLint v[4];
  180. glGetIntegerv(GL_VIEWPORT, v);
  181. int width = v[2], height = v[3];
  182. #if defined HAVE_GL_1X || defined HAVE_GL_2X
  183. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  184. #endif
  185. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  186. #if defined GL_BGRA
  187. glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
  188. #else
  189. glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  190. #endif
  191. for (int j = 0; j < height / 2; j++)
  192. for (int i = 0; i < width; i++)
  193. {
  194. uint32_t tmp = buffer[j * width + i];
  195. buffer[j * width + i] = buffer[(height - j - 1) * width + i];
  196. buffer[(height - j - 1) * width + i] = tmp;
  197. }
  198. }
  199. int Video::GetWidth()
  200. {
  201. #if defined ANDROID_NDK
  202. return saved_viewport.x;
  203. #else
  204. GLint v[4];
  205. glGetIntegerv(GL_VIEWPORT, v);
  206. return v[2];
  207. #endif
  208. }
  209. int Video::GetHeight()
  210. {
  211. #if defined ANDROID_NDK
  212. return saved_viewport.y;
  213. #else
  214. GLint v[4];
  215. glGetIntegerv(GL_VIEWPORT, v);
  216. return v[3];
  217. #endif
  218. }
  219. } /* namespace lol */