source: trunk/src/video.cpp @ 2372

Last change on this file since 2372 was 2372, checked in by sam, 7 years ago

gpu: disable glPolygonMode calls on OpenGL ES.

  • Property svn:keywords set to Id
File size: 9.5 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined _XBOX
16#   include <xtl.h>
17#   undef near /* Fuck Microsoft */
18#   undef far /* Fuck Microsoft again */
19#elif defined _WIN32
20#   if defined USE_D3D9
21#      include <d3d9.h>
22#   endif
23#   define WIN32_LEAN_AND_MEAN
24#   include <windows.h>
25#   undef near /* Fuck Microsoft */
26#   undef far /* Fuck Microsoft again */
27#endif
28
29#include "core.h"
30#include "lolgl.h"
31
32using namespace std;
33
34/* FIXME: g_d3ddevice should never be exported */
35#if defined USE_D3D9
36IDirect3DDevice9 *g_d3ddevice;
37#   if defined USE_SDL
38extern HWND g_hwnd;
39#   endif
40#elif defined _XBOX
41D3DDevice *g_d3ddevice;
42#endif
43
44namespace lol
45{
46
47class VideoData
48{
49    friend class Video;
50
51private:
52    static mat4 proj_matrix;
53    static ivec2 saved_viewport;
54    static DebugRenderMode render_mode;
55#if defined USE_D3D9 || defined _XBOX
56#   if defined USE_D3D9
57    static IDirect3D9 *d3d_ctx;
58    static IDirect3DDevice9 *d3d_dev;
59#   elif defined _XBOX
60    static Direct3D *d3d_ctx;
61    static D3DDevice *d3d_dev;
62#   endif
63    static D3DCOLOR clear_color;
64    static float clear_depth;
65#endif
66};
67
68mat4 VideoData::proj_matrix;
69ivec2 VideoData::saved_viewport(0, 0);
70DebugRenderMode VideoData::render_mode = DebugRenderMode::Default;
71
72#if defined USE_D3D9 || defined _XBOX
73#   if defined USE_D3D9
74IDirect3D9 *VideoData::d3d_ctx;
75IDirect3DDevice9 *VideoData::d3d_dev;
76#   elif defined _XBOX
77Direct3D *VideoData::d3d_ctx;
78D3DDevice *VideoData::d3d_dev;
79#   endif
80D3DCOLOR VideoData::clear_color;
81float VideoData::clear_depth;
82#endif
83
84/*
85 * Public Video class
86 */
87
88void Video::Setup(ivec2 size)
89{
90#if defined USE_D3D9 || defined _XBOX
91    VideoData::d3d_ctx = Direct3DCreate9(D3D_SDK_VERSION);
92    if (!VideoData::d3d_ctx)
93    {
94        Log::Error("cannot initialise D3D\n");
95        exit(EXIT_FAILURE);
96    }
97
98    HWND window = 0;
99    D3DPRESENT_PARAMETERS d3dpp;
100    memset(&d3dpp, 0, sizeof(d3dpp));
101
102#   if defined USE_SDL
103    window = g_hwnd;
104    d3dpp.hDeviceWindow = g_hwnd;
105    d3dpp.Windowed = TRUE;
106#   elif defined _XBOX
107    XVIDEO_MODE VideoMode;
108    XGetVideoMode( &VideoMode );
109    if (size.x > VideoMode.dwDisplayWidth)
110        size.x = VideoMode.dwDisplayWidth;
111    if (size.y > VideoMode.dwDisplayHeight)
112        size.y = VideoMode.dwDisplayHeight;
113#   endif
114    VideoData::saved_viewport = size;
115
116    d3dpp.BackBufferWidth = size.x;
117    d3dpp.BackBufferHeight = size.y;
118    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
119    d3dpp.BackBufferCount = 1;
120    d3dpp.EnableAutoDepthStencil = TRUE;
121    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
122    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
123    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
124
125    HRESULT hr = VideoData::d3d_ctx->CreateDevice(0, D3DDEVTYPE_HAL, window,
126                                                  D3DCREATE_HARDWARE_VERTEXPROCESSING,
127                                                  &d3dpp, &VideoData::d3d_dev);
128    if (FAILED(hr))
129    {
130        Log::Error("cannot create D3D device\n");
131        exit(EXIT_FAILURE);
132    }
133
134    g_d3ddevice = VideoData::d3d_dev;
135#else
136#   if defined USE_GLEW && !defined __APPLE__
137    /* Initialise GLEW if necessary */
138    GLenum glerr = glewInit();
139    if (glerr != GLEW_OK)
140    {
141        Log::Error("cannot initialise GLEW: %s\n", glewGetErrorString(glerr));
142        exit(EXIT_FAILURE);
143    }
144#   endif
145
146    /* Initialise OpenGL */
147    glViewport(0, 0, size.x, size.y);
148    VideoData::saved_viewport = size;
149
150#   if defined HAVE_GL_2X && !defined __APPLE__
151    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
152#   endif
153#endif
154
155    /* Initialise reasonable scene default properties */
156    SetClearColor(vec4(0.1f, 0.2f, 0.3f, 1.0f));
157    SetClearDepth(1.f);
158    SetDebugRenderMode(DebugRenderMode::Default);
159}
160
161void Video::SetFov(float theta)
162{
163    vec2 size = GetSize();
164    float near = -size.x - size.y;
165    float far = size.x + size.y;
166
167#if defined __ANDROID__
168    size = vec2(640.0f, 480.0f);
169#endif
170
171    /* Set the projection matrix */
172    if (theta < 1e-4f)
173    {
174        /* The easy way: purely orthogonal projection. */
175        VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
176    }
177    else
178    {
179        /* Compute a view that approximates the glOrtho view when theta
180         * approaches zero. This view ensures that the z=0 plane fills
181         * the screen. */
182        float t1 = tanf(theta / 2);
183        float t2 = t1 * size.y / size.y;
184        float dist = size.x / (2.0f * t1);
185
186        near += dist;
187        far += dist;
188
189        if (near <= 0.0f)
190        {
191            far -= (near - 1.0f);
192            near = 1.0f;
193        }
194
195        mat4 proj = mat4::frustum(-near * t1, near * t1,
196                                  -near * t2, near * t2, near, far);
197        mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist);
198        VideoData::proj_matrix = proj * trans;
199    }
200}
201
202void Video::SetDepth(bool set)
203{
204#if defined USE_D3D9 || defined _XBOX
205#   define STR0(x) #x
206#   define STR(x) STR0(x)
207#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetDepth() not implemented")
208#else
209    if (set)
210        glEnable(GL_DEPTH_TEST);
211    else
212        glDisable(GL_DEPTH_TEST);
213#endif
214}
215
216void Video::SetClearColor(vec4 color)
217{
218#if defined USE_D3D9 || defined _XBOX
219    VideoData::clear_color = D3DCOLOR_XRGB((int)(color.r * 255.999f),
220                                           (int)(color.g * 255.999f),
221                                           (int)(color.b * 255.999f));
222#else
223    glClearColor(color.r, color.g, color.b, color.a);
224#endif
225}
226
227void Video::SetClearDepth(float f)
228{
229#if defined USE_D3D9 || defined _XBOX
230    VideoData::clear_depth = f;
231#elif defined HAVE_GLES_2X
232    glClearDepthf(f);
233#else
234    glClearDepth(f);
235#endif
236}
237
238void Video::SetDebugRenderMode(DebugRenderMode d)
239{
240    if (d == DebugRenderMode::Max)
241        return;
242
243    switch(d)
244    {
245        //All these modes are handled in the shaders.
246        case DebugRenderMode::Default:
247        case DebugRenderMode::Lighting:
248        case DebugRenderMode::Normal:
249        case DebugRenderMode::UV:
250        {
251#if defined USE_D3D9 || defined _XBOX
252#elif defined HAVE_GLES_2X
253            glEnable(GL_CULL_FACE);
254#else
255            glEnable(GL_CULL_FACE);
256            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
257#endif
258            break;
259        }
260        case DebugRenderMode::Wireframe:
261        {
262            if (VideoData::render_mode == DebugRenderMode::Wireframe)
263            {
264#if defined USE_D3D9 || defined _XBOX
265#else
266                if (glIsEnabled(GL_CULL_FACE) == GL_TRUE)
267                    glDisable(GL_CULL_FACE);
268                else
269                    glEnable(GL_CULL_FACE);
270#endif
271            }
272            else
273            {
274#if defined USE_D3D9 || defined _XBOX
275#else
276                glDisable(GL_CULL_FACE);
277#endif
278            }
279#if defined USE_D3D9 || defined _XBOX
280#elif defined HAVE_GLES_2X
281#else
282            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
283#endif
284            break;
285        }
286    }
287    VideoData::render_mode = d;
288}
289
290DebugRenderMode Video::GetDebugRenderMode()
291{
292    return VideoData::render_mode;
293}
294
295void Video::Clear(ClearMask m)
296{
297#if defined USE_D3D9 || defined _XBOX
298    int mask = 0;
299    if (m & ClearMask::Color)
300        mask |= D3DCLEAR_TARGET;
301    if (m & ClearMask::Depth)
302        mask |= D3DCLEAR_ZBUFFER;
303    if (m & ClearMask::Stencil)
304        mask |= D3DCLEAR_STENCIL;
305    if (FAILED(VideoData::d3d_dev->Clear(0, NULL, mask,
306                                         VideoData::clear_color,
307                                         VideoData::clear_depth, 0)))
308        Abort();
309#else
310    /* FIXME: is this necessary here? */
311    ivec2 size = GetSize();
312    glViewport(0, 0, size.x, size.y);
313
314    GLbitfield mask = 0;
315    if (m & ClearMask::Color)
316        mask |= GL_COLOR_BUFFER_BIT;
317    if (m & ClearMask::Depth)
318        mask |= GL_DEPTH_BUFFER_BIT;
319    if (m & ClearMask::Stencil)
320        mask |= GL_STENCIL_BUFFER_BIT;
321    glClear(mask);
322#endif
323
324    SetFov(0.0f);
325}
326
327void Video::Destroy()
328{
329    ;
330}
331
332void Video::Capture(uint32_t *buffer)
333{
334#if defined USE_D3D9 || defined _XBOX
335    /* TODO */
336#else
337    GLint v[4];
338#   if defined __CELLOS_LV2__
339    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
340    v[2] = 1920;
341    v[3] = 1080;
342#   else
343    glGetIntegerv(GL_VIEWPORT, v);
344#   endif
345    int width = v[2], height = v[3];
346
347#   if defined HAVE_GL_2X
348    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
349#   endif
350    glPixelStorei(GL_PACK_ALIGNMENT, 1);
351
352#   if defined GL_BGRA
353    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
354#   else
355    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
356#   endif
357
358    for (int j = 0; j < height / 2; j++)
359        for (int i = 0; i < width; i++)
360        {
361            uint32_t tmp = buffer[j * width + i];
362            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
363            buffer[(height - j - 1) * width + i] = tmp;
364        }
365#endif
366}
367
368ivec2 Video::GetSize()
369{
370#if defined USE_D3D9 || defined _XBOX
371    return VideoData::saved_viewport;
372#elif 1
373    /* GetSize() is called too often on the game thread; we cannot rely on
374     * the GL context at this point */
375    return VideoData::saved_viewport;
376#elif defined __CELLOS_LV2__
377    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
378#else
379    GLint v[4];
380    glGetIntegerv(GL_VIEWPORT, v);
381    return ivec2(v[2], v[3]);
382#endif
383}
384
385} /* namespace lol */
386
Note: See TracBrowser for help on using the repository browser.