Skip to content

Commit

Permalink
MRView: modify Texture class to set mipmap level
Browse files Browse the repository at this point in the history
Texture class now explicitly sets the mipmap level range to 0. This
could otherwise cause problems with texture lookup since the default
GL_TEXTURE_MIN_FILTER was set to use mipmapping.
  • Loading branch information
jdtournier committed Dec 29, 2013
1 parent 465b97a commit 3afcd2c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 60 deletions.
52 changes: 17 additions & 35 deletions src/gui/mrview/image.cpp
Expand Up @@ -130,9 +130,6 @@ namespace MR
{
update_texture2D (plane, slice);

glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, interpolation);
glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, interpolation);

int x, y;
get_axes (plane, x, y);
float xdim = header().dim (x)-0.5, ydim = header().dim (y)-0.5;
Expand Down Expand Up @@ -178,12 +175,8 @@ namespace MR

void Image::render3D (Displayable::Shader& shader_program, const Projection& projection, float depth)
{

update_texture3D();

glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, interpolation);
glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, interpolation);

start (shader_program, windowing_scale_3D);
projection.set (shader_program);

Expand Down Expand Up @@ -212,18 +205,16 @@ namespace MR



inline void Image::update_texture2D (int plane, int slice)
void Image::update_texture2D (int plane, int slice)
{
if (!texture2D[plane]) { // allocate:
texture2D[plane].gen();
texture2D[plane].bind (GL_TEXTURE_3D);
glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
texture2D[plane].gen (GL_TEXTURE_3D);
texture2D[plane].bind();
}
else
texture2D[plane].bind (GL_TEXTURE_3D);
texture2D[plane].bind();
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
texture2D[plane].set_interp (interpolation);

if (position[plane] == slice && volume_unchanged())
return;
Expand Down Expand Up @@ -356,8 +347,19 @@ namespace MR



inline void Image::update_texture3D ()
void Image::update_texture3D ()
{
if (!texture3D) { // allocate:
texture3D.gen (GL_TEXTURE_3D);
texture3D.bind();
}
else
texture3D.bind();
texture3D.set_interp (interpolation);

if (volume_unchanged() && texture_mode_3D_unchanged)
return;

std::string cmap_name = ColourMap::maps[colourmap].name;

if (cmap_name == "RGB") format = GL_RGB;
Expand Down Expand Up @@ -410,31 +412,13 @@ namespace MR
}


if (volume_unchanged() && texture_mode_3D_unchanged) {
texture3D.bind (GL_TEXTURE_3D);
return;
}

if (!texture3D) { // allocate:
texture3D.gen();
texture3D.bind (GL_TEXTURE_3D);
glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
DEBUG_OPENGL;
}
else
texture3D.bind (GL_TEXTURE_3D);

texture_mode_3D_unchanged = true;

glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

DEBUG_OPENGL;
glTexImage3D (GL_TEXTURE_3D, 0, internal_format,
header().dim(0), header().dim(1), header().dim(2),
0, format, type, NULL);
DEBUG_OPENGL;

value_min = std::numeric_limits<float>::infinity();
value_max = -std::numeric_limits<float>::infinity();
Expand Down Expand Up @@ -586,7 +570,6 @@ namespace MR

++progress;
}
DEBUG_OPENGL;

windowing_scale_3D = scale_factor_3D<ValueType>();
}
Expand Down Expand Up @@ -623,7 +606,6 @@ namespace MR
0, 0, V[2],
V.dim (0), V.dim (1), 1,
GL_RG, GL_FLOAT, data);
DEBUG_OPENGL;
++progress;
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/mrview/image.h
Expand Up @@ -106,7 +106,7 @@ namespace MR
return windowing_scale_3D;
}

GLuint texture3D_index () const {
const GL::Texture& texture () const {
return texture3D;
}

Expand Down
24 changes: 7 additions & 17 deletions src/gui/mrview/mode/volume.cpp
Expand Up @@ -471,22 +471,16 @@ namespace MR
glUniform1i (glGetUniformLocation (volume_shader, "image_sampler"), 0);

glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_3D, image()->texture3D_index());

glBindTexture (GL_TEXTURE_3D, image()->texture());

glActiveTexture (GL_TEXTURE1);
if (!depth_texture) {
depth_texture.gen();
depth_texture.bind (GL_TEXTURE_2D);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_ALPHA);
depth_texture.gen (GL_TEXTURE_2D);
depth_texture.bind();
depth_texture.set_interp (GL_NEAREST);
}
else
depth_texture.bind (GL_TEXTURE_2D);
depth_texture.bind();

glReadBuffer (GL_BACK);
glCopyTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, projection.width(), projection.height(), 0);
Expand All @@ -508,11 +502,10 @@ namespace MR
glUniform3fv (glGetUniformLocation (volume_shader, ("overlay_ray"+str(n)).c_str()), 1, overlay_ray);

glActiveTexture (GL_TEXTURE2 + n);
glBindTexture (GL_TEXTURE_3D, overlays_for_3D[n]->texture3D_index());
glBindTexture (GL_TEXTURE_3D, overlays_for_3D[n]->texture());
glUniform1i (glGetUniformLocation (volume_shader, ("overlay_sampler"+str(n)).c_str()), 2+n);
overlays_for_3D[n]->set_shader_variables (volume_shader, overlays_for_3D[n]->scaling_3D(), "overlay"+str(n)+"_");
glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, overlays_for_3D[n]->interpolate() ? GL_LINEAR : GL_NEAREST);
glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, overlays_for_3D[n]->interpolate() ? GL_LINEAR : GL_NEAREST);
overlays_for_3D[n]->texture().set_interp (overlays_for_3D[n]->interpolate());
}

GL::vec4 ray_eye = M * GL::vec4 (ray, 0.0);
Expand All @@ -524,9 +517,6 @@ namespace MR
glDepthMask (GL_FALSE);
glActiveTexture (GL_TEXTURE0);

glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, image()->interpolate() ? GL_LINEAR : GL_NEAREST);
glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, image()->interpolate() ? GL_LINEAR : GL_NEAREST);

glDrawElements (GL_QUADS, 12, GL_UNSIGNED_BYTE, (void*) 0);
image()->stop (volume_shader);

Expand Down
9 changes: 4 additions & 5 deletions src/gui/opengl/font.cpp
Expand Up @@ -128,10 +128,9 @@ namespace MR
}
}

tex.gen();
tex.bind (GL_TEXTURE_2D);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
tex.gen (GL_TEXTURE_2D);
tex.bind();
tex.set_interp (false);
glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, tex_width, font_height,
0, GL_LUMINANCE_ALPHA, GL_FLOAT, tex_data);

Expand Down Expand Up @@ -201,7 +200,7 @@ namespace MR
vertex_buffer[1].bind (GL_ARRAY_BUFFER);
glBufferData (GL_ARRAY_BUFFER, sizeof (tex_pos), tex_pos, GL_STREAM_DRAW);

tex.bind (GL_TEXTURE_2D);
tex.bind();
vertex_array_object.bind();

glDrawArrays (GL_QUADS, 0, 4*text.size());
Expand Down
26 changes: 24 additions & 2 deletions src/gui/opengl/gl.h
Expand Up @@ -47,11 +47,33 @@ namespace MR
Texture () : id (0) { }
~Texture () { clear(); }
operator GLuint () const { return id; }
void gen () { if (!id) glGenTextures (1, &id); }
void gen (GLenum target) {
if (!id) {
tex_type = target;
glGenTextures (1, &id);
bind();
glTexParameteri (tex_type, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri (tex_type, GL_TEXTURE_MAX_LEVEL, 0);
glTexParameteri (tex_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (tex_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glTexParameteri (tex_type, GL_TEXTURE_WRAP_S, GL_CLAMP);
//glTexParameteri (tex_type, GL_TEXTURE_WRAP_T, GL_CLAMP);
//if (tex_type == GL_TEXTURE_3D)
//glTexParameteri (tex_type, GL_TEXTURE_WRAP_R, GL_CLAMP);
}
}
GLenum type () const { return tex_type; }
void clear () { if (id) glDeleteTextures (1, &id); id = 0; }
void bind (GLenum target) const { assert (id); glBindTexture (target, id); }
void bind () const { assert (id); glBindTexture (tex_type, id); }
void set_interp (GLint type) const {
bind();
glTexParameteri (tex_type, GL_TEXTURE_MAG_FILTER, type);
glTexParameteri (tex_type, GL_TEXTURE_MIN_FILTER, type);
}
void set_interp (bool interpolate) const { set_interp (interpolate ? GL_LINEAR : GL_NEAREST); }
protected:
GLuint id;
GLenum tex_type;
};


Expand Down

0 comments on commit 3afcd2c

Please sign in to comment.