source: trunk/src/video.cpp @ 207

Last change on this file since 207 was 207, checked in by sam, 10 years ago

Proper depth buffer handling with minimal alpha support.

  • Property svn:keywords set to Id
File size: 2.0 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#ifdef WIN32
11#   define WIN32_LEAN_AND_MEAN
12#   include <windows.h>
13#endif
14#if defined __APPLE__ && defined __MACH__
15#   include <OpenGL/gl.h>
16#else
17#   define GL_GLEXT_PROTOTYPES
18#   include <GL/gl.h>
19#endif
20
21#include "core.h"
22
23/*
24 * Public Video class
25 */
26
27void Video::Setup(int width, int height)
28{
29    /* Initialise OpenGL */
30    glViewport(0, 0, width, height);
31
32    glEnable(GL_TEXTURE_2D);
33    glShadeModel(GL_SMOOTH);
34    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
35    glClearDepth(1.0);
36
37    glEnable(GL_DEPTH_TEST);
38    glDepthFunc(GL_LEQUAL);
39    glEnable(GL_ALPHA_TEST);
40    glAlphaFunc(GL_GEQUAL, 0.01f);
41    glEnable(GL_BLEND);
42    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
43
44    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
45
46    /* Projection matrix: once and for all */
47    glMatrixMode(GL_PROJECTION);
48    glLoadIdentity();
49    glOrtho(0, width, 0, height, -(width + height), width + height);
50}
51
52void Video::Clear()
53{
54    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
55
56    /* Model view matrix: for each frame, just in case */
57    glMatrixMode(GL_MODELVIEW);
58    glLoadIdentity();
59}
60
61void Video::Capture(uint32_t *buffer)
62{
63    GLint v[4];
64    glGetIntegerv(GL_VIEWPORT, v);
65    int width = v[2], height = v[3];
66
67    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
68    glPixelStorei(GL_PACK_ALIGNMENT, 1);
69
70    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
71
72    for (int j = 0; j < height / 2; j++)
73        for (int i = 0; i < width; i++)
74        {
75            uint32_t tmp = buffer[j * width + i];
76            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
77            buffer[(height - j - 1) * width + i] = tmp;
78        }
79}
80
81int Video::GetWidth()
82{
83    GLint v[4];
84    glGetIntegerv(GL_VIEWPORT, v);
85    return v[2];
86}
87
88int Video::GetHeight()
89{
90    GLint v[4];
91    glGetIntegerv(GL_VIEWPORT, v);
92    return v[3];
93}
94
Note: See TracBrowser for help on using the repository browser.