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