1 #include "vmtkRender3D.h" 5 m_bInitialized =
false;
8 m_rotationMatrix.identity();
16 float volume_real_dimension[3];
18 m_maxSliceLeft = m_RefData->dims[0];
19 for(
int i = 0; i < 3; i++)
20 volume_real_dimension[i] = m_RefData->dims[i] * m_RefData->space[i];
22 float maxDimension = std::max(std::max(volume_real_dimension[0], volume_real_dimension[1]), volume_real_dimension[2]);
24 for (
int i = 0; i < 3; i++) {
25 m_refScaleFactors[i] = volume_real_dimension[i] / maxDimension;
27 m_refScaleFactors[3] = 1.0;
31 for(
int i = 0; i < 3; i++)
32 volume_real_dimension[i] = m_FloatData->dims[i] * m_FloatData->space[i];
34 maxDimension = std::max(std::max(volume_real_dimension[0], volume_real_dimension[1]), volume_real_dimension[2]);
35 for (
int i = 0; i < 3; i++) {
36 m_floatScaleFactors[i] = volume_real_dimension[i] / maxDimension;
38 m_floatScaleFactors[3] = 1.0;
40 for (
int i = 0; i < 3; i++) {
41 m_refPhyDimensions[i] = m_RefData->dims[i];
43 m_refPhyDimensions[3] = 1.0;
45 for (
int i = 0; i < 3; i++) {
46 m_floatPhyDimensions[i] = m_FloatData->dims[i];
48 m_floatPhyDimensions[3] = 1.0;
53 m_rotationMatrix = m_rotationMatrix.createRotationAroundAxis(ax,ay,az);
58 m_refThreshold = m_mapRef[(
unsigned short)(threshold)] / (pow(2, m_RefData->nbitsalloc) - 1);
64 std::cout<<
"blender: "<< blender <<std::endl;
71 this->m_iHeight = width;
72 this->m_iWidth = height;
80 m_fClipYBottom = -1.0f;
82 m_fClipZFront = -1.0f;
84 m_ColorShader = sh.carregueShaders(
"./shaders/position_is_color.vert",
"./shaders/position_is_color.frag");
85 m_RaytraceShader = sh.carregueShaders(
"./shaders/raytrace.vert",
"./shaders/castregister_with_planar_clipping.frag");
87 loadVolumetoTexture();
88 loadTransferFtoTexture();
94 m_bInitialized =
true;
97 void vmtkRender3D::createVectorPlanes()
99 v1=vmath::Vector2f(-1.0f,-1.0f);
100 v2=vmath::Vector2f(1.0f,-1.0f);
101 v3=vmath::Vector2f(1.0f,1.0f);
102 v4=vmath::Vector2f(-1.0f,1.0f);
103 vt1=vmath::Vector2f(0.0f,0.0f);
104 vt2=vmath::Vector2f(1.0f,0.0f);
105 vt3=vmath::Vector2f(1.0f,1.0f);
106 vt4=vmath::Vector2f(0.0f,1.0f);
109 void vmtkRender3D::loadVolumetoTexture()
114 int intensidade, di, iz, iy, ix, volSize;
115 unsigned short *texbuffer;
118 glActiveTexture(GL_TEXTURE0);
119 glGenTextures(1, &m_refTexture);
121 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
128 eq.EqualizeHistogram (m_RefData->dims[0], m_RefData->dims[1], m_RefData->dims[2],
129 (reinterpret_cast<unsigned short*>(m_RefData->buffer)),
130 m_RefData->nbitsalloc, m_RefData->umax, &m_mapRef);
132 volSize = m_RefData->dims[0]*m_RefData->dims[1]*m_RefData->dims[2];
133 texbuffer =
new unsigned short[volSize];
134 memset(texbuffer, 0x00, volSize*
sizeof(
unsigned short));
136 for (di=0, iz = 0; iz < m_RefData->dims[2]; iz++) {
137 for (iy =0; iy < m_RefData->dims[1]; iy++) {
138 for (ix =0; ix < m_RefData->dims[0]; ix++) {
139 intensidade = (int)((reinterpret_cast<unsigned short*>(m_RefData->buffer))[iz*m_RefData->dims[0]*m_RefData->dims[1]+iy*m_RefData->dims[0]+ix]);
140 texbuffer[di++] = (
unsigned short)(m_mapRef[intensidade]);
145 m_refThreshold = m_mapRef[(
unsigned short)(threshold)] / (pow(2, m_RefData->nbitsalloc) - 1);
147 glBindTexture(GL_TEXTURE_3D, m_refTexture);
148 glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, m_RefData->dims[0],
149 m_RefData->dims[1], m_RefData->dims[2], 0, GL_LUMINANCE,
150 GL_UNSIGNED_SHORT, texbuffer);
152 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
153 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
155 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
156 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
157 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
162 eq.EqualizeHistogram (m_FloatData->dims[0], m_FloatData->dims[1], m_FloatData->dims[2],
163 (reinterpret_cast<unsigned short*>(m_FloatData->buffer)),
164 m_FloatData->nbitsalloc, m_FloatData->umax, &m_mapFloat);
166 volSize = m_FloatData->dims[0]*m_FloatData->dims[1]*m_FloatData->dims[2];
167 texbuffer =
new unsigned short[volSize];
168 memset(texbuffer, 0x00, volSize*
sizeof(
unsigned short));
170 for (di=0, iz = 0; iz < m_FloatData->dims[2]; iz++) {
171 for (iy =0; iy < m_FloatData->dims[1]; iy++) {
172 for (ix =0; ix < m_FloatData->dims[0]; ix++) {
173 intensidade = (int)((reinterpret_cast<unsigned short*>(m_FloatData->buffer))[iz*m_FloatData->dims[0]*m_FloatData->dims[1]+iy*m_FloatData->dims[0]+ix]);
174 texbuffer[di++] = (
unsigned short)(m_mapFloat[intensidade]);
179 glActiveTexture(GL_TEXTURE1);
180 glGenTextures(1, &m_floatTexture);
181 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
184 glBindTexture(GL_TEXTURE_3D, m_floatTexture);
185 glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, m_FloatData->dims[0],
186 m_FloatData->dims[1], m_FloatData->dims[2], 0, GL_LUMINANCE,
187 GL_UNSIGNED_SHORT, texbuffer);
189 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
190 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
192 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
193 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
194 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
199 void vmtkRender3D::loadTransferFtoTexture()
206 tfunc.GetGrayScaleTF (4, 0, pow(2,m_RefData->nbitsalloc), &dim, &tf, m_RefData->nbitsalloc);
208 glActiveTexture(GL_TEXTURE2);
209 glGenTextures(1, &m_refTF);
210 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
212 glBindTexture(GL_TEXTURE_1D, m_refTF);
213 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE, dim, 0, GL_LUMINANCE,
214 GL_UNSIGNED_BYTE, tf);
216 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
217 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
219 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
220 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
221 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
224 tfunc.GetGrayScaleTF (4, 0, pow(2,m_FloatData->nbitsalloc), &dim, &tf, m_FloatData->nbitsalloc);
226 glActiveTexture(GL_TEXTURE3);
227 glGenTextures(1, &m_floatTF);
228 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
230 glBindTexture(GL_TEXTURE_1D, m_floatTF);
231 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE, dim, 0, GL_LUMINANCE,
232 GL_UNSIGNED_BYTE, tf);
234 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
235 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
237 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
238 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
239 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
242 void vmtkRender3D::createBuffers()
245 glGenVertexArrays(1, &vaosCube);
246 glGenBuffers(1, &vboCube);
247 glGenBuffers(1, &eboCube);
250 glGenVertexArrays(1, &vaosRenderPlane);
251 glGenBuffers(1, &vboRenderPlane);
252 glGenBuffers(1, &eboRenderPlane);
255 void vmtkRender3D::initRenderPlane()
257 VertexData vertices[] = {
264 GLushort indices[] = { 0, 1, 2, 3};
266 glBindVertexArray(vaosRenderPlane);
268 glBindBuffer(GL_ARRAY_BUFFER, vboRenderPlane);
269 glBufferData(GL_ARRAY_BUFFER, 4 *
sizeof(VertexData), vertices, GL_STATIC_DRAW);
272 glEnableVertexAttribArray(0);
273 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,
sizeof(VertexData), (
const void *)offset);
275 offset +=
sizeof(vmath::Vector2f);
277 glEnableVertexAttribArray(1);
278 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
sizeof(VertexData), (
const void *)offset);
280 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboRenderPlane);
281 glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 *
sizeof(GLushort), indices, GL_STATIC_DRAW);
282 glBindVertexArray(0);
289 m_modelViewMatrix=m_translationMatrix*m_rotationMatrix*m_scalationMatrix;
290 mvp = m_projectionMatrix * m_modelViewMatrix;
298 glEnable(GL_CULL_FACE);
300 glCullFace(GL_FRONT);
302 glClearColor(0.,0.,0.,1.);
303 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
304 glViewport(0,0,m_iWidth, m_iHeight);
305 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
311 glClearColor(0.,0.,0.,1.);
312 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
313 glViewport(0,0,m_iWidth,m_iHeight);
314 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
319 void vmtkRender3D::raycasting()
321 glClearColor(0.,0.,0.,1.);
322 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
323 glViewport(0,0,m_iWidth,m_iHeight);
325 glUseProgram(m_RaytraceShader);
327 glUniform1i(glGetUniformLocation(m_RaytraceShader,
"width"), m_RefData->dims[0]);
328 glUniform1i(glGetUniformLocation(m_RaytraceShader,
"height"), m_RefData->dims[1]);
329 glUniform1i(glGetUniformLocation(m_RaytraceShader,
"depth"), m_RefData->dims[2]);
331 glUniform1i(glGetUniformLocation(m_RaytraceShader,
"steps_mode"), 300);
334 glActiveTexture(GL_TEXTURE0);
335 glBindTexture(GL_TEXTURE_3D, m_refTexture);
336 glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
337 glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
338 glUniform1i(glGetUniformLocation(m_RaytraceShader,
"ref_volumetexture"),0);
340 glActiveTexture(GL_TEXTURE1);
341 glBindTexture(GL_TEXTURE_3D, m_floatTexture);
342 glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
343 glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
344 glUniform1i(glGetUniformLocation(m_RaytraceShader,
"float_volumetexture"),1);
347 glActiveTexture(GL_TEXTURE2);
348 glBindTexture(GL_TEXTURE_1D, m_refTF);
349 glUniform1i(glGetUniformLocation(m_RaytraceShader,
"ref_transferfunction"),2);
351 glActiveTexture(GL_TEXTURE3);
352 glBindTexture(GL_TEXTURE_1D, m_floatTF);
353 glUniform1i(glGetUniformLocation(m_RaytraceShader,
"float_transferfunction"),3);
356 glActiveTexture(GL_TEXTURE4);
357 glBindTexture(GL_TEXTURE_2D, this->m_BackFBO->
getTexture());
358 glUniform1i(glGetUniformLocation(m_RaytraceShader,
"backface_fbo"), 4);
360 glActiveTexture(GL_TEXTURE5);
361 glBindTexture(GL_TEXTURE_2D, this->m_FrontFBO->
getTexture());
362 glUniform1i(glGetUniformLocation(m_RaytraceShader,
"frontface_fbo"), 5);
365 glUniformMatrix4fv(glGetUniformLocation(m_RaytraceShader,
"inv_registration_matrix"), 1, GL_TRUE , m_registration_matrix_inv);
367 glUniform4fv(glGetUniformLocation(m_RaytraceShader,
"ref_phyDimensions"), 1, m_refPhyDimensions);
368 glUniform4fv(glGetUniformLocation(m_RaytraceShader,
"float_phyDimensions"), 1, m_floatPhyDimensions);
370 glUniform1f(glGetUniformLocation(m_RaytraceShader,
"noise_threshold"), m_refThreshold);
371 glUniform1f(glGetUniformLocation(m_RaytraceShader,
"blending_factor"), m_blender);
373 drawPlaneRayTraced();
377 void vmtkRender3D::drawPlaneRayTraced()
379 glBindVertexArray(vaosRenderPlane);
380 glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, 0);
381 glBindVertexArray(0);
386 const float zNear = -100.0f, zFar =100.0f;
388 m_projectionMatrix = m_projectionMatrix.createOrtho(-1.0f,1.0f,-1.0f,1.0f,zNear,zFar);
390 delete this->m_BackFBO;
391 delete this->m_FrontFBO;
396 this->m_iHeight = height;
397 this->m_iWidth = width;
405 glUseProgram(m_ColorShader);
406 GLuint
id = glGetUniformLocation(m_ColorShader,
"mvp_matrix");
407 glUniformMatrix4fv(
id, 1, GL_FALSE, mvp);
409 glUniform4fv(glGetUniformLocation(m_ColorShader,
"scaleFactors"), 1, m_refScaleFactors);
416 const float clip_x_left = m_fClipXLeft;
417 const float clip_x_right = m_fClipXRight;
418 const float clip_y_top = m_fClipYTop;
419 const float clip_y_bottom = m_fClipYBottom;
420 const float clip_z_front = m_fClipZBack;
421 const float clip_z_back = m_fClipZFront;
424 vc1=vmath::Vector3f(clip_x_left, clip_y_bottom, clip_z_back);
425 vc2=vmath::Vector3f(clip_x_left, clip_y_bottom, clip_z_front);
426 vc3=vmath::Vector3f(clip_x_left, clip_y_top, clip_z_front);
427 vc4=vmath::Vector3f(clip_x_left, clip_y_top, clip_z_back);
430 vc5=vmath::Vector3f(clip_x_left, clip_y_top, clip_z_front);
431 vc6=vmath::Vector3f(clip_x_left, clip_y_bottom, clip_z_front);
432 vc7=vmath::Vector3f(clip_x_right, clip_y_bottom, clip_z_front);
433 vc8=vmath::Vector3f(clip_x_right, clip_y_top, clip_z_front);
436 vc9=vmath::Vector3f(clip_x_right, clip_y_top, clip_z_back);
437 vc10=vmath::Vector3f(clip_x_right, clip_y_top, clip_z_front);
438 vc11=vmath::Vector3f(clip_x_right, clip_y_bottom, clip_z_front);
439 vc12=vmath::Vector3f(clip_x_right, clip_y_bottom, clip_z_back);
442 vc13=vmath::Vector3f(clip_x_right, clip_y_top, clip_z_back);
443 vc14=vmath::Vector3f(clip_x_right, clip_y_bottom, clip_z_back);
444 vc15=vmath::Vector3f(clip_x_left, clip_y_bottom, clip_z_back);
445 vc16=vmath::Vector3f(clip_x_left, clip_y_top, clip_z_back);
448 vc17=vmath::Vector3f(clip_x_left, clip_y_bottom, clip_z_back);
449 vc18=vmath::Vector3f(clip_x_right, clip_y_bottom, clip_z_back);
450 vc19=vmath::Vector3f(clip_x_right, clip_y_bottom, clip_z_front);
451 vc20=vmath::Vector3f(clip_x_left, clip_y_bottom, clip_z_front);
454 vc21=vmath::Vector3f(clip_x_left, clip_y_top, clip_z_back);
455 vc22=vmath::Vector3f(clip_x_left, clip_y_top, clip_z_front);
456 vc23=vmath::Vector3f(clip_x_right, clip_y_top, clip_z_front);
457 vc24=vmath::Vector3f(clip_x_right, clip_y_top, clip_z_back);
459 VertexDataCube vertices[] = {
486 GLushort indices[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
487 glBindVertexArray(vaosCube);
488 glBindBuffer(GL_ARRAY_BUFFER, vboCube);
489 glBufferData(GL_ARRAY_BUFFER, 24 *
sizeof(VertexDataCube), vertices, GL_DYNAMIC_DRAW);
490 glEnableVertexAttribArray(0);
491 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (
const void *) 0);
493 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboCube);
494 glBufferData(GL_ELEMENT_ARRAY_BUFFER, 24 *
sizeof(GLushort), indices, GL_DYNAMIC_DRAW);
501 std::ifstream file(s);
507 m_registration_matrix.data[k] = x;
511 std::cout <<
"Co-register matrix" << std::endl;
512 for (
int i = 0; i < 4; i++)
514 for (
int j = 0; j < 4; j++)
515 std::cout << m_registration_matrix.at(i,j) <<
" ";
516 std::cout << std::endl;
519 m_registration_matrix_inv = m_registration_matrix.inverse();
523 std::cerr <<
"Matrix not found!" << std::endl;
529 m_fClipXLeft = (left_x/m_RefData->dims[0])*2-1;
532 void vmtkRender3D::drawCube()
534 glBindVertexArray(vaosCube);
535 glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, 0);
536 glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, (
const GLvoid *)(
sizeof(GLushort)*4));
537 glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, (
const GLvoid *)(
sizeof(GLushort)*8));
538 glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, (
const GLvoid *)(
sizeof(GLushort)*12));
539 glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, (
const GLvoid *)(
sizeof(GLushort)*16));
540 glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, (
const GLvoid *)(
sizeof(GLushort)*20));
542 glBindVertexArray(0);
547 return m_maxSliceLeft;
void render()
renders the data with the specified redering mode.
void preRender(vmath::Matrix4f mvp)
renders the color cube in the fbos.
void initialize(int width, int height)
initializes 3D volume rendering
void initDrawCube()
initializes the geometry of a cube to be rendered for getting back and front depth maps...
vmtkRender3D()
vmtkRenderer3D constructor
void itlDrawColorCube(vmath::Matrix4f mvp)
draws a unit cube to get the front and the back depth maps of the volume cube, after updating the cli...
void resize(int width, int height)
resizes the dimensions of the display
GLuint getTexture() const
gets the texture bound to the FBO.
void setThreshold(int threshold)
sets the threshold.
void setBlender(float blender)
sets the blender factor.
void setClipLeftX(float left_x)
sets the clipping plane.
bool readMatrix(const char *s)
reads the co-register matrix.
void setRotation(float ax, float ay, float az)
sets the rotation matrix.
int getMaxSliceLeft()
gets the maximum number of the slices from the axis X.
void setAcquisition(Import::ImgFormat *acq1, Import::ImgFormat *acq2)
sets the volumes to be registered
bool binding()
binds the FBO to the framebuffer target.
bool releasing()
releases the FBO from the framebuffer target.