Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

75
Views
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 ?

5 months ago · Juan Pablo Isaza
1 answers
Answer question

0

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;
} 
5 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs