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

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

gpu: implement all depth test functions in the renderer.

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