source: trunk/src/video.cpp @ 211

Last change on this file since 211 was 211, checked in by sam, 11 years ago

Put HUD objects back on front of the scene.

  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <math.h>
11
12#ifdef WIN32
13#   define WIN32_LEAN_AND_MEAN
14#   include <windows.h>
15#endif
16#if defined __APPLE__ && defined __MACH__
17#   include <OpenGL/gl.h>
18#else
19#   define GL_GLEXT_PROTOTYPES
20#   include <GL/gl.h>
21#endif
22
23#include "core.h"
24
25/*
26 * Public Video class
27 */
28
29void Video::Setup(int width, int height)
30{
31    /* Initialise OpenGL */
32    glViewport(0, 0, width, height);
33
34    glEnable(GL_TEXTURE_2D);
35    glShadeModel(GL_SMOOTH);
36    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
37    glClearDepth(1.0);
38
39    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
40}
41
42void Video::SetFov(float theta)
43{
44    glMatrixMode(GL_PROJECTION);
45    glLoadIdentity();
46
47    float width = GetWidth();
48    float height = GetHeight();
49    float near = -width - height;
50    float far = width + height;
51
52    /* Set the projection matrix */
53    if (theta < 1e-4f)
54    {
55        /* The easy way: purely orthogonal projection. */
56        glOrtho(0, width, 0, height, near, far);
57    }
58    else
59    {
60        /* Compute a view that approximates the glOrtho view when theta
61         * approaches zero. This view ensures that the z=0 plane fills
62         * the screen. */
63        float t1 = tanf(theta / 2);
64        float t2 = t1 * height / width;
65        float dist = (float)width / (2.0f * t1);
66
67        near += dist;
68        far += dist;
69
70        if (near <= 0.0f)
71        {
72            far -= (near - 1.0f);
73            near = 1.0f;
74        }
75
76        glFrustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
77        glTranslatef(-0.5f * width, -0.5f * height, -dist);
78    }
79
80    /* Reset the model view matrix, just in case */
81    glMatrixMode(GL_MODELVIEW);
82    glLoadIdentity();
83}
84
85void Video::SetDepth(bool set)
86{
87    if (set)
88        glEnable(GL_DEPTH_TEST);
89    else
90        glDisable(GL_DEPTH_TEST);
91}
92
93void Video::Clear()
94{
95    glEnable(GL_DEPTH_TEST);
96    glDepthFunc(GL_LEQUAL);
97    glEnable(GL_ALPHA_TEST);
98    glAlphaFunc(GL_GEQUAL, 0.01f);
99    glEnable(GL_BLEND);
100    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
101
102    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
103
104    SetFov(0.0f);
105}
106
107void Video::Capture(uint32_t *buffer)
108{
109    GLint v[4];
110    glGetIntegerv(GL_VIEWPORT, v);
111    int width = v[2], height = v[3];
112
113    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
114    glPixelStorei(GL_PACK_ALIGNMENT, 1);
115
116    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
117
118    for (int j = 0; j < height / 2; j++)
119        for (int i = 0; i < width; i++)
120        {
121            uint32_t tmp = buffer[j * width + i];
122            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
123            buffer[(height - j - 1) * width + i] = tmp;
124        }
125}
126
127int Video::GetWidth()
128{
129    GLint v[4];
130    glGetIntegerv(GL_VIEWPORT, v);
131    return v[2];
132}
133
134int Video::GetHeight()
135{
136    GLint v[4];
137    glGetIntegerv(GL_VIEWPORT, v);
138    return v[3];
139}
140
Note: See TracBrowser for help on using the repository browser.