使用gouraud底纹处理任何输出

时间:2020-01-23 11:20:31

标签: glsl processing shader gouraud

对不起,我是opengl es和Processing的新手 下面的处理和着色器仅输出背景

Phader 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;
}

请帮助! 涂抹古拉乌德底纹之前 enter image description here

应用gouraud阴影后 after Gouraud

在处理过程中加载obj并绘制一个立方体并应用gouraud着色器,但是在仅显示backgroud之后,加载的obj消失了。什么都没显示!

1 个答案:

答案 0 :(得分:1)

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

varying vec4 color;

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

gl_Position = projectionMatrix * modelview * position; 

缺少vN的类型规范,并且ambientdiffusespec的类型为vec4而不是{ {1}}。

顶点着色器:

vec3

片段着色器:

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;
}

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

在endDraw()顶部出现OpenGL错误1282:无效操作

如果要使用多个光源,则必须在varying vec4 color; void main() { gl_FragColor = color; } lightPositionlightAmbientlightDiffuse的顶点着色器中使用统一的数组。参见Types of shaders in Processinghttps://processing.org/tutorials/pshader/

相关问题