source: trunk/src/video.cpp @ 666

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

Experimental GL mode works!

  • 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 = new Shader(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    glMatrixMode(GL_PROJECTION);
136    glLoadIdentity();
137    glMultMatrixf(&proj_matrix[0][0]);
138
139    /* Reset the model view matrix, just in case */
140    glMatrixMode(GL_MODELVIEW);
141    glLoadIdentity();
142    glMultMatrixf(&view_matrix[0][0]);
143#endif
144}
145
146void Video::SetDepth(bool set)
147{
148    if (set)
149        glEnable(GL_DEPTH_TEST);
150    else
151        glDisable(GL_DEPTH_TEST);
152}
153
154void Video::Clear()
155{
156    glViewport(0, 0, GetWidth(), GetHeight());
157    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
158
159#if LOL_EXPERIMENTAL
160    GLuint uni;
161    uni = stdshader->GetUniformLocation("proj_matrix");
162    glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]);
163    uni = stdshader->GetUniformLocation("view_matrix");
164    glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
165#endif
166
167    SetFov(0.0f);
168}
169
170void Video::Destroy()
171{
172#if LOL_EXPERIMENTAL
173    delete stdshader;
174#endif
175}
176
177void Video::Capture(uint32_t *buffer)
178{
179    GLint v[4];
180    glGetIntegerv(GL_VIEWPORT, v);
181    int width = v[2], height = v[3];
182
183    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
184    glPixelStorei(GL_PACK_ALIGNMENT, 1);
185
186#if defined GL_BGRA
187    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
188#else
189    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
190#endif
191
192    for (int j = 0; j < height / 2; j++)
193        for (int i = 0; i < width; i++)
194        {
195            uint32_t tmp = buffer[j * width + i];
196            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
197            buffer[(height - j - 1) * width + i] = tmp;
198        }
199}
200
201int Video::GetWidth()
202{
203    GLint v[4];
204    glGetIntegerv(GL_VIEWPORT, v);
205    return v[2];
206}
207
208int Video::GetHeight()
209{
210    GLint v[4];
211    glGetIntegerv(GL_VIEWPORT, v);
212    return v[3];
213}
214
Note: See TracBrowser for help on using the repository browser.