0

大家好,有人可以帮我将 glsl 转换为 c# 吗?我是 glsl 的新手,我真的需要很多帮助!很高兴感谢您的所有帮助!:)

#version 120

uniform sampler2D tex;

void main()
{
 vec4 pixcol = texture2D(tex, gl_TexCoord[0].xy);
 vec4 colors[3];
 colors[0] = vec4(0.,0.,1.,1.);
 colors[1] = vec4(1.,1.,0.,1.);
 colors[2] = vec4(1.,0.,0.,1.);
 float lum = (pixcol.r+pixcol.g+pixcol.b)/3.;
 int ix = (lum < 0.5)? 0:1;
 vec4 thermal = mix(colors[ix],colors[ix+1],(lum-float(ix)*0.5)/0.5);
 gl_FragColor = thermal;
}
4

2 回答 2

3

您无需将 GLSL 转换为 c# 即可使用它,因为 OpenGL API 直接使用它。c# 有几个 OpenGl 包装器,不确定是否都支持着色器,但 openTk 肯定支持,示例在这里:

using (StreamReader sr = new StreamReader("vertex_shader.glsl"))
{
    GL.ShaderSource(m_shader_handle, sr.ReadToEnd());
}

您可以从文件加载着色器,也可以直接从字符串加载:

string shader = "void main() { // your shader code }"
GL.ShaderSource(m_shader_handle, shader);
于 2013-10-03T07:01:34.863 回答
0

与您的参考代码类似的 c# 代码是(伪代码)

//Declares the texture/picture i.e.  uniform sampler2D tex;
Bitmap x = new Bitmap();

float[] main()
{
    // Looks up the color of a pixel for the specified coordinates, the color (as a rule of thumb) 
    // a normalized value i.e. vec4 pixcol = texture2D(tex, gl_TexCoord[0].xy);
    Color pixcolUnnormalized=  x.GetPixel();
    float[] pixcol = new float[] { pixcolUnnormalized.r / 255.0f, pixcolUnnormalized.g / 255.0f, pixcolUnnormalized.b / 255.0f, pixcolUnnormalized.a / 255.0f);

    //Setup coefficients  i.e.  vec4 colors[3];  colors[0] = vec4(0.,0.,1.,1.);  colors[1] = vec4(1.,1.,0.,1.);  colors[2] = vec4(1.,0.,0.,1.);     
    float[] color1[] = new float[] { 0.0,0.0,1.0,1.0 };
    float[] color2[] = new float[] { 0.0,0.0,1.0,1.0 };
    float[] color3[] = new float[] { 0.0,0.0,1.0,1.0 };
    float[ float[] ] colors = new float[] { colors1, colors2, colors3 };

    ///Obtain luminance value from the pixel. 
    float lum = (pixcol[0]+pixcol[1] +pixcol[2])/3.;
    int ix = (lum < 0.5)? 0:1;

    //Interpolate the color values i.e.         vec4 thermal = mix(colors[ix],colors[ix+1],(lum-float(ix)*0.5)/0.5);
    float[] thermal = new float[] {
          colors[ix][0] * ( 1 -  (lum-float(ix)*0.5)/0.5)  )  + colors[ix + 1][0] * (lum-float(ix)*0.5)/0.5)
          colors[ix][1] * ( 1 -  (lum-float(ix)*0.5)/0.5)  )  + colors[ix + 1][1] * (lum-float(ix)*0.5)/0.5)
          colors[ix][2] * ( 1 -  (lum-float(ix)*0.5)/0.5)  )  + colors[ix + 1][2] * (lum-float(ix)*0.5)/0.5)
          colors[ix][3] * ( 1 -  (lum-float(ix)*0.5)/0.5)  )  + colors[ix + 1][3] * (lum-float(ix)*0.5)/0.5)
    };

    //return the value
    return thermal;
}
于 2013-10-03T07:33:43.780 回答