source: trunk/src/video.cpp @ 658

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

Get rid of the SHADER_CRAP macro and use LOL_EXPERIMENTAL instead,
together with the --enable-experimental flag. Use LOL_DEBUG and
LOL_RELEASE instead of the previous values.

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