blob: d879eb0630bf2b1b4102a4b94d791aee3b22b897 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/**
\file G3D/unorm16.h
\maintainer Morgan McGuire, http://graphics.cs.williams.edu
\created 2012-03-02 by Zina Cigolle
\edited 2012-03-02
Copyright 2000-2012, Morgan McGuire.
All rights reserved.
*/
#ifndef G3D_unorm16_h
#define G3D_unorm16_h
#include "G3D/platform.h"
#include "G3D/g3dmath.h"
namespace G3D {
/** Represents numbers on [0, 1] in 16 bits as an unsigned normalized
0.8 fixed-point value using the same encoding scheme as OpenGL.
Note that arithmetic operations may over and under-flow, just like
uint16 arithmetic.
OpenGL specifications can be found here:
http://www.opengl.org/registry/specs/ARB/shading_language_packing.txt
*/
G3D_BEGIN_PACKED_CLASS(1)
unorm16 {
private:
uint16 m_bits;
/** Private to prevent illogical conversions without explicitly
stating that the input should be treated as bits; see fromBits. */
explicit unorm16(uint16 b) : m_bits(b) {}
public:
/** Equivalent to: \code unorm16 u = reinterpret_cast<const unorm16&>(255);\endcode */
static unorm16 fromBits(uint16 b) {
return unorm16(b);
}
/** \copydoc fromBits */
static unorm16 reinterpretFrom(uint16 b) {
return unorm16(b);
}
unorm16() : m_bits(0) {}
unorm16(const unorm16& other) : m_bits(other.m_bits) {}
explicit unorm16(const class Any& a);
class Any toAny() const;
/** Maps f to round(f * 65535).*/
explicit unorm16(float f) {
m_bits = uint16(clamp(f, 0.0f, 1.0f) * 65535.0f + 0.5f);
}
explicit unorm16(double f) {
m_bits = uint16(clamp(f, 0.0, 1.0) * 65535.0 + 0.5);
}
/** Returns a number on [0.0f, 1.0f] */
operator float() const {
return float(m_bits) * (1.0f / 65535.0f);
}
operator double() const {
return double(m_bits) * (1.0 / 65535.0);
}
static unorm16 one() {
return fromBits(65535);
}
static unorm16 zero() {
return fromBits(0);
}
/**\brief Returns the underlying bits in this representation.
Equivalent to:
\code uint16 i = reinterpret_cast<const uint16&>(u); \endcode */
uint16 bits() const {
return m_bits;
}
/** \copydoc bits */
uint16 reinterpretAsUInt16() const {
return m_bits;
}
bool operator>(const unorm16 other) const {
return m_bits > other.m_bits;
}
bool operator<(const unorm16 other) const {
return m_bits < other.m_bits;
}
bool operator>=(const unorm16 other) const {
return m_bits >= other.m_bits;
}
bool operator<=(const unorm16 other) const {
return m_bits <= other.m_bits;
}
bool operator==(const unorm16 other) const {
return m_bits <= other.m_bits;
}
bool operator!=(const unorm16 other) const {
return m_bits != other.m_bits;
}
unorm16 operator+(const unorm16 other) const {
return unorm16(uint16(m_bits + other.m_bits));
}
unorm16& operator+=(const unorm16 other) {
m_bits += other.m_bits;
return *this;
}
unorm16 operator-(const unorm16 other) const {
return unorm16(uint16(m_bits - other.m_bits));
}
unorm16& operator-=(const unorm16 other) {
m_bits -= other.m_bits;
return *this;
}
unorm16 operator*(const int i) const {
return unorm16(uint16(m_bits * i));
}
unorm16& operator*=(const int i) {
m_bits *= i;
return *this;
}
unorm16 operator/(const int i) const {
return unorm16(uint16(m_bits / i));
}
unorm16& operator/=(const int i) {
m_bits /= i;
return *this;
}
unorm16 operator<<(const int i) const {
return unorm16((uint16)(m_bits << i));
}
unorm16& operator<<=(const int i) {
m_bits <<= i;
return *this;
}
unorm16 operator>>(const int i) const {
return unorm16(uint16(m_bits >> i));
}
unorm16& operator>>=(const int i) {
m_bits >>= i;
return *this;
}
}
G3D_END_PACKED_CLASS(1)
} // namespace G3D
#endif // G3D_unorm16
|