source: trunk/src/video.cpp @ 1194

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

xbox: fix a few problems in the shader handling code; the first tutorial now
works on the Xbox.

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