Beginner: Implementing both colours and textures in fragment shader (for picking)

Hi all, this is my first post.

I am learning about shaders in a very piecemeal fashion so don’t be surprised if I seem to not understand really obvious things.

I have to implement picking in a program (for uni) with shaders with objects that are arrays of vertices and texture co-ordinates parsed from Collada files.

I started off trying to do ray picking but I just couldn’t make it work. I then decided to try colour id picking because that’s what I did in my previous graphics course which didn’t introduce shaders.

I always wanted to draw some axes on the screen to help with debugging but never could, I didn’t really understand what the shader we were given did at the time so I didn’t worry about it. But now if I want to implement colour id picking I will have to be able to draw in colour and not in textures.

I’ve now learned a tiny bit about shaders and tried this in my fragment shader:


varying  vec2 vTexCoord; 
uniform sampler2D myTexture;

void main (void)  
{
	if(gl_Color.r <= 0.01 && gl_Color.g <= 0.01 && gl_Color.b <= 0.01)
	{
		gl_FragColor =  texture2D(myTexture, vTexCoord); 
	}
	else
	{
		gl_FragColor = gl_Color; 
	}
}

The shader used to just have gl_FragColor = texture2D(myTexture, vTexCoord); in the main ().

In my drawing part, if not in picking mode I enable textures and colour in black, and if in picking mode I disable textures and draw in the colour id of the object.

I may have mistakes in my drawing code, but I am sure that my shader code has mistakes because my matrix handles return -1. I can’t seem to find any info on the net about testing what colour you have in the shader and doing different things based on the colour. So my shader code was just a blind guess as to what might work but I can’t think of any other guesses to try.

Thanks heaps if anyone can help. And/or point me to some good shader tutorials.

If you want a test a value in the shaders it is more normal to just use a uniform that you set.

eg


varying  vec2 vTexCoord; 
uniform sampler2D myTexture;
uniform vec4 myColour; 

void main (void)  
{    
  if(myColour.a == 0.0f)
  {        
    gl_FragColor =  texture2D(myTexture, vTexCoord);     
  }    
  else    
  {        
    gl_FragColor = myColour;
  }
}

in main code


  // replace glSetColor with this
  float val[4];val[0] = 1.0f; val[1] = 0.0f;  val[2] = 0.0f; // red
  if (pick)  
    val[3] = 0.0f;
  else  
    val[3] = 1.0f;

  GLuint loc = glGetUniformLocation(shader_program_id,"myColour");
  glUniform4fv(loc,1,val);

Thanks for your fast reply tonyo!! That looks way better than what I had. I also just figured out I have to disable the attrib arrays for textures so it didn’t crash on glDrawArrays. I now have a problem with the picking in that glReadPixels puts rubbish data into my array to hold the pixel info but when I figure that out I’m sure it will work :slight_smile:

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