Hey guys. Im trying to render two methods shown below. RenderA() is using VBOs and RenderB() isnt. Im getting an EXC_BAD_ACCESS error when it reaches glDrawArrays() in RenderB().
RenderB() works fine though if i dont create and use any VBOs, ie when i comment out CreateVBOs() and RenderA().
So does this mean once you start using VBOs, all renderings must use VBOs?
Or what am I doing wrong here?
Render() {
CreateVBOs(); //glGenBuffers,glBindBuffer,glBufferData etc
RenderA();
RenderB();
}
RenderA(){
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo->indexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vbo->vertexBuffer);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), 0);
glColorPointer(4, GL_FLOAT, sizeof(Vertex), colorOffset);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLES,vbo->indexCount, GL_UNSIGNED_SHORT, bodyOffset);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
RenderB(){
static GLfloat vertices[] = {1.0,1.0,1.0,2.0,1.0,1.0};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawArrays(GL_POINTS, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);
}
Edit(Solved)
Figured it out. Apparently you have to make sure you unbind the buffer if it was binded before in order to render without vbo.
My CreateVBOs() function binded but did not unbind the buffer so thats what created bad access when RenderB() was trying to use glDrawArrays. Unbinding the buffer is just binding it to 0 like so:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Try binding GL_ELEMENT_ARRAY_BUFFER and GL_ARRAY_BUFFER to zero before doing your non-VBO draw calls.
Add the following lines of code to the end of your VBO draw calls (RenderA):
glBindBuffer(GL_ElEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Related
I'm creating a VBO, populating it with data, then that data is being rendered using the following code:
// Buffer data
glGenBuffers(1, &VBOID);
glBindBuffer(VBOID, GL_ARRAY_BUFFER); // Shouldn't these be the other way around?
glBufferData(GL_ARRAY_BUFFER, bufferSize, buffer, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, buffer);
// Draw arrays
glBindBuffer(VBOID, GL_ARRAY_BUFFER);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, bufferSize);
glDisableClientState(GL_VERTEX_ARRAY);
However, the openGL reference (https://www.opengl.org/sdk/docs/man/html/glBindBuffer.xhtml) says that the glBindBuffer function takes the target TYPE of buffer, then the buffer ID, not the other way around. When I put them in that way around, nothing draws to the screen, however when they're the 'wrong' way around, it seems to work just fine.
To clarify:
// Should be this
glBindBuffer(GL_ARRAY_BUFFER, VBOID);
// Only this works
glBindBuffer(VBOID, GL_ARRAY_BUFFER);
I feel like this is one of those really dumb issues, but I just can't see where the problem is. Could anyone shed some light on the situation?
Thanks.
It should definitely be written like this:
glBindBuffer(GL_ARRAY_BUFFER, VBOID);
I can't explain why reversing those arguments results in successful drawing. However, there's a few oddities you have in your code that you need to revise:
glVertexPointer(3, GL_FLOAT, 0, buffer);
This is immediate mode code. I don't think it's been deprecated, but the correct way to write this is
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, nullptr);
On top of that, you have two references to setting client state—glEnableClientState(GL_VERTEX_ARRAY); and glDisableClientState(GL_VERTEX_ARRAY);—These are also part of immediate mode, and should be removed.
Finally, the whole thing should be wrapped up inside a Vertex Array Object, like so:
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &VBOID);
glBindBuffer(GL_ARRAY_BUFFER, VBOID);
glBufferData(GL_ARRAY_BUFFER, bufferSize, buffer, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, nullptr);
glEnableVertexAttribArray(0);
// Draw arrays
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, num_of_vertices); //num_of_vertices is usually the number of floats in the buffer divided by the number of dimensions of the vertex, or 3 in this case, since each vertex is a vec3 object.
all tutorials online are written in opengl 3+ with intesive use of shader.
I was wondering, can I create a texture buffer object and show the texture without using shader? In very old opengl uber deprecate api calls you can write gltexcoord, I just tried this way only to see if my loader was OK, and it's alright.
Using TBO I'm encountering some problems..
Let's suppose I'm creating a quad, then after loading the image (named txtr), I will store coordinates in a vector
terrainTBO.push_back(1);
terrainTBO.push_back(1);
terrainTBO.push_back(0);
terrainTBO.push_back(1);
terrainTBO.push_back(1);
terrainTBO.push_back(0);
terrainTBO.push_back(0);
terrainTBO.push_back(0);
then, in initializing process of terrain I'm doing
(obviously I'm not writing here about vertices and indices)
glBindBuffer(GL_ARRAY_BUFFER, *texture_buffer);
glBufferData(GL_ARRAY_BUFFER, vectorSizeOf(*texture_indices), &(*texture_indices)[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
In drawing part:
glEnable(GL_TEXTURE_2D);
glhBindTexture(txtr);
glBindBuffer(GL_ARRAY_BUFFER, *vboId);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, *tboId);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *iboId);
glDrawElements(GL_TRIANGLES, (*indices).size(), GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisable(GL_TEXTURE_2D);
The quad is created correctly, but no texture is shown into.. How can I achieve my texture? Is shader mandatory in these cases?
So I wanted to implement a simple VBO to see if it was worth switching from display lists for static objects in my scene. So far, I don't think Im doing it anytime soon. So heres my problem: I can render vertices fine, but as soon as I throw in texture coordinates, it crashes. So I do a bit of experimenting and its because I'm binding a texture to the thing. I have NEVER heard of this as being a problem. Literally the exact same code, using display lists works. Here is my code:
//create a vertex buffer object for the particles to use
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//create the data, this is really sloppy
float QuadVertextData[] = {0,0,0,1,0,0,1,1,0,0,1,0};
float QuadTextureData[] = {0,1,1,1,1,0,0,0};
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*3, &QuadVertextData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//generate another buffer for the texcoords
glGenBuffers(1, &VBOT);
glBindBuffer(GL_ARRAY_BUFFER, VBOT);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*2, &QuadTextureData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
and rendering:
glUseProgram(shader);
glBindTexture(GL_TEXTURE_2D, texture);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, VBOT);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
I really have no idea why this is happening, Doing a texture without a shader crashes it, binding a shader without a texture crashes it. Any help or advice?
In a core opengl profile, you need to use VAOs in order to work with calls such as glDrawArrays and glDrawElements. A VAO (vertex array object) is a list of binding points for VBOs that encapsulates information such as data format and number of components for each VBO that is bound to it. In code it looks something like this.
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*3, &QuadVertextData, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glGenBuffers(1, &vbot);
glBindBuffer(GL_ARRAY_BUFFER, vbot);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*2, &QuadTextureData, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glBindVertexArray(0);
The 0 and 1 (the first argument to glVertexAttribPointer) specifies the generic vertex attribute index in your shader. The 2 and 3 (the second argument) specify the number of components. Your input should be defined like this in the shader in order to work with this VAO:
layout(location=0) in vec3 position;
layout(location=1) in vec2 textureCoordinates;
Before drawing anything, you just have to use the shader program, bind your texture and bind the VAO:
glUseProgram(shader);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(vao);
glDrawArrays(GL_QUADS, 0, 4);
Don't forget to set the texture location of the sampler2D uniform in the shader.
If you want to learn more about switching to the OpenGL core profile, I recommand the tutorials from http://www.opengl-tutorial.org/
So I've implemented a really quick VBO just to see if its worth switching from display lists. I've ran into a bit of trouble though when I try to put in texture coordinates. It crashes with EXC_BAD_ACCESS with glDrawArrays. Im pretty sure its the way Im rendering the VBO's, specifically in the order that Im doing it, but I've tried just about everything and haven't been able to get it to work properly. Here is my code:
//create a vertex buffer object for the particles to use
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//create the data, this is really sloppy
float QuadVertextData[] = {0,0,0,1,0,0,1,1,0,0,1,0};
float QuadTextureData[] = {0,1,1,1,1,0,0,0};
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*3, &QuadVertextData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//generate another buffer for the texcoords
glGenBuffers(1, &VBOT);
glBindBuffer(GL_ARRAY_BUFFER, VBOT);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*2, &QuadTextureData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
The rendering code:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, VBOT);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
From what I've seen on the internet, Im not doing anything wrong.
I am looking for a way to dynamically edit the data that displays for a vertex buffer object. I have tried glBufferSubData, glMapBuffer, glBufferData, and some others, however had no luck. I have found that the time consuming method is glBindBuffer. I think I am using VBOs right, but I am not completely sure. Heres some sample code of my problem:
verticesId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, verticesId);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
normalsId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, normalsId);
glBufferData(GL_ARRAY_BUFFER, normalsBuffer, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
texturesId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, texturesId);
glBufferData(GL_ARRAY_BUFFER, texturesBuffer, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
the verticesBuffer and other variables are the FloatBuffers that have the data in them. Next, I render them this way:
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, verticesId);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, texturesId);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
glDrawArrays(GL_QUADS, 0, amountOfVertices);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
Here is how I edit the VBOs:
int position = 0;
///////////////////////////////////////////////////////////////////
glBindBuffer(GL_ARRAY_BUFFER, verticesId);
mapBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY, null);
verticesBuffer = mapBuffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
verticesBuffer.position(position);
// ... edit some values in the 'vertices' float array ...
verticesBuffer.put(vertices);
verticesBuffer.rewind();
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Is there any way to speed up the glBindBuffer method, or am I doing this wrong? And also, how should I edit the data for the most efficiency.
You can speed up the drawing part by using Vertex Array Objects.
About the uploading part, your code is fine; what you can do is "double buffering": keep a copy of the data, process it using another thread (multiple processor system are frequent nowadays), and then upload with glBufferSubData.
Another option is the use of SIMD assembly, but this depends on your application.