source: trunk/src/video.cpp @ 1325

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

core: move some of the camera view logic from the Video class to the
Scene and Camera classes.

  • Property svn:keywords set to Id
File size: 7.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 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 _XBOX
18#   include <xtl.h>
19#   undef near /* Fuck Microsoft */
20#   undef far /* Fuck Microsoft again */
21#elif defined _WIN32
22#   if defined USE_D3D9
23#      include <d3d9.h>
24#   endif
25#   define WIN32_LEAN_AND_MEAN
26#   include <windows.h>
27#   undef near /* Fuck Microsoft */
28#   undef far /* Fuck Microsoft again */
29#endif
30
31#include "core.h"
32#include "lolgl.h"
33
34using namespace std;
35
36/* FIXME: g_d3ddevice should never be exported */
37#if defined USE_D3D9
38IDirect3DDevice9 *g_d3ddevice;
39#   if defined USE_SDL
40extern HWND g_hwnd;
41#   endif
42#elif defined _XBOX
43D3DDevice *g_d3ddevice;
44#endif
45
46namespace lol
47{
48
49class VideoData
50{
51    friend class Video;
52
53private:
54    static mat4 proj_matrix;
55    static ivec2 saved_viewport;
56#if defined USE_D3D9
57    static IDirect3D9 *d3d_ctx;
58    static IDirect3DDevice9 *d3d_dev;
59    static D3DCOLOR clear_color;
60#elif defined _XBOX
61    static Direct3D *d3d_ctx;
62    static D3DDevice *d3d_dev;
63    static D3DCOLOR clear_color;
64#endif
65};
66
67mat4 VideoData::proj_matrix;
68ivec2 VideoData::saved_viewport(0, 0);
69
70#if defined USE_D3D9
71IDirect3D9 *VideoData::d3d_ctx;
72IDirect3DDevice9 *VideoData::d3d_dev;
73D3DCOLOR VideoData::clear_color;
74#elif defined _XBOX
75Direct3D *VideoData::d3d_ctx;
76D3DDevice *VideoData::d3d_dev;
77D3DCOLOR VideoData::clear_color;
78#endif
79
80/*
81 * Public Video class
82 */
83
84void Video::Setup(ivec2 size)
85{
86#if defined USE_D3D9 || defined _XBOX
87    VideoData::d3d_ctx = Direct3DCreate9(D3D_SDK_VERSION);
88    if (!VideoData::d3d_ctx)
89    {
90        Log::Error("cannot initialise D3D\n");
91        exit(EXIT_FAILURE);
92    }
93
94    HWND window = 0;
95    D3DPRESENT_PARAMETERS d3dpp;
96    memset(&d3dpp, 0, sizeof(d3dpp));
97
98#   if defined USE_SDL
99    window = g_hwnd;
100    d3dpp.hDeviceWindow = g_hwnd;
101    d3dpp.Windowed = TRUE;
102#   elif defined _XBOX
103    XVIDEO_MODE VideoMode;
104    XGetVideoMode( &VideoMode );
105    if (size.x > VideoMode.dwDisplayWidth)
106        size.x = VideoMode.dwDisplayWidth;
107    if (size.y > VideoMode.dwDisplayHeight)
108        size.y = VideoMode.dwDisplayHeight;
109#   endif
110    VideoData::saved_viewport = size;
111
112    VideoData::clear_color = D3DCOLOR_XRGB(26, 51, 77);
113
114    d3dpp.BackBufferWidth = size.x;
115    d3dpp.BackBufferHeight = size.y;
116    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
117    d3dpp.BackBufferCount = 1;
118    d3dpp.EnableAutoDepthStencil = TRUE;
119    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
120    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
121    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
122
123    HRESULT hr = VideoData::d3d_ctx->CreateDevice(0, D3DDEVTYPE_HAL, window,
124                                                  D3DCREATE_HARDWARE_VERTEXPROCESSING,
125                                                  &d3dpp, &VideoData::d3d_dev);
126    if (FAILED(hr))
127    {
128        Log::Error("cannot create D3D device\n");
129        exit(EXIT_FAILURE);
130    }
131
132    g_d3ddevice = VideoData::d3d_dev;
133#else
134#   if defined USE_GLEW && !defined __APPLE__
135    /* Initialise GLEW if necessary */
136    GLenum glerr = glewInit();
137    if (glerr != GLEW_OK)
138    {
139        Log::Error("cannot initialise GLEW: %s\n", glewGetErrorString(glerr));
140        exit(EXIT_FAILURE);
141    }
142#   endif
143
144    /* Initialise OpenGL */
145    glViewport(0, 0, size.x, size.y);
146    VideoData::saved_viewport = size;
147
148    glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
149    glClearDepth(1.0);
150
151#   if defined HAVE_GL_2X && !defined __APPLE__
152    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
153#   endif
154#endif
155}
156
157void Video::SetFov(float theta)
158{
159    vec2 size = GetSize();
160    float near = -size.x - size.y;
161    float far = size.x + size.y;
162
163#if defined __ANDROID__
164    size = vec2(640.0f, 480.0f);
165#endif
166
167    /* Set the projection matrix */
168    if (theta < 1e-4f)
169    {
170        /* The easy way: purely orthogonal projection. */
171        VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
172    }
173    else
174    {
175        /* Compute a view that approximates the glOrtho view when theta
176         * approaches zero. This view ensures that the z=0 plane fills
177         * the screen. */
178        float t1 = tanf(theta / 2);
179        float t2 = t1 * size.y / size.y;
180        float dist = size.x / (2.0f * t1);
181
182        near += dist;
183        far += dist;
184
185        if (near <= 0.0f)
186        {
187            far -= (near - 1.0f);
188            near = 1.0f;
189        }
190
191        mat4 proj = mat4::frustum(-near * t1, near * t1,
192                                  -near * t2, near * t2, near, far);
193        mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist);
194        VideoData::proj_matrix = proj * trans;
195    }
196}
197
198void Video::SetDepth(bool set)
199{
200#if defined USE_D3D9 || defined _XBOX
201#   define STR0(x) #x
202#   define STR(x) STR0(x)
203#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetDepth() not implemented")
204#else
205    if (set)
206        glEnable(GL_DEPTH_TEST);
207    else
208        glDisable(GL_DEPTH_TEST);
209#endif
210}
211
212void Video::SetClearColor(vec4 color)
213{
214#if defined USE_D3D9 || defined _XBOX
215    VideoData::clear_color = D3DCOLOR_XRGB((int)(color.r * 255.999f),
216                                           (int)(color.g * 255.999f),
217                                           (int)(color.b * 255.999f));
218#else
219    glClearColor(color.r, color.g, color.b, color.a);
220#endif
221}
222
223void Video::Clear()
224{
225    ivec2 size = GetSize();
226#if defined USE_D3D9 || defined _XBOX
227    if (FAILED(VideoData::d3d_dev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
228                                        | D3DCLEAR_STENCIL,
229                              VideoData::clear_color, 1.0f, 0)))
230        Abort();
231#else
232    glViewport(0, 0, size.x, size.y);
233    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
234#endif
235
236    SetFov(0.0f);
237}
238
239void Video::Destroy()
240{
241    ;
242}
243
244void Video::Capture(uint32_t *buffer)
245{
246#if defined USE_D3D9 || defined _XBOX
247    /* TODO */
248#else
249    GLint v[4];
250#   if defined __CELLOS_LV2__
251    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
252    v[2] = 1920;
253    v[3] = 1080;
254#   else
255    glGetIntegerv(GL_VIEWPORT, v);
256#   endif
257    int width = v[2], height = v[3];
258
259#   if defined HAVE_GL_2X
260    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
261#   endif
262    glPixelStorei(GL_PACK_ALIGNMENT, 1);
263
264#   if defined GL_BGRA
265    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
266#   else
267    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
268#   endif
269
270    for (int j = 0; j < height / 2; j++)
271        for (int i = 0; i < width; i++)
272        {
273            uint32_t tmp = buffer[j * width + i];
274            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
275            buffer[(height - j - 1) * width + i] = tmp;
276        }
277#endif
278}
279
280ivec2 Video::GetSize()
281{
282#if defined USE_D3D9 || defined _XBOX
283    return VideoData::saved_viewport;
284#elif 1
285    /* GetSize() is called too often on the game thread; we cannot rely on
286     * the GL context at this point */
287    return VideoData::saved_viewport;
288#elif defined __CELLOS_LV2__
289    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
290#else
291    GLint v[4];
292    glGetIntegerv(GL_VIEWPORT, v);
293    return ivec2(v[2], v[3]);
294#endif
295}
296
297} /* namespace lol */
298
Note: See TracBrowser for help on using the repository browser.