动态更改GLSL着色器中的参数值 [英] Changing the parameter value in GLSL shader dynamically

查看:610
本文介绍了动态更改GLSL着色器中的参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



是否可以在运行时动态发送片段着色器的gl_FragColor.a值。例如,用户在文本框中输入alpha值,该值将分配给gl_FragColor.a变量;



我的片段着色器代码是



void main()

{

gl_FragColor = gl_Color;

gl_FragColor.a = 0.2; //这里是静态分配的,但是我需要将它分配给

//给定文本框的用户值。可能吗??



}



感谢您的帮助。

Arpan

Hello,

Is it possible to send the gl_FragColor.a value for fragment shader dynamically on the runtime. say for example, user input the alpha value in a text box and the value will be assigned to the gl_FragColor.a variable;

my fragment shader code is

void main()
{
gl_FragColor = gl_Color;
gl_FragColor.a = 0.2; // here it is assigned statically but i need to assign it so
// value from the user given textbox. Is it possible??

}

Thank you for your help.
Arpan

推荐答案

请在你的glsl着色器程序中添加一个统一参数。

然后使用glGetUniformLocation将值设置为此参数[将参数id设置为cpp程序]和glUniform [改变参数的值]



Please add a uniform parameter in your glsl shader program.
Then set value to this parameter using glGetUniformLocation[To get the parameter id to cpp program] and glUniform [To change the value of parameter]

// This value can be changed before rendering a frame.
// Whenever you need to change you can change value of this parameter
uniform float fAlphaValueByUser_i;

void main()
{
  gl_FragColor = gl_Color;
  gl_FragColor.a = fAlphaValueByUser_i; // Change output alpha color with input float value
}







// cpp code.
GLint nAlphaLocation = glGetUniformLocation( m_ProgramID, "fAlphaValueByUser_i" );
glUniform1f( nAlphaLocation, fAlphaValueFromGUI );


我发现在计算顶点着色器中的位置时遇到了一些错误。它适用于Santhosh G_方法。
I found out that I had some mistake in calculating position in vertex shader. It works fine accroding to Santhosh G_ method.


这篇关于动态更改GLSL着色器中的参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆