Chi*_*ell 1 c++ opengl macos fragment-shader
我有一个使用OpenGL的OSX应用程序.我正在使用GL_TEXTURE_2D类型的纹理绘制我的大多数东西,只要我坚持使用GL_TEXTURE_2D,一切正常.但我需要有几个GL_TEXTURE_RECTANGLE_ARB类型的纹理.
要创建GL_TEXTURE_RECTANGLE_ARB类型的纹理,请执行以下操作:
// 4x4 RGBA texture data
GLubyte TexData[4*4*4] =
{
0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff,
0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff,
0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff,
0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
};
GLuint myArbTexture;
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glGenTextures(1, &myArbTexture);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, myArbTexture);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAX_LEVEL, 0 );
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, TexData);
Run Code Online (Sandbox Code Playgroud)
要绘制纹理,我执行以下操作:
SetupMyShader();
SetupMyMatrices();
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, myArbTexture);
DrawMyQuads();
Run Code Online (Sandbox Code Playgroud)
我的着色器非常简单:
void main (void)
{
gl_FragColor = texture2D(Tex, v_texcoords)*u_color;
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我的着色器始终引用最后使用的纹理:
glBindTexture(GL_TEXTURE_2D, lastTexture)
Run Code Online (Sandbox Code Playgroud)
而不是引用在以下指定的纹理:
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, myArbTexture);
Run Code Online (Sandbox Code Playgroud)
有些事情需要注意:
glGetError(),我没有收到任何错误.所以,任何人都猜到我错过了什么?在使用GL_TEXTURE_RECTANGLE_ARB时,我的着色器是否需要调用glBindTexture以外的其他调用?
您需要更新着色器代码以使用矩形纹理.制服需要声明为:
uniform sampler2DRect Tex;
Run Code Online (Sandbox Code Playgroud)
和访问:
gl_FragColor = texture2DRect(Tex, v_texcoords)*u_color;
Run Code Online (Sandbox Code Playgroud)
要记住的另一个方面是纹理坐标对于矩形纹理的定义不同.矩形纹理使用0.0到宽度和0.0到高度范围内的非标准化纹理坐标,而不是所有其他纹理类型使用的范围为0.0到1.0的标准化纹理坐标.
| 归档时间: |
|
| 查看次数: |
507 次 |
| 最近记录: |