简体   繁体   English

OpenGL/GLSL 将顶点坐标传递给类型为 float[3] 的顶点着色器

[英]OpenGL/GLSL passing vertex coordinates to the vertex shader with type float[3]

I'm learning modern OpenGL using the tutorial from opengl-tutorial.org .我正在使用opengl-tutorial.org的教程学习现代 OpenGL。

Their vertex shaders alwas use the type vec3 to pass vertex coordinates to the vertex shader.他们的顶点着色器总是使用类型 vec3 将顶点坐标传递给顶点着色器。 According to the manuals it should be possible to use the type float as well.根据手册,也应该可以使用浮点型。 As my application gets the vertex coordinates as an array of floats I tried to use float to pass them, which doesn't work as expected.当我的应用程序将顶点坐标作为浮点数组获取时,我尝试使用浮点数来传递它们,但这并没有按预期工作。 Even the Tutorial 5 : A Textured Cube of the tutorials shows the effect.甚至教程 5 : A Textured Cube的教程也展示了这种效果。 If I change the vertex shader code from:如果我将顶点着色器代码更改为:

#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;

// Output data ; will be interpolated for each fragment.
out vec2 UV;

// Values that stay constant for the whole mesh.
uniform mat4 MVP;

void main(){

    // Output position of the vertex, in clip space : MVP * position
    gl_Position =  MVP * vec4(vertexPosition_modelspace,1);
    
    // UV of the vertex. No special space for this one.
    UV = vertexUV;
}

To:到:

#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in float vertexPosition_modelspace[3];
layout(location = 1) in vec2 vertexUV;

// Output data ; will be interpolated for each fragment.
out vec2 UV;

// Values that stay constant for the whole mesh.
uniform mat4 MVP;

void main(){

    // Output position of the vertex, in clip space : MVP * position
    gl_Position =  MVP * vec4(vertexPosition_modelspace[0], vertexPosition_modelspace[1], vertexPosition_modelspace[2], 1);
    
    // UV of the vertex. No special space for this one.
    UV = vertexUV;
}

The cube isn't displayed correctly.多维数据集未正确显示。 I can see that passing arrays of floats to the vertex shader and accept them there as vec3 works, but what is wrong using float[3] as the type to pass arrays of floats or how is this to be done?我可以看到将浮点数数组传递给顶点着色器并在那里接受它们作为 vec3 工作,但是使用 float[3] 作为传递浮点数数组的类型有什么问题,或者这是如何完成的?

Isn't float[3] equal to vec3? float[3] 不等于 vec3 吗?

Furthermore I'm somewhat surprised, that writing the following line in variants:此外,我有点惊讶,在变体中编写以下行:

layout(location = 0) in float[] vertexPosition_modelspace;

layout(location = 0) in float[3] vertexPosition_modelspace;

layout(location = 0) in float vertexPosition_modelspace[3];

Seems to compile and at least to run, without proper display, however.然而,似乎可以编译并且至少可以运行,但没有正确显示。

The declaration:声明:

layout(location = 0) in float vertexPosition_modelspace[3];

specifies that the array of float is distributed throughout the 3 consecutive layout locations, beginning at the location 0 , specified in layout(location = 0) .指定float分布在3个连续的布局位置,从位置0开始,在layout(location = 0)指定。

So, the array values:因此,数组值:

vertexPosition_modelspace[0], vertexPosition_modelspace[1], vertexPosition_modelspace[2]

are distributed respectively throughout layout locations from 0 to 2 .分别分布在从02整个布局位置。

Then, the next declaration:然后,下一个声明:

layout(location = 1) in vec2 vertexUV;

causes overwriting of the preceding array data at location = 1 , resulting in deformations.导致覆盖location = 1处的先前数组数据,从而导致变形。

Therefore, to avoid this, you would need to either: declare the array last in the layout:因此,为了避免这种情况,您需要: 在布局中最后声明数组:

layout(location = 1) in float vertexPosition_modelspace[3];
layout(location = 0) in vec2 vertexUV;

or declare the other variable at location = 3 , after the end of the preceding array:或者在location = 3处声明另一个变量,在前面的数组末尾之后:

layout(location = 0) in float vertexPosition_modelspace[3];
layout(location = 3) in vec2 vertexUV;

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

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