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

Last change on this file since 2241 was 2241, checked in by sam, 8 years ago

core: pass the project directory to the binary build and get rid of
that 2-year old "temporary Win32 hack".

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