How to use Blend in shader?

this is my code:

	GLint texLoc;
	texLoc = glGetUniformLocation(p, "volume");
	glUniform1i(texLoc, 0);
	texLoc = glGetUniformLocation(p, "transferfunction");
	glUniform1i(texLoc, 1);

	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, 0);
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_1D, 1);

	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 0.0);
	glVertex3f(0.0, 0.0, 0.0);
	glTexCoord2f(0.0, 1.0);
	glVertex3f(5.0, 0.0, 0.0);
	glTexCoord2f(1.0, 1.0);
	glVertex3f(5.0, 5.0, 0.0);
	glTexCoord2f(1.0, 0.0);
	glVertex3f(0.0, 5.0, 0.0);
	glEnd();

	glDisable(GL_BLEND);

I used glBlendFunc,but it seems no affection on my quad. why?

Does your shader write anything to alpha channel?

And this is my fragment shader:

     uniform sampler3D volume;
     uniform sampler1D transferfunction;
     void main()
     {	
        gl_FragColor.rgb = texture3D(volume, gl_TexCoord[0].sqt).rgb;
        float data = texture3D(volume,gl_TexCoord[0].sqt);	
        gl_FragColor.a = texture1D(transferfunction, data)a;
}

Why do you bind a texture with ID 0?
You should use glGenTextures

float data = texture3D(volume,gl_TexCoord[0].sqt);	

This returns a rgba. You should put a .r
Evene if you have a lumincance texture, it gets processed into a rgba

float data = texture3D(volume,gl_TexCoord[0].sqt).r;	

I am a beginer of GLSL. I think this

float data = texture3D(volume,gl_TexCoord[0].sqt).r;
just return R value. why just return R Value?

why just return R Value?

because you put it into:

float data

which is just one float.
Evewy texture function returns vec4 - if you want to use just one value, then you must specify which one - in this case it’s R.

Can I do this?

uniform sampler3D volume;     
uniform sampler1D transferfunction;    
void main()    
{	        
   gl_FragColor.rgb = texture3D(volume, gl_TexCoord[0].sqt).rgb;        
   gl_FragColor.a = texture1D(transferfunction, gl_TexCoord[1].s).a;
}

Can I do this?
I don’t see why not. It looks reasonable.

Then I want to Render a quad which middle part is transparence.my lookup table is

      GLubyte table[256][4];   
	int i;

	for (i=0;i<256;i++) 
	{      
		table[i][0] = 0;      
		table[i][1] = 0;      
		table[i][2] = 255;      
		table[i][3] = 255;   
	}

	for (i=100;i<200;i++) 
	{      
		table[i][0] = 0;      
		table[i][1] = 0;      
		table[i][2] = 255;      
		table[i][3] = 0;   
	}

	glBindTexture(GL_TEXTURE_1D, 1);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);  
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);  
	glTexImage1D(GL_TEXTURE_1D,                     
		0,							               
		GL_RGBA,		                 
		256,						                 
		0,							                 
		GL_RGBA,				                 
		GL_UNSIGNED_BYTE,			                 
		table);

But the quad is still opaque all, why?What should I do?

When I change my lookup table like this:

for (i=0;i<256;i++) 	
{    
   table[i][0] = 0;      		
   table[i][1] = 0;      		
   table[i][2] = 255;      		
   table[i][3] = 255;   	
}	
for (i=0;i<1;i++) 	
{      		
   table[i][0] = 0;      		
   table[i][1] = 0;      		
   table[i][2] = 255;      		
   table[i][3] = variable;   	
}

It change the whole quad transparence with the variable. That means only the first value of the table have the affection. How to deal with these?

If this is used with the same shaders as above it’s because you have not specified gl_TexCoord[1] data inside your program and all four vertices of your quad use the default 0.0 to lookup the color in the 1D texture.

But that’s hard to say if you continue to present incomplete code snippets.
Studying some OpenGL tutorials will help.
At least skim over the OpenGL specs and GLSL specs first. They are more complicated than books on OpenGL, but it helps knowing what’s available.

I’m not sure what you mean with “render a quad which is transparent in the middle” but if you mean transparent in the center of a single quad and differently opaque at its edges, that won’t work with a 1D texture and four texture coordinates using linear interpolation.

this is a test. I want to build a lookup table for fast rendering.I will study the GLSL tutorials carefully again.

Ha, I find my problem. And I deal with it.

it’s because you have not specified gl_TexCoord[1] data inside your program and all four vertices of your quad use the default 0.0 to lookup the color in the 1D texture.
Relic, you are right.The problem is in my vert shader as below:

void main()
{
   //gl_TexCoord[0] = gl_MultiTexCoord0;
   //gl_TexCoord[1] = gl_MultiTexCoord1;
   gl_Position = ftransform();
}

I thought “gl_TexCoord[0] = gl_MultiTexCoord0” is unuseful .So I do not make it work.But it is very important.And “gl_TexCoord[1] = gl_MultiTexCoord1” is really unuseful. 1DTexture and 2D Texture should use the same TextureCoordinate. Is it right?

“1DTexture and 2D Texture should use the same TextureCoordinate. Is it right?”

I have no idea what your test should look like but right, it doesn’t make sense in context of your other questions about volume rendering and color tables on all forums. (Edit: Just saw that you figured it out in the other thread.)

For the usual case of one-component density data stored as luminance values in a 3D texture you don’t need a second texture coordinate for the dependent lookup into the 1D texture, because the result of the 3D texture lookup is the 1D coordinate then. That had been explained before.

For RGB data and programmable alpha it’s either the same if you calculate the 1D s-coord inside the fragment shader from these RGB values, or if it is constant like in your current example it’s not needed anyway and can be done with the quad’s color and texture modulation.

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