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

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

问题描述

...因为浮子看起来很好,但是int出了点问题.

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

基本上,我有一个名为"BlockInstance"的结构,其中包含一个vec3和一个int.我有一个像这样缓冲的BlockInstance数组(为清楚起见,将其从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着色器的最后一行用常量0、1或2替换TexIndex时,我的纹理效果很好,但是如果我将其保留原样,它们的纹理将全部变形,因此数字一定有问题吧?但是我不知道它会发出什么,因此很难调试.

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.

我已经查看了我的BlockInstance数组,它们都设置为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 px,但我的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会进行整数数学运算,对不对?一个整数除以一个整数会得出整数吗?

-- 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天全站免登陆