Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

if else condition is not working in opengl glsl shading language

I am using this code on glsl script

but when i use if else condition it not works , every time i run script it choose the first value of color variable which is declared before if else condition.

float color = 0.3 * clr.r + 0.59 * clr.g + 0.11 * clr.b; // script always choose this value instead of if else values

float maxrgb = max(max(clr.r, clr.g), clr.b);

if (maxrgb < 128.0) {
  float color = 0.21 * clr.r + 0.71 * clr.g + 0.80 * clr.b;
} else if (maxrgb > 128.0) {
  float color = 0.30 * clr.r + 0.60 * clr.g + 0.82 * clr.b;
} // the values in the condition are not working.

is am i doing something wrong ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have declared the color variable twice. You have declared a new variable with the same name in scope of the if-statement. Declare the variable before the if statement, but assign new values to the existing variable in the statement:

float color = 0.3 * clr.r + 0.59 * clr.g + 0.11 * clr.b; 

float maxrgb = max(max(clr.r, clr.g), clr.b);

if (maxrgb < 128.0) {
    color = 0.21 * clr.r + 0.71 * clr.g + 0.80 * clr.b;
} else if (maxrgb > 128.0) {
    color = 0.30 * clr.r + 0.60 * clr.g + 0.82 * clr.b;
} 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading