Opengl 4.3 gl_VertexID没有使用glDrawArrays递增

时间:2013-07-11 12:58:51

标签: opengl glsl

我很难理解为什么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上下文。

Reference to the original author's code

1 个答案:

答案 0 :(得分:1)

我使用VBO的代码工作正如Bartek暗示的那样:我基本上已经取代了

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);