【问题标题】:Processing output nothing using gouraud shading使用 gouraud 着色处理不输出任何内容
【发布时间】:2020-01-23 11:20:31
【问题描述】:

对不起,我是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和立方体消失了。没有显示!

【问题讨论】:

    标签: glsl processing shader gouraud


    【解决方案1】:

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

    varying vec4 color;
    

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

    gl_Position = projectionMatrix * modelview * position; 
    

    缺少vN 的类型规范,ambientdiffusespec 的类型是vec4 而不是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;
    }
    

    片段着色器:

    varying vec4 color;
    
    void main()
    {
        gl_FragColor = color;
    }
    

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

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

    如果您想使用超过 1 个光源,则必须在顶点着色器中为 lightPositionlightAmbientlightDiffuselightSpecular 使用统一数组。见Types of shaders in Processing(https://processing.org/tutorials/pshader/)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多