source: trunk/src/video.cpp @ 1173

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

xbox: start working on an Xbox/Direct3D port.

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