glDrawArrays bad access - c++

I'm trying to do a simple Pong game, but I'm running into some issues. Essentially, I have an array of four points with an x & y value meant to represent a hardcoded ball, and I need to get that ball to display properly. I keep crashing when I try to use glDrawArray because the four that I'm passing in the last parameter (meant to draw four vertices) is out of bounds. Any idea why?
In my setup:
//put in vertices for ball
//point 1
ballPosArr[0] = 0.1; //x
ballPosArr[1] = 0.1; //y
//pt 2
ballPosArr[2] = -0.1;
ballPosArr[3] = 0.1;
//pt 3
ballPosArr[4] = 0.1;
ballPosArr[5] = -0.1;
//pt 4
ballPosArr[6] = -0.1;
ballPosArr[7] = 0.1;
//ball position buffer
GLuint buffer;
glGenBuffers( 1, &buffer);
glBindBuffer( GL_ARRAY_BUFFER, buffer);
glBufferData( GL_ARRAY_BUFFER, 8*sizeof(GLuint), ballPosArr, GL_STATIC_DRAW );
_buffers.push_back(buffer); //_buffers is a vector of GLuint's
// Initialize the attributes from the vertex shader
GLuint bPos = glGetAttribLocation(_shaderProgram, "ballPosition" );
glEnableVertexAttribArray(bPos);
glVertexAttribPointer(bPos, 2, GL_FLOAT, GL_FALSE, 0, &ballPosArr[0]);
In my display callback:
GLuint bPos = glGetAttribLocation(_shaderProgram, "ballPosition");
glEnableVertexAttribArray(bPos);
//rebind buffers and send data again
//ball position
glBindBuffer(GL_ARRAY_BUFFER, _buffers[0]);
glVertexAttribPointer(bPos, 2, GL_FLOAT, GL_FALSE, 0, &ballPosArr[0]);
glDrawArrays( GL_POLYGON, 0, 4); //bad access error at 4
In my vshader.txt:
attribute vec2 ballPosition;
void main() {
}

If you use VBOs, which you do, the last argument of glVertexAttribPointer is a relative offset into the buffer, not the CPU address of the buffer. In your case, pass 0 for the last argument, since the vertex data you want to use is at the start of the buffer.

Related

OpenGL Vertex Buffer Object doesn't work

I've got a problem with Vertex Buffer Object, it seems it doesn't work properly.
It doesn't show anything on screen.
Here is my Code:
void gl::glRecti(int x,int y,int w,int h,glColor *color)
{
GLuint vbo = 0;
GLfloat verts[] =
{
x,y,
x,y + h,
x + w,y + h,
x + w,y,
};
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glVertexPointer( 4 , GL_FLOAT , sizeof(float) * 8, NULL );
glDrawArrays(GL_QUADS,0,4);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
}
PS: I'm very new in OpenGL programming. Any help would be appreciated.
Your vertex pointer does not make sense:
glVertexPointer( 4 , GL_FLOAT , sizeof(float) * 8, NULL );
You are telling the GL that each vertex position is specified as a 4-dimensional vector, and that the offset between two consecutive vertices is 8 floats.
What you supply is a tighlty packed array of 2-dimensional positions, so you should use 2 as the size parameter, and 2*sizeof(float) for the stride (or 0, which is a shortcut for thigly packed arrays).

Using Vertex Buffer in OpenGL

I using many polygons (in RAM), this very slowly. Say, please: how using a vertex buffer in OpenGL? (functions etc.)
(programing language - C++)
You should really read a tutorial about this like this one:
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/#The_VAO
I can give you this code snippet:
float vertices[] = {
1, 0, 0, 1,
0, 1, 0, 1,
0, 0, 1, 1,
};
GLuint vertexnumber = 3; //Amount of vertices in your array
int VertexStrideSize = 4*sizeof(float); //How much values you give for one vertex
// Create the vertex buffer object
GLuint buf;
glGenBuffers( 1, &buf ); //Create the buffer
glBindBuffer( GL_ARRAY_BUFFER, buf ); //Binding the buffer
glBufferData( GL_ARRAY_BUFFER, VertexStrideSize*vertexnumber, vertices, GL_STATIC_DRAW ); //Fill the buffer
// For your vertex shader
GLuint posLoc = glGetAttribLocation(shadername, "aPosition"); //In your shader you can use the variable aPosition now as a input with "in vec4 aPosition"
glVertexAttribPointer(posLoc, 4, GL_FLOAT, GL_FALSE, VertexStrideSize, 0);
glEnableVertexAttribArray(posLoc);

OpenGL drawing meshes incorrectly

I'm attempting to make an OpenGL Engine in C++, but cannot render meshes correctly. Meshes, when rendered, create faces that connect two random points on the mesh, or a random point on the mesh with 0,0,0.
The problem can be seen here:
(I made it a wireframe to see the problem more clearly)
Code:
// Render all meshes (Graphics.cpp)
for( int curMesh = 0; curMesh < numMesh; curMesh++ ) {
// Save pointer of buffer
meshes[curMesh]->updatebuf();
Buffer buffer = meshes[curMesh]->buffer;
// Update model matrix
glm::mat4 mvp = Proj*View*(meshes[curMesh]->model);
// Initialize vertex array
glBindBuffer( GL_ARRAY_BUFFER, vertbuffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(GLfloat)*buffer.numcoords*3, meshes[curMesh]->verts, GL_STATIC_DRAW );
// Pass information to shader
GLuint posID = glGetAttribLocation( shader, "s_vPosition" );
glVertexAttribPointer( posID, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 );
glEnableVertexAttribArray( posID );
// Check if texture applicable
if( meshes[curMesh]->texID != NULL && meshes[curMesh]->uvs != NULL ) {
// Initialize uv array
glBindBuffer( GL_ARRAY_BUFFER, uvbuffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(GLfloat)*buffer.numcoords*2, meshes[curMesh]->uvs, GL_STATIC_DRAW );
// Pass information to shader
GLuint uvID = glGetAttribLocation( shader, "s_vUV" );
glVertexAttribPointer( uvID, 2, GL_FLOAT, GL_FALSE, 0, (void*)(0) );
glEnableVertexAttribArray( uvID );
// Set mesh texture
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, meshes[curMesh]->texID );
GLuint texID = glGetUniformLocation( shader, "Sampler" );
glUniform1i( texID, 0 );
}
// Actiavte shader
glUseProgram( shader );
// Set MVP matrix
GLuint mvpID = glGetUniformLocation( shader, "MVP" );
glUniformMatrix4fv( mvpID, 1, GL_FALSE, &mvp[0][0] );
// Draw verticies on screen
bool wireframe = true;
if( wireframe )
for(int i = 0; i < buffer.numcoords; i += 3)
glDrawArrays(GL_LINE_LOOP, i, 3);
else
glDrawArrays( GL_TRIANGLES, 0, buffer.numcoords );
}
// Mesh Class (Graphics.h)
class mesh {
public:
mesh();
void updatebuf();
Buffer buffer;
GLuint texID;
bool updated;
GLfloat* verts;
GLfloat* uvs;
glm::mat4 model;
};
My Obj loading code is here: https://www.dropbox.com/s/tdcpg4vok11lf9d/ObjReader.txt (It's pretty crude and isn't organized, but should still work)
This looks like a primitive restart issue to me. Hard to tell what exactly is the problem without seeing some code. It would help a lot to see the about 20 lines above and below and including the drawing calls render the teapot. I.e. the 20 lines before the corresponding glDrawArrays, glDrawElements or glBegin call and the 20 lines after.
subtract 1 from the indices for your use, since these are 1-based indices, and you will almost certainly need 0-based indices.
This is because your triangles are not connected for the wireframe to look perfect.
In case triangles is not connected you should construct index buffer.

OpenGL Basic IBO/VBO Not Working

I've been following the tutorial listed here.
I want to draw a single triangle using an Index Buffer Object, a Vertex Buffer Object, my own Vertex and Fragment Shader, and my own vertex structure.
My problem is that nothing shows up when I draw. I'm not sure what i'm doing wrong. My shaders work fine, I've tested them without the use of ibo/vbo's and they are fine.
Here is my code:
GLuint vao[1], vbo_vertex[1], index_buffer[1];
typedef struct{
GLfloat x,y,z; // Vertex.
GLfloat r,g,b; // Colors.
} spicyVertex;
void initializeBuffers(){
spicyVertex* simple_triangle = new spicyVertex[3];
// V0 - bottom
simple_triangle[0].x = 0.0f;
simple_triangle[0].y = -0.5f;
simple_triangle[0].z = 0.0f;
simple_triangle[0].r = 1.0f;
simple_triangle[0].g = 0.0f;
simple_triangle[0].b = 0.0f;
// V1 - top right
simple_triangle[0].x = 0.5f;
simple_triangle[0].y = 0.5f;
simple_triangle[0].z = 0.0f;
simple_triangle[0].r = 1.0f;
simple_triangle[0].g = 0.0f;
simple_triangle[0].b = 0.0f;
// V2 - top left
simple_triangle[0].x = -0.5f;
simple_triangle[0].y = 0.5f;
simple_triangle[0].z = 0.0f;
simple_triangle[0].r = 1.0f;
simple_triangle[0].g = 0.0f;
simple_triangle[0].b = 0.0f;
// Setup the vertex buffer data.
glGenBuffers(1, &vbo_vertex[0]);
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex[0]);
glBufferData(GL_ARRAY_BUFFER, 3*sizeof(spicyVertex), simple_triangle, GL_STATIC_DRAW);
// Index setup
GLushort *indices = new GLushort[3];
indices[0]=0;
indices[1]=1;
indices[2]=2;
glGenBuffers(1, &index_buffer[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3*sizeof(GLushort), indices, GL_STATIC_DRAW);
// By this point all of our data should be on the graphics device.
// VAO setup.
glGenVertexArrays(1, &vao[0]);
glBindVertexArray(vao[0]);
// Bind the vertex buffer and setup pointers for the VAO.
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(spicyVertex), BUFFER_OFFSET(0));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(spicyVertex), BUFFER_OFFSET(sizeof(spicyVertex)*3));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
// Bind the index buffeer for the VAO.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer[0]);
// Cleanup.
delete [] simple_triangle;
delete [] indices;
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
}
void Draw_indexed_Vao(){
glBindVertexArray(vao[0]); // select first VAO
glDrawRangeElements(GL_TRIANGLES,0, 3, 3, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
}
static void display(void){
glUseProgramObjectARB( programObj );
Draw_indexed_Vao();
}
I'm not performing any view transformations, when I use more basic means of drawing everything shows up just fine right in front of the camera. I really do think it's something about the way I'm declaring these buffers.
EDIT 1: The application is double buffered.
EDIT 2: SOLVED. The 3 vertices V0, V1 and V2 were all modifying the same array element. As in, I wasn't using simple_triangle[0],simple_triangle[1], simple_triangle[2], but that I was only working with simple_triangle[0]. Thank you again for catching my silly error.
Adding an actual answer.
V1 and V2 are both modifying simple_triangle[0] so there is only ever one vertex.
You might need to call glFlush() in order to push the contents of the buffers you've drawn to onto the screen. Also, depending on whether you're using double buffering, the call required may be glutSwapBuffers() (if you're using GLUT) or some other swap call.
You might wanna change
' glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(spicyVertex), BUFFER_OFFSET(sizeof(spicyVertex)*3));'
to
'glColorAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(spicyVertex), BUFFER_OFFSET(sizeof(spicyVertex)*3));'
But I'm not 100% sure.

OpenGL 3.x: Access violation when using vertex buffer object and glDrawElements(...)

I have trouble rendering some geometry by using a vertex buffer object. I intend to draw a plane of points, so basically one vertex at every discrete position in my space. However, I cannot render that plane, as every time I call glDrawElements(...), application crashes returning an access violation exception. There must be some mistake while initialization, I guess.
This is what I have so far:
#define SPACE_X 512
#define SPACE_Z 512
typedef struct{
GLfloat x, y, z; // position
GLfloat nx, ny, nz; // normals
GLfloat r, g, b, a; // colors
} Vertex;
typedef struct{
GLuint i; // index
} Index;
// create vertex buffer
GLuint vertexBufferObject;
glGenBuffers(1, &vertexBufferObject);
// create index buffer
GLuint indexBufferObject;
glGenBuffers(1, &indexBufferObject);
// determine number of vertices / primitives
const int numberOfVertices = SPACE_X * SPACE_Z;
const int numberOfPrimitives = numberOfVertices; // As I'm going to render GL_POINTS, number of primitives is the same as number of vertices
// create vertex array
Vertex* vertexArray = new Vertex[numberOfVertices];
// create index array
Index* indexArray = new Index[numberOfPrimitives];
// create planes (vertex array)
// color of the vertices is red for now
int index = -1;
for(GLfloat x = -SPACE_X / 2; x < SPACE_X / 2; x++) {
for(GLfloat z = -SPACE_Z / 2; z < SPACE_Z / 2; z++) {
index++;
vertexArray[index].x = x;
vertexArray[index].y = 0.0f;
vertexArray[index].z = z;
vertexArray[index].nx = 0.0f;
vertexArray[index].ny = 0.0f;
vertexArray[index].nz = 1.0f;
vertexArray[index].r = 1.0;
vertexArray[index].g = 0.0;
vertexArray[index].b = 0.0;
vertexArray[index].a = 1.0;
}
}
// bind vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
// buffer vertex array
glBufferData(GL_ARRAY_BUFFER, numberOfVertices * sizeof(Vertex), vertexArray, GL_DTREAM_DRAW);
// bind vertex buffer again
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
// enable attrib index 0 (positions)
glEnableVertexAttribArray(0);
// pass positions in
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), vertexArray);
// enable attribute index 1 (normals)
glEnableVertexAttribArray(1);
// pass normals in
glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), &vertexArray[0].nx);
// enable attribute index 2 (colors)
glEnableVertexAttribArray(2);
// pass colors in
glVertexAttribPointer((GLuint)2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), &vertexArray[0].r);
// create index array
for(GLunit i = 0; i < numberOfPrimitives; i++) {
indexArray[i].i = i;
}
// bind buffer
glBindBuffer(GL_ELEMENET_ARRAY_BUFFER, indexBufferObject);
// buffer indices
glBufferData(GL_ELEMENET_ARRAY_BUFFER, numberOfPrimitives * sizeof(Index), indexArray, GL_STREAM_DRAW);
// bind buffer again
glBindBuffer(GL_ELEMENET_ARRAY_BUFFER, indexBufferObject);
// AND HERE IT CRASHES!
// draw plane of GL_POINTS
glDrawElements(GL_POINTS, numberOfPrimitives, GL_UNSIGNED_INT, indexArray);
// bind default buffers
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// delete vertex / index buffers
glDeleteBuffers(1, &vertexBufferObject);
glDeleteBuffers(1, &indexBufferObject);
delete[] vertexArray;
vertexArray = NULL;
delete[] indexArray;
indexArray = NULL;
When you are using buffer objects, the last parameters in the gl*Pointer and 4th parameter in glDrawElements are no longer addresses in main memory (yours still are!), but offsets into the buffer objects. Make sure to compute these offsets in bytes! The "offsetof" macro is very helpful there.
Look at the second example on this page and compare it to what you did: http://www.opengl.org/wiki/VBO_-_just_examples
And you have one typo: GL_DTREAM_DRAW.
The method glEnableClientState(...) is deprecated! Sorry, for some reason I had overseen that fact.