Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Posted
Updated 21-Jun-13 2:09am
v2
Comments
[no name] 21-Jun-13 8:16am    
"Is it possible?"... yes, yes it is possible.
amsainju 21-Jun-13 8:23am    
ohh... so Everytime a User change a value the shader will receive the value and render accrodingly?? or it will just be a 1 time effect. can u please explain how..
Thank you
[no name] 21-Jun-13 8:27am    
That is going to greatly depend on how you wrote your code to accomplish this task. If you want the color to change when the user changes the text in the textbox then change the color in the text changed event.

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]

C++
// 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
}


C++
// cpp code.
GLint nAlphaLocation = glGetUniformLocation( m_ProgramID, "fAlphaValueByUser_i" );
glUniform1f( nAlphaLocation, fAlphaValueFromGUI );
 
Share this answer
 
Comments
amsainju 21-Jun-13 13:35pm    
Thank you I tried the same thing but its not working for me while testing I found that nAlphaLocation get the value -1. m_ProgamID is getting value 1. should m_ProgramID be the one which is assigned the return of glCreateProgram(); I am using the same thing but can't figure out why it is not working for me
Santhosh G_ 21-Jun-13 13:42pm    
Please ensure parameter name and the referenced name are same.

http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml

This function returns -1 if name does not correspond to an active uniform variable in program, if name starts with the reserved prefix "gl_", or if name is associated with an atomic counter or a named uniform block.

Please ensure glUseProgram called before calling glGetUniformLocation.

http://www.codeproject.com/Articles/89337/GLSL-Shader-for-Interpolating-Two-Textures
Implementation of simple shader to interpolate two textures with the input value from user. Please refer GLSLShader.cpp and GLSLShader.h.
Santhosh G_ 21-Jun-13 14:02pm    
Please ensure glUseProgram called before calling glGetUniformLocation.

http://www.codeproject.com/Articles/89337/GLSL-Shader-for-Interpolating-Two-Textures
Implementation of simple shader to interpolate two textures with the input value from user. Please refer GLSLShader.cpp and GLSLShader.h.
amsainju 22-Jun-13 9:27am    
Thank you alot for your help. I will check into it.. thank you
Soham Railkar 24-Jan-18 7:07am    
Thanks for this info. it was really helpful,
now I have set of shades, I want to change them at runtime. Do you have any idea about the implementation of the same
I found out that I had some mistake in calculating position in vertex shader. It works fine accroding to Santhosh G_ method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900