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

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

gpu: get rid of the glClearColor, glClearDepth and glClear calls in all
projects, we now use Video::SetClearColor, Video::SetClearDepth and
Video::Clear instead, so that the Direct3D equivalents can be called.

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