Commit 922cbfcb by Corentin Wallez

common: Add a vector arithmetic helper classes

Change-Id: I2f96baedf10d346eaa150bab04f8f6ca3ba573b9 Reviewed-on: https://chromium-review.googlesource.com/414272Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org>
parent f2a6aecf
......@@ -14,7 +14,8 @@
// http://www.opengles-book.com
#include "SampleApplication.h"
#include "Vector.h"
#include "common/vector_utils.h"
#include "shader_utils.h"
#include "random_utils.h"
#include "system_utils.h"
......@@ -100,17 +101,17 @@ class ParticleSystemSample : public SampleApplication
{
mParticles[i].lifetime = mRNG.randomFloatBetween(0.0f, 1.0f);
float endAngle = mRNG.randomFloatBetween(0, 2.0f * float(M_PI));
float endRadius = mRNG.randomFloatBetween(0.0f, 2.0f);
mParticles[i].endPosition.x = sinf(endAngle) * endRadius;
mParticles[i].endPosition.y = cosf(endAngle) * endRadius;
mParticles[i].endPosition.z = 0.0f;
float startAngle = mRNG.randomFloatBetween(0, 2.0f * float(M_PI));
float startRadius = mRNG.randomFloatBetween(0.0f, 0.25f);
mParticles[i].startPosition.x = sinf(startAngle) * startRadius;
mParticles[i].startPosition.y = cosf(startAngle) * startRadius;
mParticles[i].startPosition.z = 0.0f;
float endAngle = mRNG.randomFloatBetween(0, 2.0f * float(M_PI));
float endRadius = mRNG.randomFloatBetween(0.0f, 2.0f);
mParticles[i].endPosition.x() = sinf(endAngle) * endRadius;
mParticles[i].endPosition.y() = cosf(endAngle) * endRadius;
mParticles[i].endPosition.z() = 0.0f;
float startAngle = mRNG.randomFloatBetween(0, 2.0f * float(M_PI));
float startRadius = mRNG.randomFloatBetween(0.0f, 0.25f);
mParticles[i].startPosition.x() = sinf(startAngle) * startRadius;
mParticles[i].startPosition.y() = cosf(startAngle) * startRadius;
mParticles[i].startPosition.z() = 0.0f;
}
mParticleTime = 1.0f;
......
......@@ -17,7 +17,6 @@
#include "shader_utils.h"
#include "texture_utils.h"
#include "geometry_utils.h"
#include "Vector.h"
#include "Matrix.h"
#include <cmath>
......@@ -106,8 +105,8 @@ class PostSubBufferSample : public SampleApplication
Matrix4 perspectiveMatrix = Matrix4::perspective(60.0f, float(getWindow()->getWidth()) / getWindow()->getHeight(),
1.0f, 20.0f);
Matrix4 modelMatrix = Matrix4::translate(Vector3(0.0f, 0.0f, -2.0f)) *
Matrix4::rotate(mRotation, Vector3(1.0f, 0.0f, 1.0f));
Matrix4 modelMatrix = Matrix4::translate(angle::Vector3(0.0f, 0.0f, -2.0f)) *
Matrix4::rotate(mRotation, angle::Vector3(1.0f, 0.0f, 1.0f));
Matrix4 viewMatrix = Matrix4::identity();
......
......@@ -14,7 +14,8 @@
// http://www.opengles-book.com
#include "SampleApplication.h"
#include "Vector.h"
#include "common/vector_utils.h"
#include "shader_utils.h"
#include "texture_utils.h"
......@@ -22,6 +23,8 @@
#include <iostream>
#include <vector>
using namespace angle;
class SimpleInstancingSample : public SampleApplication
{
public:
......
......@@ -17,7 +17,6 @@
#include "shader_utils.h"
#include "texture_utils.h"
#include "geometry_utils.h"
#include "Vector.h"
class SimpleTextureCubemapSample : public SampleApplication
{
......
......@@ -14,10 +14,10 @@
// http://www.opengles-book.com
#include "SampleApplication.h"
#include "shader_utils.h"
#include "texture_utils.h"
#include "geometry_utils.h"
#include "Vector.h"
#include "Matrix.h"
#include <cmath>
......@@ -93,8 +93,8 @@ class SimpleVertexShaderSample : public SampleApplication
Matrix4 perspectiveMatrix = Matrix4::perspective(60.0f, float(getWindow()->getWidth()) / getWindow()->getHeight(),
1.0f, 20.0f);
Matrix4 modelMatrix = Matrix4::translate(Vector3(0.0f, 0.0f, -2.0f)) *
Matrix4::rotate(mRotation, Vector3(1.0f, 0.0f, 1.0f));
Matrix4 modelMatrix = Matrix4::translate(angle::Vector3(0.0f, 0.0f, -2.0f)) *
Matrix4::rotate(mRotation, angle::Vector3(1.0f, 0.0f, 1.0f));
Matrix4 viewMatrix = Matrix4::identity();
......
......@@ -33,26 +33,6 @@ namespace gl
const unsigned int Float32One = 0x3F800000;
const unsigned short Float16One = 0x3C00;
struct Vector4
{
Vector4() {}
Vector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
float x;
float y;
float z;
float w;
};
struct Vector2
{
Vector2() {}
Vector2(float x, float y) : x(x), y(y) {}
float x;
float y;
};
inline bool isPow2(int x)
{
return (x & (x - 1)) == 0 && (x != 0);
......
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// vector_utils_unittests.cpp: Unit tests for the vector utils.
//
#include "vector_utils.h"
#include <gtest/gtest.h>
using namespace angle;
namespace
{
// First test that comparing vectors work
TEST(VectorUtilsTest, Comparison)
{
// Don't use ASSERT_EQ at first because the == is more hidden
ASSERT_TRUE(Vector2(2.0, 3.0) == Vector2(2.0, 3.0));
ASSERT_TRUE(Vector2(2.0, 3.0) != Vector2(2.0, 4.0));
// Check ASSERT_EQ and ASSERT_NE work correctly
ASSERT_EQ(Vector2(2.0, 3.0), Vector2(2.0, 3.0));
ASSERT_NE(Vector2(2.0, 3.0), Vector2(2.0, 4.0));
// Check comparison works on all elements
ASSERT_EQ(Vector4(0.0), Vector4(0.0));
ASSERT_NE(Vector4(1.0, 0.0, 0.0, 0.0), Vector4(0.0));
ASSERT_NE(Vector4(0.0, 1.0, 0.0, 0.0), Vector4(0.0));
ASSERT_NE(Vector4(0.0, 0.0, 1.0, 0.0), Vector4(0.0));
ASSERT_NE(Vector4(0.0, 0.0, 0.0, 1.0), Vector4(0.0));
}
// Test indexing
TEST(VectorUtilsTest, Indexing)
{
Vector2 vec(1.0, 2.0);
ASSERT_EQ(1.0, vec[0]);
ASSERT_EQ(2.0, vec[1]);
vec[0] = 3.0;
vec[1] = 4.0;
ASSERT_EQ(Vector2(3.0, 4.0), vec);
}
// Test for the various constructors
TEST(VectorUtilsTest, Constructors)
{
// Constructor initializing all to a single element
{
Vector2 vec(3.0);
ASSERT_EQ(3.0, vec[0]);
ASSERT_EQ(3.0, vec[1]);
}
// Constructor initializing from another Vector
{
Vector2 vec(Vector2(1.0, 2.0));
ASSERT_EQ(1.0, vec[0]);
ASSERT_EQ(2.0, vec[1]);
}
// Mixed constructor
{
Vector4 vec(1.0, Vector2(2.0, 3.0), 4.0);
ASSERT_EQ(1.0, vec[0]);
ASSERT_EQ(2.0, vec[1]);
ASSERT_EQ(3.0, vec[2]);
ASSERT_EQ(4.0, vec[3]);
}
}
// Test accessing the data directly
TEST(VectorUtilsTest, DataAccess)
{
Vector2 vec(1.0, 2.0);
ASSERT_EQ(2u, vec.size());
ASSERT_EQ(1.0, vec.data()[0]);
ASSERT_EQ(2.0, vec.data()[1]);
vec.data()[0] = 3.0;
vec.data()[1] = 4.0;
ASSERT_EQ(Vector2(3.0, 4.0), vec);
}
// Test accessing the data directly
TEST(VectorUtilsTest, LoadStore)
{
float data[] = {1.0, 2.0};
Vector2 vec = Vector2::Load(data);
ASSERT_EQ(1.0, vec.data()[0]);
ASSERT_EQ(2.0, vec.data()[1]);
vec = Vector2(3.0, 4.0);
Vector2::Store(vec, data);
ASSERT_EQ(3.0, data[0]);
ASSERT_EQ(4.0, data[1]);
}
// Test basic arithmetic operations
TEST(VectorUtilsTest, BasicArithmetic)
{
ASSERT_EQ(Vector2(2.0, 3.0), +Vector2(2.0, 3.0));
ASSERT_EQ(Vector2(-2.0, -3.0), -Vector2(2.0, 3.0));
ASSERT_EQ(Vector2(4.0, 6.0), Vector2(1.0, 2.0) + Vector2(3.0, 4.0));
ASSERT_EQ(Vector2(-2.0, -2.0), Vector2(1.0, 2.0) - Vector2(3.0, 4.0));
ASSERT_EQ(Vector2(3.0, 8.0), Vector2(1.0, 2.0) * Vector2(3.0, 4.0));
ASSERT_EQ(Vector2(3.0, 2.0), Vector2(3.0, 4.0) / Vector2(1.0, 2.0));
ASSERT_EQ(Vector2(2.0, 4.0), Vector2(1.0, 2.0) * 2);
ASSERT_EQ(Vector2(2.0, 4.0), 2 * Vector2(1.0, 2.0));
ASSERT_EQ(Vector2(0.5, 1.0), Vector2(1.0, 2.0) / 2);
}
// Test compound arithmetic operations
TEST(VectorUtilsTest, CompoundArithmetic)
{
{
Vector2 vec(1.0, 2.0);
vec += Vector2(3.0, 4.0);
ASSERT_EQ(Vector2(4.0, 6.0), vec);
}
{
Vector2 vec(1.0, 2.0);
vec -= Vector2(3.0, 4.0);
ASSERT_EQ(Vector2(-2.0, -2.0), vec);
}
{
Vector2 vec(1.0, 2.0);
vec *= Vector2(3.0, 4.0);
ASSERT_EQ(Vector2(3.0, 8.0), vec);
}
{
Vector2 vec(3.0, 4.0);
vec /= Vector2(1.0, 2.0);
ASSERT_EQ(Vector2(3.0, 2.0), vec);
}
{
Vector2 vec(1.0, 2.0);
vec *= 2.0;
ASSERT_EQ(Vector2(2.0, 4.0), vec);
}
{
Vector2 vec(1.0, 2.0);
vec /= 2.0;
ASSERT_EQ(Vector2(0.5, 1.0), vec);
}
}
// Test other arithmetic operations
TEST(VectorUtilsTest, OtherArithmeticOperations)
{
Vector2 vec(3.0, 4.0);
ASSERT_EQ(25.0, vec.lengthSquared());
ASSERT_EQ(5.0, vec.length());
ASSERT_EQ(Vector2(0.6, 0.8), vec.normalized());
ASSERT_EQ(11.0, vec.dot(Vector2(1.0, 2.0)));
}
// Test element shortcuts
TEST(VectorUtilsTest, ElementShortcuts)
{
Vector2 vec2(1.0, 2.0);
Vector3 vec3(1.0, 2.0, 3.0);
Vector4 vec4(1.0, 2.0, 3.0, 4.0);
ASSERT_EQ(1.0, vec2.x());
ASSERT_EQ(1.0, vec3.x());
ASSERT_EQ(1.0, vec4.x());
ASSERT_EQ(2.0, vec2.y());
ASSERT_EQ(2.0, vec3.y());
ASSERT_EQ(2.0, vec4.y());
ASSERT_EQ(3.0, vec3.z());
ASSERT_EQ(3.0, vec4.z());
ASSERT_EQ(4.0, vec4.w());
vec2.x() = 0.0;
ASSERT_EQ(Vector2(0.0, 2.0), vec2);
}
// Test the cross product
TEST(VectorUtilsTest, CrossProduct)
{
ASSERT_EQ(Vector3(0.0, 0.0, 1.0), Vector3(1.0, 0.0, 0.0).cross(Vector3(0.0, 1.0, 0.0)));
ASSERT_EQ(Vector3(-3.0, 6.0, -3.0), Vector3(1.0, 2.0, 3.0).cross(Vector3(4.0, 5.0, 6.0)));
}
// Test basic functionality of int vectors
TEST(VectorUtilsTest, IntVector)
{
Vector2I vec(0);
int *data = vec.data();
data[1] = 1;
ASSERT_EQ(0, vec[0]);
ASSERT_EQ(1, vec[1]);
}
// Test basic functionality of int vectors
TEST(VectorUtilsTest, UIntVector)
{
Vector2U vec(0);
unsigned int *data = vec.data();
data[1] = 1;
ASSERT_EQ(0u, vec[0]);
ASSERT_EQ(1u, vec[1]);
}
} // anonymous namespace
......@@ -8,6 +8,7 @@
#include "libANGLE/renderer/gl/BlitGL.h"
#include "common/vector_utils.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/Framebuffer.h"
#include "libANGLE/renderer/gl/formatutilsgl.h"
......@@ -17,6 +18,8 @@
#include "libANGLE/renderer/gl/StateManagerGL.h"
#include "libANGLE/renderer/gl/WorkaroundsGL.h"
using angle::Vector2;
namespace rx
{
......@@ -336,24 +339,24 @@ gl::Error BlitGL::blitColorBufferWithShader(const gl::Framebuffer *source,
// = P * (T.size / D.size) + (T.offset - D.offset * T.size / D.size)
GLuint textureId;
gl::Vector2 TOffset;
gl::Vector2 TSize;
Vector2 TOffset;
Vector2 TSize;
// TODO(cwallez) once texture dirty bits are landed, reuse attached texture instead of using
// CopyTexImage2D
{
textureId = mScratchTextures[0];
TOffset = gl::Vector2(0.0, 0.0);
TSize = gl::Vector2(1.0, 1.0);
TOffset = Vector2(0.0);
TSize = Vector2(1.0);
if (sourceArea.width < 0)
{
TOffset.x = 1.0;
TSize.x = -1.0;
TOffset.x() = 1.0;
TSize.x() = -1.0;
}
if (sourceArea.height < 0)
{
TOffset.y = 1.0;
TSize.y = -1.0;
TOffset.y() = 1.0;
TSize.y() = -1.0;
}
GLenum format = readAttachment->getFormat().info->internalFormat;
......@@ -372,8 +375,8 @@ gl::Error BlitGL::blitColorBufferWithShader(const gl::Framebuffer *source,
// Compute normalized sampled draw quad region
// It is the same as the region of the source rectangle that is in bounds.
gl::Vector2 DOffset;
gl::Vector2 DSize;
Vector2 DOffset;
Vector2 DSize;
{
ASSERT(sourceArea.width != 0 && sourceArea.height != 0);
gl::Rectangle orientedInBounds = inBoundsSource;
......@@ -389,16 +392,15 @@ gl::Error BlitGL::blitColorBufferWithShader(const gl::Framebuffer *source,
}
DOffset =
gl::Vector2(static_cast<float>(orientedInBounds.x - sourceArea.x) / sourceArea.width,
static_cast<float>(orientedInBounds.y - sourceArea.y) / sourceArea.height);
DSize = gl::Vector2(static_cast<float>(orientedInBounds.width) / sourceArea.width,
static_cast<float>(orientedInBounds.height) / sourceArea.height);
Vector2(static_cast<float>(orientedInBounds.x - sourceArea.x) / sourceArea.width,
static_cast<float>(orientedInBounds.y - sourceArea.y) / sourceArea.height);
DSize = Vector2(static_cast<float>(orientedInBounds.width) / sourceArea.width,
static_cast<float>(orientedInBounds.height) / sourceArea.height);
}
ASSERT(DSize.x != 0.0 && DSize.y != 0.0);
gl::Vector2 texCoordScale = gl::Vector2(TSize.x / DSize.x, TSize.y / DSize.y);
gl::Vector2 texCoordOffset = gl::Vector2(TOffset.x - DOffset.x * texCoordScale.x,
TOffset.y - DOffset.y * texCoordScale.y);
ASSERT(DSize.x() != 0.0 && DSize.y() != 0.0);
Vector2 texCoordScale = TSize / DSize;
Vector2 texCoordOffset = TOffset - DOffset * texCoordScale;
// Reset all the state except scissor and use the viewport to draw exactly to the destination
// rectangle
......@@ -411,8 +413,8 @@ gl::Error BlitGL::blitColorBufferWithShader(const gl::Framebuffer *source,
mStateManager->useProgram(mBlitProgram);
mFunctions->uniform1i(mSourceTextureLocation, 0);
mFunctions->uniform2f(mScaleLocation, texCoordScale.x, texCoordScale.y);
mFunctions->uniform2f(mOffsetLocation, texCoordOffset.x, texCoordOffset.y);
mFunctions->uniform2f(mScaleLocation, texCoordScale.x(), texCoordScale.y());
mFunctions->uniform2f(mOffsetLocation, texCoordOffset.x(), texCoordOffset.y());
mFunctions->uniform1i(mMultiplyAlphaLocation, 0);
mFunctions->uniform1i(mUnMultiplyAlphaLocation, 0);
......@@ -472,20 +474,20 @@ gl::Error BlitGL::copySubTexture(TextureGL *source,
mStateManager->activeTexture(0);
mStateManager->bindTexture(GL_TEXTURE_2D, source->getTextureID());
gl::Vector2 scale(sourceArea.width / static_cast<float>(sourceSize.width),
sourceArea.height / static_cast<float>(sourceSize.height));
gl::Vector2 offset(sourceArea.x / static_cast<float>(sourceSize.width),
sourceArea.y / static_cast<float>(sourceSize.height));
Vector2 scale(sourceArea.width / static_cast<float>(sourceSize.width),
sourceArea.height / static_cast<float>(sourceSize.height));
Vector2 offset(sourceArea.x / static_cast<float>(sourceSize.width),
sourceArea.y / static_cast<float>(sourceSize.height));
if (unpackFlipY)
{
offset.y += scale.y;
scale.y = -scale.y;
offset.y() += scale.y();
scale.y() = -scale.y();
}
mStateManager->useProgram(mBlitProgram);
mFunctions->uniform1i(mSourceTextureLocation, 0);
mFunctions->uniform2f(mScaleLocation, scale.x, scale.y);
mFunctions->uniform2f(mOffsetLocation, offset.x, offset.y);
mFunctions->uniform2f(mScaleLocation, scale.x(), scale.y());
mFunctions->uniform2f(mOffsetLocation, offset.x(), offset.y());
if (unpackPremultiplyAlpha == unpackUnmultiplyAlpha)
{
mFunctions->uniform1i(mMultiplyAlphaLocation, 0);
......
......@@ -34,6 +34,7 @@
'common/tls.h',
'common/utilities.cpp',
'common/utilities.h',
'common/vector_utils.h',
'common/version.h',
],
'libangle_image_util_sources':
......
......@@ -20,6 +20,7 @@
'<(angle_path)/src/common/matrix_utils_unittest.cpp',
'<(angle_path)/src/common/string_utils_unittest.cpp',
'<(angle_path)/src/common/utilities_unittest.cpp',
'<(angle_path)/src/common/vector_utils_unittest.cpp',
'<(angle_path)/src/libANGLE/BinaryStream_unittest.cpp',
'<(angle_path)/src/libANGLE/Config_unittest.cpp',
'<(angle_path)/src/libANGLE/Fence_unittest.cpp',
......
......@@ -7,7 +7,6 @@
#include "test_utils/ANGLETest.h"
#include "random_utils.h"
#include "Vector.h"
using namespace angle;
......@@ -26,10 +25,10 @@ Vector4 RandomVec4(int seed, float minValue, float maxValue)
GLColor Vec4ToColor(const Vector4 &vec)
{
GLColor color;
color.R = static_cast<uint8_t>(vec.x * 255.0f);
color.G = static_cast<uint8_t>(vec.y * 255.0f);
color.B = static_cast<uint8_t>(vec.z * 255.0f);
color.A = static_cast<uint8_t>(vec.w * 255.0f);
color.R = static_cast<uint8_t>(vec.x() * 255.0f);
color.G = static_cast<uint8_t>(vec.y() * 255.0f);
color.B = static_cast<uint8_t>(vec.z() * 255.0f);
color.A = static_cast<uint8_t>(vec.w() * 255.0f);
return color;
};
......
......@@ -27,7 +27,7 @@ GLColor Convert565(const R5G6B5 &rgb565)
R5G6B5 Convert565(const GLColor &glColor)
{
const Vector4 &vecColor = glColor.toNormalizedVector();
gl::ColorF colorf(vecColor.x, vecColor.y, vecColor.z, vecColor.w);
gl::ColorF colorf(vecColor.x(), vecColor.y(), vecColor.z(), vecColor.w());
R5G6B5 rgb565;
R5G6B5::writeColor(&rgb565, &colorf);
return rgb565;
......@@ -417,7 +417,7 @@ TEST_P(SixteenBppTextureTestES3, RGBA4FramebufferReadback)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex.get(), 0);
glClearColor(rawColor.x, rawColor.y, rawColor.z, rawColor.w);
glClearColor(rawColor.x(), rawColor.y(), rawColor.z(), rawColor.w());
glClear(GL_COLOR_BUFFER_BIT);
ASSERT_GL_NO_ERROR();
......
......@@ -6,7 +6,6 @@
#include "test_utils/ANGLETest.h"
#include "random_utils.h"
#include "Vector.h"
using namespace angle;
......
......@@ -10,9 +10,9 @@
//
#include "ANGLEPerfTest.h"
#include "common/vector_utils.h"
#include "random_utils.h"
#include "shader_utils.h"
#include "Vector.h"
using namespace angle;
......
......@@ -14,7 +14,6 @@
#include "Matrix.h"
#include "random_utils.h"
#include "shader_utils.h"
#include "Vector.h"
using namespace angle;
using namespace egl_platform;
......@@ -290,17 +289,16 @@ void InstancingPerfBenchmark::drawBenchmark()
{
const Vector3 &translate = mTranslateData[pointIndex];
float tx = translate.x + time;
float ty = translate.y + time;
float tz = translate.z + time;
float tx = translate.x() + time;
float ty = translate.y() + time;
float tz = translate.z() + time;
float scale = AnimationSignal(tx) * 0.01f + 0.01f;
mSizeData[pointIndex] = scale;
Vector3 color;
color.x = AnimationSignal(tx) * 0.5f + 0.5f;
color.y = AnimationSignal(ty) * 0.5f + 0.5f;
color.z = AnimationSignal(tz) * 0.5f + 0.5f;
Vector3 color =
Vector3(AnimationSignal(tx), AnimationSignal(ty), AnimationSignal(tz)) * 0.5f +
Vector3(0.5f);
mColorData[pointIndex] = color;
}
......
......@@ -11,7 +11,7 @@
#include <array>
#include "Vector.h"
#include "common/vector_utils.h"
#include "shader_utils.h"
using namespace angle;
......
......@@ -95,13 +95,13 @@ void TestPlatform::enableMessages()
TestPlatform g_testPlatformInstance;
std::array<Vector3, 4> GetIndexedQuadVertices()
std::array<angle::Vector3, 4> GetIndexedQuadVertices()
{
std::array<Vector3, 4> vertices;
vertices[0] = Vector3(-1.0f, 1.0f, 0.5f);
vertices[1] = Vector3(-1.0f, -1.0f, 0.5f);
vertices[2] = Vector3(1.0f, -1.0f, 0.5f);
vertices[3] = Vector3(1.0f, 1.0f, 0.5f);
std::array<angle::Vector3, 4> vertices;
vertices[0] = angle::Vector3(-1.0f, 1.0f, 0.5f);
vertices[1] = angle::Vector3(-1.0f, -1.0f, 0.5f);
vertices[2] = angle::Vector3(1.0f, -1.0f, 0.5f);
vertices[3] = angle::Vector3(1.0f, 1.0f, 0.5f);
return vertices;
}
......@@ -115,8 +115,8 @@ GLColorRGB::GLColorRGB(GLubyte r, GLubyte g, GLubyte b) : R(r), G(g), B(b)
{
}
GLColorRGB::GLColorRGB(const Vector3 &floatColor)
: R(ColorDenorm(floatColor.x)), G(ColorDenorm(floatColor.y)), B(ColorDenorm(floatColor.z))
GLColorRGB::GLColorRGB(const angle::Vector3 &floatColor)
: R(ColorDenorm(floatColor.x())), G(ColorDenorm(floatColor.y())), B(ColorDenorm(floatColor.z()))
{
}
......@@ -128,11 +128,11 @@ GLColor::GLColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a) : R(r), G(g), B(b),
{
}
GLColor::GLColor(const Vector4 &floatColor)
: R(ColorDenorm(floatColor.x)),
G(ColorDenorm(floatColor.y)),
B(ColorDenorm(floatColor.z)),
A(ColorDenorm(floatColor.w))
GLColor::GLColor(const angle::Vector4 &floatColor)
: R(ColorDenorm(floatColor.x())),
G(ColorDenorm(floatColor.y())),
B(ColorDenorm(floatColor.z())),
A(ColorDenorm(floatColor.w()))
{
}
......@@ -141,9 +141,9 @@ GLColor::GLColor(GLuint colorValue) : R(0), G(0), B(0), A(0)
memcpy(&R, &colorValue, sizeof(GLuint));
}
Vector4 GLColor::toNormalizedVector() const
angle::Vector4 GLColor::toNormalizedVector() const
{
return Vector4(ColorNorm(R), ColorNorm(G), ColorNorm(B), ColorNorm(A));
return angle::Vector4(ColorNorm(R), ColorNorm(G), ColorNorm(B), ColorNorm(A));
}
GLColor ReadColor(GLint x, GLint y)
......@@ -170,15 +170,15 @@ std::ostream &operator<<(std::ostream &ostream, const GLColor &color)
} // namespace angle
// static
std::array<Vector3, 6> ANGLETest::GetQuadVertices()
{
std::array<Vector3, 6> vertices;
vertices[0] = Vector3(-1.0f, 1.0f, 0.5f);
vertices[1] = Vector3(-1.0f, -1.0f, 0.5f);
vertices[2] = Vector3(1.0f, -1.0f, 0.5f);
vertices[3] = Vector3(-1.0f, 1.0f, 0.5f);
vertices[4] = Vector3(1.0f, -1.0f, 0.5f);
vertices[5] = Vector3(1.0f, 1.0f, 0.5f);
std::array<angle::Vector3, 6> ANGLETest::GetQuadVertices()
{
std::array<angle::Vector3, 6> vertices;
vertices[0] = angle::Vector3(-1.0f, 1.0f, 0.5f);
vertices[1] = angle::Vector3(-1.0f, -1.0f, 0.5f);
vertices[2] = angle::Vector3(1.0f, -1.0f, 0.5f);
vertices[3] = angle::Vector3(-1.0f, 1.0f, 0.5f);
vertices[4] = angle::Vector3(1.0f, -1.0f, 0.5f);
vertices[5] = angle::Vector3(1.0f, 1.0f, 0.5f);
return vertices;
}
......@@ -282,11 +282,11 @@ void ANGLETest::setupQuadVertexBuffer(GLfloat positionAttribZ, GLfloat positionA
}
auto quadVertices = GetQuadVertices();
for (Vector3 &vertex : quadVertices)
for (angle::Vector3 &vertex : quadVertices)
{
vertex.x *= positionAttribXYScale;
vertex.y *= positionAttribXYScale;
vertex.z = positionAttribZ;
vertex.x() *= positionAttribXYScale;
vertex.y() *= positionAttribXYScale;
vertex.z() = positionAttribZ;
}
glBindBuffer(GL_ARRAY_BUFFER, mQuadVertexBuffer);
......@@ -301,11 +301,11 @@ void ANGLETest::setupIndexedQuadVertexBuffer(GLfloat positionAttribZ, GLfloat po
}
auto quadVertices = angle::GetIndexedQuadVertices();
for (Vector3 &vertex : quadVertices)
for (angle::Vector3 &vertex : quadVertices)
{
vertex.x *= positionAttribXYScale;
vertex.y *= positionAttribXYScale;
vertex.z = positionAttribZ;
vertex.x() *= positionAttribXYScale;
vertex.y() *= positionAttribXYScale;
vertex.z() = positionAttribZ;
}
glBindBuffer(GL_ARRAY_BUFFER, mQuadVertexBuffer);
......@@ -353,11 +353,11 @@ void ANGLETest::drawQuad(GLuint program,
else
{
auto quadVertices = GetQuadVertices();
for (Vector3 &vertex : quadVertices)
for (angle::Vector3 &vertex : quadVertices)
{
vertex.x *= positionAttribXYScale;
vertex.y *= positionAttribXYScale;
vertex.z = positionAttribZ;
vertex.x() *= positionAttribXYScale;
vertex.y() *= positionAttribXYScale;
vertex.z() = positionAttribZ;
}
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, quadVertices.data());
......
......@@ -17,9 +17,9 @@
#include "angle_gl.h"
#include "angle_test_configs.h"
#include "common/angleutils.h"
#include "common/vector_utils.h"
#include "shader_utils.h"
#include "system_utils.h"
#include "Vector.h"
#define EXPECT_GL_ERROR(err) EXPECT_EQ(static_cast<GLenum>(err), glGetError())
#define EXPECT_GL_NO_ERROR() EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError())
......@@ -48,7 +48,7 @@ struct GLColorRGB
{
GLColorRGB();
GLColorRGB(GLubyte r, GLubyte g, GLubyte b);
GLColorRGB(const Vector3 &floatColor);
GLColorRGB(const angle::Vector3 &floatColor);
GLubyte R, G, B;
......@@ -63,10 +63,10 @@ struct GLColor
{
GLColor();
GLColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a);
GLColor(const Vector4 &floatColor);
GLColor(const angle::Vector4 &floatColor);
GLColor(GLuint colorValue);
Vector4 toNormalizedVector() const;
angle::Vector4 toNormalizedVector() const;
GLubyte R, G, B, A;
......@@ -160,7 +160,7 @@ class ANGLETest : public ::testing::TestWithParam<angle::PlatformParameters>
GLfloat positionAttribZ,
GLfloat positionAttribXYScale,
bool useVertexBuffer);
static std::array<Vector3, 6> GetQuadVertices();
static std::array<angle::Vector3, 6> GetQuadVertices();
void drawIndexedQuad(GLuint program,
const std::string &positionAttribName,
GLfloat positionAttribZ);
......
......@@ -13,6 +13,8 @@
#include <math.h>
#include <cstddef>
using namespace angle;
Matrix4::Matrix4()
{
data[0] = 1.0f;
......@@ -76,30 +78,32 @@ Matrix4 Matrix4::identity()
Matrix4 Matrix4::rotate(float angle, const Vector3 &p)
{
Vector3 u = Vector3::normalize(p);
Vector3 u = p.normalized();
float theta = static_cast<float>(angle * (M_PI / 180.0f));
float cos_t = cosf(theta);
float sin_t = sinf(theta);
return Matrix4(
cos_t + (u.x * u.x * (1.0f - cos_t)), (u.x * u.y * (1.0f - cos_t)) - (u.z * sin_t),
(u.x * u.z * (1.0f - cos_t)) + (u.y * sin_t), 0.0f,
(u.y * u.x * (1.0f - cos_t)) + (u.z * sin_t), cos_t + (u.y * u.y * (1.0f - cos_t)),
(u.y * u.z * (1.0f - cos_t)) - (u.x * sin_t), 0.0f,
(u.z * u.x * (1.0f - cos_t)) - (u.y * sin_t), (u.z * u.y * (1.0f - cos_t)) + (u.x * sin_t),
cos_t + (u.z * u.z * (1.0f - cos_t)), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
return Matrix4(cos_t + (u.x() * u.x() * (1.0f - cos_t)),
(u.x() * u.y() * (1.0f - cos_t)) - (u.z() * sin_t),
(u.x() * u.z() * (1.0f - cos_t)) + (u.y() * sin_t), 0.0f,
(u.y() * u.x() * (1.0f - cos_t)) + (u.z() * sin_t),
cos_t + (u.y() * u.y() * (1.0f - cos_t)),
(u.y() * u.z() * (1.0f - cos_t)) - (u.x() * sin_t), 0.0f,
(u.z() * u.x() * (1.0f - cos_t)) - (u.y() * sin_t),
(u.z() * u.y() * (1.0f - cos_t)) + (u.x() * sin_t),
cos_t + (u.z() * u.z() * (1.0f - cos_t)), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
}
Matrix4 Matrix4::translate(const Vector3 &t)
{
return Matrix4(1.0f, 0.0f, 0.0f, t.x, 0.0f, 1.0f, 0.0f, t.y, 0.0f, 0.0f, 1.0f, t.z, 0.0f, 0.0f,
0.0f, 1.0f);
return Matrix4(1.0f, 0.0f, 0.0f, t.x(), 0.0f, 1.0f, 0.0f, t.y(), 0.0f, 0.0f, 1.0f, t.z(), 0.0f,
0.0f, 0.0f, 1.0f);
}
Matrix4 Matrix4::scale(const Vector3 &s)
{
return Matrix4(s.x, 0.0f, 0.0f, 0.0f, 0.0f, s.y, 0.0f, 0.0f, 0.0f, 0.0f, s.z, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f);
return Matrix4(s.x(), 0.0f, 0.0f, 0.0f, 0.0f, s.y(), 0.0f, 0.0f, 0.0f, 0.0f, s.z(), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f);
}
Matrix4 Matrix4::frustum(float l, float r, float b, float t, float n, float f)
......@@ -205,14 +209,14 @@ Matrix4 Matrix4::transpose(const Matrix4 &mat)
Vector3 Matrix4::transform(const Matrix4 &mat, const Vector3 &pt)
{
Vector4 transformed = Vector4::normalize(mat * Vector4(pt.x, pt.y, pt.z, 1.0f));
return Vector3(transformed.x, transformed.y, transformed.z);
Vector4 transformed = (mat * Vector4(pt, 1.0f)).normalized();
return Vector3(transformed.x(), transformed.y(), transformed.z());
}
Vector3 Matrix4::transform(const Matrix4 &mat, const Vector4 &pt)
{
Vector4 transformed = Vector4::normalize(mat * pt);
return Vector3(transformed.x, transformed.y, transformed.z);
Vector4 transformed = (mat * pt).normalized();
return Vector3(transformed.x(), transformed.y(), transformed.z());
}
Matrix4 operator*(const Matrix4 &a, const Matrix4 &b)
......@@ -278,10 +282,10 @@ Matrix4 &operator*=(Matrix4 &a, float b)
Vector4 operator*(const Matrix4 &a, const Vector4 &b)
{
return Vector4(a.data[0] * b.x + a.data[4] * b.y + a.data[8] * b.z + a.data[12] * b.w,
a.data[1] * b.x + a.data[5] * b.y + a.data[9] * b.z + a.data[13] * b.w,
a.data[2] * b.x + a.data[6] * b.y + a.data[10] * b.z + a.data[14] * b.w,
a.data[3] * b.x + a.data[7] * b.y + a.data[11] * b.z + a.data[15] * b.w);
return Vector4(a.data[0] * b.x() + a.data[4] * b.y() + a.data[8] * b.z() + a.data[12] * b.w(),
a.data[1] * b.x() + a.data[5] * b.y() + a.data[9] * b.z() + a.data[13] * b.w(),
a.data[2] * b.x() + a.data[6] * b.y() + a.data[10] * b.z() + a.data[14] * b.w(),
a.data[3] * b.x() + a.data[7] * b.y() + a.data[11] * b.z() + a.data[15] * b.w());
}
bool operator==(const Matrix4 &a, const Matrix4 &b)
......
......@@ -12,7 +12,7 @@
#include <export.h>
#include "Vector.h"
#include "common/vector_utils.h"
struct ANGLE_EXPORT Matrix4
{
......@@ -37,9 +37,9 @@ struct ANGLE_EXPORT Matrix4
float m33);
static Matrix4 identity();
static Matrix4 rotate(float angle, const Vector3 &p);
static Matrix4 translate(const Vector3 &t);
static Matrix4 scale(const Vector3 &s);
static Matrix4 rotate(float angle, const angle::Vector3 &p);
static Matrix4 translate(const angle::Vector3 &t);
static Matrix4 scale(const angle::Vector3 &s);
static Matrix4 frustum(float l, float r, float b, float t, float n, float f);
static Matrix4 perspective(float fov, float aspectRatio, float n, float f);
static Matrix4 ortho(float l, float r, float b, float t, float n, float f);
......@@ -47,15 +47,15 @@ struct ANGLE_EXPORT Matrix4
static Matrix4 invert(const Matrix4 &mat);
static Matrix4 transpose(const Matrix4 &mat);
static Vector3 transform(const Matrix4 &mat, const Vector3 &pt);
static Vector3 transform(const Matrix4 &mat, const Vector4 &pt);
static angle::Vector3 transform(const Matrix4 &mat, const angle::Vector3 &pt);
static angle::Vector3 transform(const Matrix4 &mat, const angle::Vector4 &pt);
};
ANGLE_EXPORT Matrix4 operator*(const Matrix4 &a, const Matrix4 &b);
ANGLE_EXPORT Matrix4 &operator*=(Matrix4 &a, const Matrix4 &b);
ANGLE_EXPORT Matrix4 operator*(const Matrix4 &a, float b);
ANGLE_EXPORT Matrix4 &operator*=(Matrix4 &a, float b);
ANGLE_EXPORT Vector4 operator*(const Matrix4 &a, const Vector4 &b);
ANGLE_EXPORT angle::Vector4 operator*(const Matrix4 &a, const angle::Vector4 &b);
ANGLE_EXPORT bool operator==(const Matrix4 &a, const Matrix4 &b);
ANGLE_EXPORT bool operator!=(const Matrix4 &a, const Matrix4 &b);
......
//
// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Vector:
// Vector class for linear math.
//
#include "Vector.h"
#include <math.h>
Vector2::Vector2() : x(0.0), y(0.0)
{
}
Vector2::Vector2(float x, float y) : x(x), y(y)
{
}
bool Vector2::operator==(const Vector2 &vec) const
{
return x == vec.x && y == vec.y;
}
bool Vector2::operator!=(const Vector2 &vec) const
{
return !(*this == vec);
}
std::ostream &operator<<(std::ostream &stream, const Vector2 &vec)
{
stream << "(" << vec.x << "," << vec.y << ")";
return stream;
}
float Vector2::length(const Vector2 &vec)
{
float lenSquared = lengthSquared(vec);
return (lenSquared != 0.0f) ? sqrtf(lenSquared) : 0.0f;
}
float Vector2::lengthSquared(const Vector2 &vec)
{
return vec.x * vec.x + vec.y * vec.y;
}
Vector2 Vector2::normalize(const Vector2 &vec)
{
Vector2 ret(0.0f, 0.0f);
float len = length(vec);
if (len != 0.0f)
{
float invLen = 1.0f / len;
ret.x = vec.x * invLen;
ret.y = vec.y * invLen;
}
return ret;
}
Vector3::Vector3() : x(0.0), y(0.0), z(0.0)
{
}
Vector3::Vector3(float x, float y, float z) : x(x), y(y), z(z)
{
}
float Vector3::length(const Vector3 &vec)
{
float lenSquared = lengthSquared(vec);
return (lenSquared != 0.0f) ? sqrtf(lenSquared) : 0.0f;
}
float Vector3::lengthSquared(const Vector3 &vec)
{
return vec.x * vec.x + vec.y * vec.y + vec.z * vec.z;
}
Vector3 Vector3::normalize(const Vector3 &vec)
{
Vector3 ret(0.0f, 0.0f, 0.0f);
float len = length(vec);
if (len != 0.0f)
{
float invLen = 1.0f / len;
ret.x = vec.x * invLen;
ret.y = vec.y * invLen;
ret.z = vec.z * invLen;
}
return ret;
}
float Vector3::dot(const Vector3 &a, const Vector3 &b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
Vector3 Vector3::cross(const Vector3 &a, const Vector3 &b)
{
return Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
Vector3 operator*(const Vector3 &a, const Vector3 &b)
{
return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
}
Vector3 operator*(const Vector3 &a, const float &b)
{
return Vector3(a.x * b, a.y * b, a.z * b);
}
Vector3 operator/(const Vector3 &a, const Vector3 &b)
{
return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
}
Vector3 operator/(const Vector3 &a, const float &b)
{
return Vector3(a.x / b, a.y / b, a.z / b);
}
Vector3 operator+(const Vector3 &a, const Vector3 &b)
{
return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
}
Vector3 operator-(const Vector3 &a, const Vector3 &b)
{
return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
}
bool operator==(const Vector3 &a, const Vector3 &b)
{
return (a.x == b.x && a.y == b.y && a.z == b.z);
}
bool operator!=(const Vector3 &a, const Vector3 &b)
{
return !(a == b);
}
Vector4::Vector4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f)
{
}
Vector4::Vector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w)
{
}
float Vector4::length(const Vector4 &vec)
{
float lenSquared = lengthSquared(vec);
return (lenSquared != 0.0f) ? sqrtf(lenSquared) : 0.0f;
}
float Vector4::lengthSquared(const Vector4 &vec)
{
return vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w;
}
Vector4 Vector4::normalize(const Vector4 &vec)
{
Vector4 ret(0.0f, 0.0f, 0.0f, 1.0f);
if (vec.w != 0.0f)
{
float invLen = 1.0f / vec.w;
ret.x = vec.x * invLen;
ret.y = vec.y * invLen;
ret.z = vec.z * invLen;
}
return ret;
}
float Vector4::dot(const Vector4 &a, const Vector4 &b)
{
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
//
// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Vector:
// Vector class for linear math.
//
#ifndef UTIL_VECTOR_H
#define UTIL_VECTOR_H
#include <ostream>
#include <export.h>
struct ANGLE_EXPORT Vector2
{
Vector2();
Vector2(float x, float y);
bool operator==(const Vector2 &vec) const;
bool operator!=(const Vector2 &vec) const;
static float length(const Vector2 &vec);
static float lengthSquared(const Vector2 &vec);
static Vector2 normalize(const Vector2 &vec);
float *data() { return &x; }
const float *data() const { return &x; }
float x, y;
};
ANGLE_EXPORT std::ostream &operator<<(std::ostream &stream, const Vector2 &vec);
struct ANGLE_EXPORT Vector3
{
Vector3();
Vector3(float x, float y, float z);
static float length(const Vector3 &vec);
static float lengthSquared(const Vector3 &vec);
static Vector3 normalize(const Vector3 &vec);
static float dot(const Vector3 &a, const Vector3 &b);
static Vector3 cross(const Vector3 &a, const Vector3 &b);
float *data() { return &x; }
const float *data() const { return &x; }
float x, y, z;
};
ANGLE_EXPORT Vector3 operator*(const Vector3 &a, const Vector3 &b);
ANGLE_EXPORT Vector3 operator*(const Vector3 &a, const float &b);
ANGLE_EXPORT Vector3 operator/(const Vector3 &a, const Vector3 &b);
ANGLE_EXPORT Vector3 operator/(const Vector3 &a, const float &b);
ANGLE_EXPORT Vector3 operator+(const Vector3 &a, const Vector3 &b);
ANGLE_EXPORT Vector3 operator-(const Vector3 &a, const Vector3 &b);
ANGLE_EXPORT bool operator==(const Vector3 &a, const Vector3 &b);
ANGLE_EXPORT bool operator!=(const Vector3 &a, const Vector3 &b);
struct ANGLE_EXPORT Vector4
{
Vector4();
Vector4(float x, float y, float z, float w);
static float length(const Vector4 &vec);
static float lengthSquared(const Vector4 &vec);
static Vector4 normalize(const Vector4 &vec);
static float dot(const Vector4 &a, const Vector4 &b);
float *data() { return &x; }
const float *data() const { return &x; }
float x, y, z, w;
};
#endif // UTIL_VECTOR_H
......@@ -12,6 +12,8 @@
#define _USE_MATH_DEFINES
#include <math.h>
using namespace angle;
void CreateSphereGeometry(size_t sliceCount, float radius, SphereGeometry *result)
{
size_t parellelCount = sliceCount / 2;
......
......@@ -16,12 +16,12 @@
#include <export.h>
#include <GLES2/gl2.h>
#include "Vector.h"
#include "common/vector_utils.h"
struct ANGLE_EXPORT SphereGeometry
{
std::vector<Vector3> positions;
std::vector<Vector3> normals;
std::vector<angle::Vector3> positions;
std::vector<angle::Vector3> normals;
std::vector<GLushort> indices;
};
......@@ -29,9 +29,9 @@ ANGLE_EXPORT void CreateSphereGeometry(size_t sliceCount, float radius, SphereGe
struct ANGLE_EXPORT CubeGeometry
{
std::vector<Vector3> positions;
std::vector<Vector3> normals;
std::vector<Vector2> texcoords;
std::vector<angle::Vector3> positions;
std::vector<angle::Vector3> normals;
std::vector<angle::Vector2> texcoords;
std::vector<GLushort> indices;
};
......
......@@ -28,8 +28,6 @@
'OSWindow.cpp',
'OSWindow.h',
'Timer.h',
'Vector.cpp',
'Vector.h',
],
'util_win32_sources':
[
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment