• Create Account

## Modern client side vertex arrays

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.

5 replies to this topic

### #1Ender1618  Members

254
Like
0Likes
Like

Posted 26 June 2012 - 04:46 PM

I am working with OpenGL 4.0 right now. Im sort of a beginner with modern (non-depricated) OpenGL usage. Worked extensively with shader based DX, just not shader based OGL.

So i want a function that will draw a wire cube just for debugging purposes in immediate mode without using VBOs. This is what i have:
[source lang="cpp"]void orf::DrawBox(DebugShader* pDebugShader, vec3f minCoord, vec3f maxCoord, vec4f color, matrix4x4f* pModel/*=NULL*/, bool applyShader/*=true*/){ vec3f vertices[8]; unsigned short indices[24]; vertices[0] = vec3f(minCoord.x,minCoord.y,minCoord.z); // xyz vertices[1] = vec3f(maxCoord.x,minCoord.y,minCoord.z); // Xyz vertices[2] = vec3f(minCoord.x,maxCoord.y,minCoord.z); // xYz vertices[3] = vec3f(maxCoord.x,maxCoord.y,minCoord.z); // XYz vertices[4] = vec3f(minCoord.x,minCoord.y,maxCoord.z); // xyZ vertices[5] = vec3f(maxCoord.x,minCoord.y,maxCoord.z); // XyZ vertices[6] = vec3f(minCoord.x,maxCoord.y,maxCoord.z); // xYZ vertices[7] = vec3f(maxCoord.x,maxCoord.y,maxCoord.z); // XYZ indices[0] = 0; indices[1] = 1; indices[2] = 1; indices[3] = 3; indices[4] = 3; indices[5] = 2; indices[6] = 2; indices[7] = 0; indices[8] = 4; indices[9] = 5; indices[10] = 5; indices[11] = 7; indices[12] = 7; indices[13] = 6; indices[14] = 6; indices[15] = 4; indices[16] = 0; indices[17] = 4; indices[18] = 1; indices[19] = 5; indices[20] = 3; indices[21] = 7; indices[22] = 2; indices[23] = 6; glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3 * sizeof(float),vertices); glEnableVertexAttribArray(0); if(pModel != NULL) pDebugShader->SetWorldMatrix(*pModel); pDebugShader->SetColor(color); if(applyShader) pDebugShader->Use(); pDebugShader->ApplyParams(); glDrawElements(GL_LINES, 24, GL_UNSIGNED_SHORT, indices);}[/source]

The shader is simply this (and works for other geometry using VBOs):
[source lang="cpp"]<--VertexShader-->#version 400layout (location = 0) in vec3 VertexPosition;out vec4 Color;uniform mat4 MVP;uniform vec4 BaseColor;void main(){ Color = BaseColor; gl_Position = MVP * vec4(VertexPosition,1.0);}<--FragmentShader-->#version 400in vec4 Color;layout( location = 0 ) out vec4 FragColor;void main() { FragColor = Color;}[/source]
Is there something I am doing wrong to get client side vertex arrays working for modern 4.0 OpenGL?
VBO/VAO seemed over kill for a function that will not maintain state, and the box dimension can potentially change on each call, remember this is just for debugging purposes. Ive done this before, OGL 1.1 type vertex array and everything was fine. The usage of glVertexAttribPointer is throwing me off I think, all the samples I find use VBOs.

Is the appropriate thing to do to just create a VBO and VAO and free them at the end of the function?

Any help would be appreciated.

Thx

Edited by Ender1618, 26 June 2012 - 04:47 PM.

### #2mhagain  Members

12440
Like
0Likes
Like

Posted 26 June 2012 - 05:07 PM

Client-side arrays are deprecated from modern OpenGL, so if you've created a core context they won't work. A VBO of some kind is needed, but the news isn't all bad.

If all you're drawing is a box, but it's of variable scale and position, consider creating a 1x1x1 box centered around {0, 0, 0} in a static VBO, then sending scale and position to your shader as uniforms (each will be float[3] in your C/C++ and vec3 in your shader). You can then construct a matrix on-the-fly from these in your vertex shader (don't worry about that; it's insanely fast) and apply that to your per-vertex position. That equates to a 6 float upload to the GPU per-box, as opposed to reconstructing and needing to re-up the entire box CPU-side.

On the other hand, if you're not using a core context then glVertexAttribPointer should certainly work with client-side arrays provided you have everything else correct.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #3Ender1618  Members

254
Like
0Likes
Like

Posted 26 June 2012 - 05:40 PM

I am not using core context, and my above code is not working. Any idea why?

Is creating a VBO+VAO within the function using and then freeing them alot of overhead. The thing is this isnt only for wire boxes, i want to use the same method for other debug shapes. Id rather not have the functions rely anything other than the shader they will be using, sharing VBOs between calls will get a bit more complicated than i had intended.

### #4mhagain  Members

12440
Like
0Likes
Like

Posted 26 June 2012 - 06:00 PM

Can you describe what's going wrong?

And yes, creating, using and then destroying a full VBO/IBO/VAO set in a single function is not going to run well at all.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #5Ender1618  Members

254
Like
0Likes
Like

Posted 26 June 2012 - 06:40 PM

Can you describe what's going wrong?

And yes, creating, using and then destroying a full VBO/IBO/VAO set in a single function is not going to run well at all.

It is not rendering anything when I call that function.

### #6mhagain  Members

12440
Like
1Likes
Like

Posted 27 June 2012 - 03:35 AM

OK, I just copy/pasted your function and it works perfectly here. The cause of your problem is most likely coming from elsewhere. From looking at the code you posted, and without seeing anything else, I'd suggest that one possible cause may be that your min and max points are defined in local space for the object, and need a second transform to get them to world space before you can draw them.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

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.