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

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

build: OS X compilation fixes: use GL_INVALID_ENUM instead of GL_INVALID_INDEX
and support automake 1.12 in the bootstrap script.

  • 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#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
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#elif GL_VERSION_1_1
65    GLenum format = GL_RGBA8;
66    GLenum depth = GL_DEPTH_COMPONENT;
67    GLenum wrapmode = GL_REPEAT;
68    GLenum filtering = GL_NEAREST;
69
70    glGenFramebuffers(1, &m_data->m_fbo);
71    glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo);
72
73    glGenTextures(1, &m_data->m_texture);
74    glActiveTexture(GL_TEXTURE0);
75    glBindTexture(GL_TEXTURE_2D, m_data->m_texture);
76    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)wrapmode);
77    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)wrapmode);
78    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)filtering);
79    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)filtering);
80    glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, size.x, size.y, 0,
81                 GL_BGRA, GL_UNSIGNED_BYTE, NULL);
82
83    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
84                           GL_TEXTURE_2D, m_data->m_texture, 0);
85    m_data->m_depth = GL_INVALID_ENUM;
86    if (depth != GL_INVALID_ENUM)
87    {
88        glGenRenderbuffers(1, &m_data->m_depth);
89        glBindRenderbuffer(GL_RENDERBUFFER, m_data->m_depth);
90        glRenderbufferStorage(GL_RENDERBUFFER, depth, size.x, size.y);
91        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
92                                  GL_RENDERBUFFER, m_data->m_depth);
93    }
94    glCheckFramebufferStatus(GL_FRAMEBUFFER);
95
96    Unbind();
97#else
98    /* FIXME: not implemented on GL ES, see
99     * http://stackoverflow.com/q/4041682/111461 */
100#endif
101}
102
103FrameBuffer::~FrameBuffer()
104{
105#if defined USE_D3D9 || defined _XBOX
106#elif GL_VERSION_1_1
107    glDeleteFramebuffers(1, &m_data->m_fbo);
108    glDeleteTextures(1, &m_data->m_texture);
109    if (m_data->m_depth != GL_INVALID_ENUM)
110        glDeleteRenderbuffers(1, &m_data->m_depth);
111#else
112#endif
113    delete m_data;
114}
115
116int FrameBuffer::GetTexture() const
117{
118#if defined USE_D3D9 || defined _XBOX
119    return 0;
120#elif GL_VERSION_1_1
121    return m_data->m_texture;
122#else
123    return 0;
124#endif
125}
126
127void FrameBuffer::Bind()
128{
129#if defined USE_D3D9 || defined _XBOX
130#elif GL_VERSION_1_1
131    glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo);
132#else
133#endif
134}
135
136void FrameBuffer::Unbind()
137{
138#if defined USE_D3D9 || defined _XBOX
139#elif GL_VERSION_1_1
140    glBindFramebuffer(GL_FRAMEBUFFER, NULL);
141#else
142#endif
143}
144
145} /* namespace lol */
146
Note: See TracBrowser for help on using the repository browser.