source: trunk/packages/vizservers/nanovis/Texture3D.cpp @ 2831

Last change on this file since 2831 was 2831, checked in by ldelgass, 9 years ago

Refactor texture classes, misc. cleanups, cut down on header pollution -- still
need to fix header deps in Makefile.in

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * ----------------------------------------------------------------------
4 * Texture3d.cpp: 3d texture class
5 *
6 * ======================================================================
7 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
8 *           Purdue Rendering and Perceptualization Lab (PURPL)
9 *
10 *  Copyright (c) 2004-2006  Purdue Research Foundation
11 *
12 *  See the file "license.terms" for information on usage and
13 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 * ======================================================================
15 */
16
17#include "Texture3D.h"
18#include "Trace.h"
19#include <stdio.h>
20#include <assert.h>
21#include <math.h>
22
23#include "config.h"
24
25Texture3D::Texture3D() :
26    gl_resource_allocated(false),
27    id(0)
28{}
29
30Texture3D::Texture3D(int width, int height, int depth,
31                     GLuint type, GLuint interp,
32                     int numComponents, void *data) :
33    gl_resource_allocated(false),
34    id(0)
35{
36    this->width = width;
37    this->height = height;
38    this->depth = depth;
39
40    //int m = (width > height) ? width : height;
41    //m = (m > depth) ? m : depth;
42
43    //int m = max(max(width, height), depth);
44    this->aspect_ratio_width = 1.;
45    this->aspect_ratio_height = (double)height/(double)width;
46    this->aspect_ratio_depth = (double)depth/(double)width;
47
48    //this->aspect_ratio_width = (double)width/(double)m;
49    //this->aspect_ratio_height = (double)height/(double)m;
50    //this->aspect_ratio_depth = (double)depth/(double)m;
51
52    this->type = type;
53    this->interp_type = interp;
54    this->n_components = numComponents;
55
56    if (data != NULL)
57        initialize(data);
58}
59
60Texture3D::~Texture3D()
61{
62    glDeleteTextures(1, &id);
63}
64
65GLuint Texture3D::initialize(void *data)
66{
67    if (id != 0)
68        glDeleteTextures(1, &id);
69
70    glGenTextures(1, &id);
71
72    update(data);
73 
74    return id;
75}
76
77void Texture3D::update(void *data)
78{
79    assert(id > 0 && id != (GLuint)-1);
80    glBindTexture(GL_TEXTURE_3D, id);
81
82    //load texture with 16 bit half floating point precision if card is 6 series NV40
83    //half float with linear interpolation is only supported by 6 series and up cards
84    //If NV40 not defined, data is quantized to 8-bit from 32-bit.
85    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
86
87    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
88    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
89    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
90
91    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, interp_type);
92    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, interp_type);
93
94    //to do: add handling to more formats
95#ifdef NV40
96    if (type == GL_FLOAT) {
97        GLuint targetFormat[5] = { -1, GL_LUMINANCE16F_ARB, GL_LUMINANCE_ALPHA16F_ARB, GL_RGB16F_ARB, GL_RGBA16F_ARB };
98        GLuint format[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
99        glTexImage3D(GL_TEXTURE_3D, 0, targetFormat[n_components],
100                     width, height, depth, 0,
101                     format[n_components], type, data);
102    } else {
103#endif
104        GLuint format[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
105        glTexImage3D(GL_TEXTURE_3D, 0, format[n_components],
106                     width, height, depth, 0,
107                     format[n_components], type, data);
108#ifdef NV40
109    }
110#endif
111
112    assert(glGetError()==0);
113
114    gl_resource_allocated = true;
115}
116
117void Texture3D::activate()
118{
119    glEnable(GL_TEXTURE_3D);
120    glBindTexture(GL_TEXTURE_3D, id);
121}
122
123void Texture3D::deactivate()
124{
125    glDisable(GL_TEXTURE_3D);           
126}
127
128void Texture3D::check_max_size()
129{
130    GLint max = 0;
131    glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &max);
132
133    TRACE("max 3d texture size: %d\n", max);
134}
135
136void Texture3D::check_max_unit()
137{
138    int max;
139    glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
140
141    TRACE("max texture units: %d.\n", max);
142}
Note: See TracBrowser for help on using the repository browser.