GLSL shaders acting weird

Started by
25 comments, last by adam17 18 years, 9 months ago
ok ive been researching GLSL shaders for a few days now and have progressed pretty well. i used to do Cg but now im switching. anyway ive been running into some weird issues with the shaders. basically what my shaders are doing is blending between two textures based on a timer. my vertex shader transforms the position of the vertex and sets the 1st and 2nd texture units up for the fragment shader. the fragment shader then takes the two textures, grabs a fragment from each, and then does the follwing equation as the final fragment color: (alpha*tex2)+((1.0-alpha)*tex1). alpha is the uniform variable based on time; the issue i am having is i can get my first and second texture to add and/or multiply with the correct output. whenever i output only the second texture unit, or multiply it with alpha, i always get the first texture unit. the same happens when i run them through the equation last paragraph. here is my vertex shader code

void main()
{
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;	

	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_TexCoord[0] = gl_MultiTexCoord1;		
}
here is my fragment shader code

uniform sampler2D texture0, texture1;
uniform float alpha;
void main()
{
	vec4 tex1 = texture2D(texture0, gl_TexCoord[0].st);
	vec4 tex2 = texture2D(texture1, gl_TexCoord[0].st);
		
	gl_FragColor = (alpha*tex2)+((1.0-alpha)*tex1);
}
here is the code for setting the uniform variables and the shader

GLint tex0, tex1;
GLint alpha;
void SetTestShader()
{
	vert_shader.filename = "vp0.vert";
	vert_shader.type = GL_VERTEX_SHADER_ARB;

	frag_shader.filename = "fp0.frag";
	frag_shader.type = GL_FRAGMENT_SHADER_ARB;

	LoadGLSLShader(vert_shader);
	LoadGLSLShader(frag_shader);

	glUseProgramObjectARB(frag_shader.shader_handle);
	tex0 = glGetUniformLocationARB(frag_shader.shader_handle, "texture0");
	tex1 = glGetUniformLocationARB(frag_shader.shader_handle, "texture1");
	//alpha = glGetUniformLocationARB(frag_shader.shader_handle, "alpha");
}
btw when i uncomment alpha all i get is a VERY dark texture. lastly here is the code for drawing

	static float a = 0;
	if(a > 1.0)
		a = 0.0;
	else
		a += 0.01f;

	glUseProgramObjectARB(glslContext);  //this is globally defined. could this pose a future problem?
	glUniform1iARB(tex0, 0);
	glUniform1iARB(tex1, 1);
	glUniform1fARB(a, xrot);
	sphere.DrawObj();
	glUseProgramObjectARB(0);
thanks ahead of time!
Advertisement
Quote:Original post by adam17
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[0] = gl_MultiTexCoord1;

Without having read through the rest of your code..... is that really what you meant?
as far as i know thats how i access the first and second texture units. i thought gl_TexCoord[1] (or higher) was for using different sets of texture coordinates on the same object.

if i use gl_TexCoord[1] in both the vertex program and fragment program all i get is a black texture.
Quote:
glUniform1iARB(tex0, 0);
glUniform1iARB(tex1, 1);


Is that correct? I would assume you mean textureID's 1 and 2, not 0 and 1. Also, I assume it's safe to say that the two shaders are correctly linked into the single programobject glslContext?
The uniforms look right, as they dont indicate texture handles but texture units, which are numbered from zero.

Sneftel brings up a good point however, you are assigning the built in varying variable gl_TexCoord[0] first the value of gl_MultiTexCoord0, then the value of gl_MultiTexCoord1.
Now, this is ineffect just assigning it the value held in gl_MultiTexCoord1 (and the compiler should be smart enuff to spot this and remove the extra line), so what values do you pass to as the texture coordinates to texture unit 1?

Infact, post your rendering where you pass the vertex data down...
Quote:
glUniform1iARB(tex0, 0);
glUniform1iARB(tex1, 1);
glUniform1fARB(a, xrot);


What's that last line supposed to do? The first parameter should be the handle to the uniform, and the second a float value. According to the rest of your program, shouldn't it be glUniform1fARB(alpha,a)?
Quote:Original post by mikeman
Quote:
glUniform1iARB(tex0, 0);
glUniform1iARB(tex1, 1);
glUniform1fARB(a, xrot);


What's that last line supposed to do? The first parameter should be the handle to the uniform, and the second a float value. According to the rest of your program, shouldn't it be glUniform1fARB(alpha,a)?


yeah my mistake. i made some changes to the variable names so that it would make more sense to anyone reading it. it should be

glUniform1fARB(alpha, a);


->_the_phantom_
i got the code for assigning gl_MultiTexCoord0 and 1 to gl_TexCoord[0] from Lighthouse3D. ive noticed ALOT of the stuff there is wrong, confusing, or doesnt match the source code they provide.
as for the rendering code i only render models i read in. essentially it only uses glTexCoord2f(...); and glVertex3f(...);. yeah im going to optimize that later.
ah, the joys of dodgy tutorials [rolleyes]

Anyways, as you are using the default opengl commands then your vertex shader will want to look like this;

void main(){	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;		gl_TexCoord[0] = gl_MultiTexCoord0;}// infact, I'd write it like thisvoid main(){	gl_Position = ftransform();		gl_TexCoord[0] = gl_MultiTexCoord0;}


As for your alpha problem, i'm not sure about that, as it looks sane, i'll take a closer look at it in a bit when i've a little more time (dinner and all that)
For my money, there is exactly ONE text on GLSL worth reading, and that is the Orange Book. It's a little pricey, so see if there's a library somewhere you can borrow it from, but it's worth every penny.

EDIT: Actually, you're GDNet+... look here to get it from Addison-Wesley at a discount.
->_the_phantom_
i made the changes you mentioned like changing to ftransform and taking out the extra gl_TexCoord[0] assignment.

->Sneftel
i wish i could afford the Orange Book right now. ive seen it and it looks bada$$ and could help me out ALOT. in the mean time im looking around for some local libraries that would carry it.

This topic is closed to new replies.

Advertisement