简体   繁体   English

进出顶点着色器?

[英]Vertex shaders in and out?

I've got two shaders like this: 我有两个这样的着色器:

const char* vertexShaderData =
"#version 450 \n" 
"in vec3 vp;"
"in vec3 color;\n"
"out vec3 Color;\n"
"void main(){"
"Color=color;"
"gl_Position = vec4(vp, 1.0);" 
"}";

const char* fragShaderData =
"#version 410\n"
"uniform vec4 incolor;\n"
"in vec3 Color;"
"out vec4 outColor;"
"void main(){"
"outColor = vec4(Color, 1.0);"
"}";

I understand that each shader is called for each vertex. 我知道每个顶点都会调用每个着色器。

  1. Where do the in paremters in my vertexShaderData get their values? 我的vertexShaderData in in vertexShaderDatavertexShaderData获取它们的值? In no point in the code do I specify what vp is or what color is. 在代码中,我都没有指定vp是什么或颜色是什么。 In the second shader, I get that the in value comes from the first shader's out value. 在第二个着色器,我得到了in值来自第一着色器的out价值。 But where do thoes inital in s come from? 但是,最初in是从哪里来的呢?
  2. About the out value of the fragShaderData : How is this value used? 关于fragShaderDataout值:该值如何使用? In other words, how does OpenGL know that this is an RGB color value and know to paint the triangle with this color? 换句话说,OpenGL如何知道这是RGB颜色值并知道用此颜色绘制三角形?
  1. You feed the data to the vertex shader from your OpenGL calls (in CPU). 您可以从OpenGL调用(在CPU中)将数据提供给顶点着色器。 Once you compiled the program (vertex shader + fragment shader), you feed the vertex you want. 编译程序(顶点着色器+片段着色器)后,即可输入所需的顶点。

  2. Different than the vertex shader, this fragment shader will run for once for EVERY pixel inside the triangle you are rendering. 与顶点着色器不同,此片段着色器将为要渲染的三角形内的每个像素运行一次。 The outColor will be a vec4 (R,G,B,A) that "goes to your framebuffer". outColor将是vec4(R,G,B,A),“进入您的帧缓冲区”。 About the color, in theory, this is abstract for OpenGL. 关于颜色,从理论上讲,这对于OpenGL是抽象的。 They are called RGBA for convenience... you can even access the same data as XYZW (it's an alias for RGBA). 为了方便起见,它们被称为RGBA ...您甚至可以访问与XYZW相同的数据(这是RGBA的别名)。 OpenGL will output NUMBERS to the framebuffer you desire (according the rules of color attachments, etc). OpenGL将输出NUMBERS到所需的帧缓冲区(根据颜色附件的规则等)。 In fact you will have 4 channels THAT BY THE WAY are used in the monitor to output RGB (and A used for transparency).... In other words, you can used GL programs to create triangles that will output 1 channel, or 2 channels, depending on your needs, and these channels can mean anything you need. 实际上,您将在监视器中使用4个通道来输出RGB(A用于透明度)。换句话说,您可以使用GL程序来创建将输出1个通道或2个通道的三角形。渠道,取决于您的需求,这些渠道可能意味着您需要的任何东西。 For example, you can interpolate and YUV image, or a UV plane (2 channels). 例如,您可以插值和YUV图像或UV平面(2个通道)。 If you output these to monitor, you won't have the colors correct, once the monitor is expecting RGB, but the OpenGL concept is abroader than RGB. 如果将这些输出到显示器,则一旦显示器期望使用RGB,就不会获得正确的颜色,但是OpenGL的概念比RGB更为重要。 It will interpolate numbers for every pixel inside the triangle. 它将为三角形内的每个像素插入数字。 That's it. 而已。

  1. For the vertex shader, 对于顶点着色器,

you can use glGetAttribLocation in C++ to get the driver assigned location or manually set it like this: layout (location = 0) in vec3 vp; 您可以在C ++中使用glGetAttribLocation来获取驱动程序分配的位置,或手动进行如下设置: layout (location = 0) in vec3 vp; in GLSL to get the location for the attribute. 在GLSL中获取属性的位置。 Then you upload the data in C++ like this: 然后像这样用C ++上传数据:

    // (Vertex buffer must be bound at this point)
    glEnableVertexAttribArray( a ); // 'a' would be 0 if you did the latter
    glVertexAttribPointer( a, 3, GL_FLOAT, GL_FALSE, sizeof( your vertex ), nullptr );

For the fragment shader, 对于片段着色器,

'in' variables must match vertex shader's 'out' variables, like in your sample code out vec3 Color; -> in vec3 Color; “输入”变量必须与顶点着色器的“输出”变量匹配,例如在示例代码中out vec3 Color; -> in vec3 Color; out vec3 Color; -> in vec3 Color;

  1. gl_Position controls where outColor is painted. gl_Position控制outColor的绘制位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM