How does of texture coordinate run?

This question is a little long.
I just write a rectangle on my screen!Here it’s the code

ofSetColor(255,255,255,255);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(_uwidth, 0); glVertex3f(_width, 0, 0);
glTexCoord2f(_uwidth, _uheight); glVertex3f(_width, _height, 0);
glTexCoord2f(0,_uheight); glVertex3f(0,_height, 0);
glEnd();
here _width=_uwidth=_height=_uheight=600;

Then I add my base vert code,it’s easy ,and i will chang change below.

varying vec2 texCoordVarying;
void main()
{
vec2 texcoord = gl_MultiTexCoord0.xy;
texCoordVarying = vec2(texcoord.x , texcoord.y);
// send the vertices to the fragment shader
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

At last it‘s my frag code

uniform sampler2DRect BaseImage;
varying vec2 texCoordVarying;
void main (void)
{
vec2 st = texCoordVarying.st;
vec4 base = texture2DRect(BaseImage, st);
gl_FragColor = base;
}

Yeah,it work very well!

But I doubt that why the _uwidth and _uheight have to set that 600,I think _uwidth and _uheight should be range form 0 to 1. Then I can give shader different size texture, and then just find the pixel on the texture in proportion.

1 Like

By default OF makes use of ARB textures, which basically means that you need to pass “pixel coordinates” instead of normalized coordinates.

You can use ofEnableArbTex() and ofDisableArbTex() to switch between the two modes; just keep in mind that, if you want to use 0-1 coords with a specific texture, you’ll need to call ofDisableArbTex() before allocating it.

3 Likes

I do some test here! I just change my frag code like this!

uniform sampler2DRect BaseImage;
varying vec2 texCoordVarying;
void main (void)
{
vec2 st = texCoordVarying.st;
gl_FragColor=vec4(st.s,st.t,0,1);
}

What I get just a yellow rectangle,expert the first line ,it’s red.
So vec2 st = texCoordVarying.st; just get the value (0,0)~(600~600);

and I do another test like this

uniform sampler2DRect BaseImage;
varying vec2 texCoordVarying;
void main (void)
{
vec2 st = texCoordVarying.st;
gl_FragColor=vec4(st.s/600,st.t/600,0,1);
}

oh I can get the picture of fade colors.

My question,how can i set the texture coordinate range from (0,0) to (1,1);

Thank you very much ,I understand.

Yes , I change ,but it didn’t work.

May I ask ,If I makes use of ARB textures just like of default ,how can i use multiple textures with different size?

In order to work with non ARB textures you should:

  • call ofDisableArbTex() before allocating/using the textures (i.e. in setup() or calling the enable/disable functions whenever you need a specific behaviour for a specific texture)
  • in your fragment shader, you should use sampler2D instead of sampler2DRect (you can also use both in the same shader, the important thing is that you use the right one with the correct texture type)

If you follow these rules it should work fine.

As for your last question: you can use ARB textures with different texture sizes simply passing the texture resolutions as uniform arguments. I used this technique in cases where I needed a fragment shader that used information in one texture to apply rules in another one and the ratio of the 2 textures where relevant too (i.e.: sprite maps, gpu computing…)

Here’s a simple example:


texture.allocate(800,600);
texture1.allocate(100,100);

shader.begin();
shader.setUniformTexture(“tex1”,texture1);
shader.setUniform2f(“texSize”,texture.getWidth(),texture.getHeight());
shader.setUniform2f(“tex1Size”,texture1.getWidth(),texture1.getHeight());
texture.draw();
shader.end();

and in the shader:


uniform sampler2DRect tex;;
uniform sampler2DRect tex1;
uniform vec2 texSize;
uniform vec2 tex1Size;

vec2 tc = gl_TexCoord[0].st;
vec4 texelMain = texture2DRect(tex,tc);
//let’s say you want to spread texture1 over texture: you simply make a ratio
vec2 tt = tc/texSize*tex1Size;
vec4 texelMapped = texture2DRect(tex,tc);

2 Likes