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