How to draw a volume bounding box?

I’d like to extend the volumeFiltering example from the NVIDIA CUDA SDK Code Samples 6.5, by drawing the volume bounding box.

Something like this: http://www.google.de/imgres?imgurl=http://upload.wikimedia.org/wikipedia/commons/c/cd/OpenGL_Tutorial_Bounding_box.png&imgrefurl=http://commons.wikimedia.org/wiki/File:OpenGL_Tutorial_Bounding_box.png&h=627&w=808&tbnid=MERdzy4wa9spYM:&zoom=1&tbnh=95&tbnw=122&usg=__zXEfuLICqA0lLKERtpmAY9ux4uI=&docid=L4N9NtqTTKnFjM&sa=X&ei=pODwVIr9NIaqUdCNgPAC&ved=0CDgQ9QEwBA

I was able to create the box in OpenGL but it doesn’t bound the volume.

For this I

  • first invoke the InitBoundingBox that creates the bounding box geometry
  • I call the draw_bbox method from within the display-method

The applied modelview matrix seems not to be correct. The modelview matrix is created in the display-method (from the SDK example) and is passed as argument to the draw_bbox method.

Here are my questions:

  1. Does anyone have experience with volumeFiltering example from the NVIDIA CUDA SDK and knows how to draw volume bounding box for any volume that is being displayed with that example?

  2. I also don’t understand why in the volumeFiltering-example the orthographic projection is applied.
    Can anyone please explain this?

Any help would be highly appreciated!

Below are my code snippets:

void InitBoundingBox() {
	GL_CHECK_ERRORS
	//load the shader
	shader.LoadFromFile(GL_VERTEX_SHADER, "shader.vert");
	shader.LoadFromFile(GL_FRAGMENT_SHADER, "shader.frag");
	//compile and link shader
	shader.CreateAndLinkProgram();
	shader.Use();
		//add attributes and uniforms
		shader.AddAttribute("vVertex");
		shader.AddAttribute("vColor");
		shader.AddUniform("MVP");
	shader.UnUse();

	GL_CHECK_ERRORS

	//setup triangle geometry
	//setup triangle vertices
	vertices[0].color=glm::vec3(1,0,0);
	vertices[1].color=glm::vec3(0,1,0);
	vertices[2].color=glm::vec3(0,0,1);
	vertices[3].color=glm::vec3(0,0,1);
	vertices[4].color=glm::vec3(0,0,1);
	vertices[5].color=glm::vec3(0,0,1);
	vertices[6].color=glm::vec3(0,0,1);
	vertices[7].color=glm::vec3(0,0,1);

	float boxCorners = 1.0f;
	vertices[0].position=glm::vec3(-boxCorners, -boxCorners, -boxCorners);
	vertices[1].position=glm::vec3( boxCorners, -boxCorners, -boxCorners);
	vertices[2].position=glm::vec3(boxCorners,  boxCorners, -boxCorners);
	vertices[3].position=glm::vec3(-boxCorners,  boxCorners, -boxCorners);
	vertices[4].position=glm::vec3( -boxCorners, -boxCorners,  boxCorners);
	vertices[5].position=glm::vec3(boxCorners, -boxCorners,  boxCorners);
	vertices[6].position=glm::vec3(boxCorners,  boxCorners,  boxCorners);
	vertices[7].position=glm::vec3( -boxCorners,  boxCorners,  boxCorners);
	    

	//setup triangle indices
	indices[0] = 0;
	indices[1] = 1;
	indices[2] = 2;
	indices[3] = 3;
	indices[4] = 4;
	indices[5] = 5;
	indices[6] = 6;
	indices[7] = 7;
	indices[8] = 0;
	indices[9] = 4;
	indices[10] = 1;
	indices[11] = 5;
	indices[12] = 2;
	indices[13] = 6;
	indices[14] = 3;
	indices[15] = 7;



	GL_CHECK_ERRORS

	//setup triangle vao and vbo stuff
	glGenVertexArrays(1, &vaoID);
	glGenBuffers(1, &vboVerticesID);
	glGenBuffers(1, &vboIndicesID);
	GLsizei stride = sizeof(Vertex);

	glBindVertexArray(vaoID);

		glBindBuffer (GL_ARRAY_BUFFER, vboVerticesID);
		//pass triangle verteices to buffer object
		glBufferData (GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS
		//enable vertex attribute array for position
		glEnableVertexAttribArray(shader["vVertex"]);
		glVertexAttribPointer(shader["vVertex"], 3, GL_FLOAT, GL_FALSE,stride,0);
		GL_CHECK_ERRORS
		//enable vertex attribute array for colour
		glEnableVertexAttribArray(shader["vColor"]);
		glVertexAttribPointer(shader["vColor"], 3, GL_FLOAT, GL_FALSE,stride, (const GLvoid*)offsetof(Vertex, color));
		GL_CHECK_ERRORS
		//pass indices to element array buffer
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS 

	cout<<"Initialization successfull"<<endl;
}



void draw_bbox(GLfloat* modelView) 
{

	GL_CHECK_ERRORS

	glm::mat4 MVP = glm::make_mat4(modelView);

	//bind the shader
	shader.Use();
			//pass the shader uniform
			glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
			//draw cube			
			glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_SHORT, 0);
			glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_SHORT, (GLvoid*)(4*sizeof(GLushort)));
			glDrawElements(GL_LINES, 8, GL_UNSIGNED_SHORT, (GLvoid*)(8*sizeof(GLushort)));
	//unbind the shader
	shader.UnUse();
	
}

here are my fragment- and vertex- shader:

shader.frag
=========================================================================================
#version 330 core

layout(location=0) out vec4 vFragColor;	//fragment shader output

//input form the vertex shader
smooth in vec4 vSmoothColor;		//interpolated colour to fragment shader

void main()
{
	//set the interpolated colour as the shader output
	vFragColor = vSmoothColor;
}
=========================================================================================

shader.vert
=========================================================================================
#version 330 core
 
layout(location = 0) in vec3 vVertex;	//object space vertex position
layout(location = 1) in vec3 vColor;	//per-vertex colour

//output from the vertex shader
smooth out vec4 vSmoothColor;		//smooth colour to fragment shader

//uniform
uniform mat4 MVP;	//combined modelview projection matrix

void main()
{
   //assign the per-vertex colour to vSmoothColor varying
   vSmoothColor = vec4(vColor,1);

   //get the clip space position by multiplying the combined MVP matrix with the object space 
   //vertex position
   gl_Position = MVP*vec4(vVertex,1);
}
=========================================================================================