source: trunk/src/video.cpp @ 1199

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

video: allow to set the default clear color.

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