metalowy
metalowy

Reputation: 111

glVertexAttribPointer set to pointer

so I have this class:

class BRENDERDLL_API Vertex
{
private:
    glm::vec3 pos;

public:
    Vertex(glm::vec3 pos);
    ~Vertex();
    void setPos(glm::vec3 pos);
    glm::vec3 getPos();
};

I would want to change:

glm::vec3 pos;

to:

glm::vec3* pos;

Then I need to change:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

To point somehow where I storage data. There is obviously no problem with first one case, but what to do with second version? Can I even do that or should I copy data to some array before?

Upvotes: 0

Views: 1020

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

Changing this to glm::vec3* will not benefit you at all, especially in modern GL. What you need to do is create an array of instances of Vertex.

You have to store this stuff in a Vertex Buffer and that means it needs to be laid out "flatly" in memory (e.g. no pointer dereferencing). If you have a contiguous array of Vertex, then you can take the address of the first instance and use glBufferData (...) to upload it to GL-managed memory.

The following code illustrates this:

std::vector <Vertex> verts;
const size_t size = verts.size () * sizeof (Vertex);

// Generate, allocate and upload your array to an OpenGL Buffer Object
GLuint vbo;
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, size, &verts [0], GL_STATIC_DRAW);

// This points to address **0** relative to the buffer currently bound to
// GL_ARRAY_BUFFER.
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

Upvotes: 1

Related Questions