some extreemly basic GLSL questions

Started by
6 comments, last by Bobboau 15 years, 9 months ago
ok, I'm wanting to write a fragment shader that uses the results of the fixed function vertex proccessor for texture coords, I have 2d coords assigned currently and a 1d texture bound, how do I get the texture coords generated by the fixed function and use them to look up my texture? I am extremely new to GLSL and I couldn't find any examples of a texture lookup for a stand alone fragment shader. I am wanting to have the lookup of the texture be based on the distance of the UV coords, i.e. something like texture1d(currently_bound_stage0_texture, sqrt(coord.u^2+coord.v^2)). I am hopping this is possible.
Bobboau, bringing you products that work... in theory
Advertisement
With GLSL, you must specify both a vertex-shader and a fragment-shader, and link them, afaik.
The only way to make use of the "fixed function" is to compile with cgc.exe into an ARB shader (asm code), instead of using the GLSL extension.... and then hope the driver generates a vertex-shader that suits you. You can't rely on that, maybe.

Your frag-shader:

uniform sampler1D tex0;void main(){   float coord0 = length(gl_TexCoord[0].xy);   gl_FragColor = texture1D(tex0,coord0);}


And if you were to make your code reliable, here's the vertex-shader you need to have:
void main(){   gl_Position = ftransform();   gl_TexCoord[0] = gl_MultiTexCoord0;}
Quote:Original post by Bobboau
ok, I'm wanting to write a fragment shader that uses the results of the fixed function vertex proccessor for texture coords, I have 2d coords assigned currently and a 1d texture bound, how do I get the texture coords generated by the fixed function and use them to look up my texture? I am extremely new to GLSL and I couldn't find any examples of a texture lookup for a stand alone fragment shader.

I am wanting to have the lookup of the texture be based on the distance of the UV coords, i.e. something like texture1d(currently_bound_stage0_texture, sqrt(coord.u^2+coord.v^2)). I am hopping this is possible.


You would just create a fs and compile, link, get uniform locations.
There are plenty of example but if you want one, here it is

uniform sampler2D Texture0;void main(){   gl_FragColor = texture2D(Texture0, gl_TexCoord[0].xy);}
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
yeah,I knew that was simple, I just hadn't found the proper references.
a few more hours of looking and I found the document with all the answers on it. it was of course while I was looking for something completely different though.

one last quick question, is there a reliable way of getting a fragment's previous color? from what I see gl_FragColor is undefined before you write to it.
Bobboau, bringing you products that work... in theory
Quote:Original post by Bobboau
one last quick question, is there a reliable way of getting a fragment's previous color? from what I see gl_FragColor is undefined before you write to it.


The specs doesn't say anything about reading from gl_FragColor.
You should write to a render to texture and then in the next pass, bind the texture and sample it. That's the standard way of doing it.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
the spec sheet I found said it was undefined.

and unfortunately rendering to a texture and using it the next frame doesn't seem to be an option, but fortunately I was able to get my desired effect with a little manipulation of alpha blending.
Bobboau, bringing you products that work... in theory
It's possible the next generation of hw will have blending shader support. It would be a new stage I image so you create a small shader just for blending.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
wouldn't it make more sence to just extend the current shaders to have the ability to read what was there before them? it seems adding another shader stage would be just waistful.
Bobboau, bringing you products that work... in theory

This topic is closed to new replies.

Advertisement