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