source: trunk/src/video.cpp @ 664

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

Get rid of float3, float4, int3 etc. in favour of GLSL types.

  • Property svn:keywords set to Id
File size: 5.2 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 projection_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 projection_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 = projection_matrix * view_matrix *"
51    "                  model_matrix * 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 = 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 = 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#if LOL_EXPERIMENTAL
132    projection_matrix = proj;
133    view_matrix = mat4(1.0f);
134#else
135    glMatrixMode(GL_PROJECTION);
136    glLoadIdentity();
137    glMultMatrixf(&proj[0][0]);
138
139    /* Reset the model view matrix, just in case */
140    glMatrixMode(GL_MODELVIEW);
141    glLoadIdentity();
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#if LOL_EXPERIMENTAL
156    glViewport(0, 0, GetWidth(), GetHeight());
157    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
158
159    GLuint uni;
160    uni = stdshader->GetUniformLocation("projection_matrix");
161    glUniformMatrix4fv(uni, 1, GL_FALSE, &projection_matrix[0][0]);
162    uni = stdshader->GetUniformLocation("view_matrix");
163    glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
164#else
165    glEnable(GL_DEPTH_TEST);
166    glDepthFunc(GL_LEQUAL);
167    glEnable(GL_ALPHA_TEST);
168    glAlphaFunc(GL_GEQUAL, 0.01f);
169    glEnable(GL_BLEND);
170    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
171
172    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
173#endif
174
175    SetFov(0.0f);
176}
177
178void Video::Destroy()
179{
180#if LOL_EXPERIMENTAL
181    delete stdshader;
182#endif
183}
184
185void Video::Capture(uint32_t *buffer)
186{
187    GLint v[4];
188    glGetIntegerv(GL_VIEWPORT, v);
189    int width = v[2], height = v[3];
190
191    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
192    glPixelStorei(GL_PACK_ALIGNMENT, 1);
193
194#if defined GL_BGRA
195    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
196#else
197    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
198#endif
199
200    for (int j = 0; j < height / 2; j++)
201        for (int i = 0; i < width; i++)
202        {
203            uint32_t tmp = buffer[j * width + i];
204            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
205            buffer[(height - j - 1) * width + i] = tmp;
206        }
207}
208
209int Video::GetWidth()
210{
211    GLint v[4];
212    glGetIntegerv(GL_VIEWPORT, v);
213    return v[2];
214}
215
216int Video::GetHeight()
217{
218    GLint v[4];
219    glGetIntegerv(GL_VIEWPORT, v);
220    return v[3];
221}
222
Note: See TracBrowser for help on using the repository browser.