source: trunk/src/video.cpp @ 209

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

Implement Video::SetFov() to allow conic projection.

  • Property svn:keywords set to Id
File size: 2.8 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    glEnable(GL_DEPTH_TEST);
40    glDepthFunc(GL_LEQUAL);
41    glEnable(GL_ALPHA_TEST);
42    glAlphaFunc(GL_GEQUAL, 0.01f);
43    glEnable(GL_BLEND);
44    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
45
46    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
47}
48
49void Video::SetFov(float theta)
50{
51    glMatrixMode(GL_PROJECTION);
52    glLoadIdentity();
53
54    float width = GetWidth();
55    float height = GetHeight();
56    float near = -width - height;
57    float far = width + height;
58
59    /* Set the projection matrix */
60    if (theta < 1e-4f)
61    {
62        /* The easy way: purely orthogonal projection. */
63        glOrtho(0, width, 0, height, near, far);
64    }
65    else
66    {
67        /* Compute a view that approximates the glOrtho view when theta
68         * approaches zero. This view ensures that the z=0 plane fills
69         * the screen. */
70        float t1 = tanf(theta / 2);
71        float t2 = t1 * height / width;
72        float dist = (float)width / (2.0f * t1);
73
74        near += dist;
75        far += dist;
76
77        if (near <= 0.0f)
78        {
79            far -= (near - 1.0f);
80            near = 1.0f;
81        }
82
83        glFrustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
84        glTranslatef(-0.5f * width, -0.5f * height, -dist);
85    }
86
87    /* Reset the model view matrix, just in case */
88    glMatrixMode(GL_MODELVIEW);
89    glLoadIdentity();
90}
91
92void Video::Clear()
93{
94    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
95
96    SetFov(0.0f);
97}
98
99void Video::Capture(uint32_t *buffer)
100{
101    GLint v[4];
102    glGetIntegerv(GL_VIEWPORT, v);
103    int width = v[2], height = v[3];
104
105    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
106    glPixelStorei(GL_PACK_ALIGNMENT, 1);
107
108    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
109
110    for (int j = 0; j < height / 2; j++)
111        for (int i = 0; i < width; i++)
112        {
113            uint32_t tmp = buffer[j * width + i];
114            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
115            buffer[(height - j - 1) * width + i] = tmp;
116        }
117}
118
119int Video::GetWidth()
120{
121    GLint v[4];
122    glGetIntegerv(GL_VIEWPORT, v);
123    return v[2];
124}
125
126int Video::GetHeight()
127{
128    GLint v[4];
129    glGetIntegerv(GL_VIEWPORT, v);
130    return v[3];
131}
132
Note: See TracBrowser for help on using the repository browser.