source: trunk/src/video.cpp @ 665

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

OpenGL code refactoring.

  • Property svn:keywords set to Id
File size: 4.9 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#include <cstdio>
17
18#ifdef WIN32
19#   define WIN32_LEAN_AND_MEAN
20#   include <windows.h>
21#endif
22#if defined __APPLE__ && defined __MACH__
23#   include <OpenGL/gl.h>
24#else
25#   define GL_GLEXT_PROTOTYPES
26#   include <GL/gl.h>
27#endif
28
29#include "core.h"
30
31#if LOL_EXPERIMENTAL
32Shader *stdshader;
33#endif
34mat4 proj_matrix, view_matrix, model_matrix;
35
36#if LOL_EXPERIMENTAL
37static char const *vertexshader =
38    "#version 130\n"
39    "\n"
40    "in vec3 in_Position;\n"
41    "in vec3 in_Color;\n"
42    "in vec2 in_TexCoord;\n"
43    "out vec3 pass_Color;\n"
44    "uniform mat4 proj_matrix;\n"
45    "uniform mat4 view_matrix;\n"
46    "uniform mat4 model_matrix;\n"
47    "\n"
48    "void main()\n"
49    "{\n"
50    "    gl_Position = proj_matrix * view_matrix * model_matrix"
51    "                * vec4(in_Position, 1.0f);\n"
52    "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
53    "    pass_Color = in_Color;\n"
54    "}\n";
55
56static char const *fragmentshader =
57    "#version 130\n"
58    "\n"
59    "uniform sampler2D in_Texture;\n"
60    "in vec3 pass_Color;\n"
61    "out vec4 out_Color;\n"
62    "\n"
63    "void main()\n"
64    "{\n"
65    "    gl_FragColor = 0.5 * (texture2D(in_Texture, vec2(gl_TexCoord[0]))\n"
66    "                           + vec4(pass_Color, 1.0));\n"
67    "}\n";
68#endif
69
70/*
71 * Public Video class
72 */
73
74void Video::Setup(int width, int height)
75{
76    /* Initialise OpenGL */
77    glViewport(0, 0, width, height);
78
79    glEnable(GL_TEXTURE_2D);
80    glShadeModel(GL_SMOOTH);
81    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
82    glClearDepth(1.0);
83
84    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
85
86#if LOL_EXPERIMENTAL
87    stdshader = new Shader(vertexshader, fragmentshader);
88#endif
89}
90
91void Video::SetFov(float theta)
92{
93#undef near /* Fuck Microsoft */
94#undef far /* Fuck Microsoft again */
95    mat4 proj;
96
97    float width = GetWidth();
98    float height = GetHeight();
99    float near = -width - height;
100    float far = width + height;
101
102    /* Set the projection matrix */
103    if (theta < 1e-4f)
104    {
105        /* The easy way: purely orthogonal projection. */
106        proj_matrix = mat4::ortho(0, width, 0, height, near, far);
107    }
108    else
109    {
110        /* Compute a view that approximates the glOrtho view when theta
111         * approaches zero. This view ensures that the z=0 plane fills
112         * the screen. */
113        float t1 = tanf(theta / 2);
114        float t2 = t1 * height / width;
115        float dist = (float)width / (2.0f * t1);
116
117        near += dist;
118        far += dist;
119
120        if (near <= 0.0f)
121        {
122            far -= (near - 1.0f);
123            near = 1.0f;
124        }
125
126        proj_matrix = mat4::frustum(-near * t1, near * t1,
127                                    -near * t2, near * t2, near, far)
128                    * mat4::translate(-0.5f * width, -0.5f * height, -dist);
129    }
130
131    view_matrix = mat4(1.0f);
132
133#if !LOL_EXPERIMENTAL
134    glMatrixMode(GL_PROJECTION);
135    glLoadIdentity();
136    glMultMatrixf(&proj_matrix[0][0]);
137
138    /* Reset the model view matrix, just in case */
139    glMatrixMode(GL_MODELVIEW);
140    glLoadIdentity();
141    glMultMatrixf(&view_matrix[0][0]);
142#endif
143}
144
145void Video::SetDepth(bool set)
146{
147    if (set)
148        glEnable(GL_DEPTH_TEST);
149    else
150        glDisable(GL_DEPTH_TEST);
151}
152
153void Video::Clear()
154{
155    glViewport(0, 0, GetWidth(), GetHeight());
156    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
157
158#if LOL_EXPERIMENTAL
159    GLuint uni;
160    uni = stdshader->GetUniformLocation("proj_matrix");
161    glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]);
162    uni = stdshader->GetUniformLocation("view_matrix");
163    glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
164#endif
165
166    SetFov(0.0f);
167}
168
169void Video::Destroy()
170{
171#if LOL_EXPERIMENTAL
172    delete stdshader;
173#endif
174}
175
176void Video::Capture(uint32_t *buffer)
177{
178    GLint v[4];
179    glGetIntegerv(GL_VIEWPORT, v);
180    int width = v[2], height = v[3];
181
182    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
183    glPixelStorei(GL_PACK_ALIGNMENT, 1);
184
185#if defined GL_BGRA
186    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
187#else
188    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
189#endif
190
191    for (int j = 0; j < height / 2; j++)
192        for (int i = 0; i < width; i++)
193        {
194            uint32_t tmp = buffer[j * width + i];
195            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
196            buffer[(height - j - 1) * width + i] = tmp;
197        }
198}
199
200int Video::GetWidth()
201{
202    GLint v[4];
203    glGetIntegerv(GL_VIEWPORT, v);
204    return v[2];
205}
206
207int Video::GetHeight()
208{
209    GLint v[4];
210    glGetIntegerv(GL_VIEWPORT, v);
211    return v[3];
212}
213
Note: See TracBrowser for help on using the repository browser.