Passing data into different shaders

痞子三分冷 提交于 2019-12-24 15:58:55

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!