0

I am trying to render a large dataset of ~100 000 values in OpenGL, right now only as points, later using sprites.

My vector "positions" is ordered like this: +------------------------------------------------- | x | y | z | w | x | y | z | w | x | y | z | ... +------------------------------------------------- where the fourth component (w) is a scaling factor that is used in the vertex/fragment shaders..

VBO creation [ EDIT ]

...
v_size = positions.size();
GLint positionAttrib = _programObject->attributeLocation("in_position");

glGenVertexArrays(1, &_vaoID);
glGenBuffers(1, &_vboID);

glBindVertexArray(_vaoID);

glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glBufferData(GL_ARRAY_BUFFER, v_size*sizeof(GLfloat), &positions[0], GL_STATIC_DRAW);

glEnableVertexAttribArray(positionAttrib);
glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), 0 );
glBindVertexArray(0);

Render-stage: [ EDIT ]

This works now, but I am not sure if 100% correct, feel free to criticize:

GLint vertsToDraw = v_size / 4;
GLint positionAttrib = _programObject->attributeLocation("in_position");
// edit 1. added vao bind
glBindVertexArray(_vaoID); 
glEnableVertexAttribArray(positionAttrib);     
    glBindBuffer(GL_ARRAY_BUFFER, _vboID);
    //glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)0);
    // edit 2. no stride
    glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, (void*)0); 
    glDrawArrays(GL_POINTS, 0, vertsToDraw);
glDisableVertexAttribArray(positionAttrib);
glBindVertexArray(0);

Please let me know if any more code is needed.

Community
  • 1
  • 1
mike
  • 194
  • 1
  • 2
  • 18
  • `GLdouble* dataptr = new GLdouble[v_size]; dataptr = &positions[0];` is fundamentally wrong. You either fill array with data if it is required, or don't allocate extra array at all. What is `positions` type and data layout? What are your shaders? – keltar Aug 22 '14 at 04:46
  • First of all, don't use `GLdouble` for attributes. They way you set it up, they will be converted to float when they reach the shader. And trying to read them as `dvec4` in the shader will fail. The second thing I notice is that your buffer is to small. You trie to draw `v_size` points with 4 doubles each, but only have `v_size` doubles in the buffer. – derhass Aug 23 '14 at 17:16
  • I agree that having the extra pointer is unnecessary, just wanted to be sure that what I'm passing is proper, as I was a bit unsure about how data is stored in std::vector. @derhass : I might have been unclear, the vector `positions` is not a vector of vertex structs, how can my buffer be too small? Correct me if im wrong, v_size is precisely 4*(number of elements i want to draw) ... Are you saying that I should store structs in the vector, ie `struct vertex { float x, y, z, w; // or glm::vec4 _position; };` Because then I imagine id have to do multiply the size with 4. – mike Aug 25 '14 at 15:07
  • The count used at the `DrawArrays` call is the number of _vertices_. You set up the vertex array to consist of 4 doubles _per vertex_. Hence, you need `4 * v_size * sizeof(GLdoube)` bytes of buffer space. So either, `v_size` is the number of total values in that buffer, then the above buffer initialization is correct and the draw call is wrong, OR `v_size` is the number of vertices, then the buffer initialization is wrong, and the draw call correct. – derhass Aug 25 '14 at 15:37
  • @derhass: Yes! v_size is the total number of values, it is 402524 in size. The number of verticies I want to draw is 1/4th of that ie 100631. How should the drawcall be? – mike Aug 25 '14 at 16:05
  • @mike: As I said, you need the number of vertices in the draw call, so `v_size/4` then. – derhass Aug 25 '14 at 16:07
  • @derhass: I tried with these changes but then it crashes, I agree but cant fathom why the (see edit) first rendering works and the other crashes... – mike Aug 25 '14 at 16:13
  • Ok so I realized that there were 2 mistakes in the second call - 1. first bind the vao, then the vbo. 2. stride is supposed to be 0 (edited post) – mike Aug 25 '14 at 16:43

1 Answers1

0

Fixed everything as suggested by derhass and keltar, see comments for post. Everything works now.

mike
  • 194
  • 1
  • 2
  • 18