0
\$\begingroup\$

I'm following a tutorial and I want to clamp all position values of a triangle between 0 and 1 then output them as a color

this is the code for my vertex shader:

#version 330                                        
                                                    
layout (location = 0) in vec3 pos;                  
                                                   
out vec4 vCol;                                      
                                                   
uniform mat4 model;                                
                                                   
void main(){                                       
    gl_Position = model * vec4(pos, 1.0);           
    vCol = vec4(clamp(pos, 0.0f, 1.0f) 1.0f);       
}

and this is the code for my fragment shader:

#version 330                                        
                                                   
in vec4 vCol;                                      
                                                    
out vec4 color;                                    
                                                    
void main(){                                        
    color = vCol;                                   
}

these are coordinates of my shape, which is a triangle (the values that are going to be clamped):

GLfloat vertices[] = {
    -1.0f, -1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    0.0f, 1.0f, 0.0f
};

result I get is this (completely black): enter image description here

result I should get is this: enter image description here

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Turns out I had just forgot to put a comma in vertex shader. this works now:

vCol = vec4(clamp(pos, 0.0f, 1.0f), 1.0f);

in my case this was the only problem, however, there might be other factors that are causing this issue such as wrong Identity Matrix being applied to the "model" uniform

here is a more in-depth explanation of this along with some other reasons that may cause this issue.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Thats the reason why we handle errors. The compile errors for your shaders would've pointed out the problem immediately. \$\endgroup\$
    – tkausl
    Commented Jul 21, 2022 at 9:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .