source: trunk/src/video.cpp @ 683

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

Try to fix GLES 2 rendering. No luck so far.

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