在 OpenGL 3.3 中用整数坐标绘制点?
Draw points with integer coordinates in OpenGL 3.3?
我知道一点标准化设备坐标,我知道当我在 -1.0 和 1.0 之间使用 float 时,我可以获得输出。
但是,当我想使用整数作为顶点的位置属性时,我无法得到任何渲染输出。我尝试在 glVertexAttribPointer
中使用 GL_INT
和 GL_TRUE
但它不起作用。
例如。
std::vector<GLint> vex =
{
0, 0, 0,
4, 5, 0
};
glBufferData(GL_ARRAY_BUFFER, vex.size() * sizeof(GLint), vex.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_INT, GL_TRUE, 3 * sizeof(GLint), (void*)0);
// in the render loop
glBindVertexArray(VAO);
glDrawArrays(GL_LINES, 0, 2);
我使用基本的顶点着色器如下:
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
我认为 GL_TRUE
会将整数位置标准化为 [-1.0, 1.0]。
也许我忽略了一些重要的事情。那么如何使用整数坐标正确渲染我的点?
关于glVertexAttribPointer()
我已经阅读了这个reference,但我仍然无法得到我想要的。
However, when I want to use integers as vertex's position attribute, I can't get any rendering output.
你没有输出,因为两个点靠得太近了。
Signed normalized fixed-point integers represent numbers in the range [−1, 1].
The conversion from a signed normalized fixed-point value c to the corresponding
floating-point value f is performed using
f = max( c / (2^(b-1) - 1), -1.0 )
(c
为整数值,b
为整数数据格式的位数)
这意味着,对于数据类型 GLint
,4 导致浮点数 0.000000001862645 和 5 结果为浮点数 0.000000002328306.
使用 GLbyte
而不是 GLint
来测试您的代码。以下代码会在视口中生成一条对角线:
std::vector<GLbyte> vex = { 127, 127, 0,
-128, -128, 0 };
glBufferData(GL_ARRAY_BUFFER, vex.size() * sizeof(GLbyte), vex.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_BYTE, GL_TRUE, 0, (void*)0);
我知道一点标准化设备坐标,我知道当我在 -1.0 和 1.0 之间使用 float 时,我可以获得输出。
但是,当我想使用整数作为顶点的位置属性时,我无法得到任何渲染输出。我尝试在 glVertexAttribPointer
中使用 GL_INT
和 GL_TRUE
但它不起作用。
例如。
std::vector<GLint> vex =
{
0, 0, 0,
4, 5, 0
};
glBufferData(GL_ARRAY_BUFFER, vex.size() * sizeof(GLint), vex.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_INT, GL_TRUE, 3 * sizeof(GLint), (void*)0);
// in the render loop
glBindVertexArray(VAO);
glDrawArrays(GL_LINES, 0, 2);
我使用基本的顶点着色器如下:
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
我认为 GL_TRUE
会将整数位置标准化为 [-1.0, 1.0]。
也许我忽略了一些重要的事情。那么如何使用整数坐标正确渲染我的点?
关于glVertexAttribPointer()
我已经阅读了这个reference,但我仍然无法得到我想要的。
However, when I want to use integers as vertex's position attribute, I can't get any rendering output.
你没有输出,因为两个点靠得太近了。
Signed normalized fixed-point integers represent numbers in the range [−1, 1]. The conversion from a signed normalized fixed-point value c to the corresponding floating-point value f is performed using
f = max( c / (2^(b-1) - 1), -1.0 )
(c
为整数值,b
为整数数据格式的位数)
这意味着,对于数据类型 GLint
,4 导致浮点数 0.000000001862645 和 5 结果为浮点数 0.000000002328306.
使用 GLbyte
而不是 GLint
来测试您的代码。以下代码会在视口中生成一条对角线:
std::vector<GLbyte> vex = { 127, 127, 0,
-128, -128, 0 };
glBufferData(GL_ARRAY_BUFFER, vex.size() * sizeof(GLbyte), vex.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_BYTE, GL_TRUE, 0, (void*)0);