source: trunk/src/video.cpp @ 682

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

Add preliminary support for EGL applications.

  • Property svn:keywords set to Id
File size: 5.7 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
26#if defined HAVE_GL_2X || defined HAVE_GLES_2X
27Shader *stdshader;
28#endif
29mat4 proj_matrix, view_matrix, model_matrix;
30
31#if defined HAVE_GL_2X || defined HAVE_GLES_2X
32static char const *vertexshader =
33#if defined HAVE_GLES_2X
34    "attribute vec3 in_Position;\n"
35    "attribute vec2 in_TexCoord;\n"
36    "varying vec2 pass_TexCoord;\n"
37#else
38    "#version 130\n"
39    "\n"
40    "in vec3 in_Position;\n"
41    "in vec2 in_TexCoord;\n"
42#endif
43    //"in vec3 in_Color;\n"
44    //"out vec3 pass_Color;\n"
45    "uniform mat4 proj_matrix;\n"
46    "uniform mat4 view_matrix;\n"
47    "uniform mat4 model_matrix;\n"
48    "\n"
49    "void main()\n"
50    "{\n"
51    "    gl_Position = proj_matrix * view_matrix * model_matrix"
52    "                * vec4(in_Position, 1.0f);\n"
53    //"    pass_Color = in_Color;\n"
54#if defined HAVE_GLES_2X
55    "    pass_TexCoord = in_TexCoord;\n"
56#else
57    "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
58#endif
59    "}\n";
60
61static char const *fragmentshader =
62    "#version 130\n"
63    "\n"
64    "uniform sampler2D in_Texture;\n"
65    //"in vec3 pass_Color;\n"
66    //"out vec4 out_Color;\n"
67#if defined HAVE_GLES_2X
68    "varying vec2 pass_TexCoord;\n"
69#endif
70    "\n"
71    "void main()\n"
72    "{\n"
73    //"    gl_FragColor = 0.5 * (texture2D(in_Texture, vec2(gl_TexCoord[0]))\n"
74    //"                           + vec4(pass_Color, 1.0));\n"
75#if defined HAVE_GLES_2X
76    "    gl_FragColor = vec4(0.5, 1.0, 0.0, 0.5);\n"
77#else
78    "    gl_FragColor = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
79#endif
80    //"    gl_FragColor = texture2D(in_Texture, pass_TexCoord);\n"
81    "}\n";
82#endif
83
84/*
85 * Public Video class
86 */
87
88void Video::Setup(int width, int height)
89{
90    /* Initialise OpenGL */
91    glViewport(0, 0, width, height);
92
93    glEnable(GL_TEXTURE_2D);
94#if defined HAVE_GL_2X || defined HAVE_GLES_1X
95    glShadeModel(GL_SMOOTH);
96#endif
97    glClearColor(0.1f, 0.2f, 0.3f, 0.0f);
98    glClearDepthf(1.0);
99
100#if defined HAVE_GL_2X || defined HAVE_GLES_1X
101    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
102#endif
103
104#if defined HAVE_GL_2X || defined HAVE_GLES_2X
105    stdshader = Shader::Create(vertexshader, fragmentshader);
106#endif
107}
108
109void Video::SetFov(float theta)
110{
111#undef near /* Fuck Microsoft */
112#undef far /* Fuck Microsoft again */
113    mat4 proj;
114
115    float width = GetWidth();
116    float height = GetHeight();
117    float near = -width - height;
118    float far = width + height;
119
120    /* Set the projection matrix */
121    if (theta < 1e-4f)
122    {
123        /* The easy way: purely orthogonal projection. */
124        proj_matrix = mat4::ortho(0, width, 0, height, near, far);
125    }
126    else
127    {
128        /* Compute a view that approximates the glOrtho view when theta
129         * approaches zero. This view ensures that the z=0 plane fills
130         * the screen. */
131        float t1 = tanf(theta / 2);
132        float t2 = t1 * height / width;
133        float dist = (float)width / (2.0f * t1);
134
135        near += dist;
136        far += dist;
137
138        if (near <= 0.0f)
139        {
140            far -= (near - 1.0f);
141            near = 1.0f;
142        }
143
144        proj_matrix = mat4::frustum(-near * t1, near * t1,
145                                    -near * t2, near * t2, near, far)
146                    * mat4::translate(-0.5f * width, -0.5f * height, -dist);
147    }
148
149    view_matrix = mat4(1.0f);
150
151#if defined HAVE_GL_2X || defined HAVE_GLES_2X
152    stdshader->Bind(); /* Required on GLES 2.x? */
153    GLuint uni;
154    uni = stdshader->GetUniformLocation("proj_matrix");
155    glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]);
156    uni = stdshader->GetUniformLocation("view_matrix");
157    glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
158#else
159    glMatrixMode(GL_PROJECTION);
160    glLoadIdentity();
161    glMultMatrixf(&proj_matrix[0][0]);
162
163    /* Reset the model view matrix, just in case */
164    glMatrixMode(GL_MODELVIEW);
165    glLoadIdentity();
166    glMultMatrixf(&view_matrix[0][0]);
167#endif
168}
169
170void Video::SetDepth(bool set)
171{
172    if (set)
173        glEnable(GL_DEPTH_TEST);
174    else
175        glDisable(GL_DEPTH_TEST);
176}
177
178void Video::Clear()
179{
180    glViewport(0, 0, GetWidth(), GetHeight());
181    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
182
183    SetFov(0.0f);
184}
185
186void Video::Destroy()
187{
188#if defined HAVE_GL_2X || defined HAVE_GLES_2X
189    Shader::Destroy(stdshader);
190#endif
191}
192
193void Video::Capture(uint32_t *buffer)
194{
195    GLint v[4];
196    glGetIntegerv(GL_VIEWPORT, v);
197    int width = v[2], height = v[3];
198
199#if defined HAVE_GL_1X || defined HAVE_GL_2X
200    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
201#endif
202    glPixelStorei(GL_PACK_ALIGNMENT, 1);
203
204#if defined GL_BGRA
205    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
206#else
207    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
208#endif
209
210    for (int j = 0; j < height / 2; j++)
211        for (int i = 0; i < width; i++)
212        {
213            uint32_t tmp = buffer[j * width + i];
214            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
215            buffer[(height - j - 1) * width + i] = tmp;
216        }
217}
218
219int Video::GetWidth()
220{
221    GLint v[4];
222    glGetIntegerv(GL_VIEWPORT, v);
223    return v[2];
224}
225
226int Video::GetHeight()
227{
228    GLint v[4];
229    glGetIntegerv(GL_VIEWPORT, v);
230    return v[3];
231}
232
Note: See TracBrowser for help on using the repository browser.