How do I change buffer data with std::vector? - c++

So my idea is to add more data to the buffer.
My first idea was to use std::vector so I have a non fixed array, that I can add data to.
In the generation of the data:
void initData()
{
std::vector<float> vertices;
std::array<float, 12> verts = {
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f
};
for(int i = 0; i < verts.size(); i++)
vertices.push_back(verts[i]);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices), &vertices[0], GL_DYBAMIC_DRAW);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
}
this works just fine!
but then when I want to add more data:
void render()
{
std::vector<float> newVertices;
// Generates new vertices with more data, like I did in the initData()
// for-loop adding it to the new vector (newVertices)
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, newVertices.size() * sizeof(newVertices), &newVertices[0]);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLE, 0, 6 * theAmountIveGeneratedBefore);
}
that's the part that I can't seem to get to work...
what am I doing wrong??

EDIT: As pointed out in the comments, you can NOT add more data to a buffer using glBufferSubData(). So this answer only points out the mistake in calculating the size of the passed data.
sizeof(newVertices) doesn't give you what you need. The std::vector is a class and is more complicated than a simple array. Also, you don't need its whole size, actually. What you need is actually just the size of a float, since you are multiplying it by the number of elements. So just:
newVertices.size() * sizeof(float)
or
newVertices.size() * sizeof(newVertices[0])

Related

C++ OpenGL, drawing shapes is returning an error

I am trying to draw shapes onto my application. I have added #include <glad/glad.h> into my code.
I set my vertex array, vertex buffer & index buffer as unsigned ints in my header file.
In my application.h file I added this:
unsigned int m_FCvertexArray; // Textured Phong VAO
unsigned int m_FCvertexBuffer;// Textured Phong VBO
unsigned int m_FCindexBuffer; // Index buffer for texture Phong cube
In my application.cpp in my constructor I added this:
Application::Application()
{
//------------- OPENGL VALUES -----------//
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// Enabling backface culling to ensure triangle vertices are correct ordered (CCW)
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
////--------DRAW VERTICES---------//
float FCvertices[3 * 3] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
glGenVertexArrays(1, &m_FCvertexArray);
glBindVertexArray(m_FCvertexArray);
glCreateBuffers(1, &m_FCvertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_FCvertexBuffer);
//
//
glBufferData(GL_ARRAY_BUFFER, sizeof(FCvertices), FCvertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(sizeof(float) * 3));
////--------DRAW INDICES---------//
glCreateBuffers(1, &m_FCindexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_FCindexBuffer);
unsigned int indices[3] = {0, 1, 2};
glBufferData(GL_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
in my void Application::run() I added:
glUseProgram(m_FCprogram);
glBindVertexArray(m_FCvertexArray);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
now the problem is when I run the code, it gives me the error mentioned on the title:
Exception thrown at 0x000000005D78F420 (nvoglv64.dll) in Sandbox.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
I've been trying ways to fix this but it seems not to work. and if i comment out glDrawElements, the code runs and works but no shapes are drawn (obvious).
When you create the index buffer, you need to use GL_ELEMENT_ARRAY_BUFFER instead of GL_ARRAY_BUFFER.

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

Instanced drawing with texture atlas

I am currently trying to create a tilemap using instanced drawing. I have been able to draw multiple instances of a tile by sending the vertices corresponding to the geometry combined with the texture coordinates and the offsets of each tiles in a separate VBO and using the attribute divisor.
This however allows me to use only one texture. So I splitted the texture coordinates from the vertices and created a separate VBO like for the offsets, but it doesn't generate the expected results. Let's say the coordinates I send are the coordinates that match the first texture in the atlas for the first tile and the coordinates that match the second texture for the other tiles, if I use glAttributeDivisor(1, 0), all the tiles use only the first texture. If I use any other value, I get some sort of checker pattern with only one pixel of each texture used for the entire tile.
Here's what it does when the attribute divisior is 0:
And this is when the attribute divisor is at 1:
Normally, the first tile in the bottom left corner should be a grass tile, like when the divisor is 0, and all the others should be a dirt tile.
This is what I do to create the buffers:
TileMap::TileMap(std::vector<glm::vec2> positions, std::vector<glm::vec2> texCoords, glm::vec2 tileSize,
std::shared_ptr<graphics::Shader> shader,
std::shared_ptr<graphics::Texture> texture) :
m_positions(positions), m_texCoords(texCoords), m_tileSize(tileSize), m_shader(shader), m_texture(texture) {
m_model = glm::mat4();
m_model = glm::scale(m_model, glm::vec3(tileSize, 1.0f));
GLfloat vertices[] =
{
// Positions
1.0f, 1.0f, // Top Right
1.0f, 0.0f, // Bottom Right
0.0f, 0.0f, // Bottom Left
0.0f, 1.0f // Top Left
};
GLuint indices[] =
{
0, 1, 3,
1, 2, 3
};
glGenVertexArrays(1, &m_vao);
glGenBuffers(1, &m_vbo);
glGenBuffers(1, &m_ebo);
glGenBuffers(1, &m_textureVbo);
glBindBuffer(GL_ARRAY_BUFFER, m_textureVbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * m_texCoords.size(), &m_texCoords[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &m_instanceVbo);
glBindBuffer(GL_ARRAY_BUFFER, m_instanceVbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * m_positions.size(), &m_positions[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, m_textureVbo);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, m_instanceVbo);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glVertexAttribDivisor(1, 0);
glVertexAttribDivisor(2, 1);
glBindVertexArray(0);
}
And this is how I generate the tilemap:
std::vector<glm::vec2> positions;
std::vector<glm::vec2> texCoords;
bool test = false;
for(float i = 0.0f; i < 7.0f; i++) {
for(float j = 0.0f; j < 7.0f; j++) {
positions.push_back(glm::vec2(j, i));
if(test){
texCoords.push_back(glm::vec2(1.0f, 1.0f)); // Top Right
texCoords.push_back(glm::vec2(1.0f, 0.0f)); // Bottom Right
texCoords.push_back(glm::vec2(0.5f, 0.0f)); // Bottom Left
texCoords.push_back(glm::vec2(0.5f, 1.0f)); // Top Left
} else {
texCoords.push_back(glm::vec2(0.5f, 1.0f)); // Top Right
texCoords.push_back(glm::vec2(0.5f, 0.0f)); // Bottom Right
texCoords.push_back(glm::vec2(0.0f, 0.0f)); // Bottom Left
texCoords.push_back(glm::vec2(0.0f, 1.0f)); // Top Left
}
test = true; //This is just to test the texture switching after the first tile
}
}
graphics::TileMap tileMap(positions, texCoords, glm::vec2(64.0f), shader, texture);
I am not really familiar with OpenGl so I have absolutely no idea what is causing this nor what to use so that each quad read the 4 texture coordinates that should by tied to them.

OpenGL VAO,VBO,EBO accessviolation

I have following code for loading a model (here it's a triangle) and load it up into my resourcemanager:
GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f, // Left
0.5f, -0.5f, 0.0f, // Right
0.0f, 0.5f, 0.0f // Top
};
GLuint indices[] = {
0, 1, 2
};
ModelGroup* newGroup = new ModelGroup;
GLuint VBO;
glGenVertexArrays(1, &newGroup->vao);
glGenBuffers(1, &VBO);
glGenBuffers(1, &newGroup->ebo);
glBindVertexArray(newGroup->vao);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, newGroup->ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
newGroup->vbos.push_back(VBO);
vertexCount = 3;
indiciesCount = 3;
modelGroups.insert(std::pair<GLchar*, ModelGroup*>("default", newGroup));
and following code to render my triangle:
GLuint vao = model->getVAO();
GLuint count = model->getIndicesCount();
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
This code doesn't work and throws an accessviolation-error.
I don't know why ... but here is the thing
If i instead write the load-code into my resourcemanager, write the same triangle-load-code into the constructor of the renderer-class it works. What did i wrong?
PS: it also works if i use the loaded triangle from the resourcemanager and write the same load-code into the renderer-class-constructor ... that's weird
Edit: I think i found the bug ... some how the vbo-, vao-, eboIDs is only accessable in the resourcemanager and if i write the load-code into the constructor of the renderer he creates the same IDs (which shouldn't or not?)
Has that something to do with the opengl-context? And how can i fix that?
Edit2: It's the same opengl-context but the vao outside of the resourcemanager-class is not a vao(checked with glIsVertexArray). Here is more code about my resourcemanager and it's structs for more clarity (updated also the code above).
Edit3: Already made some mistakes (updated code)
Also i can't render the triangle in other classes and i checked IDs, etc.
All IDs are correct. The triangle is correctly created. But at anytime i try to access the vao outside of the loader-class it throws a violation-exception.
Its drive me crazy ... i don't know what to do
Edit4: Yaay ... i got it to work, it was just a little fail on pointers but now it works :)
class Model {
public:
struct ModelGroup {
GLuint vao;
GLuint ebo;
std::vector<GLuint> vbos;
};
private:
GLuint vertexCount;
GLuint indiciesCount;
std::map<GLchar*, ModelGroup*> modelGroups;
public:
// all the getters ...
};

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[…]);