source: trunk/src/video.cpp @ 2394

Last change on this file since 2394 was 2379, checked in by touky, 7 years ago

Added VertexDictionnary object to manage vertices with same coord && connected vertices.
Added UVs system with and UVs generation test (not very conclusive)

  • Property svn:keywords set to Id
File size: 9.8 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    static bool face_culling;
56#if defined USE_D3D9 || defined _XBOX
57#   if defined USE_D3D9
58    static IDirect3D9 *d3d_ctx;
59    static IDirect3DDevice9 *d3d_dev;
60#   elif defined _XBOX
61    static Direct3D *d3d_ctx;
62    static D3DDevice *d3d_dev;
63#   endif
64    static D3DCOLOR clear_color;
65    static float clear_depth;
66#endif
67};
68
69mat4 VideoData::proj_matrix;
70ivec2 VideoData::saved_viewport(0, 0);
71DebugRenderMode VideoData::render_mode = DebugRenderMode::Default;
72bool VideoData::face_culling;
73
74#if defined USE_D3D9 || defined _XBOX
75#   if defined USE_D3D9
76IDirect3D9 *VideoData::d3d_ctx;
77IDirect3DDevice9 *VideoData::d3d_dev;
78#   elif defined _XBOX
79Direct3D *VideoData::d3d_ctx;
80D3DDevice *VideoData::d3d_dev;
81#   endif
82D3DCOLOR VideoData::clear_color;
83float VideoData::clear_depth;
84#endif
85
86/*
87 * Public Video class
88 */
89
90void Video::Setup(ivec2 size)
91{
92#if defined USE_D3D9 || defined _XBOX
93    VideoData::d3d_ctx = Direct3DCreate9(D3D_SDK_VERSION);
94    if (!VideoData::d3d_ctx)
95    {
96        Log::Error("cannot initialise D3D\n");
97        exit(EXIT_FAILURE);
98    }
99
100    HWND window = 0;
101    D3DPRESENT_PARAMETERS d3dpp;
102    memset(&d3dpp, 0, sizeof(d3dpp));
103
104#   if defined USE_SDL
105    window = g_hwnd;
106    d3dpp.hDeviceWindow = g_hwnd;
107    d3dpp.Windowed = TRUE;
108#   elif defined _XBOX
109    XVIDEO_MODE VideoMode;
110    XGetVideoMode( &VideoMode );
111    if (size.x > VideoMode.dwDisplayWidth)
112        size.x = VideoMode.dwDisplayWidth;
113    if (size.y > VideoMode.dwDisplayHeight)
114        size.y = VideoMode.dwDisplayHeight;
115#   endif
116    VideoData::saved_viewport = size;
117
118    d3dpp.BackBufferWidth = size.x;
119    d3dpp.BackBufferHeight = size.y;
120    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
121    d3dpp.BackBufferCount = 1;
122    d3dpp.EnableAutoDepthStencil = TRUE;
123    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
124    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
125    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
126
127    HRESULT hr = VideoData::d3d_ctx->CreateDevice(0, D3DDEVTYPE_HAL, window,
128                                                  D3DCREATE_HARDWARE_VERTEXPROCESSING,
129                                                  &d3dpp, &VideoData::d3d_dev);
130    if (FAILED(hr))
131    {
132        Log::Error("cannot create D3D device\n");
133        exit(EXIT_FAILURE);
134    }
135
136    g_d3ddevice = VideoData::d3d_dev;
137#else
138#   if defined USE_GLEW && !defined __APPLE__
139    /* Initialise GLEW if necessary */
140    GLenum glerr = glewInit();
141    if (glerr != GLEW_OK)
142    {
143        Log::Error("cannot initialise GLEW: %s\n", glewGetErrorString(glerr));
144        exit(EXIT_FAILURE);
145    }
146#   endif
147
148    /* Initialise OpenGL */
149    glViewport(0, 0, size.x, size.y);
150    VideoData::saved_viewport = size;
151
152#   if defined HAVE_GL_2X && !defined __APPLE__
153    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
154#   endif
155#endif
156
157    /* Initialise reasonable scene default properties */
158    SetClearColor(vec4(0.1f, 0.2f, 0.3f, 1.0f));
159    SetClearDepth(1.f);
160    SetDebugRenderMode(DebugRenderMode::Default);
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
204void Video::SetDepth(bool set)
205{
206#if defined USE_D3D9 || defined _XBOX
207#   define STR0(x) #x
208#   define STR(x) STR0(x)
209#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetDepth() not implemented")
210#else
211    if (set)
212        glEnable(GL_DEPTH_TEST);
213    else
214        glDisable(GL_DEPTH_TEST);
215#endif
216}
217
218void Video::SetClearColor(vec4 color)
219{
220#if defined USE_D3D9 || defined _XBOX
221    VideoData::clear_color = D3DCOLOR_XRGB((int)(color.r * 255.999f),
222                                           (int)(color.g * 255.999f),
223                                           (int)(color.b * 255.999f));
224#else
225    glClearColor(color.r, color.g, color.b, color.a);
226#endif
227}
228
229void Video::SetClearDepth(float f)
230{
231#if defined USE_D3D9 || defined _XBOX
232    VideoData::clear_depth = f;
233#elif defined HAVE_GLES_2X
234    glClearDepthf(f);
235#else
236    glClearDepth(f);
237#endif
238}
239
240void Video::SetDebugRenderMode(DebugRenderMode d)
241{
242    if (d == DebugRenderMode::Max)
243        return;
244
245    switch(d)
246    {
247        //All these modes are handled in the shaders.
248        case DebugRenderMode::Default:
249        case DebugRenderMode::Lighting:
250        case DebugRenderMode::Normal:
251        case DebugRenderMode::UV:
252        {
253#if defined USE_D3D9 || defined _XBOX
254#elif defined HAVE_GLES_2X
255            glEnable(GL_CULL_FACE);
256#else
257            if (VideoData::render_mode == d && glIsEnabled(GL_CULL_FACE) == GL_TRUE)
258                glDisable(GL_CULL_FACE);
259            else
260                glEnable(GL_CULL_FACE);
261            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
262#endif
263            break;
264        }
265        case DebugRenderMode::Wireframe:
266        {
267            if (VideoData::render_mode == d)
268            {
269#if defined USE_D3D9 || defined _XBOX
270#else
271                VideoData::face_culling = !VideoData::face_culling;
272                if (VideoData::face_culling)
273                    glEnable(GL_CULL_FACE);
274                else
275                    glDisable(GL_CULL_FACE);
276#endif
277            }
278            else
279            {
280#if defined USE_D3D9 || defined _XBOX
281#else
282                glDisable(GL_CULL_FACE);
283#endif
284            }
285#if defined USE_D3D9 || defined _XBOX
286#elif defined HAVE_GLES_2X
287#else
288            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
289#endif
290            break;
291        }
292    }
293    VideoData::render_mode = d;
294}
295
296DebugRenderMode Video::GetDebugRenderMode()
297{
298    return VideoData::render_mode;
299}
300
301void Video::Clear(ClearMask m)
302{
303#if defined USE_D3D9 || defined _XBOX
304    int mask = 0;
305    if (m & ClearMask::Color)
306        mask |= D3DCLEAR_TARGET;
307    if (m & ClearMask::Depth)
308        mask |= D3DCLEAR_ZBUFFER;
309    if (m & ClearMask::Stencil)
310        mask |= D3DCLEAR_STENCIL;
311    if (FAILED(VideoData::d3d_dev->Clear(0, NULL, mask,
312                                         VideoData::clear_color,
313                                         VideoData::clear_depth, 0)))
314        Abort();
315#else
316    /* FIXME: is this necessary here? */
317    ivec2 size = GetSize();
318    glViewport(0, 0, size.x, size.y);
319
320    GLbitfield mask = 0;
321    if (m & ClearMask::Color)
322        mask |= GL_COLOR_BUFFER_BIT;
323    if (m & ClearMask::Depth)
324        mask |= GL_DEPTH_BUFFER_BIT;
325    if (m & ClearMask::Stencil)
326        mask |= GL_STENCIL_BUFFER_BIT;
327    glClear(mask);
328#endif
329
330    SetFov(0.0f);
331}
332
333void Video::Destroy()
334{
335    ;
336}
337
338void Video::Capture(uint32_t *buffer)
339{
340#if defined USE_D3D9 || defined _XBOX
341    /* TODO */
342#else
343    GLint v[4];
344#   if defined __CELLOS_LV2__
345    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
346    v[2] = 1920;
347    v[3] = 1080;
348#   else
349    glGetIntegerv(GL_VIEWPORT, v);
350#   endif
351    int width = v[2], height = v[3];
352
353#   if defined HAVE_GL_2X
354    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
355#   endif
356    glPixelStorei(GL_PACK_ALIGNMENT, 1);
357
358#   if defined GL_BGRA
359    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
360#   else
361    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
362#   endif
363
364    for (int j = 0; j < height / 2; j++)
365        for (int i = 0; i < width; i++)
366        {
367            uint32_t tmp = buffer[j * width + i];
368            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
369            buffer[(height - j - 1) * width + i] = tmp;
370        }
371#endif
372}
373
374ivec2 Video::GetSize()
375{
376#if defined USE_D3D9 || defined _XBOX
377    return VideoData::saved_viewport;
378#elif 1
379    /* GetSize() is called too often on the game thread; we cannot rely on
380     * the GL context at this point */
381    return VideoData::saved_viewport;
382#elif defined __CELLOS_LV2__
383    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
384#else
385    GLint v[4];
386    glGetIntegerv(GL_VIEWPORT, v);
387    return ivec2(v[2], v[3]);
388#endif
389}
390
391} /* namespace lol */
392
Note: See TracBrowser for help on using the repository browser.