PassThrough几何着色器不起作用

PassThrough几何着色器不起作用

本文介绍了PassThrough几何着色器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用FragmentShader和VertexShader,并且工作得很好.我无法使几何着色器正常工作.我绝对是新手,以下是我尝试过的方法.

I am using FragmentShader and VertexShader at present, and works absolutely fine. I cannot get my geometry shader working. I am absolutely new to it, below is what I have tried.

我正在使用VBO,照明和纹理以及一些几何图形,但是在使用GeometryShader之前效果很好.我唯一更改的是变量名,因为我必须在几何着色器中获取输入并提供输出.因此,我在那些变量名的末尾附加了1,这些变量将从几何着色器过渡到片段着色器.

I am using VBO, lighting and textures along with some geometry, but it works fine before using GeometryShader. the only thing I have changed is the variable names as I had to get the input in the geometry shader and give the output. So I have appended 1 at the end of those variable names those which will go out from geometry shader to the fragment shader.

我还添加了以#开头的标头,这些标头早先不存在.我正在使用GL_TRIANGLES进行绘制.

Also I have added headers starting with # which were earlier not there. I am using GL_TRIANGLES to draw.

VertexShader

VertexShader

in vec4 position;
in vec4 color1;
in vec4 normal;
in vec2 texCoord;

uniform sampler2D Tex1;

uniform int use_texture;

out vec4 pcolor;

out vec3 N;
out vec3 L;
out vec3 R;
out vec3 V;


uniform mat4 local2clip;
uniform mat4 local2eye;
uniform mat4 normal_matrix;
uniform mat4 world2eye;

uniform vec4 light_ambient;
uniform vec4 light_diffuse;
uniform vec4 light_specular;
uniform vec4 light_pos;
#version 330 compatibility

uniform vec4 mat_ambient;
uniform vec4 mat_diffuse;
uniform vec4 mat_specular;
uniform float mat_shine;

//varying vec3 v_normal;  // vertex normal
out vec4 v_color;   // vertex color
out vec4 pos_in_eye;  //vertex position in eye space
out vec2 FtexCoord;

void main(){

       gl_Position = local2clip * position;

       N =     normalize(vec3(normal_matrix * normal));    //v_normal
       vec4 Lpos =  world2eye * light_pos;                  //light pos. in eye
       vec4 Vpos =  local2eye * position;                   //pos_in_eye
       L = normalize(vec3(Lpos - Vpos));                    //light_vector

       R = normalize(reflect(-L, N));
       V = normalize(vec3(-Vpos));                          //eye vector

       vec3 halfv = normalize(L+V);
       FtexCoord = texCoord;

       //pcolor = color1;
}

这是我的FragemntShader

This is my FragemntShader

#version 330 compatibility

uniform int use_texture;

in vec4 pcolor;

in vec3 N1;
in vec3 L1;
in vec3 R1;
in vec3 V1;


uniform mat4 local2clip;
uniform mat4 local2eye;
uniform mat4 normal_matrix;
uniform mat4 world2eye;

uniform vec4 light_ambient;
uniform vec4 light_diffuse;
uniform vec4 light_specular;
uniform vec4 light_pos;

uniform vec4 mat_ambient;
uniform vec4 mat_diffuse;
uniform vec4 mat_specular;
uniform float mat_shine;

uniform sampler2D Tex1;
in vec2 FtexCoord1;

void main() {

       vec4 ambient = light_ambient * mat_ambient;
       float NdotL;
       if (dot(N1,L1) <0.0) NdotL = 0.0;
       else NdotL = dot(N1, L1);

       vec4 diffuse = light_diffuse * mat_diffuse * NdotL;

       float RdotV;
       RdotV = dot(R1, V1);

       if (NdotL == 0.0) RdotV = 0.0;
       if (RdotV <0.0) RdotV = 0.0;

       vec4 specular = light_specular * mat_specular * pow(RdotV,mat_shine);

       vec4 texcolor;


       if( use_texture == 1 ) {
          texcolor = texture2D(Tex1, FtexCoord1);
          gl_FragColor = texcolor;
       }
       else
          gl_FragColor = (diffuse + ambient + specular);
 }

这是我的GeometryShader

This is my GeometryShader

#version 330

layout (triangles) in;
layout (triangles) out;
layout (max_vertices = 3) out;

out vec3 N1;
out vec3 L1;
out vec3 R1;
out vec3 V1;

in vec3 N;
in vec3 L;
in vec3 R;
in vec3 V;

uniform mat4 local2clip;
uniform mat4 local2eye;
uniform mat4 normal_matrix;
uniform mat4 world2eye;

uniform vec4 light_ambient;
uniform vec4 light_diffuse;
uniform vec4 light_specular;
uniform vec4 light_pos;

uniform vec4 mat_ambient;
uniform vec4 mat_diffuse;
uniform vec4 mat_specular;
uniform float mat_shine;

//varying vec3 v_normal;  // vertex normal
out vec4 v_color1;  // vertex color
out vec4 pos_in_eye1;  //vertex position in eye space
out vec2 FtexCoord1;

in vec4 v_color;    // vertex color
in vec4 pos_in_eye;  //vertex position in eye space
in vec2 FtexCoord;

void main(void)
{
    int i;
        N1=N;
        L1=L;
        R1=R;
        V1=R;

        FtexCoord1=FtexCoord;
        v_color1=v_color;
        pos_in_eye1=pos_in_eye;



    for (i = 0; i < gl_in.length(); i++)
    {


        gl_Position = gl_in[i].gl_Position;
        EmitVertex();
    }
    EndPrimitive();
}

我只希望以前存在的内容通过几何着色器从顶点着色器传递到片段着色器,以便以后可以使用该着色器.当前屏幕只有黑色

I just want that what ever was there earlier is passed from vertex shader to fragment shader via geometry shader, so that I can manipulate the shader later. Currently the screen is just black

推荐答案

问题的核心是,在构建几何着色器"时,您不必费心检查编译错误.我知道这是因为我看到了一些语法错误.特别是:

The core of your problem is that you didn't bother to check for compilation errors when you built your Geometry Shader. I know that because I see several syntax errors for it. In particular:

in vec3 N;
in vec3 L;
in vec3 R;
in vec3 V;

in vec4 v_color;    // vertex color
in vec4 pos_in_eye;  //vertex position in eye space
in vec2 FtexCoord;

几何着色器输入总是 聚集到数组中.请记住:几何体着色器在 基元 上运行,这些基元定义为一个或多个顶点的集合.因此,每个GS调用都会获得一组每个顶点的输入值,每个原始layout in限定符定义的类型.

Geometry Shader inputs are always aggregated into arrays. Remember: a geometry shader operates on primitives, which are defined as a collection of one or more vertices. Each GS invocation therefore gets a set of per-vertex input values, one for each vertex in the primitive type defined by your layout in qualifier.

注意如何遍历图元中的顶点数量,并使用gl_in[i]获取图元中每个顶点的输入值.这就是您需要访问几何着色器"输入的所有 的方式.并且您需要将每个变量写入其相应的输出变量,然后调用EmitVertex.都在那个循环中.

Notice how you loop over the number of vertices in a primitive and use gl_in[i] to get the input value for each vertex in the primitive. That's how you need to access all of your Geometry Shader inputs. And you need to write each one to its corresponding output variable, then call EmitVertex. All in that loop.

这篇关于PassThrough几何着色器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:15