OpenGL通过GL_ARRAY_BUFFER传递整数数组

问题描述 投票:0回答:1

我正在尝试将一些整数值与顶点数据一起传递给Vertex Shader。

我在绑定顶点数组时生成一个缓冲区,然后尝试将其附加到某个位置,但是在顶点着色器中,该值始终为0。

这是生成缓冲区及其在着色器中的用法的代码的一部分。

glm::vec3 materialStuff = glm::vec3(31, 32, 33);

glGenBuffers(1, &materialBufferIndex);
glBindBuffer(GL_ARRAY_BUFFER, materialBufferIndex);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3), &materialStuff, GL_STATIC_DRAW);

glEnableVertexAttribArray(9);
glVertexAttribIPointer(9, 3, GL_INT, sizeof(glm::vec3), (void*)0);

这是着色器的一部分,假定它接收整数值

// Some other locations
layout (location = 0) in vec3 vertex_position;
layout (location = 1) in vec2 vertex_texcoord;
layout (location = 2) in vec3 vertex_normal;
layout (location = 3) in vec3 vertex_tangent;
layout (location = 4) in vec3 vertex_bitangent;
layout (location = 5) in mat4 vertex_modelMatrix;
// layout (location = 6) in_use...
// layout (location = 7) in_use...
// layout (location = 8) in_use...

// The location I am attaching my integer buffer to
layout (location = 9) in ivec3 vertex_material;
// I also tried with these variations
//layout (location = 9) in int vertex_material[3];
//layout (location = 9) in int[3] vertex_material;

// and then in vertex shader I try to retrieve the int value by doing something like this
diffuseTextureInd = vertex_material[0];

[diffuseTextureInd应该通过片段着色器通过

out flat int diffuseTextureInd;

而且我计划使用它来索引我已经设置并工作的无边界纹理数组。问题是,似乎vertex_material只是在数组中包含第0 s since my fragment shader always displays the 0个纹理。

注意:我知道我的片段着色器很好,因为如果这样做的话

diffuseTextureInd = 31;

在顶点着色器中,片段着色器正确接收正确的索引并显示正确的纹理。但是,当我尝试使用布局位置9中的值时,似乎总是得到0。在这里我做错了吗?

opengl glsl shader game-engine
1个回答
0
投票

以下定义:

glm::vec3 materialStuff = glm::vec3(31, 32, 33);
glVertexAttribIPointer(9, 3, GL_INT, sizeof(glm::vec3), (void*)0);

...

layout (location = 9) in ivec3 vertex_material;

实际上是指:

  • glm::vec3表示您声明的向量为3个浮点数,而不是整数。 glm::ivec3应该用于整数向量。
  • ivec3顶点属性意味着每个顶点期望有3个整数的向量。同时,materialStuff仅定义单个顶点的值(对于三角形,则至少需要3 glm::ivec3没有意义。)>
  • 应该为传递单个整数顶点属性声明什么:

  • layout (location = 9) in int vertex_material;(没有任何数组限定符)
  • GLint materialStuff[3] = { 31, 32, 33 };;但是应该注意,将不同的每个顶点整数传递给片段着色器没有任何意义,我想您是通过flat关键字解决的。
  • glVertexAttribIPointer(9, 1, GL_INT, sizeof(GLint)*3, (void*)0);
© www.soinside.com 2019 - 2024. All rights reserved.