source: trunk/src/video.cpp @ 673

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

Put the OpenGL header handling in <lolgl.h> for more convenience.

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