Regarding multiple textures in opengl, off-screen and rendering to screen switching, programmable pipelines have an understanding of fixed pipeline switching

Recently, I encountered several problems when writing projects with opengl , and I feel that it is more common to write and share;

One, the use of            opengl multi-texture

Second,            opengl off-screen rendering and rendering to screen switching use

Three,            opengl programmable pipeline and fixed pipeline switching use

After a magic of success and failure, I finally understand the use of textures, buffer objects ( FBOs ), and shaders in opengl .

1. The Prime Minister talks about the simple use of texture objects:

Void glGenTextures(GLsizen, GLuint * textureNames);// Get n unused texture identifiers (not necessarily consecutive integers), only for this function, the state and dimension information of the texture object are bound for the first time will get;

void glBindTexture(GLenum target, Gluint name);

//Create and use texture objects. When a texture object name is first bound, opengl creates a new texture object and sets the texture image and texture attributes to default values, glTexImage*(), glTexSubImage*(), glCopyTexImage*(), glCopyTexSubImage*(), Subsequent calls to glTexParameter*() and glPrioritizeTextures() will be stored in this texture object. The texture object contains the texture image and related mipmap images, etc. The stored texture properties include zoom out and zoom filters, wrap mode border color and texture priority

glBindTexture can accomplish three different tasks: when first binding with a non-zero texture object name, create a unique texture object and assign this name to it; when binding again later, the texture object becomes a written activity The texture object; if the texture object is bound to 0, it stops using the texture object and returns to the default texture object without a name;

When a texture object is first bound (that is, when it is created), its dimensions are determined by the target parameter, and the state of the texture object is immediately initialized to the default state of the OpenGL initialization target. In the initial state, the texture attribute are set to default values.

 

glTexImage2D(GL_TEXTURE_2D,0, GL_RGBA8,  width, height, 0, GL_RGB,GL_UNSIGNED_BYTE, NULL);

Allocate the texture graphics space to specify the texture internal and data format. The following are the parameters for setting the properties of the texture object

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_CLAMP);

Update texture data without allocating storage space

       glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width,height,GL_RGB,GL_UNSIGNED_BYTE,srcImgData);

glDeleteTextures(GLsize n, GLuint * textureNames) deletes n texture objects specified by textureNames, the released texture pair name can be allocated again later, when a previously used texture object is deleted, the binding will be restored to Default texture, attempt to delete texture object with name 0, this operation will be ignored.

2.多重纹理的使用

多重纹理引入了纹理单位每个纹理单位都拥有相同的功能,并且拥有自己的纹理状态,通过调用glActiveTexture(GLenum texUnit)选择当前纹理单位,上面介绍的glBindTexture就是把纹理对象绑定到当前纹理单位上,后续的纹理函数调用(如纹理矩阵)也只会影响当前纹理单位(OPENGL状态机的表现)

多重纹理的使用就是在先glActiveTexture选择当前纹理单位,然后创建纹理对象或将已经创建好的纹理对象调用glBindTexture重新绑定到这个当前纹理单位,设置好所需的纹理单位属性后在绘制时调用glMultiTexCoord设置对应的纹理单位的纹理坐标即可。

在固定管线中多重纹理是顺序对片断起作用的,即根据纹理单位状态,把原先的片断颜色与纹理图像进行组合,然后把上述操作产生的片断颜色传给下一个纹理单位进行处理。在编程管线中没有这个规律而是根据片段着色器中进行的处理;

 

理解纹理单位与纹理对象的关系是很重要的,纹理对象与纹理单位通过glBindTexture绑定,这点横重要;

 

3.FBO的使用

上面说了一下纹理对象,及多重纹理的使用,下面再说说FBO(帧缓冲区对象的使用)

FBO是用来进行离屏渲染的,当利用显卡进行科学计算时并不需要把数据显示到屏幕上,或者需要把一次渲染的数据保存下来供下一次渲染使用这时就用FBO

 

FBO的使用与纹理对象的使用方式类似

glGenFramebuffersEXT(1,&fbo);//得到fbo名称类似glGenTextures

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fbo);指定一个FBO用于读取或写入,这里和纹理对象的绑定有区别;

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, img, 0);

将img纹理对象绑定到fbo的颜色缓冲区,这样渲染时就可以把渲染结果写入到img纹理对象中了;

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);     使用默认的帧缓冲区对像,这样就可以在离屏渲染后再把以后的结果渲染到屏幕

这里主要是对离屏渲染和渲染到屏幕进行切换的理解

4.着色器的使用

不想写详细的着色器编译连接绑定的过程了,opengl着色语言第七章写的很详细,写下来费时不也没多大的贡献,下面主要介绍一下可编程管线和固定管线交替使用的方法。其实很简单与FBO和默认FBO的切换类似

通过调用glUseProgramn(0)使用固定管线,glUseProgramn(program)(其中program是编译连接好的程序对象)使用可编程管线;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324891795&siteId=291194637