root/OpenSceneGraph/trunk/src/osgPlugins/osg/Texture.cpp @ 9475

Revision 9475, 15.1 kB (checked in by robert, 6 years ago)

From Andy Skinner, fixes for Solaris build

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Texture>
2#include <osg/Notify>
3#include <osg/io_utils>
4
5#include <osgDB/Registry>
6#include <osgDB/Input>
7#include <osgDB/Output>
8
9#include <stdlib.h>
10#include <string.h>
11
12using namespace osg;
13using namespace osgDB;
14
15// forward declare functions to use later.
16bool Texture_readLocalData(Object& obj, Input& fr);
17bool Texture_writeLocalData(const Object& obj, Output& fw);
18
19bool Texture_matchWrapStr(const char* str,Texture::WrapMode& wrap);
20const char* Texture_getWrapStr(Texture::WrapMode wrap);
21bool Texture_matchFilterStr(const char* str,Texture::FilterMode& filter);
22const char* Texture_getFilterStr(Texture::FilterMode filter);
23bool Texture_matchInternalFormatModeStr(const char* str,Texture::InternalFormatMode& mode);
24const char* Texture_getInternalFormatModeStr(Texture::InternalFormatMode mode);
25bool Texture_matchInternalFormatStr(const char* str,int& value);
26const char* Texture_getInternalFormatStr(int value);
27bool Texture_matchSourceTypeStr(const char* str,int& value);
28const char* Texture_getSourceTypeStr(int value);
29
30// register the read and write functions with the osgDB::Registry.
31REGISTER_DOTOSGWRAPPER(Texture)
32(
33    0,
34    "TextureBase",
35    "Object StateAttribute TextureBase",
36    &Texture_readLocalData,
37    &Texture_writeLocalData
38);
39
40
41bool Texture_readLocalData(Object& obj, Input& fr)
42{
43    bool iteratorAdvanced = false;
44
45    Texture& texture = static_cast<Texture&>(obj);
46
47    Texture::WrapMode wrap;
48    if (fr[0].matchWord("wrap_s") && Texture_matchWrapStr(fr[1].getStr(),wrap))
49    {
50        texture.setWrap(Texture::WRAP_S,wrap);
51        fr+=2;
52        iteratorAdvanced = true;
53    }
54
55    if (fr[0].matchWord("wrap_t") && Texture_matchWrapStr(fr[1].getStr(),wrap))
56    {
57        texture.setWrap(Texture::WRAP_T,wrap);
58        fr+=2;
59        iteratorAdvanced = true;
60    }
61
62    if (fr[0].matchWord("wrap_r") && Texture_matchWrapStr(fr[1].getStr(),wrap))
63    {
64        texture.setWrap(Texture::WRAP_R,wrap);
65        fr+=2;
66        iteratorAdvanced = true;
67    }
68
69    Texture::FilterMode filter;
70    if (fr[0].matchWord("min_filter") && Texture_matchFilterStr(fr[1].getStr(),filter))
71    {
72        texture.setFilter(Texture::MIN_FILTER,filter);
73        fr+=2;
74        iteratorAdvanced = true;
75    }
76
77    if (fr[0].matchWord("mag_filter") && Texture_matchFilterStr(fr[1].getStr(),filter))
78    {
79        texture.setFilter(Texture::MAG_FILTER,filter);
80        fr+=2;
81        iteratorAdvanced = true;
82    }
83
84    if (fr.matchSequence("maxAnisotropy %f"))
85    {
86        float anis=1.0f;
87        fr[1].getFloat(anis);
88        texture.setMaxAnisotropy(anis);
89        fr +=2 ;
90        iteratorAdvanced = true;
91    }
92
93    if (fr.matchSequence("borderColor %f %f %f %f"))
94    {
95        Vec4 color;
96        fr[1].getFloat(color[0]);
97        fr[2].getFloat(color[1]);
98        fr[3].getFloat(color[2]);
99        fr[4].getFloat(color[3]);
100        texture.setBorderColor(color);
101        fr +=5 ;
102        iteratorAdvanced = true;
103    }
104
105    if (fr.matchSequence("borderWidth %i"))
106    {
107        int width=0;
108        fr[1].getInt(width);
109        texture.setBorderWidth(width);
110        fr +=2 ;
111        iteratorAdvanced = true;
112    }
113
114    if (fr[0].matchWord("useHardwareMipMapGeneration"))
115    {
116        if (fr[1].matchWord("TRUE"))
117        {
118            texture.setUseHardwareMipMapGeneration(true);
119            fr +=2 ;
120            iteratorAdvanced = true;
121        }
122        else if (fr[1].matchWord("FALSE"))
123        {
124            texture.setUseHardwareMipMapGeneration(false);
125            fr +=2 ;
126            iteratorAdvanced = true;
127        }
128    }
129
130    if (fr[0].matchWord("unRefImageDataAfterApply"))
131    {
132        if (fr[1].matchWord("TRUE"))
133        {
134            texture.setUnRefImageDataAfterApply(true);
135            fr +=2 ;
136            iteratorAdvanced = true;
137        }
138        else if (fr[1].matchWord("FALSE"))
139        {
140            texture.setUnRefImageDataAfterApply(false);
141            fr +=2 ;
142            iteratorAdvanced = true;
143        }
144    }
145
146
147    Texture::InternalFormatMode mode;
148    if (fr[0].matchWord("internalFormatMode") && Texture_matchInternalFormatModeStr(fr[1].getStr(),mode))
149    {
150        texture.setInternalFormatMode(mode);
151        fr+=2;
152        iteratorAdvanced = true;
153    }
154
155    if (fr[0].matchWord("internalFormat"))
156    {
157        int value;
158        if (Texture_matchInternalFormatStr(fr[1].getStr(),value) || fr[1].getInt(value))
159        {
160            texture.setInternalFormat(value);
161            fr+=2;
162            iteratorAdvanced = true;
163        }
164    }
165
166    if (fr[0].matchWord("sourceFormat"))
167    {
168        int value;
169        if (Texture_matchInternalFormatStr(fr[1].getStr(),value) || fr[1].getInt(value))
170        {
171            texture.setSourceFormat(value);
172            fr+=2;
173            iteratorAdvanced = true;
174        }
175    }
176
177    if (fr[0].matchWord("sourceType"))
178    {
179        int value;
180        if (Texture_matchInternalFormatStr(fr[1].getStr(),value) || fr[1].getInt(value))
181        {
182            texture.setSourceType(value);
183            fr+=2;
184            iteratorAdvanced = true;
185        }
186    }
187
188    if (fr[0].matchWord("resizeNonPowerOfTwo"))
189    {
190        if (fr[1].matchWord("TRUE"))
191        {
192            texture.setResizeNonPowerOfTwoHint(true);
193            fr +=2 ;
194            iteratorAdvanced = true;
195        }
196        else if (fr[1].matchWord("FALSE"))
197        {
198            texture.setResizeNonPowerOfTwoHint(false);
199            fr +=2 ;
200            iteratorAdvanced = true;
201        }
202    }
203
204    return iteratorAdvanced;
205}
206
207
208bool Texture_writeLocalData(const Object& obj, Output& fw)
209{
210    const Texture& texture = static_cast<const Texture&>(obj);
211
212    fw.indent() << "wrap_s " << Texture_getWrapStr(texture.getWrap(Texture::WRAP_S)) << std::endl;
213    fw.indent() << "wrap_t " << Texture_getWrapStr(texture.getWrap(Texture::WRAP_T)) << std::endl;
214    fw.indent() << "wrap_r " << Texture_getWrapStr(texture.getWrap(Texture::WRAP_R)) << std::endl;
215
216    fw.indent() << "min_filter " << Texture_getFilterStr(texture.getFilter(Texture::MIN_FILTER)) << std::endl;
217    fw.indent() << "mag_filter " << Texture_getFilterStr(texture.getFilter(Texture::MAG_FILTER)) << std::endl;
218    fw.indent() << "maxAnisotropy " << texture.getMaxAnisotropy() << std::endl;
219   
220    fw.indent() << "borderColor " << texture.getBorderColor() << std::endl;
221    fw.indent() << "borderWidth " << texture.getBorderWidth() << std::endl;
222
223    fw.indent() << "useHardwareMipMapGeneration "<< (texture.getUseHardwareMipMapGeneration()?"TRUE":"FALSE") << std::endl;
224    fw.indent() << "unRefImageDataAfterApply "<< (texture.getUnRefImageDataAfterApply()?"TRUE":"FALSE") << std::endl;
225           
226    fw.indent() << "internalFormatMode " << Texture_getInternalFormatModeStr(texture.getInternalFormatMode()) << std::endl;
227    if (texture.getInternalFormatMode()==Texture::USE_USER_DEFINED_FORMAT)
228    {
229
230        const char* str = Texture_getInternalFormatStr(texture.getInternalFormat());
231
232        if (str) fw.indent() << "internalFormat " << str << std::endl;
233        else fw.indent() << "internalFormat " << texture.getInternalFormat() << std::endl;
234
235    }
236   
237    if (texture.getSourceFormat())
238    {
239        const char* str = Texture_getInternalFormatStr(texture.getSourceFormat());
240
241        if (str) fw.indent() << "sourceFormat " << str << std::endl;
242        else fw.indent() << "sourceFormat " << texture.getSourceFormat() << std::endl;
243       
244    }
245
246    if (texture.getSourceType())
247    {
248        const char* str = Texture_getSourceTypeStr(texture.getSourceType());
249
250        if (str) fw.indent() << "sourceType " << str << std::endl;
251        else fw.indent() << "sourceType " << texture.getSourceType() << std::endl;
252       
253    }
254
255    fw.indent() << "resizeNonPowerOfTwo "<< (texture.getResizeNonPowerOfTwoHint()?"TRUE":"FALSE") << std::endl;
256
257    return true;
258}
259
260
261bool Texture_matchWrapStr(const char* str,Texture::WrapMode& wrap)
262{
263    if (strcmp(str,"CLAMP")==0) wrap = Texture::CLAMP;
264    else if (strcmp(str,"CLAMP_TO_EDGE")==0) wrap = Texture::CLAMP_TO_EDGE;
265    else if (strcmp(str,"CLAMP_TO_BORDER")==0) wrap = Texture::CLAMP_TO_BORDER;
266    else if (strcmp(str,"REPEAT")==0) wrap = Texture::REPEAT;
267    else if (strcmp(str,"MIRROR")==0) wrap = Texture::MIRROR;
268    else return false;
269    return true;
270}
271
272
273const char* Texture_getWrapStr(Texture::WrapMode wrap)
274{
275    switch(wrap)
276    {
277        case(Texture::CLAMP): return "CLAMP";
278        case(Texture::CLAMP_TO_EDGE): return "CLAMP_TO_EDGE";
279        case(Texture::CLAMP_TO_BORDER): return "CLAMP_TO_BORDER";
280        case(Texture::REPEAT): return "REPEAT";
281        case(Texture::MIRROR): return "MIRROR";
282    }
283    return "";
284}
285
286
287bool Texture_matchFilterStr(const char* str,Texture::FilterMode& filter)
288{
289    if (strcmp(str,"NEAREST")==0) filter = Texture::NEAREST;
290    else if (strcmp(str,"LINEAR")==0) filter = Texture::LINEAR;
291    else if (strcmp(str,"NEAREST_MIPMAP_NEAREST")==0) filter = Texture::NEAREST_MIPMAP_NEAREST;
292    else if (strcmp(str,"LINEAR_MIPMAP_NEAREST")==0) filter = Texture::LINEAR_MIPMAP_NEAREST;
293    else if (strcmp(str,"NEAREST_MIPMAP_LINEAR")==0) filter = Texture::NEAREST_MIPMAP_LINEAR;
294    else if (strcmp(str,"LINEAR_MIPMAP_LINEAR")==0) filter = Texture::LINEAR_MIPMAP_LINEAR;
295    else if (strcmp(str,"ANISOTROPIC")==0) filter = Texture::LINEAR;
296    else return false;
297    return true;
298}
299
300
301const char* Texture_getFilterStr(Texture::FilterMode filter)
302{
303    switch(filter)
304    {
305        case(Texture::NEAREST): return "NEAREST";
306        case(Texture::LINEAR): return "LINEAR";
307        case(Texture::NEAREST_MIPMAP_NEAREST): return "NEAREST_MIPMAP_NEAREST";
308        case(Texture::LINEAR_MIPMAP_NEAREST): return "LINEAR_MIPMAP_NEAREST";
309        case(Texture::NEAREST_MIPMAP_LINEAR): return "NEAREST_MIPMAP_LINEAR";
310        case(Texture::LINEAR_MIPMAP_LINEAR): return "LINEAR_MIPMAP_LINEAR";
311    }
312    return "";
313}
314
315bool Texture_matchInternalFormatModeStr(const char* str,Texture::InternalFormatMode& mode)
316{
317    if (strcmp(str,"USE_IMAGE_DATA_FORMAT")==0)          mode = Texture::USE_IMAGE_DATA_FORMAT;
318    else if (strcmp(str,"USE_USER_DEFINED_FORMAT")==0)   mode = Texture::USE_USER_DEFINED_FORMAT;
319    else if (strcmp(str,"USE_ARB_COMPRESSION")==0)       mode = Texture::USE_ARB_COMPRESSION;
320    else if (strcmp(str,"USE_S3TC_DXT1_COMPRESSION")==0) mode = Texture::USE_S3TC_DXT1_COMPRESSION;
321    else if (strcmp(str,"USE_S3TC_DXT3_COMPRESSION")==0) mode = Texture::USE_S3TC_DXT3_COMPRESSION;
322    else if (strcmp(str,"USE_S3TC_DXT5_COMPRESSION")==0) mode = Texture::USE_S3TC_DXT5_COMPRESSION;
323    else return false;
324    return true;
325}
326
327
328const char* Texture_getInternalFormatModeStr(Texture::InternalFormatMode mode)
329{
330    switch(mode)
331    {
332        case(Texture::USE_IMAGE_DATA_FORMAT):        return "USE_IMAGE_DATA_FORMAT";
333        case(Texture::USE_USER_DEFINED_FORMAT):      return "USE_USER_DEFINED_FORMAT";
334        case(Texture::USE_ARB_COMPRESSION):          return "USE_ARB_COMPRESSION";
335        case(Texture::USE_S3TC_DXT1_COMPRESSION):    return "USE_S3TC_DXT1_COMPRESSION";
336        case(Texture::USE_S3TC_DXT3_COMPRESSION):    return "USE_S3TC_DXT3_COMPRESSION";
337        case(Texture::USE_S3TC_DXT5_COMPRESSION):    return "USE_S3TC_DXT5_COMPRESSION";
338    }
339    return "";
340}
341
342
343bool Texture_matchInternalFormatStr(const char* str,int& value)
344{
345    if (     strcmp(str,"GL_INTENSITY")==0) value = GL_INTENSITY;
346    else if (strcmp(str,"GL_LUMINANCE")==0) value = GL_LUMINANCE;
347    else if (strcmp(str,"GL_ALPHA")==0) value = GL_ALPHA;
348    else if (strcmp(str,"GL_LUMINANCE_ALPHA")==0) value = GL_LUMINANCE_ALPHA;
349    else if (strcmp(str,"GL_RGB")==0) value = GL_RGB;
350    else if (strcmp(str,"GL_RGBA")==0) value = GL_RGBA;
351    else if (strcmp(str,"GL_COMPRESSED_ALPHA_ARB")==0) value = GL_COMPRESSED_ALPHA_ARB;
352    else if (strcmp(str,"GL_COMPRESSED_LUMINANCE_ARB")==0) value = GL_COMPRESSED_LUMINANCE_ARB;
353    else if (strcmp(str,"GL_COMPRESSED_INTENSITY_ARB")==0) value = GL_COMPRESSED_INTENSITY_ARB;
354    else if (strcmp(str,"GL_COMPRESSED_LUMINANCE_ALPHA_ARB")==0) value = GL_COMPRESSED_LUMINANCE_ALPHA_ARB;
355    else if (strcmp(str,"GL_COMPRESSED_RGB_ARB")==0) value = GL_COMPRESSED_RGB_ARB;
356    else if (strcmp(str,"GL_COMPRESSED_RGBA_ARB")==0) value = GL_COMPRESSED_RGBA_ARB;
357    else if (strcmp(str,"GL_COMPRESSED_RGB_S3TC_DXT1_EXT")==0) value = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
358    else if (strcmp(str,"GL_COMPRESSED_RGBA_S3TC_DXT1_EXT")==0) value = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
359    else if (strcmp(str,"GL_COMPRESSED_RGBA_S3TC_DXT3_EXT")==0) value = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
360    else if (strcmp(str,"GL_COMPRESSED_RGBA_S3TC_DXT5_EXT")==0) value = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
361    else
362    {
363        osgDB::Field::FieldType type = osgDB::Field::calculateFieldType(str);
364        if (type==osgDB::Field::INTEGER)
365        {
366            value = atoi(str);
367            return true;
368        }
369        else
370        {
371            return false;
372        }
373    }
374
375    return true;
376}
377
378
379const char* Texture_getInternalFormatStr(int value)
380{
381    switch(value)
382    {
383        case(GL_INTENSITY): return "GL_INTENSITY";
384        case(GL_LUMINANCE): return "GL_LUMINANCE";
385        case(GL_ALPHA): return "GL_ALPHA";
386        case(GL_LUMINANCE_ALPHA): return "GL_LUMINANCE_ALPHA";
387        case(GL_RGB): return "GL_RGB";
388        case(GL_RGBA): return "GL_RGBA";
389        case(GL_COMPRESSED_ALPHA_ARB): return "GL_COMPRESSED_ALPHA_ARB";
390        case(GL_COMPRESSED_LUMINANCE_ARB): return "GL_COMPRESSED_LUMINANCE_ARB";
391        case(GL_COMPRESSED_INTENSITY_ARB): return "GL_COMPRESSED_INTENSITY_ARB";
392        case(GL_COMPRESSED_LUMINANCE_ALPHA_ARB): return "GL_COMPRESSED_LUMINANCE_ALPHA_ARB";
393        case(GL_COMPRESSED_RGB_ARB): return "GL_COMPRESSED_RGB_ARB";
394        case(GL_COMPRESSED_RGBA_ARB): return "GL_COMPRESSED_RGBA_ARB";
395        case(GL_COMPRESSED_RGB_S3TC_DXT1_EXT): return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT";
396        case(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT): return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT";
397        case(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT): return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT";
398        case(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT): return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT";
399    }
400    return NULL;
401}
402
403bool Texture_matchSourceTypeStr(const char* str,int& value)
404{
405    if (     strcmp(str,"GL_BYTE")==0) value = GL_BYTE;
406    else if (strcmp(str,"GL_SHORT")==0) value = GL_SHORT;
407    else if (strcmp(str,"GL_INT")==0) value = GL_INT;
408    else if (strcmp(str,"GL_UNSIGNED_BYTE")==0) value = GL_UNSIGNED_BYTE;
409    else if (strcmp(str,"GL_UNSIGNED_SHORT")==0) value = GL_UNSIGNED_SHORT;
410    else if (strcmp(str,"GL_UNSIGNED_INT")==0) value = GL_UNSIGNED_INT;
411    else if (strcmp(str,"GL_FLOAT")==0) value = GL_FLOAT;
412    else
413    {
414        osgDB::Field::FieldType type = osgDB::Field::calculateFieldType(str);
415        if (type==osgDB::Field::INTEGER)
416        {
417            value = atoi(str);
418            return true;
419        }
420        else
421        {
422            return false;
423        }
424    }
425
426    return true;
427}
428
429
430const char* Texture_getSourceTypeStr(int value)
431{
432    switch(value)
433    {
434        case(GL_BYTE): return "GL_BYTE";
435        case(GL_SHORT): return "GL_SHORT";
436        case(GL_INT): return "GL_INT";
437        case(GL_FLOAT): return "GL_FLOAT";
438        case(GL_UNSIGNED_BYTE): return "GL_UNSIGNED_BYTE";
439        case(GL_UNSIGNED_SHORT): return "GL_UNSIGNED_SHORT";
440        case(GL_UNSIGNED_INT): return "GL_UNSIGNED_INT";
441    }
442    return NULL;
443}
Note: See TracBrowser for help on using the browser.