Added osg::UByte4 class, which can be used to represent packed colors.

Changed osg::UByte4Array across to use the new osg::UByte4 class rather
than a GLuint as do previously.

Cleaned up some of the paramters in osg::Vec4.
This commit is contained in:
Robert Osfield
2002-07-15 22:18:47 +00:00
parent 5530406c9a
commit e280b50d31
3 changed files with 195 additions and 21 deletions

View File

@@ -10,11 +10,13 @@
#include <osg/Vec2>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/UByte4>
#include <vector>
namespace osg {
class ArrayVisitor;
class SG_EXPORT Array : public Object
{
@@ -51,6 +53,8 @@ class SG_EXPORT Array : public Object
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Array*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const;
virtual void accept(ArrayVisitor&) {}
Type getType() const { return _arrayType; }
@@ -88,33 +92,49 @@ class TemplateArray : public Array, public std::vector<T>
Array(ARRAYTYPE,DataSize,DataType),
std::vector<T>(ptr,ptr+no) {}
template <class InputIterator>
TemplateArray(InputIterator first,InputIterator last) :
Array(ARRAYTYPE,DataSize,DataType),
std::vector<T>(first,last) {}
// TemplateArray(T* first,T* last) :
// Array(ARRAYTYPE,DataSize,DataType),
// std::vector<T>(first,last) {}
virtual Object* cloneType() const { return osgNew TemplateArray(); }
virtual Object* clone(const CopyOp& copyop) const { return osgNew TemplateArray(*this,copyop); }
virtual void accept(ArrayVisitor& av) { av.apply(*this); }
virtual const GLvoid* dataPointer() const { if (!empty()) return &front(); else return 0; }
};
typedef TemplateArray<char,Array::ByteArrayType,1,GL_BYTE> ByteArray;
typedef TemplateArray<short,Array::ShortArrayType,1,GL_SHORT> ShortArray;
typedef TemplateArray<int,Array::IntArrayType,1,GL_INT> IntArray;
typedef TemplateArray<unsigned char,Array::UByteArrayType,1,GL_UNSIGNED_BYTE> UByteArray;
typedef TemplateArray<unsigned short,Array::UShortArrayType,1,GL_UNSIGNED_SHORT> UShortArray;
typedef TemplateArray<unsigned int,Array::UIntArrayType,1,GL_UNSIGNED_INT> UIntArray;
typedef TemplateArray<unsigned int,Array::UByte4ArrayType,4,GL_UNSIGNED_BYTE> UByte4Array;
typedef TemplateArray<float,Array::FloatArrayType,1,GL_FLOAT> FloatArray;
typedef TemplateArray<Vec2,Array::Vec2ArrayType,2,GL_FLOAT> Vec2Array;
typedef TemplateArray<Vec3,Array::Vec3ArrayType,3,GL_FLOAT> Vec3Array;
typedef TemplateArray<Vec4,Array::Vec4ArrayType,4,GL_FLOAT> Vec4Array;
typedef TemplateArray<GLbyte,Array::ByteArrayType,1,GL_BYTE> ByteArray;
typedef TemplateArray<GLshort,Array::ShortArrayType,1,GL_SHORT> ShortArray;
typedef TemplateArray<GLint,Array::IntArrayType,1,GL_INT> IntArray;
typedef TemplateArray<GLubyte,Array::UByteArrayType,1,GL_UNSIGNED_BYTE> UByteArray;
typedef TemplateArray<GLushort,Array::UShortArrayType,1,GL_UNSIGNED_SHORT> UShortArray;
typedef TemplateArray<GLuint,Array::UIntArrayType,1,GL_UNSIGNED_INT> UIntArray;
typedef TemplateArray<UByte4,Array::UByte4ArrayType,4,GL_UNSIGNED_BYTE> UByte4Array;
typedef TemplateArray<float,Array::FloatArrayType,1,GL_FLOAT> FloatArray;
typedef TemplateArray<Vec2,Array::Vec2ArrayType,2,GL_FLOAT> Vec2Array;
typedef TemplateArray<Vec3,Array::Vec3ArrayType,3,GL_FLOAT> Vec3Array;
typedef TemplateArray<Vec4,Array::Vec4ArrayType,4,GL_FLOAT> Vec4Array;
class ArrayVisitor
{
public:
ArrayVisitor() {}
virtual void apply(Array&) {}
virtual void apply(ByteArray&) {}
virtual void apply(ShortArray&) {}
virtual void apply(IntArray&) {}
virtual void apply(UByteArray&) {}
virtual void apply(UShortArray&) {}
virtual void apply(UIntArray&) {}
virtual void apply(UByte4Array&) {}
virtual void apply(FloatArray&) {}
virtual void apply(Vec2Array&) {}
virtual void apply(Vec3Array&) {}
virtual void apply(Vec4Array&) {}
};
}

155
include/osg/UByte4 Normal file
View File

@@ -0,0 +1,155 @@
//C++ header - Open Scene Graph - Copyright (C) 1998-2001 Robert Osfield
//Distributed under the terms of the GNU Library General Public License (LGPL)
//as published by the Free Software Foundation.
#ifndef OSG_UBYTE4
#define OSG_UBUTE 1
#include <osg/Vec3>
#include <iostream>
namespace osg {
/** General purpose float quad, uses include representation
of colour coordinates.
No support yet added for float * UByte4 - is it necessary?
Need to define a non-member non-friend operator* etc.
UByte4 * float is okay
*/
class UByte4
{
public:
// Methods are defined here so that they are implicitly inlined
UByte4() { _v[0]=0; _v[1]=0; _v[2]=0; _v[3]=0;}
UByte4(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
_v[0]=r; _v[1]=g; _v[2]=b; _v[3]=a;
}
unsigned char _v[4];
inline const bool operator == (const UByte4& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2] && _v[3]==v._v[3]; }
inline const bool operator != (const UByte4& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; }
inline const bool operator < (const UByte4& v) const
{
if (_v[0]<v._v[0]) return true;
else if (_v[0]>v._v[0]) return false;
else if (_v[1]<v._v[1]) return true;
else if (_v[1]>v._v[1]) return false;
else if (_v[2]<v._v[2]) return true;
else if (_v[2]>v._v[2]) return false;
else return (_v[3]<v._v[3]);
}
inline unsigned char* ptr() { return _v; }
inline const unsigned char* ptr() const { return _v; }
inline void set(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
_v[0]=r; _v[1]=g; _v[2]=b; _v[3]=a;
}
inline unsigned char& operator [] (const int i) { return _v[i]; }
inline unsigned char operator [] (const int i) const { return _v[i]; }
inline unsigned char& r() { return _v[0]; }
inline unsigned char& g() { return _v[1]; }
inline unsigned char& b() { return _v[2]; }
inline unsigned char& a() { return _v[3]; }
inline unsigned char r() const { return _v[0]; }
inline unsigned char g() const { return _v[1]; }
inline unsigned char b() const { return _v[2]; }
inline unsigned char a() const { return _v[3]; }
/// multiply by scalar
inline UByte4 operator * (const float rhs) const
{
UByte4 col(*this);
col *= rhs;
return col;
}
/// unary multiply by scalar
inline UByte4& operator *= (const float rhs)
{
_v[0]=(unsigned char)((float)_v[0]*rhs);
_v[1]=(unsigned char)((float)_v[1]*rhs);
_v[2]=(unsigned char)((float)_v[2]*rhs);
_v[3]=(unsigned char)((float)_v[3]*rhs);
return *this;
}
/// divide by scalar
inline UByte4 operator / (const float rhs) const
{
UByte4 col(*this);
col /= rhs;
return col;
}
/// unary divide by scalar
inline UByte4& operator /= (const float rhs)
{
float div = 1.0f/rhs;
*this *= div;
return *this;
}
/// binary vector add
inline UByte4 operator + (const UByte4& rhs) const
{
return UByte4(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
_v[2]+rhs._v[2], _v[3]+rhs._v[3]);
}
/** unary vector add. Slightly more efficient because no temporary
intermediate object*/
inline UByte4& operator += (const UByte4& rhs)
{
_v[0] += rhs._v[0];
_v[1] += rhs._v[1];
_v[2] += rhs._v[2];
_v[3] += rhs._v[3];
return *this;
}
/// binary vector subtract
inline UByte4 operator - (const UByte4& rhs) const
{
return UByte4(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
_v[2]-rhs._v[2], _v[3]-rhs._v[3] );
}
/// unary vector subtract
inline UByte4& operator -= (const UByte4& rhs)
{
_v[0]-=rhs._v[0];
_v[1]-=rhs._v[1];
_v[2]-=rhs._v[2];
_v[3]-=rhs._v[3];
return *this;
}
friend inline std::ostream& operator << (std::ostream& output, const UByte4& vec)
{
output << vec._v[0] << " "
<< vec._v[1] << " "
<< vec._v[2] << " "
<< vec._v[3];
return output; // to enable cascading
}
}; // end of class UByte4
} // end of namespace osg
#endif

View File

@@ -96,7 +96,6 @@ class Vec4
(unsigned long)clampTo((_v[0]*255.0f),0.0f,255.0f);
}
inline const bool valid() const { return !isNaN(); }
inline const bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]) || osg::isNaN(_v[3]); }
@@ -110,13 +109,13 @@ class Vec4
}
/// multiply by scalar
inline Vec4 operator * (const float& rhs) const
inline Vec4 operator * (const float rhs) const
{
return Vec4(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
}
/// unary multiply by scalar
inline Vec4& operator *= (const float& rhs)
inline Vec4& operator *= (const float rhs)
{
_v[0]*=rhs;
_v[1]*=rhs;
@@ -126,13 +125,13 @@ class Vec4
}
/// divide by scalar
inline Vec4 operator / (const float& rhs) const
inline Vec4 operator / (const float rhs) const
{
return Vec4(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs);
}
/// unary divide by scalar
inline Vec4& operator /= (const float& rhs)
inline Vec4& operator /= (const float rhs)
{
_v[0]/=rhs;
_v[1]/=rhs;