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
Show differencesHistory of post edits
#1Ender1618
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
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