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

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

core: switch the Win32/x64 binaries to the Windows subsystem so that they
do not open a console at startup.

File size: 2.5 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://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
18using namespace std;
19using namespace lol;
20
21#if USE_SDL
22#   include <SDL_main.h>
23#endif
24
25#if defined _WIN32
26#   include <direct.h>
27#endif
28
29extern char const *lolfx_08_fbo;
30
31class FBO : public WorldEntity
32{
33public:
34    FBO()
35    {
36        m_vertices << vec2( 0.0,  0.8);
37        m_vertices << vec2(-0.8, -0.8);
38        m_vertices << vec2( 0.8, -0.8);
39        m_ready = false;
40    }
41
42    virtual void TickDraw(float seconds)
43    {
44        WorldEntity::TickDraw(seconds);
45
46        if (!m_ready)
47        {
48            m_shader = Shader::Create(lolfx_08_fbo);
49            m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
50
51            m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
52
53            m_vbo = new VertexBuffer(m_vertices.Bytes());
54            void *vertices = m_vbo->Lock(0, 0);
55            memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
56            m_vbo->Unlock();
57
58            m_fbo = new FrameBuffer(Video::GetSize());
59
60            m_ready = true;
61
62            /* FIXME: this object never cleans up */
63        }
64
65        m_fbo->Bind();
66        m_shader->Bind();
67        m_vdecl->SetStream(m_vbo, m_coord);
68        m_vdecl->Bind();
69        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1);
70        m_vdecl->Unbind();
71        m_shader->Unbind();
72        m_fbo->Unbind();
73
74        m_shader->Bind();
75        m_vdecl->SetStream(m_vbo, m_coord);
76        m_vdecl->Bind();
77        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1);
78        m_vdecl->Unbind();
79        m_shader->Unbind();
80    }
81
82private:
83    Array<vec2> m_vertices;
84    Shader *m_shader;
85    ShaderAttrib m_coord;
86    VertexDeclaration *m_vdecl;
87    VertexBuffer *m_vbo;
88    FrameBuffer *m_fbo;
89    bool m_ready;
90};
91
92int main(int argc, char **argv)
93{
94    Application app("Tutorial 08: Framebuffer Object", ivec2(640, 480), 60.0f);
95
96#if defined _MSC_VER && !defined _XBOX
97    _chdir("..");
98#elif defined _WIN32 && !defined _XBOX
99    _chdir("../..");
100#endif
101
102    new DebugFps(5, 5);
103    new FBO();
104
105    app.Run();
106    return EXIT_SUCCESS;
107}
108
Note: See TracBrowser for help on using the repository browser.