source: trunk/src/video.cpp @ 863

Last change on this file since 863 was 863, checked in by sam, 12 years ago

core: rename vec2i to ivec2 etc. to better match GLSL.

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