source: trunk/src/video.cpp @ 1108

Last change on this file since 1108 was 1108, checked in by sam, 8 years ago

gl: cache viewport size on all platforms, because we cannot query it from
the game thread.

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