Texture Coordinate Problem

this is my glsl codes: :confused:

vertex program:

[b]varying vec2 tex0st;
varying vec2 tex1st;

uniform sampler2D Texture0;
uniform sampler2D Texture1;

void main(void)
{

vec4 texcolor;
vec4 texcolor2;

texcolor = texture2D(Texture0,tex0st);
texcolor2 = texture2D(Texture1,tex0st);
gl_FragColor = texcolor + texcolor2;
}[/b]

fragment program:

[b]varying vec2 tex0st;
varying vec2 tex1st;

uniform sampler2D Texture0;
uniform sampler2D Texture1;

void main(void)
{
vec4 texcolor;
vec4 texcolor2;

texcolor = texture2D(Texture0,tex0st);
texcolor2 = texture2D(Texture1,tex0st);
gl_FragColor = texcolor + texcolor2;
}[/b]

==========================================================

In the fragment programent,I got the texture coordinate but the coordinate is the vertex’s coordinate ,the texture coordinate is not the other pixcel’s coordinate … who can tell me how it works! Thanks in advanc [/b] :stuck_out_tongue:

:confused: :confused:

The code snippet is confusing, not sure what you are doing.

Typicall you have:
Vertex shader:

  1. texcoord that comes in from the vertices, such as:
    attribute vec2 texCoord0; // passed in as an attribute
  2. A varying that you use to pass linearly interpolate tex coords to the frag shader:
    out vec2 ftexCoord0;

ftexCoord0 = texCoord0 + time*scroll_speed; // :slight_smile:

Frag Shader:

  1. an in to catch the interpolated coord:

in vec2 ftexCoord0;

  1. A statement to sample the texture:
    texcolor = texture2D(texture0, ftexCoord0);

In your snippet, above, text0st appears to be an uninitialized variable, so using it could lead to a meltdown in the universe :slight_smile:

Oh,sorry! I made a mistake when I paste the vertex codes.
The following is the right codes.
this is my glsl codes:

vertex program:

varying vec2 tex0st;

uniform float time;

void main(void)
{
vec4 v = vec4(gl_Vertex);

tex0st = gl_MultiTexCoord0.st;//gl_TexCoord[0];
gl_Position = gl_ModelViewProjectionMatrix * v;
}

fragment program:

varying vec2 tex0st;

uniform sampler2D Texture0;
uniform sampler2D Texture1;

void main(void)
{
vec4 texcolor;
vec4 texcolor2;

texcolor = texture2D(Texture0,tex0st);
texcolor2 = texture2D(Texture1,tex0st);
gl_FragColor = texcolor + texcolor2;
}

==========================================================
I want to know that I did not pass the texture coordinate(tex0st) interpolation to fragment program,but every fragment seemly get the right coordinate(tex0st) interpolation . Can you tell me,how it works?

Okay, that code makes more sense! It seems correct, and, I think you are saying that it works, yes?

When you assign the varying in the vertex shader the driver will collect the tex0st values on all the vertices of a triangle, and the value it passes to the frag shader is not the value you pass out of the vertex shader (which would be ill defined, since which of the three vertices would it choose??) but instead it uses a value that is linearly interpolated from the vertices (depending on where the frag position lies within the triangle).

There is also a flat shading mode, where it will send exactly one of the vertex values (no interpolation), but that is not the default.

Is that what you were asking?
Cheers.

Thanks!
That’s just what I want to get.
But I don’t know how it works definitly!!
Can you tell me where I could get some resource about this or glsl.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.