source: trunk/src/video.cpp @ 1297

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

gpu: activate alpha blending on the Direct3D backend.

  • Property svn:keywords set to Id
File size: 7.9 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
135    g_d3ddevice->SetRenderState(D3DRS_ALPHABLENDENABLE, 1);
136    g_d3ddevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
137    g_d3ddevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
138#else
139#   if defined USE_GLEW && !defined __APPLE__
140    /* Initialise GLEW if necessary */
141    GLenum glerr = glewInit();
142    if (glerr != GLEW_OK)
143    {
144        Log::Error("cannot initialise GLEW: %s\n", glewGetErrorString(glerr));
145        exit(EXIT_FAILURE);
146    }
147#   endif
148
149    /* Initialise OpenGL */
150    glViewport(0, 0, size.x, size.y);
151    VideoData::saved_viewport = size;
152
153    glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
154    glClearDepth(1.0);
155
156#   if defined HAVE_GL_2X && !defined __APPLE__
157    glShadeModel(GL_SMOOTH);
158    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
159#   endif
160#endif
161}
162
163void Video::SetFov(float theta)
164{
165    vec2 size = GetSize();
166    float near = -size.x - size.y;
167    float far = size.x + size.y;
168
169#if defined __ANDROID__
170    size = vec2(640.0f, 480.0f);
171#endif
172
173    /* Set the projection matrix */
174    if (theta < 1e-4f)
175    {
176        /* The easy way: purely orthogonal projection. */
177        VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
178    }
179    else
180    {
181        /* Compute a view that approximates the glOrtho view when theta
182         * approaches zero. This view ensures that the z=0 plane fills
183         * the screen. */
184        float t1 = tanf(theta / 2);
185        float t2 = t1 * size.y / size.y;
186        float dist = size.x / (2.0f * t1);
187
188        near += dist;
189        far += dist;
190
191        if (near <= 0.0f)
192        {
193            far -= (near - 1.0f);
194            near = 1.0f;
195        }
196
197        mat4 proj = mat4::frustum(-near * t1, near * t1,
198                                  -near * t2, near * t2, near, far);
199        mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist);
200        VideoData::proj_matrix = proj * trans;
201    }
202
203    VideoData::view_matrix = mat4(1.0f);
204}
205
206void Video::SetDepth(bool set)
207{
208#if defined USE_D3D9 || defined _XBOX
209#   define STR0(x) #x
210#   define STR(x) STR0(x)
211#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetDepth() not implemented")
212#else
213    if (set)
214        glEnable(GL_DEPTH_TEST);
215    else
216        glDisable(GL_DEPTH_TEST);
217#endif
218}
219
220void Video::SetClearColor(vec4 color)
221{
222#if defined USE_D3D9 || defined _XBOX
223    VideoData::clear_color = D3DCOLOR_XRGB((int)(color.r * 255.999f),
224                                           (int)(color.g * 255.999f),
225                                           (int)(color.b * 255.999f));
226#else
227    glClearColor(color.r, color.g, color.b, color.a);
228#endif
229}
230
231void Video::Clear()
232{
233    ivec2 size = GetSize();
234#if defined USE_D3D9 || defined _XBOX
235    if (FAILED(VideoData::d3d_dev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
236                                        | D3DCLEAR_STENCIL,
237                              VideoData::clear_color, 1.0f, 0)))
238        Abort();
239#else
240    glViewport(0, 0, size.x, size.y);
241    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
242#endif
243
244    SetFov(0.0f);
245}
246
247void Video::Destroy()
248{
249    ;
250}
251
252void Video::Capture(uint32_t *buffer)
253{
254#if defined USE_D3D9 || defined _XBOX
255    /* TODO */
256#else
257    GLint v[4];
258#   if defined __CELLOS_LV2__
259    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
260    v[2] = 1920;
261    v[3] = 1080;
262#   else
263    glGetIntegerv(GL_VIEWPORT, v);
264#   endif
265    int width = v[2], height = v[3];
266
267#   if defined HAVE_GL_2X
268    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
269#   endif
270    glPixelStorei(GL_PACK_ALIGNMENT, 1);
271
272#   if defined GL_BGRA
273    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
274#   else
275    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
276#   endif
277
278    for (int j = 0; j < height / 2; j++)
279        for (int i = 0; i < width; i++)
280        {
281            uint32_t tmp = buffer[j * width + i];
282            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
283            buffer[(height - j - 1) * width + i] = tmp;
284        }
285#endif
286}
287
288ivec2 Video::GetSize()
289{
290#if defined USE_D3D9 || defined _XBOX
291    return VideoData::saved_viewport;
292#elif 1
293    /* GetSize() is called too often on the game thread; we cannot rely on
294     * the GL context at this point */
295    return VideoData::saved_viewport;
296#elif defined __CELLOS_LV2__
297    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
298#else
299    GLint v[4];
300    glGetIntegerv(GL_VIEWPORT, v);
301    return ivec2(v[2], v[3]);
302#endif
303}
304
305mat4 const & Video::GetProjMatrix()
306{
307    return VideoData::proj_matrix;
308}
309
310mat4 const & Video::GetViewMatrix()
311{
312    return VideoData::view_matrix;
313}
314
315} /* namespace lol */
316
Note: See TracBrowser for help on using the repository browser.