Steven De Bock
Steven De Bock

Reputation: 429

Opengl 4.3 gl_VertexID not incrementing with glDrawArrays

I'm having difficulty in understanding why gl_VertexID is not properly incrementing for each new vertex in the code below for rendering "debug text". Hints/tips?

(Original code is referenced at the bottom of this post)

Hereafter is the vertex shader:

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

The geometry shader:

#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();
}

The draw call and other relevant code:

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

Basically this code will be used for some text rendering. When I use this code, I see that my letters are put on top of each other. When I modify

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

into

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

I notice there is a shift in the x-direction as expected from the Geometry shader, nevertheless the letters are still on top of each other.

I'm using an NVIDIA Geforce GT 630M, driver version: 320.18 and an OpenGL 4.3 context.

Reference to the original author's code

Upvotes: 1

Views: 2119

Answers (1)

Steven De Bock
Steven De Bock

Reputation: 429

I got the code working by using VBOs as Bartek hinted at: I basically replaced

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

with

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

Upvotes: 1

Related Questions