Using std::vector to load data to VBO

Started by
4 comments, last by Jevi 11 years, 4 months ago
I have tried looking for the solution but I haven't found anything discussing the whole process.

I'm trying to write my own fractal generator. What I do is I generate coordinates with a color associated with it which is loaded into vector<Point> _points. _points is then used to load the vector<float> _vertexBuffer and vector<float> _colorBuffer which are to be used to draw with the vbo. There might be a problem with the libraries that I'm linking. So suggestions on what opengl extension library I should use for vbos is welcomed. I have a feeling that I'm also not loading data in correctly from the buffers... any help is appreciated.

Here is my code, I'm sure someone could notice the problem pretty easily:



#define NO_SDL_GLEXT
#include <GL/glew.h>
#include <SDL.h>
#include <SDL_opengl.h>

...

unsigned int FractalGenerator::_vertexHandle;
unsigned int FractalGenerator::_colorHandle;
vector<float> FractalGenerator::_vertexBuffer;
vector<float> FractalGenerator::_colorBuffer;

...

struct Color
{
float r, g, b;
};
struct Point
{
float x, y;
Color c;
};

....

for (int i = 0; i < _points.size(); i++)
{
_vertexBuffer.push_back(_points.x);
_vertexBuffer.push_back(_points.y);
_colorBuffer.push_back(_points.c.r);
_colorBuffer.push_back(_points.c.g);
_colorBuffer.push_back(_points.c.b);
}

...


I'm getting a fault here, when I generate the the vbo for _vertexHandle


glGenBuffers(1, &_vertexHandle);
glBindBuffer(GL_ARRAY_BUFFER, _vertexHandle);
glBufferData(GL_ARRAY_BUFFER, _vertexBuffer.size() * sizeof(float), &_vertexBuffer.front(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &_colorHandle);
glBindBuffer(GL_ARRAY_BUFFER, _colorHandle);
glBufferData(GL_ARRAY_BUFFER, _colorBuffer.size() * sizeof(float), &_colorBuffer.front(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

...

glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glScaled(_zoomFactor, _zoomFactor, 1);
glTranslated(_xOffSet, _yOffSet, 0);
glBindBuffer(GL_ARRAY_BUFFER, _vertexHandle);
glVertexPointer(2, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, _colorHandle);
glColorPointer(3, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_POINTS, 0, _vertexBuffer.size());
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
SDL_GL_SwapBuffers();
Advertisement
When drawing, you're binding the color buffer object but setting the vertex pointer. Sounds from your description though that you are much better of drawing pixel rectangle with glDrawPixels instead of drawing the individual pixels as points.

When drawing, you're binding the color buffer object but setting the vertex pointer. Sounds from your description though that you are much better of drawing pixel rectangle with glDrawPixels instead of drawing the individual pixels as points.


Yeah I have updated the code to use glColorPointer for the color buffer but that is not where my original problem is.

Is there any reason why my code won't get passed

glGenBuffers(1, &_vertexHandle);
What do you mean it won't get passed; what isn't passed to that function? Does it not generate a unique buffer handle?

What do you mean it won't get passed; what isn't passed to that function? Does it not generate a unique buffer handle?


I'm using Microsoft Visual Studio and it gives me the option to Break or Continue. This is what the message says "Unhandled exception at 0x77a215de in FractalGenerator.exe: 0xC0000005: Access violation." at that glGenBuffer line. Am I using the correct libraries for this? Im using glew for the extensions and sdl_opengl

[quote name='Brother Bob' timestamp='1355612739' post='5011076']
What do you mean it won't get passed; what isn't passed to that function? Does it not generate a unique buffer handle?


I'm using Microsoft Visual Studio and it gives me the option to Break or Continue. This is what the message says "Unhandled exception at 0x77a215de in FractalGenerator.exe: 0xC0000005: Access violation." at that glGenBuffer line. Am I using the correct libraries for this? Im using glew for the extensions and sdl_opengl
[/quote]

I figured it out. I did not initialize glew functions. All I needed was glewInit(): and everything worked... :) thanks!

This topic is closed to new replies.

Advertisement