Browse Source

gpu: first FrameBuffer GPU class.

legacy
Sam Hocevar sam 12 years ago
parent
commit
e2295392b8
5 changed files with 202 additions and 1 deletions
  1. +1
    -0
      src/Makefile.am
  2. +1
    -0
      src/core.h
  3. +155
    -0
      src/gpu/framebuffer.cpp
  4. +44
    -0
      src/gpu/framebuffer.h
  5. +1
    -1
      src/gpu/indexbuffer.cpp

+ 1
- 0
src/Makefile.am View File

@@ -35,6 +35,7 @@ liblol_a_SOURCES = \
gpu/shader.cpp gpu/shader.h \
gpu/indexbuffer.cpp gpu/indexbuffer.h \
gpu/vertexbuffer.cpp gpu/vertexbuffer.h \
gpu/framebuffer.cpp gpu/framebuffer.h \
\
image/image.cpp image/image.h image/image-private.h \
image/codec/gdiplus-image.cpp \


+ 1
- 0
src/core.h View File

@@ -109,6 +109,7 @@ static inline int isnan(float f)
#include "gpu/shader.h"
#include "gpu/indexbuffer.h"
#include "gpu/vertexbuffer.h"
#include "gpu/framebuffer.h"
#include "image/image.h"
#include "application/application.h"



+ 155
- 0
src/gpu/framebuffer.cpp View File

@@ -0,0 +1,155 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

#if defined HAVE_CONFIG_H
# include "config.h"
#endif

#include "core.h"
#include "lolgl.h"

#if defined _WIN32 && defined USE_D3D9
# define FAR
# define NEAR
# include <d3d9.h>
#endif

using namespace std;

#if defined USE_D3D9
extern IDirect3DDevice9 *g_d3ddevice;
#elif defined _XBOX
extern D3DDevice *g_d3ddevice;
#endif

namespace lol
{

//
// The FrameBufferData class
// -------------------------
//

class FrameBufferData
{
friend class FrameBuffer;

ivec2 m_size;

#if defined USE_D3D9
#elif defined _XBOX
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
GLuint m_fbo, m_texture, m_depth;
#endif
};

//
// The FrameBuffer class
// ----------------------
//

FrameBuffer::FrameBuffer(ivec2 size)
: m_data(new FrameBufferData)
{
m_data->m_size = size;
#if defined USE_D3D9 || defined _XBOX
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
GLenum format = GL_RGBA8;
GLenum depth = GL_DEPTH_COMPONENT;
GLenum wrapmode = GL_REPEAT;
GLenum filtering = GL_NEAREST;

glGenFramebuffers(1, &m_data->m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo);

glGenTextures(1, &m_data->m_texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_data->m_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)wrapmode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)wrapmode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)filtering);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)filtering);
glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, size.x, size.y, 0,
GL_BGRA, GL_UNSIGNED_BYTE, NULL);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, m_data->m_texture, 0);
m_data->m_depth = GL_INVALID_INDEX;
if (depth != GL_INVALID_INDEX)
{
glGenRenderbuffers(1, &m_data->m_depth);
glBindRenderbuffer(GL_RENDERBUFFER, m_data->m_depth);
glRenderbufferStorage(GL_RENDERBUFFER, depth, size.x, size.y);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER, m_data->m_depth);
}
glCheckFramebufferStatus(GL_FRAMEBUFFER);

Unbind();
#endif
}

FrameBuffer::~FrameBuffer()
{
#if defined USE_D3D9 || defined _XBOX
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
glDeleteFramebuffers(1, &m_data->m_fbo);
glDeleteTextures(1, &m_data->m_texture);
if (m_data->m_depth != GL_INVALID_INDEX)
glDeleteRenderbuffers(1, &m_data->m_depth);
#endif
delete m_data;
}

int FrameBuffer::GetTexture() const
{
#if defined USE_D3D9 || defined _XBOX
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
return m_data->m_texture;
#endif
}

void FrameBuffer::Clear(vec4 color)
{
#if defined USE_D3D9 || defined _XBOX
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
glClearColor(color.r, color.g, color.b, color.a);
glClear(GL_COLOR_BUFFER_BIT);
#endif
}

void FrameBuffer::Clear(vec4 color, float depth)
{
#if defined USE_D3D9 || defined _XBOX
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
glClearColor(color.r, color.g, color.b, color.a);
//glClearDepthf(depth);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#endif
}

void FrameBuffer::Bind()
{
#if defined USE_D3D9 || defined _XBOX
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
glBindFramebuffer(GL_FRAMEBUFFER, m_data->m_fbo);
#endif
}

void FrameBuffer::Unbind()
{
#if defined USE_D3D9 || defined _XBOX
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
glBindFramebuffer(GL_FRAMEBUFFER, NULL);
#endif
}

} /* namespace lol */


+ 44
- 0
src/gpu/framebuffer.h View File

@@ -0,0 +1,44 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

//
// The FrameBuffer class
// ---------------------
//

#if !defined __LOL_FRAMEBUFFER_H__
#define __LOL_FRAMEBUFFER_H__

#include <cstring>

namespace lol
{

class FrameBuffer
{
public:
FrameBuffer(ivec2 size);
~FrameBuffer();

int GetTexture() const;
void Clear(vec4 color);
void Clear(vec4 color, float depth);

void Bind();
void Unbind();

private:
class FrameBufferData *m_data;
};

} /* namespace lol */

#endif // __LOL_FRAMEBUFFER_H__


+ 1
- 1
src/gpu/indexbuffer.cpp View File

@@ -34,7 +34,7 @@ namespace lol

//
// The IndexBufferData class
// --------------------------
// -------------------------
//

class IndexBufferData


Loading…
Cancel
Save