source: trunk/src/video.cpp @ 758

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

ps3: start a minimal PS3 port using PSGL, but not CG yet.

  • Property svn:keywords set to Id
File size: 6.9 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
37static char const *vertexshader =
38#if defined HAVE_GL_2X
39    "#version 130\n"
40#endif
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    //"in vec3 in_Color;\n"
51    //"out vec3 pass_Color;\n"
52    "uniform mat4 proj_matrix;\n"
53    "uniform mat4 view_matrix;\n"
54    "uniform mat4 model_matrix;\n"
55    "\n"
56    "void main()\n"
57    "{\n"
58    "    gl_Position = proj_matrix * view_matrix * model_matrix"
59    "                * vec4(in_Position, 1.0);\n"
60    //"    pass_Color = in_Color;\n"
61#if defined HAVE_GLES_2X
62    "    pass_TexCoord = in_TexCoord;\n"
63#else
64    "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
65#endif
66    "}\n";
67
68static char const *fragmentshader =
69#if !defined HAVE_GLES_2X
70    "#version 130\n"
71#endif
72    "\n"
73    "uniform sampler2D in_Texture;\n"
74    //"in vec3 pass_Color;\n"
75    //"out vec4 out_Color;\n"
76#if defined HAVE_GLES_2X
77    "varying vec2 pass_TexCoord;\n"
78#endif
79    "\n"
80    "void main()\n"
81    "{\n"
82#if defined HAVE_GLES_2X
83    "    vec4 col = texture2D(in_Texture, pass_TexCoord);\n"
84    //"    vec4 col = vec4(0.5, 1.0, 0.0, 0.5);\n"
85    //"    vec4 col = vec4(pass_TexCoord * 4.0, 0.0, 0.25);\n"
86#else
87    "    vec4 col = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
88#endif
89#if 0
90    "    float mul = 2.0;\n"
91#if 0
92    "    vec2 d1 = mod(vec2(gl_FragCoord), vec2(2.0, 2.0));\n"
93    "    float t1 = mod(3.0 * d1.x + 2.0 * d1.y, 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 t1 = (1.0 + 16.0 * t1 + 4.0 * t2 + t3) / 65.0;\n"
101    "    float t2 = t1;\n"
102    "    float t3 = t1;\n"
103#else
104    "    float rand = sin(gl_FragCoord.x * 1.23456) * 123.456\n"
105    "               + cos(gl_FragCoord.y * 2.34567) * 789.012;\n"
106    "    float t1 = mod(sin(rand) * 17.13043, 1.0);\n"
107    "    float t2 = mod(sin(rand) * 27.13043, 1.0);\n"
108    "    float t3 = mod(sin(rand) * 37.13043, 1.0);\n"
109#endif
110    "    float fracx = fract(col.x * mul);\n"
111    "    float fracy = fract(col.y * mul);\n"
112    "    float fracz = fract(col.z * mul);\n"
113    "    fracx = fracx > t1 ? 1.0 : 0.0;\n"
114    "    fracy = fracy > t2 ? 1.0 : 0.0;\n"
115    "    fracz = fracz > t3 ? 1.0 : 0.0;\n"
116    "    col.x = (floor(col.x * mul) + fracx) / mul;\n"
117    "    col.y = (floor(col.y * mul) + fracy) / mul;\n"
118    "    col.z = (floor(col.z * mul) + fracz) / mul;\n"
119#endif
120    "    gl_FragColor = col;\n"
121    "}\n";
122
123/*
124 * Public Video class
125 */
126
127void Video::Setup(vec2i size)
128{
129    /* Initialise OpenGL */
130    glViewport(0, 0, size.x, size.y);
131
132#if defined ANDROID_NDK
133    saved_viewport = vec2i(size.x, size.y);
134#endif
135
136    glClearColor(0.1f, 0.2f, 0.3f, 0.0f);
137    glClearDepthf(1.0);
138
139#if defined HAVE_GL_2X
140    glShadeModel(GL_SMOOTH);
141    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
142#endif
143
144    stdshader = Shader::Create(vertexshader, fragmentshader);
145}
146
147void Video::SetFov(float theta)
148{
149#undef near /* Fuck Microsoft */
150#undef far /* Fuck Microsoft again */
151    mat4 proj;
152
153    vec2 size = GetSize();
154    float near = -size.x - size.y;
155    float far = size.x + size.y;
156
157#if defined ANDROID_NDK
158    size = vec(640.0f, 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, size.x, 0, size.y, 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 * size.y / size.y;
174        float dist = size.x / (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 * size.x, -0.5f * size.y, -dist);
188    }
189
190    view_matrix = mat4(1.0f);
191
192    stdshader->Bind(); /* Required on GLES 2.x? */
193#if !defined __CELLOS_LV2__ // Use cgGetNamedParameter etc.
194    GLuint uni;
195    uni = stdshader->GetUniformLocation("proj_matrix");
196    glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]);
197    uni = stdshader->GetUniformLocation("view_matrix");
198    glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
199#endif
200}
201
202void Video::SetDepth(bool set)
203{
204    if (set)
205        glEnable(GL_DEPTH_TEST);
206    else
207        glDisable(GL_DEPTH_TEST);
208}
209
210void Video::Clear()
211{
212    vec2i size = GetSize();
213    glViewport(0, 0, size.x, size.y);
214    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
215
216    SetFov(0.0f);
217}
218
219void Video::Destroy()
220{
221    Shader::Destroy(stdshader);
222}
223
224void Video::Capture(uint32_t *buffer)
225{
226    GLint v[4];
227#if defined __CELLOS_LV2__
228    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
229    v[2] = 1920;
230    v[3] = 1080;
231#else
232    glGetIntegerv(GL_VIEWPORT, v);
233#endif
234    int width = v[2], height = v[3];
235
236#if defined HAVE_GL_2X
237    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
238#endif
239    glPixelStorei(GL_PACK_ALIGNMENT, 1);
240
241#if defined GL_BGRA
242    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
243#else
244    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
245#endif
246
247    for (int j = 0; j < height / 2; j++)
248        for (int i = 0; i < width; i++)
249        {
250            uint32_t tmp = buffer[j * width + i];
251            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
252            buffer[(height - j - 1) * width + i] = tmp;
253        }
254}
255
256vec2i Video::GetSize()
257{
258#if defined ANDROID_NDK
259    return saved_viewport;
260#elif defined __CELLOS_LV2__
261    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
262    return vec2i(1920, 1080);
263#else
264    GLint v[4];
265    glGetIntegerv(GL_VIEWPORT, v);
266    return vec2i(v[2], v[3]);
267#endif
268}
269
270} /* namespace lol */
271
Note: See TracBrowser for help on using the repository browser.