Wednesday, June 15, 2011

glPixelStorei example c c++ java objc

Name

glPixelStorei — set pixel storage modes

C Specification

void glPixelStorei(GLenum pname,
GLint param);

Parameters

pname
Specifies the symbolic name of the parameter to be set. GL_PACK_ALIGNMENT affects the packing of pixel data into memory. GL_UNPACK_ALIGNMENTaffects the unpacking of pixel data from memory.
param
Specifies the value that pname is set to.

Description

glPixelStorei sets pixel storage modes that affect the operation of subsequent glReadPixels as well as the unpacking of glTexImage2D, andglTexSubImage2D.
pname is a symbolic constant indicating the parameter to be set, and param is the new value. The following storage parameter affects how pixel data is returned to client memory. This value is significant for glReadPixels:

GL_PACK_ALIGNMENT
Specifies the alignment requirements for the start of each pixel row in memory. The allowable values are 1 (byte-alignment), 2 (rows aligned to even-numbered bytes), 4 (word-alignment), and 8 (rows start on double-word boundaries). The initial value is 4.
The following storage parameter affects how pixel data is read from client memory. This value is significant for glTexImage2D and glTexSubImage2D:

GL_UNPACK_ALIGNMENT
Specifies the alignment requirements for the start of each pixel row in memory. The allowable values are 1 (byte-alignment), 2 (rows aligned to even-numbered bytes), 4 (word-alignment), and 8 (rows start on double-word boundaries). The initial value is 4.

Notes

Pixel storage modes are client states.
glCompressedTexImage2D and glCompressedTexSubImage2D are not affected by glPixelStorei.

Errors

GL_INVALID_ENUM is generated if pname is not an accepted value.
GL_INVALID_VALUE is generated if alignment is specified as other than 1, 2, 4, or 8.

Associated Gets

glGet with argument GL_PACK_ALIGNMENT or GL_UNPACK_ALIGNMENT

Copyright

Copyright © 2003-2004 Silicon Graphics, Inc. This document is licensed under the SGI Free Software B License. For details, seehttp://oss.sgi.com/projects/FreeB/.



Example

void DrawIt()
{
unsigned char *pBuffer = new unsigned char[300*300*4];

// Drawing OpenGL Geometry.
//
glClear(GL_COLOR_BUFFER_BIT);

//glPushMatrix();

glScalef(0.5f, 0.5f, 0.5f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 1.0f, -1.0f);
glEnd();

//glPopMatrix();

SwapBuffers(g_hDC);

// Read pixels from color buffer.
//
memset(pBuffer, 0, 300*300*4);
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

// Choose where to read from.
//
glReadBuffer(GL_FRONT);
glReadPixels((GLdouble)300, (GLdouble)300, 30, 30, GL_RGBA, GL_UNSIGNED_BYTE, pBuffer);
delete[] pBuffer;

return;
}

glReadBuffer example c c++ java objc

Name

glReadBuffer — select a color buffer source for pixels

C Specification

void glReadBuffer(GLenum  mode);

Parameters

mode
Specifies a color buffer. Accepted values are GL_FRONT_LEFTGL_FRONT_RIGHTGL_BACK_LEFTGL_BACK_RIGHTGL_FRONTGL_BACKGL_LEFTGL_RIGHT, andGL_AUXi, where i is between 0 and the value of GL_AUX_BUFFERS minus 1.

Description

glReadBuffer specifies a color buffer as the source for subsequent glReadPixelsglCopyTexImage1DglCopyTexImage2DglCopyTexSubImage1D,glCopyTexSubImage2DglCopyTexSubImage3D, and glCopyPixels commands. mode accepts one of twelve or more predefined values. (GL_AUX0 throughGL_AUX3 are always defined.) In a fully configured system, GL_FRONTGL_LEFT, and GL_FRONT_LEFT all name the front left buffer, GL_FRONT_RIGHT and GL_RIGHTname the front right buffer, and GL_BACK_LEFT and GL_BACK name the back left buffer.
Nonstereo double-buffered configurations have only a front left and a back left buffer. Single-buffered configurations have a front left and a front right buffer if stereo, and only a front left buffer if nonstereo. It is an error to specify a nonexistent buffer to glReadBuffer.
mode is initially GL_FRONT in single-buffered configurations and GL_BACK in double-buffered configurations.

Errors

GL_INVALID_ENUM is generated if mode is not one of the twelve (or more) accepted values.
GL_INVALID_OPERATION is generated if mode specifies a buffer that does not exist.
GL_INVALID_OPERATION is generated if glReadBuffer is executed between the execution of glBegin and the corresponding execution of glEnd.

Associated Gets

glGet with argument GL_READ_BUFFER

Copyright

Copyright © 1991-2006 Silicon Graphics, Inc. This document is licensed under the SGI Free Software B License. For details, seehttp://oss.sgi.com/projects/FreeB/.

Example




void DrawIt()
{
unsigned char *pBuffer = new unsigned char[300*300*4];

// Drawing OpenGL Geometry.
//
glClear(GL_COLOR_BUFFER_BIT);

//glPushMatrix();

glScalef(0.5f, 0.5f, 0.5f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 1.0f, -1.0f);
glEnd();

//glPopMatrix();

SwapBuffers(g_hDC);

// Read pixels from color buffer.
//
memset(pBuffer, 0, 300*300*4);
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

// Choose where to read from.
//
glReadBuffer(GL_FRONT);
glReadPixels((GLdouble)300, (GLdouble)300, 30, 30, GL_RGBA, GL_UNSIGNED_BYTE, pBuffer);
delete[] pBuffer;

return;
}

Tuesday, June 14, 2011

glBindRenderbuffer example c c++ java objc

Name

glBindRenderbuffer — bind a renderbuffer to a renderbuffer target

C Specification

void glBindRenderbuffer(GLenum  target,
GLuint  renderbuffer);

Parameters


target
Specifies the renderbuffer target of the binding operation. target must be GL_RENDERBUFFER.
renderbuffer
Specifies the name of the renderbuffer object to bind.

Description

glBindRenderbuffer binds the renderbuffer object with name renderbuffer to the renderbuffer target specified by targettarget must be GL_RENDERBUFFER.renderbuffer is the name of a renderbuffer object previously returned from a call to glGenRenderbuffers, or zero to break the existing binding of a renderbuffer object to target.

Errors

GL_INVALID_ENUM is generated if target is not GL_RENDERBUFFER.
GL_INVALID_OPERATION is generated if renderbuffer is not zero or the name of a renderbuffer previously returned from a call to glGenRenderbuffers.

Copyright

Copyright © 2010 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999.http://opencontent.org/openpub/.

Example of glBindRenderbuffer

// This function does any needed initialization on the rendering
// context. 
void SetupRC()
{
    fprintf(stdout, "FBO Draw Buffers Demo\n\n");

    // Make sure required functionality is available: cube maps, auto mip gen, etc.
    if (!GLEE_VERSION_1_4)
    {
        fprintf(stderr, "OpenGL 1.4 is not available!\n");
        Sleep(2000);
        exit(0);
    }

    if (GLEE_ARB_texture_non_power_of_two)
    {
        npotTexturesAvailable = GL_TRUE;
    }
    else
    {
        fprintf(stderr, "GL_ARB_texture_non_power_of_two extension is not available!\n");
        fprintf(stderr, "Framebuffer effects will be lower resolution (lower quality).\n\n");
    }

    if (!GLEE_EXT_framebuffer_object)
    {
        fprintf(stderr, "GL_EXT_framebuffer_object extension is unavailable!\n");
        Sleep(2000);
        exit(0);
    }

    // we'll use up to 4 render targets if they're available
    glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
    glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &maxColorAttachments);
    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTexUnits);
    maxDrawBuffers = (maxDrawBuffers > maxColorAttachments) ? maxColorAttachments : maxDrawBuffers;
    maxDrawBuffers = (maxDrawBuffers > (maxTexUnits-1)) ? (maxTexUnits-1) : maxDrawBuffers;
    maxDrawBuffers = (maxDrawBuffers > 4) ? 4 : maxDrawBuffers;
    if (((!GLEE_ARB_draw_buffers || !GLEE_ARB_fragment_shader || !GLEE_ARB_shader_objects) 
        && !GLEE_VERSION_2_0) || (maxDrawBuffers != 4))
    {
        fprintf(stderr, "Support for at least 4 draw buffers is unavailable!\n");
        Sleep(2000);
        exit(0);
    }

    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
    glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE_EXT, &maxRenderbufferSize);
    maxTexSize = (maxRenderbufferSize > maxTexSize) ? maxTexSize : maxRenderbufferSize;

    fprintf(stdout, "Controls:\n");
    fprintf(stdout, "\tRight-click for menu\n\n");
    fprintf(stdout, "\tx/X\t\tMove +/- in x direction\n");
    fprintf(stdout, "\ty/Y\t\tMove +/- in y direction\n");
    fprintf(stdout, "\tz/Z\t\tMove +/- in z direction\n\n");
    fprintf(stdout, "\td/D\t\tToggle use of draw buffers\n\n");
    fprintf(stdout, "\tq\t\tExit demo\n\n");
    
    // Black background
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

    // Hidden surface removal
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    // Set up some lighting state that never changes
    glShadeModel(GL_SMOOTH);
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight);
    glMateriali(GL_FRONT, GL_SHININESS, 128);
    glEnable(GL_LIGHTING);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_NORMALIZE);
    glEnable(GL_LIGHT0);

    // Set up textures & shaders
    SetupTextures();
    SetupShaders();

    // Set up some renderbuffer state
    glGenRenderbuffersEXT(1, &renderbufferID);
    // glBindRenderbuffer(GL_RENDERBUFFER, renderbufferID);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbufferID);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT32, fboWidth, fboHeight);

    glGenFramebuffersEXT(2, framebufferID);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[0]);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderbufferID);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, renderTextureID[0], 0);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[1]);
    for (int i = 0; i < maxDrawBuffers; i++)
    {
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + i, GL_TEXTURE_2D, renderTextureID[i+1], 0);
    }
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

glRenderbufferStorage example c c++ java objc

Name

glRenderbufferStorage — establish data storage, format and dimensions of a renderbuffer object's image

C Specification

void glRenderbufferStorage(GLenum  target,
GLenum  internalformat,
GLsizei  width,
GLsizei  height);

Parameters

target
Specifies a binding to which the target of the allocation and must be GL_RENDERBUFFER.
internalformat
Specifies the internal format to use for the renderbuffer object's image.
width
Specifies the width of the renderbuffer, in pixels.
height
Specifies the height of the renderbuffer, in pixels.

Description

glRenderbufferStorage is equivalent to calling glRenderbufferStorageMultisample with the samples set to zero.
The target of the operation, specified by target must be GL_RENDERBUFFERinternalformat specifies the internal format to be used for the renderbuffer object's storage and must be a color-renderable, depth-renderable, or stencil-renderable format. width and height are the dimensions, in pixels, of the renderbuffer. Both width and height must be less than or equal to the value of GL_MAX_RENDERBUFFER_SIZE.
Upon success, glRenderbufferStorage deletes any existing data store for the renderbuffer image and the contents of the data store after calling glRenderbufferStorage are undefined.

Errors

GL_INVALID_ENUM is generated if target is not GL_RENDERBUFFER.
GL_INVALID_VALUE is generated if either of width or height is negative, or greater than the value of GL_MAX_RENDERBUFFER_SIZE.
GL_INVALID_ENUM is generated if internalformat is not a color-renderable, depth-renderable, or stencil-renderable format.
GL_OUT_OF_MEMORY is generated if the GL is unable to create a data store of the requested size.

Copyright

Copyright © 2010 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. http://opencontent.org/openpub/.

Example of glRenderbufferStorage

// This function does any needed initialization on the rendering
// context. 
void SetupRC()
{
    fprintf(stdout, "FBO Draw Buffers Demo\n\n");

    // Make sure required functionality is available: cube maps, auto mip gen, etc.
    if (!GLEE_VERSION_1_4)
    {
        fprintf(stderr, "OpenGL 1.4 is not available!\n");
        Sleep(2000);
        exit(0);
    }

    if (GLEE_ARB_texture_non_power_of_two)
    {
        npotTexturesAvailable = GL_TRUE;
    }
    else
    {
        fprintf(stderr, "GL_ARB_texture_non_power_of_two extension is not available!\n");
        fprintf(stderr, "Framebuffer effects will be lower resolution (lower quality).\n\n");
    }

    if (!GLEE_EXT_framebuffer_object)
    {
        fprintf(stderr, "GL_EXT_framebuffer_object extension is unavailable!\n");
        Sleep(2000);
        exit(0);
    }

    // we'll use up to 4 render targets if they're available
    glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
    glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &maxColorAttachments);
    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTexUnits);
    maxDrawBuffers = (maxDrawBuffers > maxColorAttachments) ? maxColorAttachments : maxDrawBuffers;
    maxDrawBuffers = (maxDrawBuffers > (maxTexUnits-1)) ? (maxTexUnits-1) : maxDrawBuffers;
    maxDrawBuffers = (maxDrawBuffers > 4) ? 4 : maxDrawBuffers;
    if (((!GLEE_ARB_draw_buffers || !GLEE_ARB_fragment_shader || !GLEE_ARB_shader_objects) 
        && !GLEE_VERSION_2_0) || (maxDrawBuffers != 4))
    {
        fprintf(stderr, "Support for at least 4 draw buffers is unavailable!\n");
        Sleep(2000);
        exit(0);
    }

    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
    glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE_EXT, &maxRenderbufferSize);
    maxTexSize = (maxRenderbufferSize > maxTexSize) ? maxTexSize : maxRenderbufferSize;

    fprintf(stdout, "Controls:\n");
    fprintf(stdout, "\tRight-click for menu\n\n");
    fprintf(stdout, "\tx/X\t\tMove +/- in x direction\n");
    fprintf(stdout, "\ty/Y\t\tMove +/- in y direction\n");
    fprintf(stdout, "\tz/Z\t\tMove +/- in z direction\n\n");
    fprintf(stdout, "\td/D\t\tToggle use of draw buffers\n\n");
    fprintf(stdout, "\tq\t\tExit demo\n\n");
    
    // Black background
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

    // Hidden surface removal
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    // Set up some lighting state that never changes
    glShadeModel(GL_SMOOTH);
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight);
    glMateriali(GL_FRONT, GL_SHININESS, 128);
    glEnable(GL_LIGHTING);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_NORMALIZE);
    glEnable(GL_LIGHT0);

    // Set up textures & shaders
    SetupTextures();
    SetupShaders();

    // Set up some renderbuffer state
    glGenRenderbuffersEXT(1, &renderbufferID);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbufferID);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT32, fboWidth, fboHeight);

    glGenFramebuffersEXT(2, framebufferID);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[0]);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderbufferID);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, renderTextureID[0], 0);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[1]);
    for (int i = 0; i < maxDrawBuffers; i++)
    {
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + i, GL_TEXTURE_2D, renderTextureID[i+1], 0);
    }
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

glFramebufferTexture2D example c c++ java objc

Name

glFramebufferTexture2D — attach a level of a texture object as a logical buffer to the currently bound framebuffer object

C Specification

void glFramebufferTexture(GLenum target,
GLenum attachment,
GLuint texture,
GLint level);
void glFramebufferTexture1D(GLenum target,
GLenum attachment,
GLenum textarget,
GLuint texture,
GLint level);
void glFramebufferTexture2D(GLenum target,
GLenum attachment,
GLenum textarget,
GLuint texture,
GLint level);
void glFramebufferTexture3D(GLenum target,
GLenum attachment,
GLenum textarget,
GLuint texture,
GLint level,
GLint layer);

Parameters

target
Specifies the framebuffer target. target must be GL_DRAW_FRAMEBUFFERGL_READ_FRAMEBUFFER, or GL_FRAMEBUFFERGL_FRAMEBUFFER is equivalent to GL_DRAW_FRAMEBUFFER.
attachment
Specifies the attachment point of the framebuffer. attachment must be GL_COLOR_ATTACHMENTiGL_DEPTH_ATTACHMENTGL_STENCIL_ATTACHMENT or GL_DEPTH_STENCIL_ATTACHMMENT.
textarget
For glFramebufferTexture1DglFramebufferTexture2D and glFramebufferTexture3D, specifies what type of texture is expected in the texture parameter, or for cube map textures, which face is to be attached.
texture
Specifies the texture object to attach to the framebuffer attachment point named by attachment.
level
Specifies the mipmap level of texture to attach.

Description

glFramebufferTextureglFramebufferTexture1DglFramebufferTexture2D, and glFramebufferTexture attach a selected mipmap level or image of a texture object as one of the logical buffers of the framebuffer object currently bound to targettarget must be GL_DRAW_FRAMEBUFFERGL_READ_FRAMEBUFFER, or GL_FRAMEBUFFERGL_FRAMEBUFFER is equivalent to GL_DRAW_FRAMEBUFFER.
attachment specifies the logical attachment of the framebuffer and must be GL_COLOR_ATTACHMENTiGL_DEPTH_ATTACHMENTGL_STENCIL_ATTACHMENT or GL_DEPTH_STENCIL_ATTACHMMENTi inGL_COLOR_ATTACHMENTi may range from zero to the value of GL_MAX_COLOR_ATTACHMENTS - 1. Attaching a level of a texture to GL_DEPTH_STENCIL_ATTACHMENT is equivalent to attaching that level to both the GL_DEPTH_ATTACHMENT and the GL_STENCIL_ATTACHMENT attachment points simultaneously.
textarget specifies what type of texture is named by texture, and for cube map textures, specifies the face that is to be attached. If texture is not zero, it must be the name of an existing texture with typetextarget, unless it is a cube map texture, in which case textarget must be GL_TEXTURE_CUBE_MAP_POSITIVE_X GL_TEXTURE_CUBE_MAP_NEGATIVE_XGL_TEXTURE_CUBE_MAP_POSITIVE_Y,GL_TEXTURE_CUBE_MAP_NEGATIVE_YGL_TEXTURE_CUBE_MAP_POSITIVE_Z, or GL_TEXTURE_CUBE_MAP_NEGATIVE_Z.
If texture is non-zero, the specified level of the texture object named texture is attached to the framebfufer attachment point named by attachment. For glFramebufferTexture1D,glFramebufferTexture2D, and glFramebufferTexture3Dtexture must be zero or the name of an existing texture with a target of textarget, or texture must be the name of an existing cube-map texture and textarget must be one of GL_TEXTURE_CUBE_MAP_POSITIVE_XGL_TEXTURE_CUBE_MAP_POSITIVE_YGL_TEXTURE_CUBE_MAP_POSITIVE_ZGL_TEXTURE_CUBE_MAP_NEGATIVE_X,GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, or GL_TEXTURE_CUBE_MAP_NEGATIVE_Z.
If textarget is GL_TEXTURE_RECTANGLEGL_TEXTURE_2D_MULTISAMPLE, or GL_TEXTURE_2D_MULTISAMPLE_ARRAY, then level must be zero. If textarget is GL_TEXTURE_3D, then level must be greater than or equal to zero and less than or equal to log2 of the value of GL_MAX_3D_TEXTURE_SIZE. If textarget is one of GL_TEXTURE_CUBE_MAP_POSITIVE_XGL_TEXTURE_CUBE_MAP_POSITIVE_Y,GL_TEXTURE_CUBE_MAP_POSITIVE_ZGL_TEXTURE_CUBE_MAP_NEGATIVE_XGL_TEXTURE_CUBE_MAP_NEGATIVE_Y, or GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, then level must be greater than or equal to zero and less than or equal to log2 of the value of GL_MAX_CUBE_MAP_TEXTURE_SIZE. For all other values of textargetlevel must be greater than or equal to zero and no larger than log2 of the value of GL_MAX_TEXTURE_SIZE.
layer specifies the layer of a 2-dimensional image within a 3-dimensional texture.
For glFramebufferTexture1D, if texture is not zero, then textarget must be GL_TEXTURE_1D. For glFramebufferTexture2D, if texture is not zero, textarget must be one of GL_TEXTURE_2D,GL_TEXTURE_RECTANGLEGL_TEXTURE_CUBE_MAP_POSITIVE_XGL_TEXTURE_CUBE_MAP_POSITIVE_YGL_TEXTURE_CUBE_MAP_POSITIVE_ZGL_TEXTURE_CUBE_MAP_NEGATIVE_X,GL_TEXTURE_CUBE_MAP_NEGATIVE_YGL_TEXTURE_CUBE_MAP_NEGATIVE_Z, or GL_TEXTURE_2D_MULTISAMPLE. For glFramebufferTexture3D, if texture is not zero, then textarget must beGL_TEXTURE_3D.

Notes on glFramebufferTexture2D

glFramebufferTexture is available only if the GL version is 3.2 or greater.

Errors

GL_INVALID_ENUM is generated if target is not one of the accepted tokens.
GL_INVALID_ENUM is generated if renderbuffertarget is not GL_RENDERBUFFER.
GL_INVALID_OPERATION is generated if zero is bound to target.
GL_INVALID_OPERATION is generated if textarget and texture are not compatible.

Copyright

Copyright © 2010 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. http://opencontent.org/openpub/.

Example of glFramebufferTexture2D

// This function does any needed initialization on the rendering
// context. 
void SetupRC()
{
    fprintf(stdout, "FBO Draw Buffers Demo\n\n");

    // Make sure required functionality is available: cube maps, auto mip gen, etc.
    if (!GLEE_VERSION_1_4)
    {
        fprintf(stderr, "OpenGL 1.4 is not available!\n");
        Sleep(2000);
        exit(0);
    }

    if (GLEE_ARB_texture_non_power_of_two)
    {
        npotTexturesAvailable = GL_TRUE;
    }
    else
    {
        fprintf(stderr, "GL_ARB_texture_non_power_of_two extension is not available!\n");
        fprintf(stderr, "Framebuffer effects will be lower resolution (lower quality).\n\n");
    }

    if (!GLEE_EXT_framebuffer_object)
    {
        fprintf(stderr, "GL_EXT_framebuffer_object extension is unavailable!\n");
        Sleep(2000);
        exit(0);
    }

    // we'll use up to 4 render targets if they're available
    glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
    glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &maxColorAttachments);
    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTexUnits);
    maxDrawBuffers = (maxDrawBuffers > maxColorAttachments) ? maxColorAttachments : maxDrawBuffers;
    maxDrawBuffers = (maxDrawBuffers > (maxTexUnits-1)) ? (maxTexUnits-1) : maxDrawBuffers;
    maxDrawBuffers = (maxDrawBuffers > 4) ? 4 : maxDrawBuffers;
    if (((!GLEE_ARB_draw_buffers || !GLEE_ARB_fragment_shader || !GLEE_ARB_shader_objects) 
        && !GLEE_VERSION_2_0) || (maxDrawBuffers != 4))
    {
        fprintf(stderr, "Support for at least 4 draw buffers is unavailable!\n");
        Sleep(2000);
        exit(0);
    }

    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
    glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE_EXT, &maxRenderbufferSize);
    maxTexSize = (maxRenderbufferSize > maxTexSize) ? maxTexSize : maxRenderbufferSize;

    fprintf(stdout, "Controls:\n");
    fprintf(stdout, "\tRight-click for menu\n\n");
    fprintf(stdout, "\tx/X\t\tMove +/- in x direction\n");
    fprintf(stdout, "\ty/Y\t\tMove +/- in y direction\n");
    fprintf(stdout, "\tz/Z\t\tMove +/- in z direction\n\n");
    fprintf(stdout, "\td/D\t\tToggle use of draw buffers\n\n");
    fprintf(stdout, "\tq\t\tExit demo\n\n");
    
    // Black background
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

    // Hidden surface removal
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    // Set up some lighting state that never changes
    glShadeModel(GL_SMOOTH);
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight);
    glMateriali(GL_FRONT, GL_SHININESS, 128);
    glEnable(GL_LIGHTING);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_NORMALIZE);
    glEnable(GL_LIGHT0);

    // Set up textures & shaders
    SetupTextures();
    SetupShaders();

    // Set up some renderbuffer state
    glGenRenderbuffersEXT(1, &renderbufferID);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbufferID);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT32, fboWidth, fboHeight);

    glGenFramebuffersEXT(2, framebufferID);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[0]);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderbufferID);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, renderTextureID[0], 0);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[1]);
    for (int i = 0; i < maxDrawBuffers; i++)
    {
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + i, GL_TEXTURE_2D, renderTextureID[i+1], 0);
    }
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}