source: trunk/src/gpu/framebuffer.cpp @ 1733

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

gpu: activate FBO support on Android.

  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-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 "lolgl.h"
17
18#if defined _WIN32 && defined USE_D3D9
19#   define FAR
20#   define NEAR
21#   include <d3d9.h>
22#endif
23
24using namespace std;
25
26#if defined USE_D3D9
27extern IDirect3DDevice9 *g_d3ddevice;
28#elif defined _XBOX
29extern D3DDevice *g_d3ddevice;
30#endif
31
32namespace lol
33{
34
35//
36// The FrameBufferData class
37// -------------------------
38//
39
40class FrameBufferData
41{
42    friend class FrameBuffer;
43
44    ivec2 m_size;
45
46#if defined USE_D3D9
47#elif defined _XBOX
48#else
49    GLuint m_fbo, m_texture, m_depth;
50#endif
51};
52
53//
54// The FrameBuffer class
55// ----------------------
56//
57
58FrameBuffer::FrameBuffer(ivec2 size)
59  : m_data(new FrameBufferData)
60{
61    m_data->m_size = size;
62#if defined USE_D3D9 || defined _XBOX
63    /* FIXME: not implemented on Direct3D */
64#else
65#   if GL_VERSION_1_1
66    GLenum internal_format = GL_RGBA8;
67    GLenum format = GL_BGRA;
68    GLenum depth = GL_DEPTH_COMPONENT;
69#   else
70    GLenum internal_format = GL_RGBA;
71    GLenum format = GL_RGBA;
72#   endif
73    GLenum wrapmode = GL_REPEAT;
74    GLenum filtering = GL_NEAREST;
75
76    glGenFramebuffers(1, &m_data->m_fbo);
77    glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo);
78
79    glGenTextures(1, &m_data->m_texture);
80    glActiveTexture(GL_TEXTURE0);
81    glBindTexture(GL_TEXTURE_2D, m_data->m_texture);
82    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)wrapmode);
83    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)wrapmode);
84    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)filtering);
85    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)filtering);
86    glTexImage2D(GL_TEXTURE_2D, 0, internal_format, size.x, size.y, 0,
87                 format, GL_UNSIGNED_BYTE, NULL);
88
89    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
90                           GL_TEXTURE_2D, m_data->m_texture, 0);
91    m_data->m_depth = GL_INVALID_ENUM;
92#   if GL_VERSION_1_1
93    /* FIXME: not implemented on GL ES, see
94     * http://stackoverflow.com/q/4041682/111461 */
95    if (depth != GL_INVALID_ENUM)
96    {
97        glGenRenderbuffers(1, &m_data->m_depth);
98        glBindRenderbuffer(GL_RENDERBUFFER, m_data->m_depth);
99        glRenderbufferStorage(GL_RENDERBUFFER, depth, size.x, size.y);
100        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
101                                  GL_RENDERBUFFER, m_data->m_depth);
102    }
103#   endif
104    glCheckFramebufferStatus(GL_FRAMEBUFFER);
105
106    Unbind();
107#endif
108}
109
110FrameBuffer::~FrameBuffer()
111{
112#if defined USE_D3D9 || defined _XBOX
113#else
114    glDeleteFramebuffers(1, &m_data->m_fbo);
115    glDeleteTextures(1, &m_data->m_texture);
116#   if GL_VERSION_1_1
117    if (m_data->m_depth != GL_INVALID_ENUM)
118        glDeleteRenderbuffers(1, &m_data->m_depth);
119#   endif
120#endif
121    delete m_data;
122}
123
124int FrameBuffer::GetTexture() const
125{
126#if defined USE_D3D9 || defined _XBOX
127    return 0;
128#else
129    return m_data->m_texture;
130#endif
131}
132
133void FrameBuffer::Bind()
134{
135#if defined USE_D3D9 || defined _XBOX
136#else
137    glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo);
138#endif
139}
140
141void FrameBuffer::Unbind()
142{
143#if defined USE_D3D9 || defined _XBOX
144#else
145    glBindFramebuffer(GL_FRAMEBUFFER, NULL);
146#endif
147}
148
149} /* namespace lol */
150
Note: See TracBrowser for help on using the repository browser.