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-2013 HUBzero Foundation, LLC |
---|
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 | #include <stdio.h> |
---|
17 | #include <assert.h> |
---|
18 | #include <math.h> |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #include "Texture3D.h" |
---|
23 | #include "Trace.h" |
---|
24 | |
---|
25 | using namespace nv; |
---|
26 | |
---|
27 | Texture3D::Texture3D() : |
---|
28 | _width(0), |
---|
29 | _height(0), |
---|
30 | _depth(0), |
---|
31 | _numComponents(3), |
---|
32 | _glResourceAllocated(false), |
---|
33 | _id(0), |
---|
34 | _type(GL_FLOAT), |
---|
35 | _interpType(GL_LINEAR), |
---|
36 | _wrapS(GL_CLAMP_TO_EDGE), |
---|
37 | _wrapT(GL_CLAMP_TO_EDGE), |
---|
38 | _wrapR(GL_CLAMP_TO_EDGE) |
---|
39 | {} |
---|
40 | |
---|
41 | Texture3D::Texture3D(int width, int height, int depth, |
---|
42 | GLuint type, GLuint interp, |
---|
43 | int numComponents, void *data) : |
---|
44 | _width(width), |
---|
45 | _height(height), |
---|
46 | _depth(depth), |
---|
47 | _numComponents(numComponents), |
---|
48 | _glResourceAllocated(false), |
---|
49 | _id(0), |
---|
50 | _type(type), |
---|
51 | _interpType(interp), |
---|
52 | _wrapS(GL_CLAMP_TO_EDGE), |
---|
53 | _wrapT(GL_CLAMP_TO_EDGE), |
---|
54 | _wrapR(GL_CLAMP_TO_EDGE) |
---|
55 | { |
---|
56 | if (data != NULL) |
---|
57 | initialize(data); |
---|
58 | } |
---|
59 | |
---|
60 | Texture3D::~Texture3D() |
---|
61 | { |
---|
62 | glDeleteTextures(1, &_id); |
---|
63 | } |
---|
64 | |
---|
65 | GLuint Texture3D::initialize(void *data) |
---|
66 | { |
---|
67 | if (_glResourceAllocated) |
---|
68 | glDeleteTextures(1, &_id); |
---|
69 | |
---|
70 | glGenTextures(1, &_id); |
---|
71 | |
---|
72 | update(data); |
---|
73 | |
---|
74 | _glResourceAllocated = true; |
---|
75 | return _id; |
---|
76 | } |
---|
77 | |
---|
78 | void Texture3D::update(void *data) |
---|
79 | { |
---|
80 | #ifdef USE_HALF_FLOAT |
---|
81 | static GLuint halfFloatFormats[] = { |
---|
82 | (GLuint)-1, GL_LUMINANCE16F_ARB, GL_LUMINANCE_ALPHA16F_ARB, |
---|
83 | GL_RGB16F_ARB, GL_RGBA16F_ARB |
---|
84 | }; |
---|
85 | #elif defined(HAVE_FLOAT_TEXTURES) |
---|
86 | static GLuint floatFormats[] = { |
---|
87 | (GLuint)-1, GL_LUMINANCE32F_ARB, GL_LUMINANCE_ALPHA32F_ARB, |
---|
88 | GL_RGB32F_ARB, GL_RGBA32F_ARB |
---|
89 | }; |
---|
90 | #endif |
---|
91 | static GLuint basicFormats[] = { |
---|
92 | (GLuint)-1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA |
---|
93 | }; |
---|
94 | |
---|
95 | glBindTexture(GL_TEXTURE_3D, _id); |
---|
96 | |
---|
97 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
---|
98 | |
---|
99 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, _wrapS); |
---|
100 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, _wrapT); |
---|
101 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, _wrapR); |
---|
102 | |
---|
103 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, _interpType); |
---|
104 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, _interpType); |
---|
105 | |
---|
106 | //to do: add handling to more formats |
---|
107 | GLuint *targetFormats; |
---|
108 | #ifdef HAVE_FLOAT_TEXTURES |
---|
109 | if (_type == GL_FLOAT) { |
---|
110 | # ifdef USE_HALF_FLOAT |
---|
111 | targetFormats = halfFloatFormats; |
---|
112 | # else |
---|
113 | targetFormats = floatFormats; |
---|
114 | # endif |
---|
115 | } else { |
---|
116 | #endif |
---|
117 | targetFormats = basicFormats; |
---|
118 | #ifdef HAVE_FLOAT_TEXTURES |
---|
119 | } |
---|
120 | #endif |
---|
121 | |
---|
122 | glTexImage3D(GL_TEXTURE_3D, 0, targetFormats[_numComponents], |
---|
123 | _width, _height, _depth, 0, |
---|
124 | basicFormats[_numComponents], _type, data); |
---|
125 | |
---|
126 | assert(glGetError()==0); |
---|
127 | } |
---|
128 | |
---|
129 | void Texture3D::activate() |
---|
130 | { |
---|
131 | glEnable(GL_TEXTURE_3D); |
---|
132 | glBindTexture(GL_TEXTURE_3D, _id); |
---|
133 | } |
---|
134 | |
---|
135 | void Texture3D::deactivate() |
---|
136 | { |
---|
137 | glDisable(GL_TEXTURE_3D); |
---|
138 | } |
---|
139 | |
---|
140 | void Texture3D::setWrapS(GLuint wrapMode) |
---|
141 | { |
---|
142 | _wrapS = wrapMode; |
---|
143 | } |
---|
144 | |
---|
145 | void Texture3D::setWrapT(GLuint wrapMode) |
---|
146 | { |
---|
147 | _wrapT = wrapMode; |
---|
148 | } |
---|
149 | |
---|
150 | void Texture3D::setWrapR(GLuint wrapMode) |
---|
151 | { |
---|
152 | _wrapR = wrapMode; |
---|
153 | } |
---|
154 | |
---|
155 | void Texture3D::checkMaxSize() |
---|
156 | { |
---|
157 | GLint max = 0; |
---|
158 | glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &max); |
---|
159 | |
---|
160 | TRACE("max 3d texture size: %d", max); |
---|
161 | } |
---|
162 | |
---|
163 | void Texture3D::checkMaxUnit() |
---|
164 | { |
---|
165 | int max; |
---|
166 | glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max); |
---|
167 | |
---|
168 | TRACE("max texture units: %d", max); |
---|
169 | } |
---|