1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /** |
---|
3 | * @file vrvrTexture.h |
---|
4 | * @brief An abtract class for the texture |
---|
5 | * @author Insoo Woo(vrinside@gmail.com) |
---|
6 | * @author PhD research assistants in PURPL at Purdue University |
---|
7 | */ |
---|
8 | |
---|
9 | #pragma once |
---|
10 | |
---|
11 | #ifdef WIN32 |
---|
12 | #pragma pointers_to_members(full_generality, single_inheritance) |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <vr3d/vr3d.h> |
---|
16 | #include <vr3d/vrEnums.h> |
---|
17 | |
---|
18 | class Vr3DExport vrTexture { |
---|
19 | friend class vrVRMLLoader; |
---|
20 | |
---|
21 | protected : |
---|
22 | /** |
---|
23 | * @brief texture target |
---|
24 | * @brief Default value is 0 |
---|
25 | */ |
---|
26 | TEXTARGET _target; |
---|
27 | |
---|
28 | /** |
---|
29 | * @brief vrTexture object id. |
---|
30 | * @brief Default value is 0 |
---|
31 | */ |
---|
32 | unsigned int _objectID; |
---|
33 | |
---|
34 | /** |
---|
35 | * @brief texture type |
---|
36 | * @brief Default value is GL_FLOAT |
---|
37 | */ |
---|
38 | DATATYPE _type; |
---|
39 | |
---|
40 | /** |
---|
41 | * @brief texture format |
---|
42 | * @brief Default value is GL_RGBA |
---|
43 | */ |
---|
44 | COLORFORMAT _colorFormat; |
---|
45 | |
---|
46 | /** |
---|
47 | * @brief texture internal format |
---|
48 | * @brief Default value is GL_RGBA |
---|
49 | */ |
---|
50 | COLORFORMAT _internalColorFormat; |
---|
51 | |
---|
52 | |
---|
53 | /** |
---|
54 | * @brief min filter |
---|
55 | * @brief Default value is R2_LINEAR |
---|
56 | */ |
---|
57 | TEXFILTER _minFilter; |
---|
58 | |
---|
59 | /** |
---|
60 | * @brief mag filter |
---|
61 | * @brief Default value is R2_LINEAR |
---|
62 | */ |
---|
63 | TEXFILTER _magFilter; |
---|
64 | |
---|
65 | /** |
---|
66 | * @brief component count |
---|
67 | */ |
---|
68 | int _compCount; |
---|
69 | |
---|
70 | |
---|
71 | public : |
---|
72 | vrTexture(); |
---|
73 | protected : |
---|
74 | virtual ~vrTexture(); |
---|
75 | |
---|
76 | public : |
---|
77 | /** |
---|
78 | * @brief bind texture id |
---|
79 | */ |
---|
80 | void bind(int index) const; |
---|
81 | |
---|
82 | /** |
---|
83 | * @brief unbind texture id |
---|
84 | */ |
---|
85 | void unbind() const; |
---|
86 | |
---|
87 | /** |
---|
88 | * @brief update pixel data |
---|
89 | * @param data pixel data |
---|
90 | */ |
---|
91 | virtual void updatePixels(void* data) = 0; |
---|
92 | |
---|
93 | /** |
---|
94 | * @brief retur texture object id |
---|
95 | */ |
---|
96 | unsigned int getGraphicsObjectID() const; |
---|
97 | |
---|
98 | /** |
---|
99 | * @brief return texture target |
---|
100 | */ |
---|
101 | TEXTARGET getTextureTarget() const; |
---|
102 | |
---|
103 | /** |
---|
104 | * @brief set min filter |
---|
105 | */ |
---|
106 | void setMinFilter(TEXFILTER filter); |
---|
107 | |
---|
108 | |
---|
109 | /** |
---|
110 | * @brief set mag filter |
---|
111 | */ |
---|
112 | void setMagFilter(TEXFILTER filter); |
---|
113 | |
---|
114 | /** |
---|
115 | * @brief return min filter |
---|
116 | */ |
---|
117 | TEXFILTER getMinFilter() const; |
---|
118 | |
---|
119 | /** |
---|
120 | * @brief return mag filter |
---|
121 | */ |
---|
122 | TEXFILTER getMagFilter() const; |
---|
123 | |
---|
124 | /** |
---|
125 | * @brief return color format |
---|
126 | */ |
---|
127 | COLORFORMAT getColorFormat() const; |
---|
128 | |
---|
129 | /** |
---|
130 | * @brief return data type |
---|
131 | */ |
---|
132 | DATATYPE getDataType() const; |
---|
133 | |
---|
134 | /** |
---|
135 | * @brief component count |
---|
136 | */ |
---|
137 | int getCompCount() const; |
---|
138 | |
---|
139 | }; |
---|
140 | |
---|
141 | |
---|
142 | inline void vrTexture::bind(int index) const |
---|
143 | { |
---|
144 | glEnable(_target); |
---|
145 | glActiveTexture(GL_TEXTURE0 + index); |
---|
146 | glBindTexture(_target, _objectID); |
---|
147 | } |
---|
148 | |
---|
149 | inline void vrTexture::unbind() const |
---|
150 | { |
---|
151 | glDisable(_target); |
---|
152 | } |
---|
153 | |
---|
154 | inline unsigned int vrTexture::getGraphicsObjectID() const |
---|
155 | { |
---|
156 | return _objectID; |
---|
157 | } |
---|
158 | |
---|
159 | inline TEXTARGET vrTexture::getTextureTarget() const |
---|
160 | { |
---|
161 | return _target; |
---|
162 | } |
---|
163 | |
---|
164 | inline void vrTexture::setMinFilter(TEXFILTER filter) |
---|
165 | { |
---|
166 | _minFilter = filter; |
---|
167 | } |
---|
168 | |
---|
169 | inline void vrTexture::setMagFilter(TEXFILTER filter) |
---|
170 | { |
---|
171 | _magFilter = filter; |
---|
172 | } |
---|
173 | |
---|
174 | inline TEXFILTER vrTexture::getMinFilter() const |
---|
175 | { |
---|
176 | return _minFilter; |
---|
177 | } |
---|
178 | |
---|
179 | inline TEXFILTER vrTexture::getMagFilter() const |
---|
180 | { |
---|
181 | return _magFilter; |
---|
182 | } |
---|
183 | |
---|
184 | inline COLORFORMAT vrTexture::getColorFormat() const |
---|
185 | { |
---|
186 | return _colorFormat; |
---|
187 | } |
---|
188 | |
---|
189 | inline DATATYPE vrTexture::getDataType() const |
---|
190 | { |
---|
191 | return _type; |
---|
192 | } |
---|
193 | |
---|
194 | inline int vrTexture::getCompCount() const |
---|
195 | { |
---|
196 | return _compCount; |
---|
197 | } |
---|
198 | |
---|
199 | inline int GetNumComponent(COLORFORMAT format) |
---|
200 | { |
---|
201 | switch (format) |
---|
202 | { |
---|
203 | case CF_LUMINANCE : return 1; |
---|
204 | case CF_RGB : return 3; |
---|
205 | case CF_RGBA : return 4; |
---|
206 | default : |
---|
207 | //R2Assert(0); |
---|
208 | ; |
---|
209 | } |
---|
210 | return 0; |
---|
211 | } |
---|
212 | |
---|
213 | inline int SizeOf(DATATYPE format) |
---|
214 | { |
---|
215 | switch (format) |
---|
216 | { |
---|
217 | case DT_UBYTE : return sizeof(unsigned char); |
---|
218 | case DT_FLOAT : return sizeof(float); |
---|
219 | #ifndef OPENGLES |
---|
220 | //case DT_UINT : return sizeof(unsigned int); |
---|
221 | //case DT_INT : return sizeof(unsigned int); |
---|
222 | #endif |
---|
223 | default : |
---|
224 | //R2Assert(0); |
---|
225 | ; |
---|
226 | } |
---|
227 | return 0; |
---|
228 | } |
---|
229 | |
---|
230 | inline bool IsPowerOfTwo(int value) |
---|
231 | { |
---|
232 | return ((value&(value-1))==0); |
---|
233 | } |
---|
234 | |
---|
235 | inline int GetNextPowerOfTwo(int value) |
---|
236 | { |
---|
237 | int nextPOT = 1; |
---|
238 | while (nextPOT < value) |
---|
239 | { |
---|
240 | nextPOT <<= 1; |
---|
241 | } |
---|
242 | |
---|
243 | return nextPOT; |
---|
244 | } |
---|
245 | |
---|