volmetrics

diff src/volume.cc @ 6:e6485ef45e6e

visualize volume
author Eleni Maria Stea <elene.mst@gmail.com>
date Sat, 18 Jan 2014 01:57:52 +0200
parents 92c163c939be
children 928954bfefd7
line diff
     1.1 --- a/src/volume.cc	Fri Jan 17 23:45:56 2014 +0200
     1.2 +++ b/src/volume.cc	Sat Jan 18 01:57:52 2014 +0200
     1.3 @@ -1,3 +1,7 @@
     1.4 +#include <GL/glew.h>
     1.5 +
     1.6 +#include <assert.h>
     1.7 +
     1.8  #include <ctype.h>
     1.9  #include <errno.h>
    1.10  #include <stdio.h>
    1.11 @@ -14,9 +18,40 @@
    1.12  {
    1.13  	width = height = 0;
    1.14  	zaspect = 1;
    1.15 +	vol_tex_valid = false;
    1.16 +
    1.17 +	glGenTextures(1, &vol_tex);
    1.18  }
    1.19  
    1.20 -bool Volume::load_volume(const char *fname)
    1.21 +Volume::~Volume()
    1.22 +{
    1.23 +	glDeleteTextures(1, &vol_tex);
    1.24 +}
    1.25 +
    1.26 +void Volume::create_vol_tex() const
    1.27 +{
    1.28 +	if(slices.empty())
    1.29 +		return;
    1.30 +
    1.31 +	glBindTexture(GL_TEXTURE_3D, vol_tex);
    1.32 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.33 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.34 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    1.35 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    1.36 +	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    1.37 +
    1.38 +	assert(glGetError() == GL_NO_ERROR);
    1.39 +	glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, width, height,
    1.40 +			slices.size(), 0, GL_LUMINANCE, GL_FLOAT, 0);
    1.41 +	assert(glGetError() == GL_NO_ERROR);
    1.42 +
    1.43 +	for(size_t i=0; i<slices.size(); i++) {
    1.44 +		glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, width, height,
    1.45 +				1, GL_LUMINANCE, GL_FLOAT, slices[i].get_pixels());
    1.46 +	}
    1.47 +}
    1.48 +
    1.49 +bool Volume::load(const char *fname)
    1.50  {
    1.51  	FILE *fp = fopen(fname, "r");
    1.52  	if(!fp) {
    1.53 @@ -85,6 +120,49 @@
    1.54  	return true;
    1.55  }
    1.56  
    1.57 +const Image *Volume::get_slice(int num_slice) const
    1.58 +{
    1.59 +	if(num_slice >= (int)slices.size() || num_slice < 0)
    1.60 +		return 0;
    1.61 +
    1.62 +	return &slices[num_slice];
    1.63 +}
    1.64 +
    1.65 +const Image *Volume::get_slice_by_z(float z) const
    1.66 +{
    1.67 +	int idx = get_slice_idx_by_z(z);
    1.68 +	return get_slice(idx);
    1.69 +}
    1.70 +
    1.71 +int Volume::get_slice_count() const
    1.72 +{
    1.73 +	return (int)slices.size();
    1.74 +}
    1.75 +
    1.76 +int Volume::get_slice_idx_by_z(float z) const
    1.77 +{
    1.78 +	int last_slice_idx = (int)slices.size() - 1;
    1.79 +	int idx = z * last_slice_idx;
    1.80 +	if(idx < 0) {
    1.81 +		idx = 0;
    1.82 +	}
    1.83 +	else if(idx > last_slice_idx) {
    1.84 +		idx = last_slice_idx;
    1.85 +	}
    1.86 +
    1.87 +	return idx;
    1.88 +}
    1.89 +
    1.90 +unsigned int Volume::get_texture() const
    1.91 +{
    1.92 +	if(!vol_tex_valid) {
    1.93 +		create_vol_tex();
    1.94 +		vol_tex_valid = true;
    1.95 +	}
    1.96 +
    1.97 +	return vol_tex;
    1.98 +}
    1.99 +
   1.100  void Volume::draw()
   1.101  {
   1.102  }