1 | // |
---|
2 | // Lol Engine - Framebuffer Object tutorial |
---|
3 | // |
---|
4 | // Copyright: (c) 2012 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 "core.h" |
---|
16 | #include "loldebug.h" |
---|
17 | #include "lolgl.h" |
---|
18 | |
---|
19 | using namespace std; |
---|
20 | using namespace lol; |
---|
21 | |
---|
22 | #if defined _WIN32 |
---|
23 | # include <direct.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | extern char const *lolfx_08_fbo; |
---|
27 | |
---|
28 | class FBO : public WorldEntity |
---|
29 | { |
---|
30 | public: |
---|
31 | FBO() |
---|
32 | { |
---|
33 | m_vertices << vec2( 1.0, 1.0); |
---|
34 | m_vertices << vec2(-1.0, -1.0); |
---|
35 | m_vertices << vec2( 1.0, -1.0); |
---|
36 | m_vertices << vec2(-1.0, -1.0); |
---|
37 | m_vertices << vec2( 1.0, 1.0); |
---|
38 | m_vertices << vec2(-1.0, 1.0); |
---|
39 | m_ready = false; |
---|
40 | } |
---|
41 | |
---|
42 | virtual void TickGame(float seconds) |
---|
43 | { |
---|
44 | WorldEntity::TickGame(seconds); |
---|
45 | |
---|
46 | m_time += seconds; |
---|
47 | m_hotspot = 0.4f * vec3(lol::cos(m_time * 2.f) + lol::cos(m_time * 3.3f), |
---|
48 | lol::sin(m_time * 4.4f) + lol::sin(m_time * 3.2f), |
---|
49 | lol::sin(m_time * 5.f)); |
---|
50 | m_color = 0.25f * vec3(3.f + lol::sin(m_time * 4.5f + 1.f), |
---|
51 | 3.f + lol::sin(m_time * 2.8f + 1.3f), |
---|
52 | 3.f + lol::sin(m_time * 3.7f)); |
---|
53 | } |
---|
54 | |
---|
55 | virtual void TickDraw(float seconds) |
---|
56 | { |
---|
57 | WorldEntity::TickDraw(seconds); |
---|
58 | |
---|
59 | if (!m_ready) |
---|
60 | { |
---|
61 | m_shader = Shader::Create(lolfx_08_fbo); |
---|
62 | m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0); |
---|
63 | m_uni_point = m_shader->GetUniformLocation("in_Point"); |
---|
64 | m_uni_color = m_shader->GetUniformLocation("in_Color"); |
---|
65 | m_uni_flag = m_shader->GetUniformLocation("in_Flag"); |
---|
66 | m_uni_texture = m_shader->GetUniformLocation("in_Texture"); |
---|
67 | |
---|
68 | m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position)); |
---|
69 | |
---|
70 | m_vbo = new VertexBuffer(m_vertices.Bytes()); |
---|
71 | void *vertices = m_vbo->Lock(0, 0); |
---|
72 | memcpy(vertices, &m_vertices[0], m_vertices.Bytes()); |
---|
73 | m_vbo->Unlock(); |
---|
74 | |
---|
75 | m_fbo = new FrameBuffer(Video::GetSize()); |
---|
76 | m_fbo->Bind(); |
---|
77 | glClearColor(0.0, 0.0, 0.0, 1.0f); |
---|
78 | glClearDepth(1.0f); |
---|
79 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
80 | m_fbo->Unbind(); |
---|
81 | |
---|
82 | m_ready = true; |
---|
83 | |
---|
84 | /* FIXME: this object never cleans up */ |
---|
85 | } |
---|
86 | |
---|
87 | m_fbo->Bind(); |
---|
88 | /* FIXME: we should just disable depth test in the shader */ |
---|
89 | glClear(GL_DEPTH_BUFFER_BIT); |
---|
90 | m_shader->Bind(); |
---|
91 | m_shader->SetUniform(m_uni_flag, 0.f); |
---|
92 | m_shader->SetUniform(m_uni_point, m_hotspot); |
---|
93 | m_shader->SetUniform(m_uni_color, m_color); |
---|
94 | m_vdecl->SetStream(m_vbo, m_coord); |
---|
95 | m_vdecl->Bind(); |
---|
96 | m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2); |
---|
97 | m_vdecl->Unbind(); |
---|
98 | m_shader->Unbind(); |
---|
99 | m_fbo->Unbind(); |
---|
100 | |
---|
101 | m_shader->Bind(); |
---|
102 | m_shader->SetUniform(m_uni_flag, 1.f); |
---|
103 | m_shader->SetTexture(m_uni_texture, m_fbo->GetTexture(), 0); |
---|
104 | m_vdecl->SetStream(m_vbo, m_coord); |
---|
105 | m_vdecl->Bind(); |
---|
106 | m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2); |
---|
107 | m_vdecl->Unbind(); |
---|
108 | m_shader->Unbind(); |
---|
109 | } |
---|
110 | |
---|
111 | private: |
---|
112 | Array<vec2> m_vertices; |
---|
113 | Shader *m_shader; |
---|
114 | ShaderAttrib m_coord; |
---|
115 | ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture; |
---|
116 | VertexDeclaration *m_vdecl; |
---|
117 | VertexBuffer *m_vbo; |
---|
118 | FrameBuffer *m_fbo; |
---|
119 | double m_time; |
---|
120 | vec3 m_hotspot, m_color; |
---|
121 | bool m_ready; |
---|
122 | }; |
---|
123 | |
---|
124 | int main(int argc, char **argv) |
---|
125 | { |
---|
126 | Application app("Tutorial 08: Framebuffer Object", ivec2(640, 480), 60.0f); |
---|
127 | |
---|
128 | #if defined _MSC_VER && !defined _XBOX |
---|
129 | _chdir(".."); |
---|
130 | #elif defined _WIN32 && !defined _XBOX |
---|
131 | _chdir("../.."); |
---|
132 | #endif |
---|
133 | |
---|
134 | new FBO(); |
---|
135 | |
---|
136 | app.Run(); |
---|
137 | return EXIT_SUCCESS; |
---|
138 | } |
---|
139 | |
---|