2 * Copyright © 2012-2021 VMware, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "../utils/colour.h"
37 static const Value none
;
38 static const Value vec2
;
39 static const Value vec3
;
40 static const Value vec4
;
48 Value(int components
, float value
)
51 for (int i
= 0; i
< components
; ++i
)
61 Value(float x
, float y
)
68 Value(float x
, float y
, float z
)
76 Value(float x
, float y
, float z
, float w
)
85 Value(const Colour
& colour
)
88 memcpy(mValue
, (const float*)colour
, sizeof(float) * 4);
91 Value(const Value
& other
)
94 memcpy(mValue
, other
.mValue
, sizeof(float) * 4);
98 static Value
create(const T
& val
)
101 *(value
.valuePtr
<T
>()) = val
;
129 float& operator[](int i
)
134 const float& operator[](int i
) const
139 operator const GLfloat
*() const
149 void add(float value
)
152 mValue
[mSize
++] = value
;
167 std::string
toTypeString() const
172 std::string type
= "vec";
173 type
.push_back((char)('0' + mSize
));
177 std::string
toString() const
179 std::stringstream ss
;
185 ss
<< "vec" << mSize
<< "(";
187 for (size_t i
= 0; i
< mSize
; ++i
) {
200 bool operator >(const Value
& rhs
) const
202 assert(mSize
== rhs
.mSize
);
204 for (size_t i
= 0; i
< mSize
; ++i
) {
205 if (! (mValue
[i
] > rhs
[i
]) )
212 bool operator <(const Value
& rhs
) const
214 assert(mSize
== rhs
.mSize
);
216 for (size_t i
= 0; i
< mSize
; ++i
) {
217 if (! (mValue
[i
] < rhs
[i
]) )
224 bool operator >=(const Value
& rhs
) const
226 assert(mSize
== rhs
.mSize
);
228 for (size_t i
= 0; i
< mSize
; ++i
) {
229 if (! (mValue
[i
] >= rhs
[i
]) )
236 bool operator <=(const Value
& rhs
) const
238 assert(mSize
== rhs
.mSize
);
240 for (size_t i
= 0; i
< mSize
; ++i
) {
241 if (! (mValue
[i
] <= rhs
[i
]) )
248 Value
& operator =(const Value
& rhs
)
251 memcpy(mValue
, rhs
.mValue
, sizeof(float) * 4);
255 Value
& operator =(const Colour
& rhs
)
258 memcpy(mValue
, (const float*)rhs
, sizeof(float) * 4);
262 Value
& operator +=(const Value
& rhs
)
268 Value
& operator -=(const Value
& rhs
)
274 Value
& operator *=(const Value
& rhs
)
280 Value
& operator /=(const Value
& rhs
)
286 Value
operator +(const Value
& rhs
) const
288 assert(mSize
== rhs
.mSize
);
291 result
.mSize
= mSize
;
293 for (size_t i
= 0; i
< mSize
; ++i
)
294 result
.mValue
[i
] = mValue
[i
] + rhs
.mValue
[i
];
299 Value
operator -(const Value
& rhs
) const
301 assert(mSize
== rhs
.mSize
);
304 result
.mSize
= mSize
;
306 for (size_t i
= 0; i
< mSize
; ++i
)
307 result
.mValue
[i
] = mValue
[i
] - rhs
.mValue
[i
];
312 Value
operator *(const Value
& rhs
) const
314 assert(mSize
== rhs
.mSize
|| rhs
.mSize
== 1);
317 result
.mSize
= mSize
;
319 if (rhs
.mSize
== 1) {
320 for (size_t i
= 0; i
< rhs
.mSize
; ++i
) {
321 result
.mValue
[i
] = mValue
[i
] * rhs
.mValue
[0];
324 for (size_t i
= 0; i
< mSize
; ++i
) {
325 result
.mValue
[i
] = mValue
[i
] * rhs
.mValue
[i
];
332 Value
operator /(const Value
& rhs
) const
334 assert(mSize
== rhs
.mSize
|| rhs
.mSize
== 1);
337 result
.mSize
= mSize
;
339 if (rhs
.mSize
== 1) {
340 for (size_t i
= 0; i
< rhs
.mSize
; ++i
) {
341 result
.mValue
[i
] = mValue
[i
] / rhs
.mValue
[0];
344 for (size_t i
= 0; i
< mSize
; ++i
) {
345 result
.mValue
[i
] = mValue
[i
] / rhs
.mValue
[i
];
357 Value
vec2(float x
, float y
);
358 Value
vec3(float x
, float y
, float z
);
359 Value
vec4(float x
, float y
, float z
, float w
);