Shader, 1D lookup table

Hello,
im new into shader programming. actually i use the Cg shader language from nvidia. but for my question it doesnt matter.
Currently im working on volume rendering using 2d texture mapping.
So far so good, i can create the volume out of a stack of x y and z
images. almost everywhere i read about 1d transfer functions.
For this functions they use 1d textures and apply them in the fragment shader/program on the texture.

My Questions is, how to apply this lookuptable onto a texture.
I tried this out with an example and it “worked” just a little bit.
Heres the code code


// create a sensless lookup table (RGBA)
for(int i = 0; i < 1024; i += 4)
{
        if( i < 400)
	{
		lookup[i] = 100;
		lookup[i+1] = 0;
	}
	else
	{
		lookup[i] = 0;
		lookup[i+1] = 255;
	}		
	lookup[i+2] = 0;
	lookup[i+3] = 0;
}
glGenTextures(1,&TransferID);

// load texture 8Bit texture bitmap
HBITMAP hBmp = NULL;
hBmp=(HBITMAP)::LoadImage(NULL,C_WEED2.bmp",IMAGE_BITMAP,								 0, 0,                           						 LR_LOADFROMFILE|LR_CREATEDIBSECTION);           
	
BITMAP BM;                          // Get bitmap info.	
GetObject (hBmp,sizeof (BM),&BM); //
	
BYTE* pData = NULL;
// convert 8bit texture to RGBA
if(BM.bmBitsPixel == 8)
{
    pData = new BYTE[BM.bmWidth * BM.bmHeight * 4];
    for(int i = 0, RGB = 0; i < BM.bmWidth * BM.bmHeight; i++,RGB += 4)
    {
         pData[RGB] = ((unsigned char*)BM.bmBits)[i];
         pData[RGB+1] = ((unsigned char*)BM.bmBits)[i];
	 pData[RGB+2] = ((unsigned char*)BM.bmBits)[i];
	 pData[RGB+3] = 255;
    }
}
// bind 2d texture and set parameters
glGenTextures(1,&TempID);
glBindTexture(GL_TEXTURE_2D,TempID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, BM.bmWidth, BM.bmHeight,0,GL_RGBA, GL_UNSIGNED_BYTE, pData);
glEnable(GL_TEXTURE_2D);
pData != NULL ? delete[] pData : pData;

glBindTexture(GL_TEXTURE_1D, TransferID);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexImage1D(GL_TEXTURE_1D,0,GL_RGBA,256,0,GL_RGBA,GL_UNSIGNED_BYTE,lookup);
glEnable(GL_TEXTURE_1D);

CShaderManager::GetShader("SimpleShader")->SetUniformSampler(CShaderObject::FRAGMENT,"texture",TempID);
CShaderManager::GetShader("SimpleShader")->SetUniformSampler(CShaderObject::FRAGMENT,"lookup",TransferID);


the onDraw method


//bind program and enable profile
CShaderManager::RunAllShaders();  
CShaderManager::GetShader("SimpleShader")->SetUniformModelViewProjectionMatrix(CShaderObject::VERTEX,"ModelViewMatrix");	

	
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,TempID);
CShaderManager::GetShader("SimpleShader")->EnableTextureParameter(CShaderObject::FRAGMENT,"texture");
CShaderManager::GetShader("SimpleShader")->EnableTextureParameter(CShaderObject::FRAGMENT,"lookup");

	
glBegin(GL_QUADS);	
	glTexCoord2f(1.0f, 1.0f); 
	glVertex3f( 0.5f,  0.5f, 0);
	glTexCoord2f(0.0f, 1.0f);  
	glVertex3f(-0.5f,  0.5f, 0);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-0.5f, -0.5f, 0);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f( 0.5f, -0.5f, 0);
glEnd();

	CShaderManager::GetShader("SimpleShader")->DisableTextureParameter(CShaderObject::FRAGMENT,"texture");
CShaderManager::GetShader("SimpleShader")->DisableTextureParameter(CShaderObject::FRAGMENT,"lookup");
CShaderManager::StopAllShaders();
glDisable(GL_TEXTURE_2D);

And here the fragment and vertex program

vertex


struct app2vertex
{
	varying float4 position : POSITION;
	varying float4 color : COLOR;
	varying float2 texCoord : TEXCOORD0;
	varying float2 direction : TEXCOORD1;
};

struct vertex2pixel
{
	varying float4 position : POSITION;
	varying float4 color : COLOR;
	varying float2 texCoord : TEXCOORD0;
	varying float2 direction : TEXCOORD1;
};

vertex2pixel main(app2vertex IN, uniform float4x4 ModelViewMatrix)
{
	vertex2pixel OUT;
	OUT.position = mul(ModelViewMatrix,IN.position);
	OUT.color = IN.color;
	OUT.texCoord = IN.texCoord;
	OUT.direction = IN.direction;

	return OUT;
}

fragment


struct fInput
{
	float2 texCoord : TEXCOORD0;
	float2 direction : TEXCOORD1;
};

struct fOutput
{
	float4 color : COLOR;
};

fOutput main(fInput IN, uniform sampler2D texture, 
			uniform sampler1D lookup)
{

    fOutput OUT;

    float4 position = float4(IN.texCoord,0,1);
    float4 lookupIndex = tex2D(texture, position.xy);
    OUT.color = tex1D(lookup, lookupIndex.r);
	    
    return OUT;
}

When I apply the shader onto this texture i get some red regions and some green regions. Thats ok, because i set it up in the
lookup table.
So, but when i change the alpha value, nothing happens. the texture still is opaque.

Can someone help me ?

Greetings,

missing a glEnable(GL_BLEND); probably ?

http://www.opengl.org/resources/faq/technical/transparency.htm

Yep, that was it. I set up a blend equation and enabled blending.
it works now.
thank you

lobbel

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