source: trunk/src/video.cpp @ 656

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

More shader crap. Texture coordinates now work. Still disabled.

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