[1455] | 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 | |
---|
| 24 | using namespace std; |
---|
| 25 | |
---|
| 26 | #if defined USE_D3D9 |
---|
| 27 | extern IDirect3DDevice9 *g_d3ddevice; |
---|
| 28 | #elif defined _XBOX |
---|
| 29 | extern D3DDevice *g_d3ddevice; |
---|
| 30 | #endif |
---|
| 31 | |
---|
| 32 | namespace lol |
---|
| 33 | { |
---|
| 34 | |
---|
| 35 | // |
---|
| 36 | // The FrameBufferData class |
---|
| 37 | // ------------------------- |
---|
| 38 | // |
---|
| 39 | |
---|
| 40 | class 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 | |
---|
| 58 | FrameBuffer::FrameBuffer(ivec2 size) |
---|
| 59 | : m_data(new FrameBufferData) |
---|
| 60 | { |
---|
| 61 | m_data->m_size = size; |
---|
| 62 | #if defined USE_D3D9 || defined _XBOX |
---|
[1517] | 63 | /* FIXME: not implemented on Direct3D */ |
---|
| 64 | #elif GL_VERSION_1_1 |
---|
[1455] | 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); |
---|
[1523] | 85 | m_data->m_depth = GL_INVALID_ENUM; |
---|
| 86 | if (depth != GL_INVALID_ENUM) |
---|
[1455] | 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(); |
---|
[1517] | 97 | #else |
---|
| 98 | /* FIXME: not implemented on GL ES, see |
---|
| 99 | * http://stackoverflow.com/q/4041682/111461 */ |
---|
[1455] | 100 | #endif |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | FrameBuffer::~FrameBuffer() |
---|
| 104 | { |
---|
| 105 | #if defined USE_D3D9 || defined _XBOX |
---|
[1517] | 106 | #elif GL_VERSION_1_1 |
---|
[1455] | 107 | glDeleteFramebuffers(1, &m_data->m_fbo); |
---|
| 108 | glDeleteTextures(1, &m_data->m_texture); |
---|
[1523] | 109 | if (m_data->m_depth != GL_INVALID_ENUM) |
---|
[1455] | 110 | glDeleteRenderbuffers(1, &m_data->m_depth); |
---|
[1517] | 111 | #else |
---|
[1455] | 112 | #endif |
---|
| 113 | delete m_data; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | int FrameBuffer::GetTexture() const |
---|
| 117 | { |
---|
| 118 | #if defined USE_D3D9 || defined _XBOX |
---|
[1517] | 119 | return 0; |
---|
| 120 | #elif GL_VERSION_1_1 |
---|
[1455] | 121 | return m_data->m_texture; |
---|
[1517] | 122 | #else |
---|
| 123 | return 0; |
---|
[1455] | 124 | #endif |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | void FrameBuffer::Bind() |
---|
| 128 | { |
---|
| 129 | #if defined USE_D3D9 || defined _XBOX |
---|
[1517] | 130 | #elif GL_VERSION_1_1 |
---|
[1455] | 131 | glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo); |
---|
[1517] | 132 | #else |
---|
[1455] | 133 | #endif |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | void FrameBuffer::Unbind() |
---|
| 137 | { |
---|
| 138 | #if defined USE_D3D9 || defined _XBOX |
---|
[1517] | 139 | #elif GL_VERSION_1_1 |
---|
[1455] | 140 | glBindFramebuffer(GL_FRAMEBUFFER, NULL); |
---|
[1517] | 141 | #else |
---|
[1455] | 142 | #endif |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | } /* namespace lol */ |
---|
| 146 | |
---|