source: trunk/src/video.cpp @ 1309

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

gpu: temporarily enable alpha blending in the vertex buffer code.

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