glDrawElements causes Access violation reading - opengl

I have fixed it - working solution can be found at the bottom of the page.
I am trying to play with interleaved vbo and vao. I am testing it on a simple triangle - this is how my index data and vertex data look like:
vertex_col data[] = {
vertex_col(vec3(0.5f, 0.5f, 0.5f), vec4(0.0f, 0.0f, 1.0f, 1.0f), vec3(0.0f, 0.0f, 0.0f)),
vertex_col(vec3(-0.5f, -0.5f, -0.5f), vec4(0.0f, 1.0f, 0.0f, 1.0f), vec3(0.0f, 0.0f, 0.0f)),
vertex_col(vec3(-0.5f, 0.5f, 0.5f), vec4(1.0f, 0.0f, 0.0f, 1.0f), vec3(0.0f, 0.0f, 0.0f)),
};
GLushort indices[] = {
0, 1, 2
};
ModelLoader loader;
model = loader.loadToVAO(indices, 3, data, 3);
This is vertex_col struct:
struct vertex_col {
public:
vertex_col(const vec3& pos, const vec4& col, const vec3& normal) {
this->pos = pos;
this->colour = col;
this->normal = normal;
}
vec3 pos;
vec4 colour;
vec3 normal;
};
This is how I load the data into vao and vbo:
RawModel ModelLoader::loadToVAO(const GLushort *indices, int m, const vertex_col *vertices, int n) {
GLuint vaoID = createVAO();
GLuint vboID;
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, n * sizeof(vertex_col), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, pos));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, colour));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, normal));
GLuint vboID_2;
glGenBuffers(1, &vboID_2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID_2);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m * sizeof(GLushort), indices, GL_STATIC_DRAW);
unbindVAO();
return RawModel(vaoID, m);
}
GLuint ModelLoader::createVAO() {
GLuint vaoID;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
return vaoID;
}
And this is how I draw the vao:
void Renderer::render(const RawModel &model) {
shaders["basic"].use();
glBindVertexArray(model.getVaoID());
glDrawElements(GL_TRIANGLES, model.getVertexCount(), GL_UNSIGNED_SHORT, nullptr);
glBindVertexArray(0);
shaders["basic"].release();
}
The last operation causes this error 'Access violation reading location 0x00000000' and I have no idea why. Also if it is any relevant to the question, I am using libraries glew and glfw.
Fixed version:
RawModel *ModelLoader::loadToVAO(const GLushort *indices, int m, const vertex_col *vertices, int n) {
GLuint vaoID = createVAO();
GLuint vboID = 0;
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, n * sizeof(vertex_col), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, pos));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, colour));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, normal));
vboID = 0;
glGenBuffers(1, &vboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m * sizeof(GLushort), indices, GL_STATIC_DRAW);
unbindVAO();
return new RawModel(vaoID, m);
}

Related

Drawing multiple triangles with different VAOs and VBOs

I'm trying to draw two triangles using separate VAOs and VBOs but while execution I see only one triangle being rendered. Below is my code snippet. I'm not sure where I'm messing up.
I'm using glfw and glew.
.....
//initialization and creating shader program
.....
GLfloat vertices[] = {
-0.9f, -0.5f, 0.0f, // Left
-0.0f, -0.5f, 0.0f, // Right
-0.45f, 0.5f, 0.0f, // Top
};
GLfloat vertices2[] = {
0.0f, -0.5f, 0.0f, // Left
0.9f, -0.5f, 0.0f, // Right
0.45f, 0.5f, 0.0f // Top
};
GLuint VBO1, VAO1, EBO;
glGenVertexArrays(1, &VAO1);
glGenBuffers(1, &VBO1);
glGenBuffers(1, &EBO);
GLuint VBO2, VAO2;
glGenBuffers(1, &VAO2);
glGenBuffers(1, &VBO2);
glBindVertexArray(VAO1);
glBindBuffer(GL_ARRAY_BUFFER, VBO1);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)nullptr);
glEnableVertexAttribArray(0);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindVertexArray(VAO1);
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)nullptr);
glEnableVertexAttribArray(0);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(ShaderProgramID);
glBindVertexArray(VAO1);
glDrawArrays(GL_TRIANGLES, 0, 3);
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//glBindVertexArray(0);
glBindVertexArray(VAO2);
glDrawArrays(GL_TRIANGLES, 0, 3);
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//glBindVertexArray(0);
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &VAO1);
glDeleteVertexArrays(1, &VAO2);
glDeleteBuffers(1, &VBO1);
glDeleteBuffers(1, &VBO2);
glfwTerminate();
...
Although if I create VAOs and VBOs as array like below and change the code above code accordingly I see both the triangles. I'm unable to understand why is it so?
GLuint VAO[2], VBO[2];
GLuint VBO2, VAO2;
glGenBuffers(1, &VAO2);
...
glBindVertexArray(VAO2);
You've initialized VAO2 as a buffer, not a VAO.

glDrawElements() not working

This has been bothering me for the last days and I can't figure out why.
I know there are a lot of questions like this one here on stackoverflow but none seem to solve my issue.
So here it is the initialization code:
GLuint EBO[1];
GLuint VAO[1];
GLuint VBO[1];
static vec2 tri_pos[8] = {
{-1.0f, -1.0f},
{-1.0f, -1.0f},
{-1.0f, 1.0f},
{-1.0f, 1.0f},
{ 1.0f, -1.0f},
{ 1.0f, -1.0f},
{ 1.0f, 1.0f},
{ 1.0f, 1.0f} };
static GLuint tri_indices[] = { 0, 1, 2 };
glGenBuffers(1, EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(tri_indices), tri_indices, GL_STATIC_DRAW);
glCreateVertexArrays(1, VAO2);
glBindVertexArray(VAO2[0]);
glCreateBuffers(1, VBO2);
glBindBuffer(GL_VERTEX_ARRAY, VBO2[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(tri_pos), tri_pos, GL_STATIC_DRAW);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(4);
Here I create the buffers and bind them accordingly. I create the VAO, the VBO and the Element Buffer Object.
Then I assign the tri_pos array to a vec4 in the shader and enable it.
After that I call the draw() function to draw the triangles in this case:
//...
glClearBufferfv(GL_COLOR, 0, cColor);
glBindVertexArray(VAO2[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[0]);
glBindBuffer(GL_VERTEX_ARRAY, VBO2[0]);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
//glDrawArrays(GL_TRIANGLES, 0, 3);
//glDrawArrays(GL_TRIANGLES, 5, 3);
//glDrawArraysInstanced(GL_TRIANGLES, 0, 3, 1);
//glMultiDrawArrays(GL_TRIANGLES, tri_indices, count, 2);
Here I use glDrawElements() with nullptr because Im using a EBO.
Everything compiles just fine. But at runtime it just crashes. I have no clue what I am missing. All the buffers seem me about right.
Anyone got any clue?
This line is wrong:
glCreateBuffers(1, VAO2);
You are creating a buffer but you probably want to create a vertex array:
glCreateVertexArrays(1, VAO2);
Also replace these calls:
glBindBuffer(GL_VERTEX_ARRAY, VBO2[0]);
with:
glBindBuffer(GL_ARRAY_BUFFER, VBO2[0]);

OpenGL : multiple VAOs for one VBO

I'm a newbie to OpenGL and I'm trying draw two triangles using two VAOs and only one VBO. Even if after some research, I came to have a better understanding of VAO, VBO and how the needed glew functions work, I have no clue why my program displays only one triangle instead of two. Can somebody help?
...
GLfloat points[] = {
0.5f, 0.5f, 0.0f, //First Triangle
-0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.0f, 0.0f, //Second Triangle
-1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f
};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLuint vao1;
glGenVertexArrays(1, &vao1);
glBindVertexArray(vao1);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
GLuint vao2;
glGenVertexArrays(1, &vao2);
glBindVertexArray(vao2);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL + 9);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
...
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader_program);
glBindVertexArray(vao1);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glBindVertexArray(vao2);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glUseProgram(0);
glfwPollEvents();
glfwSwapBuffers(window);
}
...
The last parameter for this function call is incorrect:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL + 9);
You're telling it to add 9 bytes, but your points are floats
Try this:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL + 9 * sizeof(float));

OpenGL Vertex Buffer Object not Showing

I have changed 3 GLFloat arrays (aka GLfloat[]) into vectors (aka vector) and my triangles stopped working! Here is my init function:
void initialize(GLuint &vao) {
// Use a Vertex Array Object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
Projection = perspective(45.0f, 16.0f / 9.0f, 0.1f, 100.0f);
//ortho(-4.0f/3.0f, 4.0f/3.0f, -1.0f, 1.0f, -1.0f, 1.0f);
Model = translate(Model, vec3(0.f, 0.f, 0.f));
Model = rotate(Model, 45.0f, vec3(0.0f, 0.0f, 1.0f));
//pos + textures array
GLuint vboId;
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, (vertices_position.size() + texture_coord.size() ) * sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices_position.size() * sizeof(GLfloat), &vertices_position[0]);
glBufferSubData(GL_ARRAY_BUFFER, vertices_position.size() * sizeof(GLfloat), texture_coord.size() * sizeof(GLfloat), &texture_coord[0]);
//Indices
GLuint eabId;
glGenBuffers(1, &eabId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eabId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLfloat), &indices[0], GL_DYNAMIC_DRAW);
//Textures
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
load_image("../squirrel.jpg");
shaderProgram = create_program("../shaders/vert.shader", "../shaders/frag.shader");
//Position attribute
positionAttrId = glGetAttribLocation(shaderProgram, "position");
glVertexAttribPointer(positionAttrId, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(positionAttrId);
//Texture attribute
textureCoordId = glGetAttribLocation(shaderProgram, "texture_coord");
glVertexAttribPointer(textureCoordId, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(vertices_position.size() * sizeof(GLfloat)));
glEnableVertexAttribArray(textureCoordId);
modelId = glGetUniformLocation(shaderProgram, "Model");
cameraId = glGetUniformLocation(shaderProgram, "Camera");
}
And below is my render function:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderProgram);
glUniformMatrix4fv(modelId, 1, GL_FALSE, glm::value_ptr(Model));
glUniformMatrix4fv(cameraId, 1, GL_FALSE, glm::value_ptr(cam.matrix()));
//VAO
glBindVertexArray(vao);
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glUseProgram(0);
You are using floating-point elements in your Index Buffer Object, that is not a valid data type.
OpenGL requires either GLubyte (not hardware accelerated on most desktop GPUs), GLushort or GLuint indices.
For best performance, you should use an array of GLushort and pass GL_UNSIGNED_SHORT as the element type in your call to glDrawElements (...).

How to bind multiple IBOs from one VBO in OpenGL

I am trying to render two different triangles with IBOs. I stored the six vertices in one VBO and tried to access them through two separate IBOs. The problem is the first IBO renders but the second doesn't. The createVertices and createIndices are called at initialization.
void createVertices()
{
//Vertex Data
GLfloat v[] = { 0.95f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
0.0f, -0.75f, 0.0f, 1.0f, // END OF TRIANGLE 1
-0.75, 0.75f, 0.5f, 1.0f,
-0.75, -0.75f, 0.5f, 1.0f,
0.0f, -0.75f, 0.5f, 1.0f }; // END OF TRIANGLE 2
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(v), v, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
//////////
void createIndices()
{
GLushort i[] = { 0,1,2};
glGenBuffers(2, IBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(i), i, GL_STATIC_DRAW);
size = (sizeof(i)/sizeof(GLushort)); // USED IN DRAWELEMENTS
GLushort w[] = { 3,4,5};
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(w), w, GL_STATIC_DRAW);
size2 = (sizeof(i)/sizeof(GLushort)); // USED IN DRAWELEMENTS
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
/////////
void Render()
{
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader.SProgram);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0,4, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[0]);
glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_SHORT,(GLvoid*)IBO[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[1]);
glDrawElements(GL_TRIANGLES,size2,GL_UNSIGNED_SHORT,(GLvoid*)IBO[1]);
glUseProgram(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
This doesn't make sense:
glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_SHORT,(GLvoid*)IBO[0]);
^^^^^^^^^^^^^^
The parameter to glDrawElements with an IBO bound is an offset into the buffer just as it is with gl…Pointer and VBOs. You probably just want this
glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_SHORT,(GLvoid*)0);
for both your IBOs and just bind the IBO itself with
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[…]);