PDA

View Full Version : Texture Coordinate Problem

mycral
10-14-2010, 07:40 AM
this is my glsl codes: :confused:
================================================== ====
vertex program:

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

fragment program:

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

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

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] :p

:confused: :confused:

nickels
10-14-2010, 09:43 AM
The code snippet is confusing, not sure what you are doing.

Typicall you have:

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; // :)

1) an in to catch the interpolated coord:

in vec2 ftexCoord0;

2) 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 :)

mycral
10-14-2010, 04:22 PM
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&amp;#65292;but every fragment seemly get the right coordinate(tex0st) interpolation . Can you tell me,how it works?

nickels
10-15-2010, 07:55 AM
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.

mycral
10-15-2010, 06:15 PM
Thanks!
That's just what I want to get.
But I don't know how it works definitly!!