PDA

View Full Version : Creating a model class



Cofyka
02-10-2018, 01:22 PM
I'm trying to create a model class which would carry one VAO and 2 VBOs, but I'm not quite sure if it should have its vertices data saved for later usage. Something like for shape changing or idk :dejection:
By the way, how do I pass vertices as an argument? In my code its not working.
Here is my code:


class LEOPModel {

GLuint VAO_;
GLuint VBO1, VBO2;

public:

LEOPModel() {

glGenVertexArrays(1, &VAO_);

}

void insertVBO(GLfloat *vertices, GLfloat *vertices_2) {

glGenBuffers(1, &VBO1);
glGenBuffers(1, &VBO2);

glBindVertexArray(VAO_);

glBindBuffer(GL_ARRAY_BUFFER, VBO1);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_2), vertices_2, GL_STATIC_DRAW);

glBindVertexArray(0);

}

void draw() {

glBindVertexArray(VAO_);

glDrawArrays(GL_TRIANGLES, 0, 6);

glBindVertexArray(0);

}

GLuint getVBO(int at) {

return VBO1;
}

};

thanks.

GClements
02-10-2018, 06:50 PM
By the way, how do I pass vertices as an argument? In my code its not working.


void insertVBO(GLfloat *vertices, GLfloat *vertices_2) {
...
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);


Applying the sizeof operator to a pointer returns the size of the pointer (typically 4 or 8), not the data it points to. You'll need to pass the array length as a parameter, or use a std::vector.

Cofyka
02-11-2018, 07:46 AM
Applying the sizeof operator to a pointer returns the size of the pointer (typically 4 or 8), not the data it points to. You'll need to pass the array length as a parameter, or use a std::vector.

But it's not working.


le_opengl::LEOPModel model;
model.insertVBO(colors_, sizeof(colors_), shape_, sizeof(shape_));

void insertVBO(GLfloat *vertices, int size, GLfloat *vertices_2, int size_2) {

glGenBuffers(1, &VBO1);
glGenBuffers(1, &VBO2);

glBindVertexArray(VAO_);

glBindBuffer(GL_ARRAY_BUFFER, VBO1);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0);
glBufferData(GL_ARRAY_BUFFER, size, &vertices, GL_STATIC_DRAW);
//
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0);
glBufferData(GL_ARRAY_BUFFER, size_2, vertices_2, GL_STATIC_DRAW);

glBindVertexArray(0);

}

GClements
02-11-2018, 01:01 PM
But it's not working.


le_opengl::LEOPModel model;
model.insertVBO(colors_, sizeof(colors_), shape_, sizeof(shape_));

What are colors_ and shape_?

Cofyka
02-11-2018, 03:38 PM
Static variables.

GLfloat shape_[3 * 6]{

-0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,

0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.0f

};

GLfloat colors_[3 * 6]{

0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,

1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.5f, 0.0f, 0.0f

};

GClements
02-11-2018, 06:38 PM
Another issue which I missed last time:





glBufferData(GL_ARRAY_BUFFER, size, &vertices, GL_STATIC_DRAW);



The "&" shouldn't be there; you want the value of the pointer, not its address. For an array, it doesn't matter (an array used as a value "decays" to a pointer to its first element); for a pointer, it matters.

The second call is correct:





glBufferData(GL_ARRAY_BUFFER, size_2, vertices_2, GL_STATIC_DRAW);

Cofyka
02-12-2018, 12:31 AM
I noticed that after but still results are the same.