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

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

tutorial: minor tweaks.

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