简体   繁体   中英

Fragment shader not creating gradient like light in OpenGL GLSL

I am trying to understand how to manipulate my renderings with shaders. I haven't changed the projection matrix of the scene but I draw a triangle with vertices = {-0.5, -0.5} {0.5, -0.5} {0, 0.5}. I then pass in a vec2 position of a "light" to the uniform of my fragment shader that i want to essentially shine onto my triangle from the top right of the triangle (lightPos = (0.5,0.5))

Here is a very bad drawing of where everything is located.

我用油漆做的可怕图像

and this is what I aim to have in my triangle (kind of.. it doesnt need to be white to blue it just needs to be brighter near the light and darker further away)

示例图片

Here is the shader

#version 460 core

in vec3 oPos;
out vec4 fragColor;

uniform vec3 u_outputColor;
uniform vec2 u_lightPosition;

void main(){

float intensity =  1 / length(oPos.xy - u_lightPosition);
vec4 col = vec4(u_outputColor, 1.0f);
fragColor = col * intensity; 
} 

Here is the basic code to compiling the shader(most of it is abstracted away so it is fairly simple)

/* Test data for shader program. */
program.init("passthrough.vert", "passthrough.frag");
program.setUniformVec3("u_outputColor", 0.3f, 0.3f, 0.8f);
program.setUniformVec2("u_lightPosition", 0.5f, 0.5f);



GLfloat vertices[9] = {-0.5f, -0.5, 0,    0,0.5f,0,    0.5, -0.5, 0};

Here is vertex shader:

#version 460 core

layout (location = 0) in vec3 aPos;
out vec3 oPos;

void main(){
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

Every single test I have run to see why I can't get this to work seems to show me that if there is a slight color change it will change the entire triangle to a different shade. All tests show a triangle of ONE color across the entire thing; no gradient at all. I want the triangle to be a gradient that is brighter near the light and darker further from it. This is driving me crazy because I have been stuck on such a simple thing for 3 hours now and it just seems like any code I write modifies all 3 vertices at once as if they are in the exact same spot. I wrote the math out and I strongly feel as if this should work. Any help is very appreciated.

EDIT

The triangle after the solution fixed my issue:

在此处输入图像描述

Try this for your vertex shader:

#version 460 core

layout (location = 0) in vec3 aPos;
out vec3 oPos;

void main(){
    oPos.xyz  = aPos.xyz; // ADD THIS LINE
    gl_Position = vec4(aPos.xyz, 1.0);
}

Your version never writes to oPos , so the fragment shader gets either a) a random value or, in your case b) vec3(0,0,0) . Since your color calculation is based off of:

float intensity =  1 / length(oPos.xy - u_lightPosition);

This is basically the same as

float intensity =  1 / length(-1*u_lightPosition);

So the color only depends on the light position.

You can debug and verify this by setting your fragment color to oPos :

vec4 col = vec4(oPos.xy, oPos.z + 0.5, 1.0f);

If oPos was set correctly in the vertex shader, then this line in the fragment shader would show you an RGB ramp. If oPos is not set correctly, you'll see 50% blue.

Always check for errors and logs returned from OpenGL. It should have emitted a warning about this that would have sent you straight to the problem.

Also, I'm surprised that your entire triangle isn't being clipped since vertices have a z of 0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM