我正在尝试生成3D地形。我的目标是将顶点着色器的高度传递给顶点着色器,并使用gl_VertexID从顶点着色器中计算x和z位置。顶点在x和z轴上相距1。这是顶点着色器:
#version 330 core
uniform mat4 projection;
uniform mat4 view;
uniform mat4 world;
layout(location = 0) in float height;
// vertex offset; position of vertex relative to the top left corner of square
const vec2 offset[6] = vec2[](
vec2(0, 0), // top left
vec2(0, 1), // bottom left
vec2(1, 1), // bottom right
vec2(1, 1), // bottom right
vec2(0, 0), // top left
vec2(1, 0) // top right
);
void main() {
// index of square
int squareIndex = gl_VertexID / 6;
// index of vertex within square
int vertexIndex = gl_VertexID % 6;
// xz position of vertex
vec2 position = offset[vertexIndex] + vec2(squareIndex % LENGTH, squareIndex / LENGTH);
gl_Position = projection * view * world * vec4(position.x, height, position.y, 1.0f);
}
三角形是从左到右,从上到下绘制的。这是EBO的生成:
for(unsigned int xSquare = 0; xSquare < LENGTH; xSquare++) {
for(unsigned int zSquare = 0; zSquare < LENGTH; zSquare++) {
size_t squareIndex = zSquare * LENGTH + xSquare;
unsigned int topLeft = squareIndex;
unsigned int topRight = squareIndex + 1;
unsigned int bottomLeft = squareIndex + LENGTH;
unsigned int bottomRight = squareIndex + LENGTH + 1;
elements[squareIndex] = topLeft;
elements[squareIndex + 1] = bottomLeft;
elements[squareIndex + 2] = bottomRight;
elements[squareIndex + 3] = bottomRight;
elements[squareIndex + 4] = topLeft;
elements[squareIndex + 5] = topRight;
}
}
我已经使用qrenderdoc检查了顶点数据输入,并且高度已正确传递到顶点着色器。然而,the rendered output looks like this.
我已经检查了顶点着色器的逻辑,但没有发现任何问题。有人知道我在做什么错吗?
切片数不等于行或列中的点数,在点上比切片多。
因此可能必须是:
vec2 position = offset[vertexIndex] +
vec2(squareIndex % (LENGTH-1), squareIndex / (LENGTH-1));