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

Last change on this file since 1053 was 1053, checked in by gah, 16 years ago

fixes to make compile under gcc-4.3

File size: 7.0 KB
Line 
1
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 <stdio.h>
19#include <assert.h>
20#include <math.h>
21
22#include "config.h"
23
24Texture3D::Texture3D(){ id=0; gl_resource_allocated = false; }
25
26Texture3D::Texture3D(int width, int height, int depth, GLuint type=GL_FLOAT, GLuint interp=GL_LINEAR, int components=4)
27{
28    assert(type == GL_UNSIGNED_BYTE || type == GL_FLOAT|| type ==GL_UNSIGNED_INT);
29    assert(interp == GL_LINEAR || interp == GL_NEAREST);
30       
31    this->width = width;
32    this->height = height;
33    this->depth = depth;
34
35    //int m = (width > height) ? width : height;
36    //m = (m > depth) ? m : depth;
37
38    //int m = max(max(width, height), depth);
39    this->aspect_ratio_width = 1.;
40    this->aspect_ratio_height = (double)height/(double)width;
41    this->aspect_ratio_depth = (double)depth/(double)width;
42
43    //this->aspect_ratio_width = (double)width/(double)m;
44    //this->aspect_ratio_height = (double)height/(double)m;
45    //this->aspect_ratio_depth = (double)depth/(double)m;
46
47    this->type = type;
48    this->interp_type = interp;
49    this->n_components = components;
50
51    this->id = 0;
52    gl_resource_allocated = false;
53}
54
55void Texture3D::update(float* data)
56{
57    //load texture with 16 bit half floating point precision if card is 6 series NV40
58    //half float with linear interpolation is only supported by 6 series and up cards
59    //If NV40 not defined, data is quantized to 8-bit from 32-bit.
60    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
61
62    glBindTexture(GL_TEXTURE_3D, id);
63    assert(id != (GLuint)-1);
64
65    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
66    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
67    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
68
69    if(interp_type==GL_LINEAR){
70        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
71        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
72    }
73    else{
74        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
75        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
76    }
77
78    //to do: add handling to more formats
79    if(type==GL_FLOAT){
80        switch(n_components){
81#ifdef NV40
82        case 1:
83            glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE16F_ARB, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
84            break;
85        case 2:
86            glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA16F_ARB, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
87            break;
88        case 3:
89            glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
90            break;
91        case 4:
92            glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F_ARB, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
93            break;
94#else
95        case 1:
96            glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
97            break;
98        case 2:
99            glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
100            break;
101        case 3:
102            glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
103            break;
104        case 4:
105            glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
106            break;
107#endif
108        default:
109            break;
110        }
111    }
112
113
114    assert(glGetError()==0);
115       
116    gl_resource_allocated = true;
117
118}
119
120GLuint Texture3D::initialize(float *data)
121{
122    if (id != 0) glDeleteTextures(1, &id);
123
124    //load texture with 16 bit half floating point precision if card is 6 series NV40
125    //half float with linear interpolation is only supported by 6 series and up cards
126    //If NV40 not defined, data is quantized to 8-bit from 32-bit.
127    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
128
129    glGenTextures(1, &id);
130    glBindTexture(GL_TEXTURE_3D, id);
131    assert(id != (GLuint)-1);
132
133    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
134    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
135    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
136
137    if(interp_type==GL_LINEAR){
138        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
139        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
140    }
141    else{
142        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
143        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
144    }
145
146    //to do: add handling to more formats
147    if(type==GL_FLOAT){
148        switch(n_components){
149#ifdef NV40
150        case 1:
151            glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE16F_ARB, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
152            break;
153        case 2:
154            glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA16F_ARB, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
155            break;
156        case 3:
157            glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
158            break;
159        case 4:
160            glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F_ARB, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
161            break;
162#else
163        case 1:
164            glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
165            break;
166        case 2:
167            glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
168            break;
169        case 3:
170            glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
171            break;
172        case 4:
173            glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
174            break;
175#endif
176        default:
177            break;
178        }
179    }
180
181
182    assert(glGetError()==0);
183       
184    gl_resource_allocated = true;
185    return id;
186}
187
188void Texture3D::activate()
189{
190    glEnable(GL_TEXTURE_3D);
191    glBindTexture(GL_TEXTURE_3D, id);
192}
193
194void Texture3D::deactivate()
195{
196    glDisable(GL_TEXTURE_3D);           
197}
198
199Texture3D::~Texture3D()
200{
201    glDeleteTextures(1, &id);
202}
203
204void Texture3D::check_max_size(){
205    GLint max = 0;
206    glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &max);
207       
208    //printf("%d", glGetError());
209    fprintf(stderr, "max 3d texture size: %d\n", max);
210}
211
212void Texture3D::check_max_unit(){
213    int max;
214    glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
215
216    fprintf(stderr, "max texture units: %d.\n", max);
217}
Note: See TracBrowser for help on using the repository browser.