0

I am using this vertex information as input to a GL_POINTS geometry shader.

struct Vertex
{
    glm::mat4 transform;
    glm::vec3 colour;
};

Vertex points [] = {...};

When setting up the colour attribute, the following code works (with m_location = glGetAttribLocation(...)):

void Attribute::point_to_vec3 (GLsizei stride, const void * offset)
{
    glVertexAttribPointer (
        m_location,
        3,
        GL_FLOAT,
        GL_FALSE,
        stride,
        offset);
}

glEnableVertexAttribArray (m_location);

The naive version for a mat4 doesn't work because apparently it has to be sent as 4 sets of 4 floats. I wrote this

void Attribute::point_to_mat4 (GLsizei size, const void * offset)
{
    auto o = static_cast <const std::uint8_t *> (offset);

    for (int i = 0; i < 4; ++i)
    {
        glVertexAttribPointer (
            m_location + i,
            4,
            GL_FLOAT,
            GL_FALSE,
            size,
            o + (4 * i * sizeof (GLfloat)));

        glEnableVertexAttribArray (m_location + i);

        glVertexAttribDivisor (m_location + i, 1);
    }
}

This is adapted from someone else's code, and having read the API documentation, I am still unsure if I'm doing this correctly. The matrix is behaving oddly in the shader.

Can someone please clarify what's going on with the multiple locations, and glVertexAttribDivisor?

Is the above code correct?

spraff
  • 32,570
  • 22
  • 121
  • 229
  • Is this a per-instance attribute? Otherwise, you don't need `glVertexAttribDivisor`. What are you passing as the `size`? – Nico Schertler Sep 13 '19 at 16:23
  • I haven't explicitly done anything with instancing. It's a bit over my head so you can probably assume "no", I guess. `size` is the stride of the whole vertex struct in C++. – spraff Sep 13 '19 at 23:14
  • 1
    Yes, if your entire vertex structure is in this buffer. Try to remove the `glVertexAttribDivisor` and see if it works. – Nico Schertler Sep 14 '19 at 00:39

0 Answers0