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 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cmath> |
---|
16 | |
---|
17 | #ifdef WIN32 |
---|
18 | # define WIN32_LEAN_AND_MEAN |
---|
19 | # include <windows.h> |
---|
20 | #endif |
---|
21 | |
---|
22 | #include "core.h" |
---|
23 | #include "lolgl.h" |
---|
24 | |
---|
25 | namespace lol |
---|
26 | { |
---|
27 | |
---|
28 | #if defined ANDROID_NDK |
---|
29 | vec2i saved_viewport; |
---|
30 | #endif |
---|
31 | |
---|
32 | #if defined HAVE_GL_2X || defined HAVE_GLES_2X |
---|
33 | Shader *stdshader; |
---|
34 | #endif |
---|
35 | mat4 proj_matrix, view_matrix, model_matrix; |
---|
36 | |
---|
37 | #if defined HAVE_GL_2X || defined HAVE_GLES_2X |
---|
38 | static char const *vertexshader = |
---|
39 | #if !defined HAVE_GLES_2X |
---|
40 | "#version 130\n" |
---|
41 | #endif |
---|
42 | "\n" |
---|
43 | #if defined HAVE_GLES_2X |
---|
44 | "attribute vec3 in_Position;\n" |
---|
45 | "attribute vec2 in_TexCoord;\n" |
---|
46 | "varying vec2 pass_TexCoord;\n" |
---|
47 | #else |
---|
48 | "in vec3 in_Position;\n" |
---|
49 | "in vec2 in_TexCoord;\n" |
---|
50 | #endif |
---|
51 | //"in vec3 in_Color;\n" |
---|
52 | //"out vec3 pass_Color;\n" |
---|
53 | "uniform mat4 proj_matrix;\n" |
---|
54 | "uniform mat4 view_matrix;\n" |
---|
55 | "uniform mat4 model_matrix;\n" |
---|
56 | "\n" |
---|
57 | "void main()\n" |
---|
58 | "{\n" |
---|
59 | " gl_Position = proj_matrix * view_matrix * model_matrix" |
---|
60 | " * vec4(in_Position, 1.0);\n" |
---|
61 | //" pass_Color = in_Color;\n" |
---|
62 | #if defined HAVE_GLES_2X |
---|
63 | " pass_TexCoord = in_TexCoord;\n" |
---|
64 | #else |
---|
65 | " gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n" |
---|
66 | #endif |
---|
67 | "}\n"; |
---|
68 | |
---|
69 | static char const *fragmentshader = |
---|
70 | #if !defined HAVE_GLES_2X |
---|
71 | "#version 130\n" |
---|
72 | #endif |
---|
73 | "\n" |
---|
74 | "uniform sampler2D in_Texture;\n" |
---|
75 | //"in vec3 pass_Color;\n" |
---|
76 | //"out vec4 out_Color;\n" |
---|
77 | #if defined HAVE_GLES_2X |
---|
78 | "varying vec2 pass_TexCoord;\n" |
---|
79 | #endif |
---|
80 | "\n" |
---|
81 | "void main()\n" |
---|
82 | "{\n" |
---|
83 | #if defined HAVE_GLES_2X |
---|
84 | " gl_FragColor = texture2D(in_Texture, pass_TexCoord);\n" |
---|
85 | //" gl_FragColor = vec4(0.5, 1.0, 0.0, 0.5);\n" |
---|
86 | //" gl_FragColor = vec4(pass_TexCoord * 4.0, 0.0, 0.25);\n" |
---|
87 | #else |
---|
88 | " vec4 col = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n" |
---|
89 | #if 0 |
---|
90 | " float mul = 2.0;\n" |
---|
91 | " float dx1 = mod(gl_FragCoord.x, 2.0);\n" |
---|
92 | " float dy1 = mod(gl_FragCoord.y, 2.0);\n" |
---|
93 | " float t1 = mod(3.0 * dx1 + 2.0 * dy1, 4.0);\n" |
---|
94 | " float dx2 = mod(floor(gl_FragCoord.x * 0.5), 2.0);\n" |
---|
95 | " float dy2 = mod(floor(gl_FragCoord.y * 0.5), 2.0);\n" |
---|
96 | " float t2 = mod(3.0 * dx2 + 2.0 * dy2, 4.0);\n" |
---|
97 | " float dx3 = mod(floor(gl_FragCoord.x * 0.25), 2.0);\n" |
---|
98 | " float dy3 = mod(floor(gl_FragCoord.y * 0.25), 2.0);\n" |
---|
99 | " float t3 = mod(3.0 * dx3 + 2.0 * dy3, 4.0);\n" |
---|
100 | " float t = (1.0 + 16.0 * t1 + 4.0 * t2 + t3) / 65.0;\n" |
---|
101 | " float fracx = fract(col.x * mul);\n" |
---|
102 | " float fracy = fract(col.y * mul);\n" |
---|
103 | " float fracz = fract(col.z * mul);\n" |
---|
104 | " fracx = fracx > t ? 1.0 : 0.0;\n" |
---|
105 | " fracy = fracy > t ? 1.0 : 0.0;\n" |
---|
106 | " fracz = fracz > t ? 1.0 : 0.0;\n" |
---|
107 | " col.x = (floor(col.x * mul) + fracx) / mul;\n" |
---|
108 | " col.y = (floor(col.y * mul) + fracy) / mul;\n" |
---|
109 | " col.z = (floor(col.z * mul) + fracz) / mul;\n" |
---|
110 | #endif |
---|
111 | " gl_FragColor = col;\n" |
---|
112 | #endif |
---|
113 | "}\n"; |
---|
114 | #endif |
---|
115 | |
---|
116 | /* |
---|
117 | * Public Video class |
---|
118 | */ |
---|
119 | |
---|
120 | void Video::Setup(int width, int height) |
---|
121 | { |
---|
122 | /* Initialise OpenGL */ |
---|
123 | glViewport(0, 0, width, height); |
---|
124 | |
---|
125 | #if defined ANDROID_NDK |
---|
126 | saved_viewport = vec2i(width, height); |
---|
127 | #endif |
---|
128 | |
---|
129 | glClearColor(0.1f, 0.2f, 0.3f, 0.0f); |
---|
130 | glClearDepthf(1.0); |
---|
131 | |
---|
132 | #if defined HAVE_GL_2X || defined HAVE_GLES_1X |
---|
133 | glShadeModel(GL_SMOOTH); |
---|
134 | #endif |
---|
135 | #if defined HAVE_GL_2X || defined HAVE_GLES_1X |
---|
136 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); |
---|
137 | #endif |
---|
138 | |
---|
139 | #if defined HAVE_GL_2X || defined HAVE_GLES_2X |
---|
140 | stdshader = Shader::Create(vertexshader, fragmentshader); |
---|
141 | #endif |
---|
142 | } |
---|
143 | |
---|
144 | void Video::SetFov(float theta) |
---|
145 | { |
---|
146 | #undef near /* Fuck Microsoft */ |
---|
147 | #undef far /* Fuck Microsoft again */ |
---|
148 | mat4 proj; |
---|
149 | |
---|
150 | float width = GetWidth(); |
---|
151 | float height = GetHeight(); |
---|
152 | float near = -width - height; |
---|
153 | float far = width + height; |
---|
154 | |
---|
155 | #if defined ANDROID_NDK |
---|
156 | width = 640.0f; |
---|
157 | height = 480.0f; |
---|
158 | #endif |
---|
159 | |
---|
160 | /* Set the projection matrix */ |
---|
161 | if (theta < 1e-4f) |
---|
162 | { |
---|
163 | /* The easy way: purely orthogonal projection. */ |
---|
164 | proj_matrix = mat4::ortho(0, width, 0, height, near, far); |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | /* Compute a view that approximates the glOrtho view when theta |
---|
169 | * approaches zero. This view ensures that the z=0 plane fills |
---|
170 | * the screen. */ |
---|
171 | float t1 = tanf(theta / 2); |
---|
172 | float t2 = t1 * height / width; |
---|
173 | float dist = (float)width / (2.0f * t1); |
---|
174 | |
---|
175 | near += dist; |
---|
176 | far += dist; |
---|
177 | |
---|
178 | if (near <= 0.0f) |
---|
179 | { |
---|
180 | far -= (near - 1.0f); |
---|
181 | near = 1.0f; |
---|
182 | } |
---|
183 | |
---|
184 | proj_matrix = mat4::frustum(-near * t1, near * t1, |
---|
185 | -near * t2, near * t2, near, far) |
---|
186 | * mat4::translate(-0.5f * width, -0.5f * height, -dist); |
---|
187 | } |
---|
188 | |
---|
189 | view_matrix = mat4(1.0f); |
---|
190 | |
---|
191 | #if defined HAVE_GL_2X || defined HAVE_GLES_2X |
---|
192 | stdshader->Bind(); /* Required on GLES 2.x? */ |
---|
193 | GLuint uni; |
---|
194 | uni = stdshader->GetUniformLocation("proj_matrix"); |
---|
195 | glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]); |
---|
196 | uni = stdshader->GetUniformLocation("view_matrix"); |
---|
197 | glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]); |
---|
198 | #else |
---|
199 | glMatrixMode(GL_PROJECTION); |
---|
200 | glLoadIdentity(); |
---|
201 | glMultMatrixf(&proj_matrix[0][0]); |
---|
202 | |
---|
203 | /* Reset the model view matrix, just in case */ |
---|
204 | glMatrixMode(GL_MODELVIEW); |
---|
205 | glLoadIdentity(); |
---|
206 | glMultMatrixf(&view_matrix[0][0]); |
---|
207 | #endif |
---|
208 | } |
---|
209 | |
---|
210 | void Video::SetDepth(bool set) |
---|
211 | { |
---|
212 | if (set) |
---|
213 | glEnable(GL_DEPTH_TEST); |
---|
214 | else |
---|
215 | glDisable(GL_DEPTH_TEST); |
---|
216 | } |
---|
217 | |
---|
218 | void Video::Clear() |
---|
219 | { |
---|
220 | glViewport(0, 0, GetWidth(), GetHeight()); |
---|
221 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
---|
222 | |
---|
223 | SetFov(0.0f); |
---|
224 | } |
---|
225 | |
---|
226 | void Video::Destroy() |
---|
227 | { |
---|
228 | #if defined HAVE_GL_2X || defined HAVE_GLES_2X |
---|
229 | Shader::Destroy(stdshader); |
---|
230 | #endif |
---|
231 | } |
---|
232 | |
---|
233 | void Video::Capture(uint32_t *buffer) |
---|
234 | { |
---|
235 | GLint v[4]; |
---|
236 | glGetIntegerv(GL_VIEWPORT, v); |
---|
237 | int width = v[2], height = v[3]; |
---|
238 | |
---|
239 | #if defined HAVE_GL_1X || defined HAVE_GL_2X |
---|
240 | glPixelStorei(GL_PACK_ROW_LENGTH, 0); |
---|
241 | #endif |
---|
242 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
---|
243 | |
---|
244 | #if defined GL_BGRA |
---|
245 | glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer); |
---|
246 | #else |
---|
247 | glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
---|
248 | #endif |
---|
249 | |
---|
250 | for (int j = 0; j < height / 2; j++) |
---|
251 | for (int i = 0; i < width; i++) |
---|
252 | { |
---|
253 | uint32_t tmp = buffer[j * width + i]; |
---|
254 | buffer[j * width + i] = buffer[(height - j - 1) * width + i]; |
---|
255 | buffer[(height - j - 1) * width + i] = tmp; |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | int Video::GetWidth() |
---|
260 | { |
---|
261 | #if defined ANDROID_NDK |
---|
262 | return saved_viewport.x; |
---|
263 | #else |
---|
264 | GLint v[4]; |
---|
265 | glGetIntegerv(GL_VIEWPORT, v); |
---|
266 | return v[2]; |
---|
267 | #endif |
---|
268 | } |
---|
269 | |
---|
270 | int Video::GetHeight() |
---|
271 | { |
---|
272 | #if defined ANDROID_NDK |
---|
273 | return saved_viewport.y; |
---|
274 | #else |
---|
275 | GLint v[4]; |
---|
276 | glGetIntegerv(GL_VIEWPORT, v); |
---|
277 | return v[3]; |
---|
278 | #endif |
---|
279 | } |
---|
280 | |
---|
281 | } /* namespace lol */ |
---|
282 | |
---|