source: trunk/src/video.cpp @ 745

Last change on this file since 745 was 745, checked in by sam, 10 years ago

video: enforce shader-enabled GL implementations, getting rid of a lot
of code.

  • Property svn:keywords set to Id
File size: 6.4 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
25namespace lol
26{
27
28#if defined ANDROID_NDK
29vec2i saved_viewport;
30#endif
31
32Shader *stdshader;
33mat4 proj_matrix, view_matrix, model_matrix;
34
35static 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
66static 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    "    vec2 d1 = mod(vec2(gl_FragCoord), vec2(2.0, 2.0));\n"
90    "    float t1 = mod(3.0 * d1.x + 2.0 * d1.y, 4.0);\n"
91    "    float dx2 = mod(floor(gl_FragCoord.x * 0.5), 2.0);\n"
92    "    float dy2 = mod(floor(gl_FragCoord.y * 0.5), 2.0);\n"
93    "    float t2 = mod(3.0 * dx2 + 2.0 * dy2, 4.0);\n"
94    "    float dx3 = mod(floor(gl_FragCoord.x * 0.25), 2.0);\n"
95    "    float dy3 = mod(floor(gl_FragCoord.y * 0.25), 2.0);\n"
96    "    float t3 = mod(3.0 * dx3 + 2.0 * dy3, 4.0);\n"
97    "    float t = (1.0 + 16.0 * t1 + 4.0 * t2 + t3) / 65.0;\n"
98    "    float fracx = fract(col.x * mul);\n"
99    "    float fracy = fract(col.y * mul);\n"
100    "    float fracz = fract(col.z * mul);\n"
101    "    fracx = fracx > t ? 1.0 : 0.0;\n"
102    "    fracy = fracy > t ? 1.0 : 0.0;\n"
103    "    fracz = fracz > t ? 1.0 : 0.0;\n"
104    "    col.x = (floor(col.x * mul) + fracx) / mul;\n"
105    "    col.y = (floor(col.y * mul) + fracy) / mul;\n"
106    "    col.z = (floor(col.z * mul) + fracz) / mul;\n"
107#endif
108    "    gl_FragColor = col;\n"
109    "}\n";
110
111/*
112 * Public Video class
113 */
114
115void Video::Setup(int width, int height)
116{
117    /* Initialise OpenGL */
118    glViewport(0, 0, width, height);
119
120#if defined ANDROID_NDK
121    saved_viewport = vec2i(width, height);
122#endif
123
124    glClearColor(0.1f, 0.2f, 0.3f, 0.0f);
125    glClearDepthf(1.0);
126
127#if defined HAVE_GL_2X
128    glShadeModel(GL_SMOOTH);
129    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
130#endif
131
132    stdshader = Shader::Create(vertexshader, fragmentshader);
133}
134
135void Video::SetFov(float theta)
136{
137#undef near /* Fuck Microsoft */
138#undef far /* Fuck Microsoft again */
139    mat4 proj;
140
141    float width = GetWidth();
142    float height = GetHeight();
143    float near = -width - height;
144    float far = width + height;
145
146#if defined ANDROID_NDK
147    width = 640.0f;
148    height = 480.0f;
149#endif
150
151    /* Set the projection matrix */
152    if (theta < 1e-4f)
153    {
154        /* The easy way: purely orthogonal projection. */
155        proj_matrix = mat4::ortho(0, width, 0, height, near, far);
156    }
157    else
158    {
159        /* Compute a view that approximates the glOrtho view when theta
160         * approaches zero. This view ensures that the z=0 plane fills
161         * the screen. */
162        float t1 = tanf(theta / 2);
163        float t2 = t1 * height / width;
164        float dist = (float)width / (2.0f * t1);
165
166        near += dist;
167        far += dist;
168
169        if (near <= 0.0f)
170        {
171            far -= (near - 1.0f);
172            near = 1.0f;
173        }
174
175        proj_matrix = mat4::frustum(-near * t1, near * t1,
176                                    -near * t2, near * t2, near, far)
177                    * mat4::translate(-0.5f * width, -0.5f * height, -dist);
178    }
179
180    view_matrix = mat4(1.0f);
181
182    stdshader->Bind(); /* Required on GLES 2.x? */
183    GLuint uni;
184    uni = stdshader->GetUniformLocation("proj_matrix");
185    glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]);
186    uni = stdshader->GetUniformLocation("view_matrix");
187    glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
188}
189
190void Video::SetDepth(bool set)
191{
192    if (set)
193        glEnable(GL_DEPTH_TEST);
194    else
195        glDisable(GL_DEPTH_TEST);
196}
197
198void Video::Clear()
199{
200    glViewport(0, 0, GetWidth(), GetHeight());
201    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
202
203    SetFov(0.0f);
204}
205
206void Video::Destroy()
207{
208    Shader::Destroy(stdshader);
209}
210
211void Video::Capture(uint32_t *buffer)
212{
213    GLint v[4];
214    glGetIntegerv(GL_VIEWPORT, v);
215    int width = v[2], height = v[3];
216
217#if defined HAVE_GL_2X
218    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
219#endif
220    glPixelStorei(GL_PACK_ALIGNMENT, 1);
221
222#if defined GL_BGRA
223    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
224#else
225    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
226#endif
227
228    for (int j = 0; j < height / 2; j++)
229        for (int i = 0; i < width; i++)
230        {
231            uint32_t tmp = buffer[j * width + i];
232            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
233            buffer[(height - j - 1) * width + i] = tmp;
234        }
235}
236
237int Video::GetWidth()
238{
239#if defined ANDROID_NDK
240    return saved_viewport.x;
241#else
242    GLint v[4];
243    glGetIntegerv(GL_VIEWPORT, v);
244    return v[2];
245#endif
246}
247
248int Video::GetHeight()
249{
250#if defined ANDROID_NDK
251    return saved_viewport.y;
252#else
253    GLint v[4];
254    glGetIntegerv(GL_VIEWPORT, v);
255    return v[3];
256#endif
257}
258
259} /* namespace lol */
260
Note: See TracBrowser for help on using the repository browser.