1

我很难理解为什么 gl_VertexID 没有为下面代码中的每个新顶点正确递增以呈现“调试文本”。提示/提示?

(本文底部引用了原始代码)

以下是顶点着色器:

#version 430 core

layout( location = 0 ) in int Character;
out int vCharacter;
out int vPosition;

void main()
{
    vPosition = gl_VertexID;
    vCharacter = Character;
    gl_Position = vec4(0, 0, 0, 1);
}

几何着色器:

#version 430 core

layout(points) in;
layout(triangle_strip, max_vertices = 4) out;

in int vCharacter[1];
in int vPosition[1];
out vec2 gTexCoord;
uniform sampler2D Sampler;

uniform vec2 CellSize;
uniform vec2 CellOffset;
uniform vec2 RenderSize;
uniform vec2 RenderOrigin;

void main()
{
    // Determine the final quad's position and size:
    float x = RenderOrigin.x + float(vPosition[0]) * RenderSize.x * 2.0f;
    float y = RenderOrigin.y;
    vec4 P = vec4(x, y, 0, 1);
    vec4 U = vec4(1, 0, 0, 0) * RenderSize.x;
    vec4 V = vec4(0, 1, 0, 0) * RenderSize.y;

    // Determine the texture coordinates:
    int letter = vCharacter[0];
    letter = clamp(letter - 32, 0, 96);
    int row = letter / 16 + 1;
    int col = letter % 16;
    float S0 = CellOffset.x + CellSize.x * col;
    float T0 = CellOffset.y + 1 - CellSize.y * row;
    float S1 = S0 + CellSize.x - CellOffset.x;
    float T1 = T0 + CellSize.y;

    // Output the quad's vertices:
    gTexCoord = vec2(S0, T1); gl_Position = P - U - V; EmitVertex();
    gTexCoord = vec2(S1, T1); gl_Position = P + U - V; EmitVertex();
    gTexCoord = vec2(S0, T0); gl_Position = P - U + V; EmitVertex();
    gTexCoord = vec2(S1, T0); gl_Position = P + U + V; EmitVertex();
    EndPrimitive();
}

绘图调用和其他相关代码:

glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLuint attribLocation = glGetAttribLocation(m_ProgramTextPrinter, "Character");
glVertexAttribIPointer(attribLocation, 1, GL_UNSIGNED_BYTE, 1, text.data()->c_str());
glEnableVertexAttribArray(attribLocation);
glDrawArrays(GL_POINTS, 0, text.data()->size());

基本上这段代码将用于一些文本渲染。当我使用此代码时,我看到我的字母被叠放在一起。当我修改

glVertexAttribIPointer(attribLocation, 1, GL_UNSIGNED_BYTE, 1, text.data()->c_str());

进入

glVertexAttribIPointer(attribLocation, 1, GL_UNSIGNED_BYTE, 2, text.data()->c_str());

我注意到几何着色器的 x 方向发生了变化,但字母仍然相互重叠。

我正在使用 NVIDIA Geforce GT 630M,驱动程序版本:320.18 和 OpenGL 4.3 上下文。

参考原作者的代码

4

1 回答 1

1

正如 Bartek 所暗示的那样,我通过使用 VBO 使代码正常工作:我基本上替换了

glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLuint attribLocation = glGetAttribLocation(m_ProgramTextPrinter, "Character");
glVertexAttribIPointer(attribLocation, 1, GL_UNSIGNED_BYTE, 1, text.data()->c_str());
glEnableVertexAttribArray(attribLocation);
glDrawArrays(GL_POINTS, 0, text.data()->size());

GLuint vaoID, bufferID;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
glGenBuffers(1, &bufferID);
glBindBuffer(GL_ARRAY_BUFFER, bufferID);
glBufferData(GL_ARRAY_BUFFER, text.data()->size() * sizeof(GL_UNSIGNED_BYTE), text.data()->data(), GL_DYNAMIC_DRAW);

GLuint attribLocation = glGetAttribLocation(m_ProgramTextPrinter, "Character");
glVertexAttribIPointer(attribLocation, 1, GL_UNSIGNED_BYTE, 0, 0);
glEnableVertexAttribArray(attribLocation);

glDrawArrays(GL_POINTS, 0, text.data()->size());

glDeleteVertexArrays(1, &vaoID);
glDeleteBuffers(1, &bufferID);
于 2013-07-11T21:15:35.960 回答