source: trunk/src/video.cpp @ 668

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

Protect the Shader constructor and provide a static creation method that
will let us cache shaders.

  • Property svn:keywords set to Id
File size: 5.0 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 proj_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 proj_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 = proj_matrix * view_matrix * model_matrix"
51    "                * 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    "    gl_FragColor = texture2D(in_Texture, vec2(gl_TexCoord[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 = Shader::Create(vertexshader, fragmentshader);
89#endif
90}
91
92void Video::SetFov(float theta)
93{
94#undef near /* Fuck Microsoft */
95#undef far /* Fuck Microsoft again */
96    mat4 proj;
97
98    float width = GetWidth();
99    float height = GetHeight();
100    float near = -width - height;
101    float far = width + height;
102
103    /* Set the projection matrix */
104    if (theta < 1e-4f)
105    {
106        /* The easy way: purely orthogonal projection. */
107        proj_matrix = mat4::ortho(0, width, 0, height, near, far);
108    }
109    else
110    {
111        /* Compute a view that approximates the glOrtho view when theta
112         * approaches zero. This view ensures that the z=0 plane fills
113         * the screen. */
114        float t1 = tanf(theta / 2);
115        float t2 = t1 * height / width;
116        float dist = (float)width / (2.0f * t1);
117
118        near += dist;
119        far += dist;
120
121        if (near <= 0.0f)
122        {
123            far -= (near - 1.0f);
124            near = 1.0f;
125        }
126
127        proj_matrix = mat4::frustum(-near * t1, near * t1,
128                                    -near * t2, near * t2, near, far)
129                    * mat4::translate(-0.5f * width, -0.5f * height, -dist);
130    }
131
132    view_matrix = mat4(1.0f);
133
134#if LOL_EXPERIMENTAL
135    GLuint uni;
136    uni = stdshader->GetUniformLocation("proj_matrix");
137    glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]);
138    uni = stdshader->GetUniformLocation("view_matrix");
139    glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
140#else
141    glMatrixMode(GL_PROJECTION);
142    glLoadIdentity();
143    glMultMatrixf(&proj_matrix[0][0]);
144
145    /* Reset the model view matrix, just in case */
146    glMatrixMode(GL_MODELVIEW);
147    glLoadIdentity();
148    glMultMatrixf(&view_matrix[0][0]);
149#endif
150}
151
152void Video::SetDepth(bool set)
153{
154    if (set)
155        glEnable(GL_DEPTH_TEST);
156    else
157        glDisable(GL_DEPTH_TEST);
158}
159
160void Video::Clear()
161{
162    glViewport(0, 0, GetWidth(), GetHeight());
163    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
164
165    SetFov(0.0f);
166}
167
168void Video::Destroy()
169{
170#if LOL_EXPERIMENTAL
171    Shader::Destroy(stdshader);
172#endif
173}
174
175void Video::Capture(uint32_t *buffer)
176{
177    GLint v[4];
178    glGetIntegerv(GL_VIEWPORT, v);
179    int width = v[2], height = v[3];
180
181    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
182    glPixelStorei(GL_PACK_ALIGNMENT, 1);
183
184#if defined GL_BGRA
185    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
186#else
187    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
188#endif
189
190    for (int j = 0; j < height / 2; j++)
191        for (int i = 0; i < width; i++)
192        {
193            uint32_t tmp = buffer[j * width + i];
194            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
195            buffer[(height - j - 1) * width + i] = tmp;
196        }
197}
198
199int Video::GetWidth()
200{
201    GLint v[4];
202    glGetIntegerv(GL_VIEWPORT, v);
203    return v[2];
204}
205
206int Video::GetHeight()
207{
208    GLint v[4];
209    glGetIntegerv(GL_VIEWPORT, v);
210    return v[3];
211}
212
Note: See TracBrowser for help on using the repository browser.