source: trunk/src/video.cpp @ 221

Last change on this file since 221 was 221, checked in by sam, 9 years ago

Complete Lol Engine / Deus Hax / Monsterz split.

  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 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#ifdef WIN32
18#   define WIN32_LEAN_AND_MEAN
19#   include <windows.h>
20#endif
21#if defined __APPLE__ && defined __MACH__
22#   include <OpenGL/gl.h>
23#else
24#   define GL_GLEXT_PROTOTYPES
25#   include <GL/gl.h>
26#endif
27
28#include "core.h"
29
30/*
31 * Public Video class
32 */
33
34void Video::Setup(int width, int height)
35{
36    /* Initialise OpenGL */
37    glViewport(0, 0, width, height);
38
39    glEnable(GL_TEXTURE_2D);
40    glShadeModel(GL_SMOOTH);
41    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
42    glClearDepth(1.0);
43
44    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
45}
46
47void Video::SetFov(float theta)
48{
49#undef near /* Fuck Microsoft */
50#undef far /* Fuck Microsoft again */
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::SetDepth(bool set)
93{
94    if (set)
95        glEnable(GL_DEPTH_TEST);
96    else
97        glDisable(GL_DEPTH_TEST);
98}
99
100void Video::Clear()
101{
102    glEnable(GL_DEPTH_TEST);
103    glDepthFunc(GL_LEQUAL);
104    glEnable(GL_ALPHA_TEST);
105    glAlphaFunc(GL_GEQUAL, 0.01f);
106    glEnable(GL_BLEND);
107    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
108
109    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
110
111    SetFov(0.0f);
112}
113
114void Video::Capture(uint32_t *buffer)
115{
116    GLint v[4];
117    glGetIntegerv(GL_VIEWPORT, v);
118    int width = v[2], height = v[3];
119
120    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
121    glPixelStorei(GL_PACK_ALIGNMENT, 1);
122
123    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
124
125    for (int j = 0; j < height / 2; j++)
126        for (int i = 0; i < width; i++)
127        {
128            uint32_t tmp = buffer[j * width + i];
129            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
130            buffer[(height - j - 1) * width + i] = tmp;
131        }
132}
133
134int Video::GetWidth()
135{
136    GLint v[4];
137    glGetIntegerv(GL_VIEWPORT, v);
138    return v[2];
139}
140
141int Video::GetHeight()
142{
143    GLint v[4];
144    glGetIntegerv(GL_VIEWPORT, v);
145    return v[3];
146}
147
Note: See TracBrowser for help on using the repository browser.