c++ - 错误修复。 OpenGL、GLSL Sprite 网格渲染问题

标签 c++ c opengl graphics glsl

这是一个非常具体的案例,我的想法已经用完了。

我试图在不使用位置作为每个 Sprite 的实例数据的情况下渲染一个 Sprite 网格。这可以使用 gl_InstanceID 变量来完成。

世界由 block 组成(16 * 16 * 2(2 因为每个位置有两个 Sprite /实例))。所有 block 本身都被正确绘制,但并非都在相对于另一个 block 的正确位置。

它是这样的:

enter image description here

我使用宽度和高度来指定新行何时开始。在这里你可以看到将相机放在正确位置时的左下角(没问题)

enter image description here

当相机稍微偏离一点时,就会画出这个:

enter image description here

现在是右下角。

enter image description here

请注意 width = 3(由 printf 语句返回),它清楚地表明,不应有 4 列(但有)。

简而言之,这就是问题所在。

我几乎可以肯定问题出在 GLSL 代码(着色器)上,因为我检查了我发送到显卡的所有值及其偏移量。我觉得问题出在 glsl 计算 block 位置的方式上。

#version 330 core
layout (location = 0) in vec2 v_pos;
layout (location = 1) in vec2 v_uv;
layout (location = 2) in float uv_index;

layout (std140) uniform Matrices
{
    mat4 view;
    mat4 projection;
};
uniform uint diagonalSpriteCount; //used for the uv indexing (of no interest here)
uniform uint chunksOnScreenRow; //what you saw as width on the pictures
uniform vec2 camOffsetToFirst; //this specifies the position to the first instance to be rendered

out vec2 uvCoordinates;

void main()
{
//////////////////////////////////////////
//THIS IS PROBABLY THE ONLY RELEVANT PART FOR YOU
    //i devide the index by two, as only every second instance the position changes
    int posIndex = gl_InstanceID / 2; 
    //every 256 Positions (16 * 16) a chunk is complete, thus the next chunk begins
    int chunkIndex = posIndex / 256;
    //the position of the vertex, first basic quad information and an offset
    gl_Position = projection * view * 
    vec4(v_pos.xy + camOffsetToFirst + 
        vec2(
            //this specifies the x within the chunk
            mod(posIndex, 16)    
                //this is the offset on the x axis, 
                //which depends on which chunk (the chunk the instance belongs to) is currently being rendered 
                //chunksOnScreenRow = width that you saw on the pictures
                + 16 * mod(chunkIndex, chunksOnScreenRow), 
            //this is the position on the y axis, relative to the first element of the current chunk
            floor(mod(posIndex, 256) / 16)   
                //same like with the x axis, offset on y depending on current chunk
                + 16 * floor(float(chunkIndex) / chunksOnScreenRow)
        ), 
    0.0, 1.0); //this is just the rest of the vec 4
//////////////////////////////////////////
    //irrelevant for you
    uvCoordinates = v_uv / diagonalSpriteCount + vec2(
        uv_index / diagonalSpriteCount - floor(uv_index / diagonalSpriteCount),
        floor(uv_index / diagonalSpriteCount) / diagonalSpriteCount
    );
}

这里是数据的结构方式,以备不时之需。
enter image description here

现在我请求你的帮助,如果你碰巧知道什么可能导致这种奇怪的行为。如果您能帮我找到造成这种行为的原因,我将永远感激不已。

编辑:您在图片中看到的每个 block 都是一个 block 。您将无法看到单个实例

编辑:显然,如果我在第二个 vec2() 参数中删除 chunkIndex 周围的 float(),偏移量仍然会发生,但向上移动
enter image description here

最佳答案

好的,我明白了。如果出现一差一错误(就像 Enfyve 所说的那样),请不要相信内置的 glsl mod() 函数,因为它会在途中将变量转换为单位并在途中把事情搞砸。

关于c++ - 错误修复。 OpenGL、GLSL Sprite 网格渲染问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49458481/

相关文章:

c++ - C++98 中的容器初始化

c++ - SDL/opengl 多重采样不起作用

c++ - fstream类成员变量

c++ - CUDA双矩阵溢出

c - 错误 : expected expression before 'student'

c++ - 带有指针数据的 OpenGL glBufferData

c++ - 透视 View openGL qt的任何解决方案

c - 为什么 write(2) 不返回 EINTR?

c - 如何使用 Union 在 1 个 C 数组中存储 2 个不同的结构

c++ - GLSL 错误 : failed to preprocess the source. 我该如何解决这个问题?