着色器代码:
#extension GL_ARB_texture_rectangle : enable
uniform sampler2D tex0;
uniform sampler2DRect tex1;
uniform int sampler_type;
void main(void){
vec4 col;
if ( sampler_type == 0 ){
// comment this line - works
col = texture2D(tex0, gl_TexCoord[0].st);
//col = texture2D(tex0, gl_TexCoord[0].st/vec2(400, 300));
}
else{
col = texture2DRect(tex1, gl_TexCoord[0].st);
}
gl_FragColor = col;
}
主要代码:
void drawQuad(int x, int y, int w, int h, float s, float t)
{
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(x, y);
glTexCoord2f(s, 0); glVertex2f(x+w, y);
glTexCoord2f(s, t); glVertex2f(x+w, y+h);
glTexCoord2f(0, t); glVertex2f(x, y+h);
glEnd();
}
//--------------------------------------------------------------
void setup(){
shader.setupShaderFromFile(GL_FRAGMENT_SHADER, "test.frag");
shader.linkProgram();
img.loadImage("1.jpg"); // DEFAULT loaded in GL_TEXTURE_RECTANGLE
GLuint tid;
glGenTextures(1, &tid);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tid);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, 16*16, 16*16, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0); // init to black...
// if i comment this 2 line everything work
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glDisable(GL_TEXTURE_2D);
}
//--------------------------------------------------------------
void draw(){
shader.begin();
shader.setUniform1i("sampler_type", 1);// use GL_TEXTURE_RECTANGLE
//shader.setUniformTexture("tex1", img.getTextureReference(), 0);
//glActiveTexture(GL_TEXTURE0);
//glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, img.getTextureReference().texData.textureID);
//glDisable(GL_TEXTURE_RECTANGLE_ARB);
shader.setUniform1i("tex1", 0);
//glActiveTexture(GL_TEXTURE0);
drawQuad(0, 0, ScrWidth, ScrHeight, ScrWidth, ScrHeight);
shader.end();
}
我不知道为什么我创建另一个纹理会使这个着色器不起作用。
有两种方法可以使这个着色器工作。
- 只需在我的着色器中使用 sampler2DRect 即可。(评论sampler2D)
评论
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
希望我已经把这个问题描述清楚了:(我的平台是WIN7,VS2012,使用openFrameWorks 0.7.4