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

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

build: hide the SDL_main.h inclusion in core.h, and only activate it
on Windows if using Visual Studio; if using MinGW, try to stick to the
original main() entry point.

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