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