source: trunk/src/video.cpp @ 796

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

gl: use Glew if available.

  • Property svn:keywords set to Id
File size: 4.1 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
30class VideoData
31{
32    friend class Video;
33
34private:
35    static mat4 proj_matrix, view_matrix;
36#if defined ANDROID_NDK
37    static vec2i saved_viewport;
38#endif
39};
40
41mat4 VideoData::proj_matrix;
42mat4 VideoData::view_matrix;
43
44#if defined ANDROID_NDK
45vec2i VideoData::saved_viewport = 0;
46#endif
47
48/*
49 * Public Video class
50 */
51
52void Video::Setup(vec2i size)
53{
54    /* Initialise OpenGL */
55    glViewport(0, 0, size.x, size.y);
56
57#if defined ANDROID_NDK
58    VideoData::saved_viewport = size;
59#endif
60
61    glClearColor(0.1f, 0.2f, 0.3f, 0.0f);
62    glClearDepth(1.0);
63
64#if defined HAVE_GL_2X
65    glShadeModel(GL_SMOOTH);
66    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
67#endif
68}
69
70void Video::SetFov(float theta)
71{
72#undef near /* Fuck Microsoft */
73#undef far /* Fuck Microsoft again */
74    vec2 size = GetSize();
75    float near = -size.x - size.y;
76    float far = size.x + size.y;
77
78#if defined ANDROID_NDK
79    size = vec2(640.0f, 480.0f);
80#endif
81
82    /* Set the projection matrix */
83    if (theta < 1e-4f)
84    {
85        /* The easy way: purely orthogonal projection. */
86        VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
87    }
88    else
89    {
90        /* Compute a view that approximates the glOrtho view when theta
91         * approaches zero. This view ensures that the z=0 plane fills
92         * the screen. */
93        float t1 = tanf(theta / 2);
94        float t2 = t1 * size.y / size.y;
95        float dist = size.x / (2.0f * t1);
96
97        near += dist;
98        far += dist;
99
100        if (near <= 0.0f)
101        {
102            far -= (near - 1.0f);
103            near = 1.0f;
104        }
105
106        mat4 proj = mat4::frustum(-near * t1, near * t1,
107                                  -near * t2, near * t2, near, far);
108        mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist);
109        VideoData::proj_matrix = proj * trans;
110    }
111
112    VideoData::view_matrix = mat4(1.0f);
113}
114
115void Video::SetDepth(bool set)
116{
117    if (set)
118        glEnable(GL_DEPTH_TEST);
119    else
120        glDisable(GL_DEPTH_TEST);
121}
122
123void Video::Clear()
124{
125    vec2i size = GetSize();
126    glViewport(0, 0, size.x, size.y);
127    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
128
129    SetFov(0.0f);
130}
131
132void Video::Destroy()
133{
134    ;
135}
136
137void Video::Capture(uint32_t *buffer)
138{
139    GLint v[4];
140#if defined __CELLOS_LV2__
141    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
142    v[2] = 1920;
143    v[3] = 1080;
144#else
145    glGetIntegerv(GL_VIEWPORT, v);
146#endif
147    int width = v[2], height = v[3];
148
149#if defined HAVE_GL_2X
150    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
151#endif
152    glPixelStorei(GL_PACK_ALIGNMENT, 1);
153
154#if defined GL_BGRA
155    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
156#else
157    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
158#endif
159
160    for (int j = 0; j < height / 2; j++)
161        for (int i = 0; i < width; i++)
162        {
163            uint32_t tmp = buffer[j * width + i];
164            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
165            buffer[(height - j - 1) * width + i] = tmp;
166        }
167}
168
169vec2i Video::GetSize()
170{
171#if defined ANDROID_NDK
172    return VideoData::saved_viewport;
173#elif defined __CELLOS_LV2__
174    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
175    return vec2i(1920, 1080);
176#else
177    GLint v[4];
178    glGetIntegerv(GL_VIEWPORT, v);
179    return vec2i(v[2], v[3]);
180#endif
181}
182
183mat4 const & Video::GetProjMatrix()
184{
185    return VideoData::proj_matrix;
186}
187
188mat4 const & Video::GetViewMatrix()
189{
190    return VideoData::view_matrix;
191}
192
193} /* namespace lol */
194
Note: See TracBrowser for help on using the repository browser.