问题
Lets say my Shaderprogram contains a Vertex- and a Fragmentshader.
How can i pass information straight to the fragmentshader?
When i Use gl_uniform... and specifiy the varaible I want to adress inside the fragmet shader it throws me an error like this:
Using ShaderProgram: The fragment shader uses varying myVarHere, but previous
shader doesnot write to it.
Since I attach the vertexshader first, I found out that I need to "pass the information through" the vertex shader using in and out.
Im not sure if this is the way to go, so my question: Is there a way to tell opengl to adress the variable in a certain shader or am I doing / understanding something horribly wrong?
VertexShader:
#version 130
precision highp float;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
in vec3 vertexData;
in vec3 normalData;
out vec3 normal;
void main(void)
{
normal = (model * view * vec4(normalData, 0)).xyz;
gl_Position = projection * model * view * vec4(vertexData, 1);
}
Fragment:
#version 130
precision highp float;
in vec3 light0; // <- this is what i want to fill with data
const vec3 ambient = vec3(0.0, 0.0, 0.0);
const vec3 lightColor = vec3(0.6, 0.0, 0.0);
in vec3 normal;
out vec4 out_frag_color;
void main(void)
{
float diffuse = clamp(dot(normalize(light0), normalize(normal)), 0.5, 1.0);
out_frag_color = vec4(ambient + diffuse * lightColor, 1.0);
}
回答1:
It seems like you want light0
to be a uniform.
Just make it a uniform:
#version 130
precision highp float;
uniform vec3 light0;
...
来源:https://stackoverflow.com/questions/14071290/passing-data-into-different-shaders