我可以将浮点数和整数都打包到同一个数组缓冲区中吗? [英] Can I pack both floats and ints into the same array buffer?

查看:21
本文介绍了我可以将浮点数和整数都打包到同一个数组缓冲区中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...因为浮点数似乎很好,但整数有问题.

...because the floats seem to be coming out fine, but there's something wrong with the ints.

本质上,我有一个名为BlockInstance"的结构,它包含一个 vec3 和一个 int.我有一个这些 BlockInstances 的数组,我像这样缓冲(为了清楚起见,从 C# 转换为 C):

Essentially I have a struct called "BlockInstance" which holds a vec3 and an int. I've got an array of these BlockInstances which I buffer like so (translating from C# to C for clarity):

glBindBuffer(GL_ARRAY_BUFFER, bufferHandle);
glBufferData(GL_ARRAY_BUFFER, sizeof(BlockInstance)*numBlocks, blockData, GL_DYNAMIC_DRAW);
glVertexAttribPointer(3,3,GL_FLOAT,false,16,0);
glVertexAttribPointer(4,1,GL_INT,false,16,12);
glVertexAttribDivisor(3,1);
glVertexAttribDivisor(4,1);

我的顶点着色器看起来像这样:

And my vertex shader looks like this:

#version 330

layout (location = 0) in vec3 Position;
layout (location = 1) in vec2 TexCoord;
layout (location = 2) in vec3 Normal;
layout (location = 3) in vec3 Translation;
layout (location = 4) in int TexIndex;

uniform mat4 ProjectionMatrix;

out vec2 TexCoord0;

void main()
{
    mat4 trans = mat4(
        1,0,0,0,
        0,1,0,0,
        0,0,1,0,
        Translation.x,Translation.y,Translation.z,1);
    gl_Position = ProjectionMatrix * trans * vec4(Position, 1.0);
    TexCoord0 = vec2(TexCoord.x+TexIndex,TexCoord.y)/16;
}

当我将 GLSL 着色器最后一行的 TexIndex 替换为 0、1 或 2 等常量时,我​​的纹理效果很好,但如果我保持原样,它们就会出现全部被弄乱了,所以号码一定有问题,对吧?但是我不知道它出来的是什么,所以很难调试.

When I replace TexIndex on the last line of my GLSL shader with a constant like 0, 1, or 2, my textures come out fine, but if I leave it like it is, they come out all mangled, so there must be something wrong with the number, right? But I don't know what it's coming out as so it's hard to debug.

我查看了我的 BlockInstances 数组,它们都设置为 1,2 或 19,所以我认为我的输入没有错误...

I've looked at my array of BlockInstances, and they're all set to 1,2, or 19 so I don't think my input is wrong...

还能是什么?

请注意,我使用的是精灵贴图纹理,其中每个图块的大小为 16x16 像素,但我的 TexCoords 的范围为 0-1,因此我向其添加一个整数以选择哪个图块,然后将其除以16(地图也是 16x16 图块)以将其放回正确的范围内.这个想法是我将用

Note that I'm using a sprite map texture where each of the tiles is 16x16 px but my TexCoords are in the range 0-1, so I add a whole number to it to choose which tile, and then divide it by 16 (the map is also 16x16 tiles) to put it back into the proper range. The idea is I'll replace that last line with

TexCoord0 = vec2(TexCoord.x+(TexIndex%16),TexCoord.y+(TexIndex/16))/16;

-- GLSL 进行整数数学运算,对吗?int 除以 int 会得到整数吗?

-- GLSL does integer math, right? An int divided by an int will come out as whole number?

如果我试试这个:

TexCoord0 = vec2(TexCoord.x+(TexIndex%16),TexCoord.y)/16;

纹理看起来不错,但它没有使用正确的精灵.(貌似用的是第一个精灵)

The texture looks fine, but it's not using the right sprite. (Looks to be using the first sprite)

如果我这样做:

TexCoord0 = vec2(TexCoord.x+(TexIndex%16),TexCoord.y+(TexIndex/16))/16;

出来的时候都是白色的.这让我相信 TexIndex 是一个非常大的数字(无论如何都大于 256)并且它可能是 16 的倍数.

It comes out all white. This leads me to believe that TexIndex is coming out to be a very large number (bigger than 256 anyway) and that it's probably a multiple of 16.

推荐答案

layout (location = 4) in int TexIndex;

这是你的问题.

glVertexAttribPointer 用于发送数据转换为浮点值.它用于提供浮点属性.传递整数是可能的,但这些整数被转换为浮点数,因为这就是 glVertexAttribPointer 的用途.

glVertexAttribPointer is used to send data that will be converted to floating-point values. It's used to feed floating-point attributes. Passing integers is possible, but those integers are converted to floats, because that's what glVertexAttribPointer is for.

你需要的是glVertexAttribIPointer(注意I).这用于提供有符号和无符号整数数据.

What you need is glVertexAttribIPointer (notice the I). This is used for providing signed and unsigned integer data.

因此,如果您将顶点着色器输入声明为 float 或一些非前缀的 vec,您可以使用 glVertexAttribPointer 来提供它.如果将输入声明为 intuintivecuvec,则使用 glVertexAttribIPointer.

So if you declare a vertex shader input as a float or some non-prefixed vec, you use glVertexAttribPointer to feed it. If you declare the input as int, uint, ivec or uvec, then you use glVertexAttribIPointer.

这篇关于我可以将浮点数和整数都打包到同一个数组缓冲区中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆