source: trunk/src/video.cpp @ 653

Last change on this file since 653 was 653, checked in by sam, 11 years ago

More shader crap for GLSL 1.30. Still disabled.

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