使用 gouraud 着色处理无输出

Processing output nothing using gouraud shading

抱歉,我是 opengl es 和处理方面的新手 下面的处理和着色器仅输出背景

PShader Gouraud,Phong;

rocket = loadShape("rocket.obj");
rocket.setFill(color(800, 0, 0));
Gouraud= loadShader("gouraudfragment.glsl","gouraudvertex.glsl");
Phong= loadShader("phongfragment.glsl","phongvertex.glsl");
background(0);

pushMatrix();
shader(Gouraud);
translate(130,height/2.0);
rotateY(rc);
rotateX(0.4);
noStroke();
fill(#800080);
box(100);

rc+=(0.02+speedCube);
rc*=dirCube;
popMatrix();

pushMatrix();
shader(Gouraud);
translate(width/2, height/2 + 100, -200);
rotateZ(PI);
rotateY(rr);
shape(rocket,100,100);

rr +=( 0.02+speedRocket);
rr*=dirRocket;
popMatrix();

顶点着色器

varying vec3 N;
varying vec3 v;
varying vec4 diffuse;
varying vec4 spec;
attribute vec4 position;
attribute vec3 normal;
uniform mat4 modelview;
uniform mat4 projectionMatrix;
uniform mat3 normalMatrix;
uniform vec4 lightPosition;
uniform vec3 lightAmbient;
uniform vec3 lightDiffuse;
uniform vec3 lightSpecular;
uniform float SpecularPower;

void main()
{
   vec4 diffuse;
   vec4 spec;
   vec4 ambient;

   v = vec3(modelview * position);
   N = normalize(normalMatrix * normal);
   gl_Position = projectionMatrix * position;  

   vec3 L = normalize(lightPosition.xyz - v);
   vec3 E = normalize(-v);
   vec3 R = normalize(reflect(-L,N)); 

   ambient = vec4(lightAmbient,100.0);
   diffuse = vec4(clamp( lightDiffuse * max(dot(N,L), 0.0)  , 0.0, 1.0 ) ,100.0);
   spec = vec4(clamp (lightSpecular * pow(max(dot(R,E),0.0),0.3*SpecularPower) , 0.0, 1.0 ),100.0);

   color = ambient + diffuse + spec;
}

片段着色器

void main()
{
    gl_FragColor = color;
}

请帮忙! 在应用 gouraud 阴影之前

应用 gouraud 阴影后

处理加载对象并绘制立方体并应用 gouraud 着色器,但之后仅显示背景,加载对象和立方体消失。没有显示!

着色器甚至无法编译 link。顶点着色器有 1 varying 个输出 (color),因此 framgent 着色器需要输入 varying vec4 color;

varying vec4 color;

设置剪辑space位置时,顶点坐标必须通过模型视图和投影矩阵进行变换:

gl_Position = projectionMatrix * modelview * position; 

缺少 vN 的类型规范,ambientdiffusespec 的类型是 vec4vec3

顶点着色器:

attribute vec4 position;
attribute vec3 normal;

varying vec4 color;

uniform mat4 modelview;
uniform mat4 projectionMatrix;
uniform mat3 normalMatrix;
uniform vec4 lightPosition;
uniform vec3 lightAmbient;
uniform vec3 lightDiffuse;
uniform vec3 lightSpecular;
uniform float SpecularPower;

void main()
{
   vec3 v      = vec3(modelview * position);
   vec3 N      = normalize(normalMatrix * normal);
   gl_Position = projectionMatrix * modelview * position;  

   vec3 L = normalize(lightPosition.xyz - v);
   vec3 E = normalize(-v);
   vec3 R = normalize(reflect(-L,N)); 

   vec4 ambient = vec4(lightAmbient,100.0);
   vec4 diffuse = vec4(clamp( lightDiffuse * max(dot(N,L), 0.0)  , 0.0, 1.0 ) ,100.0);
   vec4 spec = vec4(clamp (lightSpecular * pow(max(dot(R,E),0.0),0.3*SpecularPower) , 0.0, 1.0 ),100.0);

   color = ambient + diffuse + spec;
}

片段着色器:

varying vec4 color;

void main()
{
    gl_FragColor = color;
}

当然你至少要设置一个环境光源ambientLight()。 您也可以使用 directionalLight(), pointLight() or spotLight()
但请注意,您的着色器只能处理 1 个光源。更多 1 个光源将获得

OpenGL error 1282 at top endDraw(): invalid operation

如果您想使用 1 个以上的光源,那么您必须在 lightPositionlightAmbientlightDiffuse 和 [=31] 的顶点着色器中使用统一数组=].参见 Types of shaders in Processing(https://processing.org/tutorials/pshader/)