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

Last change on this file since 1455 was 1455, checked in by sam, 9 years ago

gpu: first FrameBuffer GPU class.

  • Property svn:keywords set to Id
File size: 3.9 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#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
64    GLenum format = GL_RGBA8;
65    GLenum depth = GL_DEPTH_COMPONENT;
66    GLenum wrapmode = GL_REPEAT;
67    GLenum filtering = GL_NEAREST;
68
69    glGenFramebuffers(1, &m_data->m_fbo);
70    glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo);
71
72    glGenTextures(1, &m_data->m_texture);
73    glActiveTexture(GL_TEXTURE0);
74    glBindTexture(GL_TEXTURE_2D, m_data->m_texture);
75    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)wrapmode);
76    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)wrapmode);
77    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)filtering);
78    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)filtering);
79    glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, size.x, size.y, 0,
80                 GL_BGRA, GL_UNSIGNED_BYTE, NULL);
81
82    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
83                           GL_TEXTURE_2D, m_data->m_texture, 0);
84    m_data->m_depth = GL_INVALID_INDEX;
85    if (depth != GL_INVALID_INDEX)
86    {
87        glGenRenderbuffers(1, &m_data->m_depth);
88        glBindRenderbuffer(GL_RENDERBUFFER, m_data->m_depth);
89        glRenderbufferStorage(GL_RENDERBUFFER, depth, size.x, size.y);
90        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
91                                  GL_RENDERBUFFER, m_data->m_depth);
92    }
93    glCheckFramebufferStatus(GL_FRAMEBUFFER);
94
95    Unbind();
96#endif
97}
98
99FrameBuffer::~FrameBuffer()
100{
101#if defined USE_D3D9 || defined _XBOX
102#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
103    glDeleteFramebuffers(1, &m_data->m_fbo);
104    glDeleteTextures(1, &m_data->m_texture);
105    if (m_data->m_depth != GL_INVALID_INDEX)
106        glDeleteRenderbuffers(1, &m_data->m_depth);
107#endif
108    delete m_data;
109}
110
111int FrameBuffer::GetTexture() const
112{
113#if defined USE_D3D9 || defined _XBOX
114#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
115    return m_data->m_texture;
116#endif
117}
118
119void FrameBuffer::Clear(vec4 color)
120{
121#if defined USE_D3D9 || defined _XBOX
122#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
123    glClearColor(color.r, color.g, color.b, color.a);
124    glClear(GL_COLOR_BUFFER_BIT);
125#endif
126}
127
128void FrameBuffer::Clear(vec4 color, float depth)
129{
130#if defined USE_D3D9 || defined _XBOX
131#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
132    glClearColor(color.r, color.g, color.b, color.a);
133    //glClearDepthf(depth);
134    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
135#endif
136}
137
138void FrameBuffer::Bind()
139{
140#if defined USE_D3D9 || defined _XBOX
141#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
142    glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo);
143#endif
144}
145
146void FrameBuffer::Unbind()
147{
148#if defined USE_D3D9 || defined _XBOX
149#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
150    glBindFramebuffer(GL_FRAMEBUFFER, NULL);
151#endif
152}
153
154} /* namespace lol */
155
Note: See TracBrowser for help on using the repository browser.