Jump to content

  • Log In with Google      Sign In   
  • Create Account

Using std::vector to load data to VBO


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Jevi   Members   -  Reputation: 127

Like
0Likes
Like

Posted 15 December 2012 - 02:13 PM

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[i].x);
  _vertexBuffer.push_back(_points[i].y);
  _colorBuffer.push_back(_points[i].c.r);
  _colorBuffer.push_back(_points[i].c.g);
  _colorBuffer.push_back(_points[i].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();

Edited by Jevi, 15 December 2012 - 04:47 PM.


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8250

Like
2Likes
Like

Posted 15 December 2012 - 02:57 PM

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.

#3 Jevi   Members   -  Reputation: 127

Like
0Likes
Like

Posted 15 December 2012 - 04:49 PM

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

#4 Brother Bob   Moderators   -  Reputation: 8250

Like
1Likes
Like

Posted 15 December 2012 - 05:05 PM

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

#5 Jevi   Members   -  Reputation: 127

Like
0Likes
Like

Posted 15 December 2012 - 05:42 PM

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

Edited by Jevi, 15 December 2012 - 05:43 PM.


#6 Jevi   Members   -  Reputation: 127

Like
0Likes
Like

Posted 15 December 2012 - 05:53 PM


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


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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS