2

对不起,我是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 着色后 在古洛之后

处理加载 obj 并绘制一个立方体并应用 gouraud 着色器,但之后只显示背景,加载了 obj 并且立方体消失了。没有显示!

4

1 回答 1

1

着色器甚至不编译和链接。顶点着色器有 1 个varying输出 ( color),因此帧着色器需要输入varying vec4 color;

varying vec4 color;

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

gl_Position = projectionMatrix * modelview * position; 

缺少 和 的类型规范,v以及和的类型,而不是。 Nambientdiffusespecvec4vec3

顶点着色器:

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()spotLight(),。
但请注意,您的着色器只能处理 1 个光源。更多的 1 光源将获得

OpenGL 错误 1282 在 top endDraw(): 无效操作

如果要使用超过 1 个光源,则必须在顶点着色器中为lightPositionlightAmbientlightDiffuse和使用统一数组lightSpecular。请参阅处理中的着色器类型( https://processing.org/tutorials/pshader/ )

于 2020-01-23T17:40:47.467 回答