简体   繁体   English

无符号int向量的差异,被解释为有符号并转换为不正确呈现的浮点向量

[英]Difference of unsigned int vectors, interpreted as signed and casted to a float vector not rendering properly

If I take the difference of the two uint vectors in my c++ code, cast to int, cast to float, and pass to GLSL, it renders fine. 如果我在我的c ++代码中采用两个uint向量的差值,将其强制转换为int,强制转换为float,然后传递给GLSL,则可以正常显示。 But when I try to make GLSL do the difference and cast, it covers the screen in a near uniform shade as if the square I'm trying to draw is really zoomed in. And yes, I really really do want to do it in GLSL, if only to see how. 但是,当我尝试让GLSL发挥作用并进行投射时,它几乎以均匀的阴影覆盖了屏幕,好像我要绘制的正方形确实放大了。是的,我真的很想在GLSL中做到这一点。 ,如果只是看怎么做。 Also, my matrices and vectors are in Eigen objects, although I'm not sure if it matters. 同样,我的矩阵和向量在Eigen对象中,尽管我不确定这是否重要。

在此处输入图片说明在此处输入图片说明

Anyway, to make sure it's clear, my projviewMatrix (the same in both cases) works perfectly fine when given what should be an identical float vector to multiply with. 无论如何,要确保它很清楚,当给定应该与之相乘的相同浮点矢量时,我的projviewMatrix(两种情况都相同)可以完美地工作。 So I'm pretty sure the issue is with the uint vector subtraction in GLSL. 因此,我很确定问题出在GLSL中的uint向量减法。 I did the exact same operation (uint vector subtract, cast to int, and cast to float) in c++ as I do in my shader, but one worked and one didn't. 我在着色器中做的操作与在着色器中执行的操作完全相同(将uint向量减,强制转换为int和强制转换为float),但其中一个有效,而一个无效。

Here's the vertex shader I'm currently trying. 这是我目前正在尝试的顶点着色器。 I tried just considering the vectors as signed ints and doing the subtract since the op should be the same... I think. 我尝试仅将向量视为有符号整数并进行减法,因为运算符应该相同...我认为。 No change in behavior. 行为无变化。

#version 150 core

uniform mat4 projviewMatrix;
uniform ivec3 camera;

in ivec3 in_Position;
in vec3 in_Color;

out vec3 pass_Color;

void main()
{
 gl_Position = projviewMatrix * vec4(in_Position - camera, 1.0);
 pass_Color = in_Color;
}

I've tried passing in_Position and camera as uvec3s and then doing ivec4(in_Position - camera, 1) and other various silly rearranging to no avail. 我尝试过将in_Positioncamera作为uvec3s传递,然后进行ivec4(in_Position - camera, 1)和其他各种愚蠢的重新排列都无济于事。 Also tried renaming camera to in_Camera . 还尝试将camera重命名为in_Camera I'll admit, that was desperate. 我承认,那是绝望的。

My uniform passing stuff. 我穿制服的东西。 When I pass in the working float vector, I simply comment out the 2 camera lines. 当我传递有效的浮动矢量时,我只需注释掉2条相机线。

GLint viewLoc = glGetUniformLocation(shader.id(), "projviewMatrix"); //this is an ortho projection with 1/2 scaling, no rotation or translation
GLint cameraLoc = glGetUniformLocation(shader.id(), "camera");

glUniformMatrix4fv(viewLoc, 1, GL_FALSE, projview.data());
glUniform3uiv(cameraLoc, 1, camera.pos.data());

This is how I bind the VBO to the VAO. 这就是我将VBO绑定到VAO的方式。

glBindBuffer(GL_ARRAY_BUFFER, vboID[0]);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLint), 0, GL_STREAM_DRAW);

glVertexAttribPointer(0, 3, GL_INT, false, 0, 0);

Here is the float vector input GLSL that works. 这是有效的浮点向量输入GLSL。 in_Position is the in_Position from above minus camera and casted to int then float in my c++ code. in_Position是减camera上方的in_Position ,并in_Position转换为int,然后在我的c ++代码中浮动。

#version 150 core

uniform mat4 projviewMatrix;

in vec3 in_Position;
in vec3 in_Color;

out vec3 pass_Color;

void main()
{
 gl_Position = projviewMatrix * vec4(in_Position, 1.0);
 pass_Color = in_Color;
}

My projviewMatrix only does two things: ortho projection and 1/2 scaling. 我的projviewMatrix仅做两件事:正投影和1/2缩放。 No translation or rotation, or whatever else. 没有平移或旋转,或其他任何方式。 The ortho part of it is generated with these parameters, with width/height being the window width/height. 使用这些参数生成其正交部分,其中width / height是窗口的宽度/高度。

proj = orthoProj(-width, width, -height, height, -4, 4);

edit: 编辑:
At Alf's request: 应阿尔夫的要求:

This happens right after I bind my uniform stuff: 这是在我绑定我的制服后立即发生的:

glBindBuffer(GL_ARRAY_BUFFER, vboID[0]);
int* buffer = (int*) glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);

static const int width = 1000;
TestObject& test = game.test;

buffer[0] = test.pos[0] - width;
buffer[1] = test.pos[1] - width;
buffer[2] = 0;

And I continually fill out buffer manually like so. 我会像这样不断手动填充buffer test.pos is the center of the square, which is equal to camera.pos xy coords (1 << 31, 1 << 31) currently. test.pos是正方形的中心,当前等于camera.pos xy坐标(1 << camera.pos << 31)。 In the case where I do the subtraction in c++, I have 3 lines like this after each vertex in buffer: 在我用c ++进行减法的情况下,在缓冲区中的每个顶点之后我都有3行:

buffer[0] -= camera.pos[0];
buffer[1] -= camera.pos[1];
buffer[2] -= camera.pos[2];

Either way, I pass in the data as integers. 无论哪种方式,我都将数据作为整数传递。 The above block (happening for each buffer vertex) is the only difference between my "subtract in GLSL" and "subtract in c++" stuff, besides the camera uniform binding and the GLSL code difference. 除了相机统一绑定和GLSL代码差异外,上述代码段(每个buffer顶点发生)是我的“ GLSL减”和“ c ++减”之间的唯一区别。

test.pos and camera.pos are both Eigen::Vector3ui objects, so uint vector 3s. test.poscamera.pos都是Eigen::Vector3ui对象,因此uint向量为3s。

edit2: EDIT2:

I think it's also worth noting that I have debug output printing any errors in the shaders, and there are none. 我认为还应该指出,我已经调试输出打印了着色器中的任何错误,并且没有任何错误。 Also, if I change in_Position and camera to vec3 , the result is the same blue-green shade as with anything else. 另外,如果将in_Positioncamera更改为vec3 ,结果将与其他任何东西一样具有相同的蓝绿色阴影。

If I create my own ivec3 in the vertex shader and use it in place of camera , I can alter the "camera" values and move around what I can now confirm is the super inflated square, roughly spanning from 0 to 1 << 32 in the x and y axes. 如果我在顶点着色器中创建自己的ivec3并将其用于代替camera ,则可以更改“ camera”值,并移动到现在可以确认的超级膨胀正方形,大致从0到1 << 32 in x和y轴。 No amount of type juggling or intermediate variables in my vertex shader seem to be capable of fixing this. 我的顶点着色器中没有多少类型的杂耍或中间变量似乎无法解决此问题。

edit 3: 编辑3:

Using glVertexAttribIPointer() to put data into in_Position has fixed part of the issue, which definitely appears to be OpenGL doing an extra cast to float before my GLSL code executes. 使用glVertexAttribIPointer()将数据放入in_Position已解决了该问题的一部分,这肯定是OpenGL在执行我的GLSL代码之前进行了额外的in_Position转换。 However, the camera vector is still bugged up in the same fashion, and I don't know of anything I can do beyond my current use of glUniform3uiv . 但是, camera矢量仍然以相同的方式被窃听,除了当前使用glUniform3uiv之外,我不知道还能做些什么。 I tried using glUniform3ui and putting in its 3 values, but that also didn't work. 我尝试使用glUniform3ui并输入其3个值,但这也没有用。

There were 2 problems. 有两个问题。

  1. Pointers to integer attributes should be set with glVertexAttribIPointer() since glVertexAttribPointer() causes openGL to perform some additional casts to/from float. 指向整数属性的指针应使用glVertexAttribIPointer()进行设置,因为glVertexAttribPointer()会导致openGL对float进行一些附加的强制转换。
  2. ivec3 uniform was set with glUniform3uiv() which also may caused unexpected casts. glUniform3uiv()设置ivec3制服,这也可能导致意外的强制转换。 The right solution is to use unsinged uniform and perform cast inside shader. 正确的解决方案是使用未着色的制服并在内部着色器中进行投射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM