• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Vincent_M

Rendering to Cubemap for Environment Mapping

8 posts in this topic

I've got my 6 cameras at the position of the object I'd like to environment map, but now I'm not sure how to render to a texture for a cubemap using FBOs. I've got rendering to a GL_TEXTURE_2D target working fine, but if I bind my the current frame buffer that's rendering to a Gl_TEXTURE_CUBE_MAP target, how do I set which cubemap target to my rendering camera?
For example, if I have a camera pointing along the positive Z axis, I want to render to the cubemap's GL_TEXTURE_CUBE_MAP_POSITIVE_Z sub-texture each frame. How would I do that?
0

Share this post


Link to post
Share on other sites
[quote name='Vincent_M' timestamp='1315945968' post='4861248']
For example, if I have a camera pointing along the positive Z axis, I want to render to the cubemap's GL_TEXTURE_CUBE_MAP_POSITIVE_Z sub-texture each frame. How would I do that?
[/quote]
Simply by using GL_TEXTURE_CUBE_MAP_POSITIVE_Z as the texture target to a glFramebufferTexture2D call, eg:
[code]
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, texture_id, 0);
[/code]

Alternatively, if you want to render the cube in a single pass, you can attach the entire cubemap using
[code]
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture_id, 0);
[/code]
and select the target face in the geometry shader.
0

Share this post


Link to post
Share on other sites
That last method you mentioned was what I was doing. Can you elaborate on how I'd select the target face in the shader? I'm guessing it's by using a cube map sampler since I'm passing a cube map through.
0

Share this post


Link to post
Share on other sites
Just so that we understand each other: you want to render [i]to[/i] a cubemap, right ? I got a bit confused when you started talking about texture samplers.

The first code snippet I posted uses a six pass technique. You render your scene six times, once for each of your 6 cameras. glFramebufferTexture2D is called to attach a specific cubemap side as the target for each of the rendering passes. A cubemap side attached this way will behave like a normal 2D texture target. You gradually build up your cubemap by cycling through all the GL_TEXTURE_CUBE_MAP_* constants as targets.

The second method is a single pass approach. You bind the entire cubemap as a target, and only perform a single render pass. This method requires a geometry shader that will duplicate each primitive six times, projecting it onto all six cubemap faces simultaneously (through your six view-projection matrices). The geometry shader directs each duplicated primitive to the appropriate cube target face using the [url="http://www.opengl.org/sdk/docs/manglsl/xhtml/gl_Layer.xml"]gl_Layer[/url] variable.
1

Share this post


Link to post
Share on other sites
[quote][color="#1C2837"][size="2"]Just so that we understand each other: you want to render [i]to[/i] a cubemap, right ? I got a bit confused when you started talking about texture samplers.[/size][/color][/quote]

My wording wasn't that clear, but you are correct. There are two reasons I'm rendering to a cubemap: to make a cube depth texture for shadow maps, and an environment map for reflective surfaces and water.




So, after I setup my FBO, I'd do something like this?
[code]
// add the color attachments to the cubemap
for(int i=0;i<6;i++)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+i, GL_TEXTURE_CUBE_MAP_POSITIVE_Z+i, textureHandle[i], 0);
[/code]


Now for rendering to the cubemap in my Scene management class:
[code]

// only render to a texture
void Scene::RenderEnvironmentMap()
{
// cancel early if the cubemap render target is invalid
if(!cubeTarget) return;

// set the cubemap render target's FBO
int oldFBO = GetBoundFBO();
SetBoundFBO(cubeTarget->GetFBO());

// render the scene to each face of the cubemap
for(int i=0;i<6;i++)
{
// set the current camera from the managed cube camera pointers
SetCurrentCamera(cubeCamera[i]);
glBindRenderbuffer(GL_TEXTURE_CUBE_MAP_POSITIVE_Z+i, cubeTarget->GetFaceTextureHandle(i));
RenderObjects(); // render the scene
}

// set the FBO back to the original one
SetBoundFBO(oldFBO);
}
[/code]
0

Share this post


Link to post
Share on other sites
You are mixing up different concepts here (cube map faces, MRTs, render buffers, etc).

It looks like you are indeed rendering the scene 6 times. That would be the first approach I outlined earlier. For this to work, you will need to bind the cube sides [i]one after the other[/i], while rendering the matching camera view:
[source]

// Bind the FBO
glBindFramebuffer(GL_FRAMEBUFFER, fboHandle);

// Attach a depth renderbuffer to the FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbufferHandle);

// Render the six views
for( int i = 0; i < 6; ++i ) {

// Bind the i'th face of the cube texture as target to color attachment 0
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, cubeTextureHandle, 0);

// All following render calls will now end up on the i'th side of the cubemap !

// Setup camera and render scene for the i'th view into the previously bound cubemap face
SetCurrentCamera(cubeCamera[i]);
RenderObjects();

}

// Unbind FBO
glBindFramebuffer(GL_FRAMEBUFFER, 0);

// Cubemap at cubeTextureHandle is now complete.
[/source]

This assumes that your are only interested in a color cubemap texture and it uses a shared depth buffer for the rendering. If you want a depth cubemap generated in the same 6 passes along with the color cubemap, then drop the render buffer and add a second glFramebufferTexture2D call to the loop, attaching the i'th depth cube texture face to GL_DEPTH_ATTACHMENT. Something like:
[code]
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, depthCubeTextureHandle, 0);
[/code]
1

Share this post


Link to post
Share on other sites
That looks like what I'm looking for!

Now, not only does this work for setting up the cubemap, but could I use this to re-render the scene each frame?

If I wanted to update the cubemap, could I call this on render frames:
[code]
glBindFramebuffer(GL_FRAMEBUFFER, fboHandle);
for(int i=0;i<6;i++)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, cubeTextureHandle, 0);
SetCurrentCamera(cubeCamera[i]);
RenderObjects();
}
[/code]

Does the call to glFramebufferTexture2D() work, or does that attempt to add another GL_COLOR_ATTACHMENT0 to the framebuffer?
0

Share this post


Link to post
Share on other sites
It works. No, it doesn't add other attachements. If you want to add others, you use GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, etc. whatever the max is for your GPU.
1

Share this post


Link to post
Share on other sites
When rendering to my cube map texture, I'm getting a black texture.

Here's how I create my cube map and FBO:
[code]
void EnvironmentMap::CreateFBO()
{
// check if the dimensions are greater than supported
int size;
glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &size);
if(width > size || height > size)
return;
DestroyFBO(); // release everything

// allocate the new buffers
int oldFBO = GetCurrentFBO();
glGenTextures(1, &handle);
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);

// setup the texture
SetBoundTexture(GL_TEXTURE_CUBE_MAP, handle);
SetFilter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SetFilter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
SetWrapMode(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
SetWrapMode(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// allocate the texture
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

// set the texture rendering targets
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, handle, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, handle, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, handle, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, handle, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, handle, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, handle, 0);
SetBoundTexture(GL_TEXTURE_CUBE_MAP, -1);

// add a depth attachment
if(useDepth)
{
glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
}

// check framebuffer status
unsigned int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
switch(status)
{
case GL_FRAMEBUFFER_COMPLETE: printf("GL_FRAMEBUFFER_COMPLETE\n"); break;
case 0x8CDB: printf("GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER\n"); break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: printf("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT\n"); break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: printf("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT\n"); break;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: printf("GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS\n"); break;
case GL_FRAMEBUFFER_UNSUPPORTED: printf("GL_FRAMEBUFFER_UNSUPPORTED\n"); break;
default: printf("Unknown issue (%X).\n",status); break;
}

// set clear the FBO to white
ColorRGBA oldColor = GetClearColor();
SetClearColor(ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// restore the previous FBO
SetBoundFBO(oldFBO); // set the old FBO
SetClearColor(oldColor); // set the old clear color
}
[/code]


Here's how I render my scene to a texture each frame:
[code]
// render the environment map
EnvironmentMap *envMap = sceneOBJModel->GetMesh(0)->GetMaterial()->GetEnvironmentMap();
SetViewport(ViewportData(0, 0, 128,128));
SetBoundFBO(envMap->GetFBO());
for(int i=0;i<6;i++)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, envMap->GetHandle(), 0);
if(cubeCameras[i]) scene->SetCurrentCamera(cubeCameras[i]);
if(scene) scene->Render();
}
[/code]

The environment map was also allocated to the dimensions of <128, 128> per face.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0