source: trunk/src/video.cpp @ 771

Last change on this file since 771 was 771, checked in by sam, 9 years ago

debug: more debug quad tests, 1.20 shaders.

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