source: trunk/src/video.cpp @ 1050

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

test: fix OS X compilation; we still need SDLmain.a on that platform.

  • Property svn:keywords set to Id
File size: 4.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
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#if defined __ANDROID__ || defined __CELLOS_LV2__ || defined __APPLE__
39    static ivec2 saved_viewport;
40#endif
41};
42
43mat4 VideoData::proj_matrix;
44mat4 VideoData::view_matrix;
45
46#if defined __ANDROID__ || defined __CELLOS_LV2__ || defined __APPLE__
47ivec2 VideoData::saved_viewport(0, 0);
48#endif
49
50/*
51 * Public Video class
52 */
53
54void Video::Setup(ivec2 size)
55{
56#if defined USE_GLEW && !defined __APPLE__
57    /* Initialise GLEW if necessary */
58    GLenum glerr = glewInit();
59    if (glerr != GLEW_OK)
60    {
61        Log::Error("cannot initialise GLEW: %s\n", glewGetErrorString(glerr));
62        exit(EXIT_FAILURE);
63    }
64#endif
65
66    /* Initialise OpenGL */
67    glViewport(0, 0, size.x, size.y);
68
69#if defined __ANDROID__ || defined __CELLOS_LV2__ || defined __APPLE__
70    VideoData::saved_viewport = size;
71#endif
72
73    glClearColor(0.1f, 0.2f, 0.3f, 0.0f);
74    glClearDepth(1.0);
75
76#if defined HAVE_GL_2X && !defined __APPLE__
77    glShadeModel(GL_SMOOTH);
78    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
79#endif
80}
81
82void Video::SetFov(float theta)
83{
84    vec2 size = GetSize();
85    float near = -size.x - size.y;
86    float far = size.x + size.y;
87
88#if defined __ANDROID__
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__
182    return VideoData::saved_viewport;
183#elif defined __CELLOS_LV2__
184    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
185    return VideoData::saved_viewport;
186#elif defined __APPLE__
187    return VideoData::saved_viewport;
188#else
189    GLint v[4];
190    glGetIntegerv(GL_VIEWPORT, v);
191    return ivec2(v[2], v[3]);
192#endif
193}
194
195mat4 const & Video::GetProjMatrix()
196{
197    return VideoData::proj_matrix;
198}
199
200mat4 const & Video::GetViewMatrix()
201{
202    return VideoData::view_matrix;
203}
204
205} /* namespace lol */
206
Note: See TracBrowser for help on using the repository browser.