Scale and Bias before texture mapping

Started by
5 comments, last by nsn 16 years, 9 months ago
Hello, I am trying to use OpenGL for visualize 2D 16bit image data. I would like to change brightness (bias) and contrast (scale). In order to achieve this I can upload the texture repeatedly and use GL_RED_SCALE, GL_RED_BIAS, ... But I prefer not to upload the texture each time I want to change brightness. So I am trying to use the texture combiner functions to achieve this. To achieve this, I use RenderScene() { ... glColor3f(scale_val,scale_val,scale_val); GLfloat bias[4] = {bias_val, bias_val, bias_val, 1.0f}; glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, bias); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); //For bias glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_SUBTRACT); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB,GL_TEXTURE); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB,GL_SRC_COLOR); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB,GL_CONSTANT); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB,GL_SRC_COLOR); //For scale glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB,GL_PREVIOUS); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB,GL_SRC_COLOR); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB,GL_PRIMARY_COLOR); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB,GL_SRC_COLOR); //After setting up the texture environment render quad glBegin(GL_QUADS) ... glEnd(); glutSwapBuffers(); } This works as long as my 0.0< scale < 1.0f Any help is much appreciated. -nsn
Advertisement
Whoops!!, I forgot to ask the question though

I am looking for way to use value of scale larger than 1.0.

I am not sure what you are trying to do, but if you want values outside of 0-1 you will need a floating point buffer setup instead of a fixed point.
If I'm understanding right what you want to do, then your input values are clamped to 0.0 to 1.0?
If so you should use Shaders, beceause with Shaders you can completely replace the fixed Function pipeline and do input values larger then 1.0
http://3d.benjamin-thaut.de
I'm not sure if all OpenGL functions or most have several versions that take different arguments. For example:

glColor3f(scale_val,scale_val,scale_val);

The F at the end of the function name is what type of arguments it takes. In the above example, you pass floats for value: 0.0 - 1.0.

So if you do:

glColor3i( ... )

...the values you pass to glColor3i would be integers. The integers work 0 - 255 instead of 0.0 - 1.0. The way the floating point works is every number between 1.0 and 0.0 is the same as 0 - 255. so 0.5 is the same as 128. 0.25 would be the same as 64. 0.75 is the same as 192. (rounded up estimates) They both are the same, just different ways of representing the value. I personally like to use the 0 - 255 method.
Hi MARS_99

Thank you for your response.

What I am trying to do is if I have an image I(x,y) having RGB values

RGB(I(x,y)) = {R,G,B}

After I upload the data using glTexImage2D, I want to change RGB values
according to equation

RGBnew = {R,G,B}* scale + bias;

Note that openGL/GPU automatically clamps the new color values from (0 to 1) prior to display. This is effectively increasing the contrast.


Hi Ingrater,

If I wrote a shader then yes this operation is simple but I am hoping to use the fixed pipeline and hack this one operation.

The reason is that I am really using this to display a 3D volume, using the glBlendFunc in my bigger scheme. What I am doing is using texture env to scale and bias my data before my texture gets applied to the quad. Then draw each slice of 3D data as a quad. Then glBlendFunc is doing the blending to visualize my 3D volume.

Since I donot want to move the data from CPU memory to GPU memory repeatedly, I want to use something in the pipe after loading the data to GPU memory to scale and bias my data.

I read that I could use glCopyTexImage2D can be used, but I donot know how that would work.

Thank you soo much for any suggestions.

-nsn


I wanted to add the comment for the equation

RGBnew = {R,G,B}* scale + bias;

Here the problem I having is that scale is being used as a color and it is being clamped to the range (0,1) becaue it is represented as color.

Can I do something clever using alpha value. If so how can I do that.

effectively I am thinking if I can use alpha to scale my colors like

RGBnew = {R,G,B}/(alpha) + bias;

where my alpha = 1/scale. Then I have achieved what I want.

thanks,

nsn

This topic is closed to new replies.

Advertisement