PDA

View Full Version : How to sum up textures in OpenGL shader code?



SirPaulMuaddib
03-21-2014, 12:02 PM
I'm using SharpGL in C#......


I did have a working shader for WPF app that uses FX file....here is the file (see bottom of post)
What I do is have one main Image (c# image) and sum it with 7 other Images. (I do this because camera data images are faint and I have to add them up for better quality final image)


I need to do this in C# as well, but with SharpGL


My current code is basic OpenGL shader stuff, (see C# SharpGL shader below)


I need to modify fragmentShader to take 7 sampler2D images and sum them up to current texture.


I don't know how to pass in the Texures (OpenGL Textures) to shader, and add them up, the OpenGL way, like I do in my original Shader code.




//--------------------------------------------------
// C# SharpGL shader
//--------------------------------------------------
// Create a vertex shader.
VertexShader vertexShader = new VertexShader();
vertexShader.CreateInContext(gl);
vertexShader.SetSource(
"void main()" + Environment.NewLine +
"{" + Environment.NewLine +
"gl_Position = ftransform();" + Environment.NewLine +
"}" + Environment.NewLine);


// Create a fragment shader.
FragmentShader fragmentShader = new FragmentShader();
fragmentShader.CreateInContext(gl);
fragmentShader.SetSource(
"void main()" + Environment.NewLine +
"{" + Environment.NewLine +
"gl_FragColor = vec4(0.4,0.4,0.8,1.0);" + Environment.NewLine +
"}" + Environment.NewLine);


// Compile them both.
vertexShader.Compile();
fragmentShader.Compile();
//--------------------------------------------------



//--------------------------------------------------------------------------------
// ORIGINAL c# SHADER CODE THAT USES fx files (non-OpenGL version)
// SUM UP main texture with 7 other textures.


sampler2D Input : register(s0); // main image
sampler2D AddImage1 : register(s1); // add image 1
sampler2D AddImage2 : register(s2); // add image 2
sampler2D AddImage3 : register(s3); // add image 3
sampler2D AddImage4 : register(s4); // add image 4
sampler2D AddImage5 : register(s5); // add image 5
sampler2D AddImage6 : register(s6); // add image 6
sampler2D AddImage7 : register(s7); // add image 7

float4 main(float2 uv : TEXCOORD) : COLOR
{
float4 g = tex2D(Input, uv);
//float high = g.g * 65535;
float low = g.b ;
//float grayValMain = high + low;



float4 g1 = tex2D(AddImage1, uv);
//float high1 = g1.g * 65535;
float low1 = g1.b ;
//float grayValAdd1 = high1 + low1;


float4 g2 = tex2D(AddImage2, uv);
//float high2 = g2.g * 65535;
float low2 = g2.b ;
//float grayValAdd2 = high2 + low2;


float4 g3 = tex2D(AddImage3, uv);
//float high3 = g3.g * 65535;
float low3 = g3.b ;
//float grayValAdd3 = high3 + low3;


float4 g4 = tex2D(AddImage4, uv);
//float high4 = g4.g * 65535;
float low4 = g4.b ;
//float grayValAdd4 = high4 + low4;


float4 g5 = tex2D(AddImage5, uv);
//float high5 = g5.g * 65535;
float low5 = g5.b ;
//float grayValAdd5 = high5 + low5;


float4 g6 = tex2D(AddImage6, uv);
//float high6 = g6.g * 65535;
float low6 = g6.b ;
//float grayValAdd6 = high6 + low6;


float4 g7 = tex2D(AddImage7, uv);
//float high7 = g7.g * 65535;
float low7 = g7.b ;
//float grayValAdd7 = high7 + low7;





//float outGrayVal = clamp(1.0 - ((1.0-low)+(1.0-low1)+(1.0-low2)+(1.0-low3)+(1.0-low4)+(1.0-low5)+(1.0-low6)+(1.0-low7)), 0, 1.0);
float outGrayVal = clamp(low+low1+low2+low3+low4+low5+low6+low7, 0, 1.0);
//float outGrayVal = low2;


float4 result;
result.a = g.a;
result.r = outGrayVal;
result.g = outGrayVal;
result.b = outGrayVal;
return result;
}
//--------------------------------------------------------------------------------

reto.koradi
03-30-2014, 10:42 PM
Should work very similarly in GLSL. You can declare a sampler2D uniform variable in the fragment shader for each of your n textures. Then sample each of the textures, and add up the values.

In your C/C++ code, bind your textures to texture units 0 to n, and set the values of the uniform variables to the index of the texture units.

If your number of textures is potentially variable, or might get larger, it would probably be more elegant to use texture arrays instead of separate textures.