source: trunk/tutorial/08_fbo.cpp @ 2183

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

File size: 4.6 KB
Line 
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://www.wtfpl.net/ 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
18using namespace std;
19using namespace lol;
20
21#if defined _WIN32
22#   include <direct.h>
23#endif
24
25extern char const *lolfx_08_fbo;
26
27class FBO : public WorldEntity
28{
29public:
30    FBO()
31    {
32        m_vertices << vec2( 1.0,  1.0);
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_ready = false;
39    }
40
41    virtual void TickGame(float seconds)
42    {
43        WorldEntity::TickGame(seconds);
44
45        m_time += seconds;
46        m_hotspot = 0.4f * vec3(lol::sin(m_time * 4.f) + lol::cos(m_time * 5.3f),
47                                lol::sin(m_time * 5.7f) + lol::cos(m_time * 4.4f),
48                                lol::sin(m_time * 5.f));
49        m_color = 0.25f * vec3(1.1f + lol::sin(m_time * 2.5f + 1.f),
50                               1.1f + lol::sin(m_time * 2.8f + 1.3f),
51                               1.1f + lol::sin(m_time * 2.7f));
52        /* Saturate dot color */
53        float x = std::max(m_color.x, std::max(m_color.y, m_color.z));
54        m_color /= x;
55    }
56
57    virtual void TickDraw(float seconds)
58    {
59        WorldEntity::TickDraw(seconds);
60
61        if (!m_ready)
62        {
63            m_shader = Shader::Create(lolfx_08_fbo);
64            m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
65            m_uni_flag = m_shader->GetUniformLocation("in_Flag");
66            m_uni_point = m_shader->GetUniformLocation("in_Point");
67            m_uni_color = m_shader->GetUniformLocation("in_Color");
68            m_uni_texture = m_shader->GetUniformLocation("in_Texture");
69
70            m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
71
72            m_vbo = new VertexBuffer(m_vertices.Bytes());
73            void *vertices = m_vbo->Lock(0, 0);
74            memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
75            m_vbo->Unlock();
76
77            m_fbo = new FrameBuffer(Video::GetSize());
78            m_fbo->Bind();
79            Video::SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
80            Video::SetClearDepth(1.f);
81            Video::Clear(ClearMask::Color | ClearMask::Depth);
82            m_fbo->Unbind();
83
84            m_ready = true;
85
86            /* FIXME: this object never cleans up */
87        }
88
89        m_fbo->Bind();
90        /* FIXME: we should just disable depth test in the shader */
91        Video::Clear(ClearMask::Depth);
92        m_shader->Bind();
93
94#if _XBOX
95        /* FIXME: the Xbox enforces full EDRAM clears on each frame, so
96         * we cannot expect the render target contents to be preserved.
97         * This code snippet should be moved inside the FrameBuffer class. */
98        m_shader->SetUniform(m_uni_flag, 1.f);
99        m_shader->SetUniform(m_uni_texture, m_fbo->GetTexture(), 0);
100        m_vdecl->SetStream(m_vbo, m_coord);
101        m_vdecl->Bind();
102        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
103        m_vdecl->Unbind();
104#endif
105
106        m_shader->SetUniform(m_uni_flag, 0.f);
107        m_shader->SetUniform(m_uni_point, m_hotspot);
108        m_shader->SetUniform(m_uni_color, m_color);
109        m_vdecl->SetStream(m_vbo, m_coord);
110        m_vdecl->Bind();
111        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
112        m_vdecl->Unbind();
113        m_shader->Unbind();
114        m_fbo->Unbind();
115
116        m_shader->Bind();
117        m_shader->SetUniform(m_uni_flag, 1.f);
118        m_shader->SetUniform(m_uni_texture, m_fbo->GetTexture(), 0);
119        m_vdecl->SetStream(m_vbo, m_coord);
120        m_vdecl->Bind();
121        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
122        m_vdecl->Unbind();
123        m_shader->Unbind();
124    }
125
126private:
127    Array<vec2> m_vertices;
128    Shader *m_shader;
129    ShaderAttrib m_coord;
130    ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
131    VertexDeclaration *m_vdecl;
132    VertexBuffer *m_vbo;
133    FrameBuffer *m_fbo;
134    double m_time;
135    vec3 m_hotspot, m_color;
136    bool m_ready;
137};
138
139int main(int argc, char **argv)
140{
141    Application app("Tutorial 08: Framebuffer Object", ivec2(640, 480), 60.0f);
142
143#if defined _MSC_VER && !defined _XBOX
144    _chdir("..");
145#elif defined _WIN32 && !defined _XBOX
146    _chdir("../..");
147#endif
148
149    new FBO();
150
151    app.Run();
152    return EXIT_SUCCESS;
153}
154
Note: See TracBrowser for help on using the repository browser.