source: trunk/src/video.cpp @ 659

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

Try to factor all the recent GLSL stuff in the new Shader class.

  • Property svn:keywords set to Id
File size: 5.7 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
34    float4x4 projection_matrix, view_matrix, model_matrix;
35#endif
36
37#if LOL_EXPERIMENTAL
38static char const *vertexshader =
39    "#version 130\n"
40    "\n"
41    "in vec3 in_Position;\n"
42    "in vec3 in_Color;\n"
43    "in vec2 in_TexCoord;\n"
44    "out vec3 pass_Color;\n"
45    "uniform mat4 projection_matrix;\n"
46    "uniform mat4 view_matrix;\n"
47    "uniform mat4 model_matrix;\n"
48    "\n"
49    "void main()\n"
50    "{\n"
51    "    gl_Position = projection_matrix * view_matrix *"
52    "                  model_matrix * vec4(in_Position, 1.0f);\n"
53    "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
54    "    pass_Color = in_Color;\n"
55    "}\n";
56
57static char const *fragmentshader =
58    "#version 130\n"
59    "\n"
60    "uniform sampler2D in_Texture;\n"
61    "in vec3 pass_Color;\n"
62    "out vec4 out_Color;\n"
63    "\n"
64    "void main()\n"
65    "{\n"
66    "    gl_FragColor = 0.5 * (texture2D(in_Texture, vec2(gl_TexCoord[0]))\n"
67    "                           + vec4(pass_Color, 1.0));\n"
68    "}\n";
69#endif
70
71/*
72 * Public Video class
73 */
74
75void Video::Setup(int width, int height)
76{
77    /* Initialise OpenGL */
78    glViewport(0, 0, width, height);
79
80    glEnable(GL_TEXTURE_2D);
81    glShadeModel(GL_SMOOTH);
82    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
83    glClearDepth(1.0);
84
85    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
86
87#if LOL_EXPERIMENTAL
88    stdshader = new Shader(vertexshader, fragmentshader);
89#endif
90}
91
92void Video::SetFov(float theta)
93{
94#if LOL_EXPERIMENTAL
95    float width = GetWidth();
96    float height = GetHeight();
97    //float near = -width - height;
98    //float far = width + height;
99    float near = 20.0f;
100    float far = 0.1f;
101    projection_matrix = float4x4::perspective(theta, width, height, near, far);
102#else
103#undef near /* Fuck Microsoft */
104#undef far /* Fuck Microsoft again */
105    glMatrixMode(GL_PROJECTION);
106    glLoadIdentity();
107
108    float width = GetWidth();
109    float height = GetHeight();
110    float near = -width - height;
111    float far = width + height;
112
113    /* Set the projection matrix */
114    if (theta < 1e-4f)
115    {
116        /* The easy way: purely orthogonal projection. */
117        glOrtho(0, width, 0, height, near, far);
118    }
119    else
120    {
121        /* Compute a view that approximates the glOrtho view when theta
122         * approaches zero. This view ensures that the z=0 plane fills
123         * the screen. */
124        float t1 = tanf(theta / 2);
125        float t2 = t1 * height / width;
126        float dist = (float)width / (2.0f * t1);
127
128        near += dist;
129        far += dist;
130
131        if (near <= 0.0f)
132        {
133            far -= (near - 1.0f);
134            near = 1.0f;
135        }
136
137        glFrustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
138        glTranslatef(-0.5f * width, -0.5f * height, -dist);
139    }
140
141    /* Reset the model view matrix, just in case */
142    glMatrixMode(GL_MODELVIEW);
143    glLoadIdentity();
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#if LOL_EXPERIMENTAL
158    glViewport(0, 0, GetWidth(), GetHeight());
159    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
160
161    view_matrix = float4x4(1.0f);
162    view_matrix[3][0] = 0.0f;
163    view_matrix[3][1] = 0.0f;
164    view_matrix[3][2] = -5.0f;
165
166    model_matrix = float4x4(1.0f);
167    model_matrix[0][0] = 0.5f;
168    model_matrix[1][1] = 0.5f;
169    model_matrix[2][2] = 0.5f;
170
171    GLuint uni;
172    uni = stdshader->GetUniformLocation("projection_matrix");
173    glUniformMatrix4fv(uni, 1, GL_FALSE, &projection_matrix[0][0]);
174    uni = stdshader->GetUniformLocation("view_matrix");
175    glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
176    uni = stdshader->GetUniformLocation("model_matrix");
177    glUniformMatrix4fv(uni, 1, GL_FALSE, &model_matrix[0][0]);
178#else
179    glEnable(GL_DEPTH_TEST);
180    glDepthFunc(GL_LEQUAL);
181    glEnable(GL_ALPHA_TEST);
182    glAlphaFunc(GL_GEQUAL, 0.01f);
183    glEnable(GL_BLEND);
184    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
185
186    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
187#endif
188
189#if LOL_EXPERIMENTAL
190    static float time;
191    time += 0.01f;
192    SetFov(1.0f + sinf(time));
193#else
194    SetFov(0.5f);
195#endif
196}
197
198void Video::Destroy()
199{
200#if LOL_EXPERIMENTAL
201    delete stdshader;
202#endif
203}
204
205void Video::Capture(uint32_t *buffer)
206{
207    GLint v[4];
208    glGetIntegerv(GL_VIEWPORT, v);
209    int width = v[2], height = v[3];
210
211    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
212    glPixelStorei(GL_PACK_ALIGNMENT, 1);
213
214#if defined GL_BGRA
215    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
216#else
217    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
218#endif
219
220    for (int j = 0; j < height / 2; j++)
221        for (int i = 0; i < width; i++)
222        {
223            uint32_t tmp = buffer[j * width + i];
224            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
225            buffer[(height - j - 1) * width + i] = tmp;
226        }
227}
228
229int Video::GetWidth()
230{
231    GLint v[4];
232    glGetIntegerv(GL_VIEWPORT, v);
233    return v[2];
234}
235
236int Video::GetHeight()
237{
238    GLint v[4];
239    glGetIntegerv(GL_VIEWPORT, v);
240    return v[3];
241}
242
Note: See TracBrowser for help on using the repository browser.