source: trunk/src/video.cpp @ 730

Last change on this file since 730 was 730, checked in by sam, 11 years ago

video: add deactivated shader code for 4x4 Bayer dithering.

  • Property svn:keywords set to Id
File size: 6.5 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#include <cstdio>
17
18#ifdef WIN32
19#   define WIN32_LEAN_AND_MEAN
20#   include <windows.h>
21#endif
22
23#include "core.h"
24#include "lolgl.h"
25
26namespace lol
27{
28
29#if defined ANDROID_NDK
30vec2i saved_viewport;
31#endif
32
33#if defined HAVE_GL_2X || defined HAVE_GLES_2X
34Shader *stdshader;
35#endif
36mat4 proj_matrix, view_matrix, model_matrix;
37
38#if defined HAVE_GL_2X || defined HAVE_GLES_2X
39static char const *vertexshader =
40#if !defined HAVE_GLES_2X
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#else
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    "    gl_Position = proj_matrix * view_matrix * model_matrix"
61    "                * vec4(in_Position, 1.0f);\n"
62    //"    pass_Color = in_Color;\n"
63#if defined HAVE_GLES_2X
64    "    pass_TexCoord = in_TexCoord;\n"
65#else
66    "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
67#endif
68    "}\n";
69
70static char const *fragmentshader =
71#if !defined HAVE_GLES_2X
72    "#version 130\n"
73#endif
74    "\n"
75    "uniform sampler2D in_Texture;\n"
76    //"in vec3 pass_Color;\n"
77    //"out vec4 out_Color;\n"
78#if defined HAVE_GLES_2X
79    "varying vec2 pass_TexCoord;\n"
80#endif
81    "\n"
82    "void main()\n"
83    "{\n"
84#if defined HAVE_GLES_2X
85    "    gl_FragColor = texture2D(in_Texture, pass_TexCoord);\n"
86    //"    gl_FragColor = vec4(0.5, 1.0, 0.0, 0.5);\n"
87    //"    gl_FragColor = vec4(pass_TexCoord * 4.0, 0.0, 0.25);\n"
88#else
89    "    vec4 col = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
90#if 0
91    "    float dx1 = mod(gl_FragCoord.x, 2.0);\n"
92    "    float dy1 = mod(gl_FragCoord.y, 2.0);\n"
93    "    float t1 = mod(3.0 * dx1 + 2.0 * dy1, 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 t = (1.0 + 4.0 * t1 + t2) / 17.0;\n"
98    "    col.x = col.x > t ? 1.0 : 0.0;\n"
99    "    col.y = col.y > t ? 1.0 : 0.0;\n"
100    "    col.z = col.z > t ? 1.0 : 0.0;\n"
101#endif
102    "    gl_FragColor = col;\n"
103#endif
104    "}\n";
105#endif
106
107/*
108 * Public Video class
109 */
110
111void Video::Setup(int width, int height)
112{
113    /* Initialise OpenGL */
114    glViewport(0, 0, width, height);
115
116#if defined ANDROID_NDK
117    saved_viewport = vec2i(width, height);
118#endif
119
120    glClearColor(0.1f, 0.2f, 0.3f, 0.0f);
121    glClearDepthf(1.0);
122
123#if defined HAVE_GL_2X || defined HAVE_GLES_1X
124    glShadeModel(GL_SMOOTH);
125#endif
126#if defined HAVE_GL_2X || defined HAVE_GLES_1X
127    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
128#endif
129
130#if defined HAVE_GL_2X || defined HAVE_GLES_2X
131    stdshader = Shader::Create(vertexshader, fragmentshader);
132#endif
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#if defined HAVE_GL_2X || defined HAVE_GLES_2X
183    stdshader->Bind(); /* Required on GLES 2.x? */
184    GLuint uni;
185    uni = stdshader->GetUniformLocation("proj_matrix");
186    glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]);
187    uni = stdshader->GetUniformLocation("view_matrix");
188    glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
189#else
190    glMatrixMode(GL_PROJECTION);
191    glLoadIdentity();
192    glMultMatrixf(&proj_matrix[0][0]);
193
194    /* Reset the model view matrix, just in case */
195    glMatrixMode(GL_MODELVIEW);
196    glLoadIdentity();
197    glMultMatrixf(&view_matrix[0][0]);
198#endif
199}
200
201void Video::SetDepth(bool set)
202{
203    if (set)
204        glEnable(GL_DEPTH_TEST);
205    else
206        glDisable(GL_DEPTH_TEST);
207}
208
209void Video::Clear()
210{
211    glViewport(0, 0, GetWidth(), GetHeight());
212    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
213
214    SetFov(0.0f);
215}
216
217void Video::Destroy()
218{
219#if defined HAVE_GL_2X || defined HAVE_GLES_2X
220    Shader::Destroy(stdshader);
221#endif
222}
223
224void Video::Capture(uint32_t *buffer)
225{
226    GLint v[4];
227    glGetIntegerv(GL_VIEWPORT, v);
228    int width = v[2], height = v[3];
229
230#if defined HAVE_GL_1X || defined HAVE_GL_2X
231    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
232#endif
233    glPixelStorei(GL_PACK_ALIGNMENT, 1);
234
235#if defined GL_BGRA
236    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
237#else
238    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
239#endif
240
241    for (int j = 0; j < height / 2; j++)
242        for (int i = 0; i < width; i++)
243        {
244            uint32_t tmp = buffer[j * width + i];
245            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
246            buffer[(height - j - 1) * width + i] = tmp;
247        }
248}
249
250int Video::GetWidth()
251{
252#if defined ANDROID_NDK
253    return saved_viewport.x;
254#else
255    GLint v[4];
256    glGetIntegerv(GL_VIEWPORT, v);
257    return v[2];
258#endif
259}
260
261int Video::GetHeight()
262{
263#if defined ANDROID_NDK
264    return saved_viewport.y;
265#else
266    GLint v[4];
267    glGetIntegerv(GL_VIEWPORT, v);
268    return v[3];
269#endif
270}
271
272} /* namespace lol */
273
Note: See TracBrowser for help on using the repository browser.