I'm making a Cube map render target class.
I already have a 2D one that works fine. But for this cube map render target class I want to be able to render to a specific face as i wish. Maybe I want to render circles on the "X+" face but triangles in the "Y+" face.
This is what my class looks like
class CRenderTarget
{
private:
//frame buffer to render on
unsigned m_uifboHandle;
//depth stencil
unsigned m_uiDepthStencilHandle;
//the texture we will render on
unsigned m_uiTextureHandle;
public:
CCubeRenderTarget( void );
~CCubeRenderTarget( void );
void Create( unsigned uiHeightWidth = 512,
int iInternalFormat = 0x1908 ); //GL_RGBA
void Bind( void );
void UnBind( void );
}
This is how my create function looks like
void Create( unsigned uiHeightWidth, int iInternalFormat )
{
//generate our variables
glGenFramebuffers(1, &m_uifboHandle);
glGenRenderbuffers(1, &m_uiDepthStencilHandle);
glGenTextures(1, &m_uiTextureHandle);
// Initialize FBO
glBindFramebuffer( GL_FRAMEBUFFER, m_uifboHandle);
glBindTexture( GL_TEXTURE_CUBE_MAP, m_uiTextureHandle);
// Pre-allocate the correct sized cube map
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
// Must attach texture to framebuffer. Has Stencil and depth
glBindRenderbuffer(GL_RENDERBUFFER, m_uiDepthStencilHandle);
glRenderbufferStorage(GL_RENDERBUFFER, /*GL_DEPTH_STENCIL*/GL_DEPTH24_STENCIL8, uiHeightWidth, uiHeightWidth );
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_uiDepthStencilHandle);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_uiDepthStencilHandle);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
this is what my bind function looks like
void Bind( void )
{
glBindFramebuffer( GL_FRAMEBUFFER, m_uifboHandle );
}
//this is what my unbind function looks like
void Unbind( void )
{
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
}
I done tons of research for the past week. I can't seem to find a way that i can just some how instead of binding my entire cube map just bind maybe 1 face of my cube map. which is my overall goal. I know that in DirectX you can by just grabbing the 6 faces and manually handling them but how can i manually handle each face on an openGL cube map?
Having bound the framebuffer, you need to specify what level of the texture should be targetted. It should be something like this:
glBindFramebuffer( GL_FRAMEBUFFER, m_uifboHandle );
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, m_uiTextureHandle, 0);
Related
I'm trying to render my scene to my FBO and then render it in a smaller size in corner of the screen. It works fine, but only if the FBO has only Color Texture. When I try to attach Depth Texture or Depth Buffer to it, it draws single pixels of my screen. Here are some screens:
FBO with Color Texture
FBO with Color and Depth Texture
Here's my FBO class code:
FrameBuffer::FrameBuffer()
{
GLcall( glGenFramebuffers(1, &m_id) );
GLcall( glBindFramebuffer(GL_FRAMEBUFFER, m_id) );
GLcall( glDrawBuffer(GL_COLOR_ATTACHMENT0) );
GLcall( glBindFramebuffer(GL_FRAMEBUFFER, 0) );
}
void FrameBuffer::attachTexture(const unsigned int width, const unsigned int height)
{
GLcall( glBindFramebuffer(GL_FRAMEBUFFER, m_id) );
m_textureAttachmentsIDs.push_back(0);
GLcall( glGenTextures(1, &m_textureAttachmentsIDs[ m_textureAttachmentsIDs.size() - 1 ]) );
GLcall( glBindTexture(GL_TEXTURE_2D, m_textureAttachmentsIDs[ m_textureAttachmentsIDs.size() - 1 ]) );
GLcall( glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr) );
GLcall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) );
GLcall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) );
GLcall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) );
GLcall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) );
GLcall( glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_textureAttachmentsIDs[ m_textureAttachmentsIDs.size() - 1 ], 0) );
GLcall( glBindTexture(GL_TEXTURE_2D, 0) );
GLcall( glBindFramebuffer(GL_FRAMEBUFFER, 0) );
}
void FrameBuffer::attachDepthTexture(const unsigned int width, const unsigned int height)
{
GLcall( glBindFramebuffer(GL_FRAMEBUFFER, m_id) );
GLcall( glGenTextures(1, &m_depthTextureAttachmentID) );
GLcall( glBindTexture(GL_TEXTURE_2D, m_depthTextureAttachmentID) );
GLcall( glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr) );
GLcall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) );
GLcall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) );
GLcall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) );
GLcall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) );
GLcall( glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_ALPHA) );
GLcall( glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTextureAttachmentID, 0) );
GLcall( glBindTexture(GL_TEXTURE_2D, 0) );
GLcall( glBindFramebuffer(GL_FRAMEBUFFER, 0) );
}
void FrameBuffer::attachDepthBuffer(const unsigned int width, const unsigned int height)
{
GLcall( glBindFramebuffer(GL_FRAMEBUFFER, m_id) );
GLcall( glGenRenderbuffers(1, &m_depthBufferAttachmentID) );
GLcall( glBindRenderbuffer(GL_RENDERBUFFER, m_depthBufferAttachmentID) );
GLcall( glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height) );
GLcall( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthBufferAttachmentID) );
GLcall( glBindRenderbuffer(GL_RENDERBUFFER, 0) );
GLcall( glBindFramebuffer(GL_FRAMEBUFFER, 0) );
}
void FrameBuffer::bind(const unsigned int width, const unsigned int height) const
{
GLcall( glBindFramebuffer(GL_FRAMEBUFFER, m_id) );
GLcall( glViewport(0, 0, width, height) );
}
void FrameBuffer::unbind(const Window& window) const
{
GLcall( glBindFramebuffer(GL_FRAMEBUFFER, 0) );
GLcall( glViewport(0, 0, window.getWidth(), window.getHeight()) );
}
Here is code that sets up my FBO and Sprite to render a texture to:
FrameBuffer testFBO;
testFBO.attachTexture(320, 180);
testFBO.attachDepthTexture(320, 180);
Layer2D layer(window);
Sprite2D screenSprite(vector2(400.0f, 600.0f), vector2(640.0f, 360.0f));
layer.addSprite(&screenSprite);
Texture screenTexture(testFBO.getTextureAttachments()[0], 320, 180);
And here's the code which handles rendering:
testFBO.bind(320, 180);
rr.render(cam);
testFBO.unbind(window);
screenSprite.setTexture(&screenTexture);
rr.render(cam);
layer.render();
I'm not posting rendering code, because it renders everything fine if I don't attach Depth Texture or Depth Buffer.
And yes, I am clearing depth buffer:
GLcall( glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) );
GLcall() function checks for any OpenGL errors and there aren't any when I'm executing this code.
Thanks in advance.
The fix was to clear FBO's Depth Buffer.
testFBO.bind(320, 180);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
rr.render(cam);
testFBO.unbind(window);
I only cleared Depth Buffer of default FBO.
I started learning OpenGL recently and I've been messing with texture. I updated my texture using glTexImage2D but I've learned that it's better to use glTexSubImage2D, so I tried to change my code but i doesn't work.
Working code
void GLWidget::updateTextures(){
QImage t = img.mirrored();
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits());
glBindTexture( GL_TEXTURE_2D, 0);
}
Not working code
void GLWidget::updateTextures(){
QImage t = img.mirrored();
glBindTexture(GL_TEXTURE_2D, tex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, t.width(), t.height(), GL_RGBA, GL_UNSIGNED_BYTE, t.bits());
glBindTexture( GL_TEXTURE_2D, 0);
}
All I have is a black screen.
Thanks.
EDIT :
Here is the initialization of my texture :
void GLWidget::initializeGL(){
...
LoadGLTextures();
...
}
void GLWidget::LoadGLTextures(){
QImage t = img.mirrored();
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture( GL_TEXTURE_2D, 0);
}
img is a QImage variable containing the pixel data.
glGetError() Returns code 1281.
glTexSubImage2D updates the content of a previously allocated texture. glTexImage2D has to be called at least once to trigger the allocation:
void GLWidget::initializeGL(){
//...
QImage t = img.mirrored();
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(
GL_TEXTURE_2D,
0,
3,
t.width(),
t.height(),
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
t.bits()
);
glBindTexture( GL_TEXTURE_2D, 0);
// ...
}
Update with glTexSubImage2D:
void GLWidget::updateTextures(){
QImage t = img.mirrored();
glBindTexture(GL_TEXTURE_2D, tex);
glTexSubImage2D(
GL_TEXTURE_2D,
0,
0,
0,
t.width(),
t.height(),
GL_RGBA,
GL_UNSIGNED_BYTE,
t.bits()
);
glBindTexture( GL_TEXTURE_2D, 0);
}
EDIT: the problem was glTexImage2D and glTexSubImage2D were called with different image sizes, generating the error GL_INVALID_VALUE (1281, 0x501) on glTexSubImage2D call.
I want to copy the opengl main framebuffer to a fbo, which attach two texture object. then I want transfer the color buffer and depth buffer to the two texture object. I use glbiltframebuffer,but the texure is black, what is right way? my code :
// Create the FBO
glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
// Create textures
glGenTextures(1, &m_colorTexture);
glGenTextures(1, &m_depthTexture);
// color texture
glBindTexture(GL_TEXTURE_2D, m_colorTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Width, Height, 0, GL_RGB, GL_FLOAT, NULL);
glFramebufferTexture2(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_colorTexture, 0);
// depth texture
glBindTexture(GL_TEXTURE_2D, m_depthTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, Width, Height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0);
GLenum Status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (Status != GL_FRAMEBUFFER_COMPLETE) {
printf("error, status: 0x%x\n", Status);
return false;
}
// restore default FBO
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
//copy the main framebuffer to FBO
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
glBlitFramebuffer(0, 0, Width, Height, 0, 0, Width, Height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
GLenum error = glGetError();
`
I solved it, the code is right. Error occurred in other place, I am sorry.
But I has been nagged by the bilt speed. I copy the main framebuffer to the fbo every frame. but it seems that the frame rate down a lot. can every one give some suggestion.
I'm working on opengl 2.1 and opengl es 2.0. I want to implement Depth-of-field effect for whole scene. I know how to do this for single model with shader, but this would require all models to have shader with same code. Is there a way to retrieve final scene depth and color buffer and store them into textures so I could use them again with DOF shader? Or is it a bad idea?
[EDIT]
Finally got it working.
The initialization code looks like this:
glGenFramebuffers(1, &frameBuffer_);
glGenTextures(1, &colorBuffer_);
glGenTextures(1, &depthBuffer_);
//glGenRenderbuffers(1, &depthBuffer_);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer_);
glBindTexture(GL_TEXTURE_2D, colorBuffer_);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA,
width,
height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, colorBuffer_, 0);
glBindTexture(GL_TEXTURE_2D, depthBuffer_);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_DEPTH24_STENCIL8,
width,
height,
0,
GL_DEPTH_STENCIL,
GL_UNSIGNED_INT_24_8,
NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(
GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT,
GL_TEXTURE_2D, depthBuffer_, 0);
//glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer_);
//glRenderbufferStorage(
// GL_RENDERBUFFER,
// GL_DEPTH_COMPONENT24,
// width,
// height);
//glFramebufferRenderbuffer(
// GL_FRAMEBUFFER,
// GL_DEPTH_ATTACHMENT,
// GL_RENDERBUFFER, depthBuffer_);
GLenum status;
status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
switch(status) {
case GL_FRAMEBUFFER_COMPLETE:
// Success.
break;
case GL_FRAMEBUFFER_UNSUPPORTED:
LOGE("Frame buffer format not supported.");
break;
default:
LOGE("Framebuffer Error.");
}
And later when rendering first call:
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer_);
Render scene, then call:
glBindFramebuffer(GL_FRAMEBUFFER, 0);
And now anything can be rendered here using depthBuffer_ or colorBuffer_ as OpengGL texture handles.
Use a framebuffer object. They're part of OpenGL-ES-2 and are available as (well supported) extension to OpenGL-2.1.
See
https://github.com/datenwolf/codesamples/tree/master/samples/OpenGL/minimalfbo
for a minimal working example using extensions on desktop OpenGL-2.x
I'm trying to use FBO to render to texture and then display the texture, but all I get is a black rectangle. My code is:
Initialization:
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <assert.h>
// ...
GLuint fbo_, rbo_, tex_;
glGenFramebuffers(1, &fbo_);
glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
glGenRenderbuffers(1, &rbo_);
glBindRenderbuffer(GL_RENDERBUFFER, rbo_);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width_, height_);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo_);
glGenTextures(1, &tex_);
glBindTexture(GL_TEXTURE_2D, tex_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width_, height_, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glGenerateMipmap(GL_TEXTURE_2D);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex_, 0);
assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
Render to the FBO:
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, width_, height_);
glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
//draw stuff
glPopAttrib();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
Use the texture (the problem is probably in the code above, because if I just use some static texture, the code below works):
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex_);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(x_, y_, 0);
glTexCoord2f(width_, 0);
glVertex3f(x_ + width_, y_, 0);
glTexCoord2f(width_, height_);
glVertex3f(x_ + width_, y_ + height_, 0);
glTexCoord2f(0, height_);
glVertex3f(x_, y_ + height_, 0);
glEnd();
Could you help me spot the problem?
Make sure the texture you attach is complete if you're going to rely on the default texture environment.
I had the same problem and used genpfault's advice to create a complete texture. Unfortunately, I do not have enough reputation to add a comment so I just added this answer here. Still, the code below may save you some time:
// generate full viewport texture
glGenTextures(1, &fb_tex);
glBindTexture(GL_TEXTURE_2D, fb_tex);
// IMPORTANT: texture must be COMPLETE (mipmaps must be specified..
// or the following parameters can be used if there are none)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA,
width,
height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
NULL
);
The only thing I changed in my original code was two add the two lines with glTexParameteri. The parameters can be changed depending on the amount of mipmaps you want/have.