0
votes

Shader code:

#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;
}

Main Code:

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();

}

I dont know why i create another texture will make this shader not works.

There is 2 way to make this shader works.

  1. Just use sampler2DRect in my shader. ( comment sampler2D )
  2. comment

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Hope that I have described this problem clearly :( My platform is WIN7, VS2012, and use openFrameWorks 0.7.4

1
It's not clear what the problem is. I don't see where img.getTextureReference().texData.textureID gets created. If that's in your setup routine, then you've got other problems. You also neglect to explain what "not works" means. What doesn't work? You only seem to be binding one texture when your shader uses two.Nicol Bolas
Where's your #version directive?genpfault
sorry about my description. I do this in openFramworks link.Fiers Mega
img.getTextureReference().texData.textureID gets created in img.loadImage("1.jpg"); You can check the code here link. Main function is allocate and loadData. "Not Works" means my shader draw nothing in screen, and "Works" means it will draw a quad-texture of my loaded jpg file. my thoughts is that glTexImage2D()(use GL_LUMINANCE_ALPHA) with glTexParameter() will make shader error to load texture(texture2DRect or texture2D), but dont know how to avoid or fixed that.Fiers Mega
i haven't set glsl version in my fragment-shader. so I think is 110Fiers Mega

1 Answers

0
votes

I'm sorry, but this is a mess.

  • You are declaring function for drawing quad, but not using it anywhere.
  • Everything is drawn within setup(). I am not really sure how is OpenGL implemented in openFrameworks, but dedicated draw() function is there for this reason.
  • Missing #version as genpfault said
  • There is no reason why to use direct OpenGL functions, as they wrapped in very easy to use functions.

I don't want to be mean or anything but instead of direct answer, I recommend this detailed tutorial, straight from OF guys.