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