[SOLVED] GLSL color mixing problem

Started by
5 comments, last by Corefanatic 13 years, 9 months ago
Hi guys,
I have the following problem. I've written a shader that takes in two textures and one float.

The first texture is my particle texture, the second is a gradient, which is 100x1.
Now I want to mix the color from a certain point from the gradient into the particle texture.

I've tested this in Render Monkey and it works, however it does not work in my render demo.The aim is to create a fireworks like effect.

Here are the sources for vert and frag shaders, where tex is particle texture, tex2 is the gradient and grad is a value from 0.0 to 1.0.

vert:
void main(void){   gl_Position = ftransform();}


frag:
uniform sampler2D tex;uniform sampler2D tex2;uniform float grad;void main(void){   vec4 color = texture2D(tex,gl_PointCoord);   vec4 color2 = texture2D(tex2, vec2(grad,1));   color.w *= (1.0 - grad); // Alpha, the particle becomes gradually more transparent   color.xyz *= color2.xyz; // multiplying the RGB values   gl_FragColor = color;}



It seems like I'm not getting the color from tex2 aka the gradient.

[Edited by - Corefanatic on August 10, 2010 12:46:25 PM]
Advertisement
Can you post the texture unit setup code?
Not sure what you mean, but here is my draw function code:

	ShaderManager::Instance()->UseProg("testprog");	GLint uniformlocation = -1;	//particle texture	CHECKGL(uniformlocation = glGetUniformLocation(ShaderManager::Instance()->GetProg("testprog"), "tex"));	CHECKGL(glActiveTexture(GL_TEXTURE0));	CHECKGL(glBindTexture(GL_TEXTURE_2D,TextureManager::Instance()->Get("firework")));	CHECKGL(glUniform1i(uniformlocation,0));	//particle color gradient	CHECKGL(uniformlocation = glGetUniformLocation(ShaderManager::Instance()->GetProg("testprog"), "tex2"));	CHECKGL(glActiveTexture(GL_TEXTURE1));	CHECKGL(glBindTexture(GL_TEXTURE_2D,TextureManager::Instance()->Get("grad")));	CHECKGL(glUniform1i(uniformlocation,1));	//grad variable	CHECKGL(uniformlocation = glGetUniformLocation(ShaderManager::Instance()->GetProg("testprog"), "grad"));	CHECKGL(glUniform1f(uniformlocation,m_curretProgress));	CHECKGL(glEnableClientState(GL_VERTEX_ARRAY));	CHECKGL(glVertexPointer(3,GL_FLOAT,0,m_particles));	CHECKGL(glDrawArrays(GL_POINTS,0,m_particleCount));	CHECKGL(glDisableClientState(GL_VERTEX_ARRAY));


CHEKGL is opengl error checking macro.

Also, I've noticed that the first time I call glGetUniformLocation I get 1, the second time I get -1 and third I get 0, so there definitely is a problem with the second one.
i had a similar problem the day before yesterday. i wanted to mix two texture maps, but it seems that only one of the two textures is loaded to the shader.

i then discovered the problem is that i created the uniform ids before linking the shade program.

like this:


uniform_texture1=glGetUniformLocation(program,"texture1");
uniform_texture2=glGetUniformLocation(program,"texture2");
linkShaderProgram();

I found out that both uniform_texture1 and uniform_texture2 were -1;

although the two uniform ids were wrong, one of the texture map did work.

so, i then reordered the three lines to:

linkShaderProgram();
uniform_texture1=glGetUniformLocation(program,"texture1");
uniform_texture2=glGetUniformLocation(program,"texture2");


everything worked.

this may not be your case, but you can check to see if you acquire for the uniform ids before linking the shader program.
Quote:Original post by billconan
i had a similar problem the day before yesterday. i wanted to mix two texture maps, but it seems that only one of the two textures is loaded to the shader.

i then discovered the problem is that i created the uniform ids before linking the shade program.

like this:


uniform_texture1=glGetUniformLocation(program,"texture1");
uniform_texture2=glGetUniformLocation(program,"texture2");
linkShaderProgram();

I found out that both uniform_texture1 and uniform_texture2 were -1;

although the two uniform ids were wrong, one of the texture map did work.

so, i then reordered the three lines to:

linkShaderProgram();
uniform_texture1=glGetUniformLocation(program,"texture1");
uniform_texture2=glGetUniformLocation(program,"texture2");


everything worked.

this may not be your case, but you can check to see if you acquire for the uniform ids before linking the shader program.


if I put it before linking, I get -1 and -1, right after I get 1 and -1
This may be your problem:
uniform sampler2D tex2;
didn't you state that one of the texture is a 1-dimensional texture. If so, why are you using a 2D texture lookup on a 1D texture?
Quote:Original post by cgrant
This may be your problem:
uniform sampler2D tex2;
didn't you state that one of the texture is a 1-dimensional texture. If so, why are you using a 2D texture lookup on a 1D texture?


Well, I made the gradient a 128x128 texture and it works! Thx! What strikes me though is that I get the uniform before I bind the texture, therefore it should not return -1, but I guess there are more mysteries of OpenGL I have to uncover :)

This topic is closed to new replies.

Advertisement