Move more sample_util sources to util.

These sample utils can also be useful for writing perf tests.

BUG=angleproject:1164

Change-Id: I44b5d63b57bfce7a541442fd02cd729a1bab17f0
Reviewed-on: https://chromium-review.googlesource.com/301468
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Tryjob-Request: Jamie Madill <jmadill@chromium.org>
Tested-by: Jamie Madill <jmadill@chromium.org>
This commit is contained in:
Jamie Madill 2015-09-22 12:40:31 -04:00
Родитель db3dd0831e
Коммит ad60e8ee26
12 изменённых файлов: 566 добавлений и 487 удалений

Просмотреть файл

@ -161,7 +161,7 @@ class MultiWindowSample : public SampleApplication
glUseProgram(mProgram); glUseProgram(mProgram);
// Load the vertex data // Load the vertex data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices[0].data); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices[0].data());
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3);

Просмотреть файл

@ -146,14 +146,14 @@ class ParticleSystemSample : public SampleApplication
Vector3 centerPos(RandomBetween(-0.5f, 0.5f), Vector3 centerPos(RandomBetween(-0.5f, 0.5f),
RandomBetween(-0.5f, 0.5f), RandomBetween(-0.5f, 0.5f),
RandomBetween(-0.5f, 0.5f)); RandomBetween(-0.5f, 0.5f));
glUniform3fv(mCenterPositionLoc, 1, centerPos.data); glUniform3fv(mCenterPositionLoc, 1, centerPos.data());
// Random color // Random color
Vector4 color(RandomBetween(0.0f, 1.0f), Vector4 color(RandomBetween(0.0f, 1.0f),
RandomBetween(0.0f, 1.0f), RandomBetween(0.0f, 1.0f),
RandomBetween(0.0f, 1.0f), RandomBetween(0.0f, 1.0f),
0.5f); 0.5f);
glUniform4fv(mColorLoc, 1, color.data); glUniform4fv(mColorLoc, 1, color.data());
} }
// Load uniform time variable // Load uniform time variable

Просмотреть файл

@ -1,220 +0,0 @@
//
// 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.
//
#include "Matrix.h"
#define _USE_MATH_DEFINES
#include <math.h>
#include <cstddef>
Matrix4::Matrix4()
{
data[ 0] = 1.0f; data[ 4] = 0.0f; data[ 8] = 0.0f; data[12] = 0.0f;
data[ 1] = 0.0f; data[ 5] = 1.0f; data[ 9] = 0.0f; data[13] = 0.0f;
data[ 2] = 0.0f; data[ 6] = 0.0f; data[10] = 1.0f; data[14] = 0.0f;
data[ 3] = 0.0f; data[ 7] = 0.0f; data[11] = 0.0f; data[15] = 1.0f;
}
Matrix4::Matrix4(float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33)
{
data[ 0] = m00; data[ 4] = m01; data[ 8] = m02; data[12] = m03;
data[ 1] = m10; data[ 5] = m11; data[ 9] = m12; data[13] = m13;
data[ 2] = m20; data[ 6] = m21; data[10] = m22; data[14] = m23;
data[ 3] = m30; data[ 7] = m31; data[11] = m32; data[15] = m33;
}
Matrix4 Matrix4::identity()
{
return Matrix4(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
Matrix4 Matrix4::rotate(float angle, const Vector3 &p)
{
Vector3 u = Vector3::normalize(p);
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);
}
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);
}
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);
}
Matrix4 Matrix4::frustum(float l, float r, float b, float t, float n, float f)
{
return Matrix4((2.0f * n) / (r - l), 0.0f, (r + l) / (r - l), 0.0f,
0.0f, (2.0f * n) / (t - b), (t + b) / (t - b), 0.0f,
0.0f, 0.0f, -(f + n) / (f - n), -(2.0f * f * n) / (f - n),
0.0f, 0.0f, -1.0f, 0.0f);
}
Matrix4 Matrix4::perspective(float fovY, float aspectRatio, float nearZ, float farZ)
{
const float frustumHeight = tanf(static_cast<float>(fovY / 360.0f * M_PI)) * nearZ;
const float frustumWidth = frustumHeight * aspectRatio;
return frustum(-frustumWidth, frustumWidth, -frustumHeight, frustumHeight, nearZ, farZ);
}
Matrix4 Matrix4::ortho(float l, float r, float b, float t, float n, float f)
{
return Matrix4(2.0f / (r - l), 0.0f, 0.0f, -(r + l) / (r - l),
0.0f, 2.0f / (t - b), 0.0f, -(t + b) / (t - b),
0.0f, 0.0f, -2.0f / (f - n), -(f + n) / (f - n),
0.0f, 0.0f, 0.0f, 1.0f);
}
Matrix4 Matrix4::rollPitchYaw(float roll, float pitch, float yaw)
{
return rotate(yaw, Vector3(0, 0, 1)) *
rotate(pitch, Vector3(0, 1, 0)) *
rotate(roll, Vector3(1, 0, 0));
}
Matrix4 Matrix4::invert(const Matrix4 &mat)
{
Matrix4 inverted( mat.data[ 5] * mat.data[10] * mat.data[15] - mat.data[5] * mat.data[11] * mat.data[14] - mat.data[ 9] * mat.data[ 6] * mat.data[15] + mat.data[ 9] * mat.data[ 7] * mat.data[14] + mat.data[13] * mat.data[ 6] * mat.data[11] - mat.data[13] * mat.data[ 7] * mat.data[10],
-mat.data[ 4] * mat.data[10] * mat.data[15] + mat.data[4] * mat.data[11] * mat.data[14] + mat.data[ 8] * mat.data[ 6] * mat.data[15] - mat.data[ 8] * mat.data[ 7] * mat.data[14] - mat.data[12] * mat.data[ 6] * mat.data[11] + mat.data[12] * mat.data[ 7] * mat.data[10],
mat.data[ 4] * mat.data[ 9] * mat.data[15] - mat.data[4] * mat.data[11] * mat.data[13] - mat.data[ 8] * mat.data[ 5] * mat.data[15] + mat.data[ 8] * mat.data[ 7] * mat.data[13] + mat.data[12] * mat.data[ 5] * mat.data[11] - mat.data[12] * mat.data[ 7] * mat.data[ 9],
-mat.data[ 4] * mat.data[ 9] * mat.data[14] + mat.data[4] * mat.data[10] * mat.data[13] + mat.data[ 8] * mat.data[ 5] * mat.data[14] - mat.data[ 8] * mat.data[ 6] * mat.data[13] - mat.data[12] * mat.data[ 5] * mat.data[10] + mat.data[12] * mat.data[ 6] * mat.data[ 9],
-mat.data[ 1] * mat.data[10] * mat.data[15] + mat.data[1] * mat.data[11] * mat.data[14] + mat.data[ 9] * mat.data[ 2] * mat.data[15] - mat.data[ 9] * mat.data[ 3] * mat.data[14] - mat.data[13] * mat.data[ 2] * mat.data[11] + mat.data[13] * mat.data[ 3] * mat.data[10],
mat.data[ 0] * mat.data[10] * mat.data[15] - mat.data[0] * mat.data[11] * mat.data[14] - mat.data[ 8] * mat.data[ 2] * mat.data[15] + mat.data[ 8] * mat.data[ 3] * mat.data[14] + mat.data[12] * mat.data[ 2] * mat.data[11] - mat.data[12] * mat.data[ 3] * mat.data[10],
-mat.data[ 0] * mat.data[ 9] * mat.data[15] + mat.data[0] * mat.data[11] * mat.data[13] + mat.data[ 8] * mat.data[ 1] * mat.data[15] - mat.data[ 8] * mat.data[ 3] * mat.data[13] - mat.data[12] * mat.data[ 1] * mat.data[11] + mat.data[12] * mat.data[ 3] * mat.data[ 9],
mat.data[ 0] * mat.data[ 9] * mat.data[14] - mat.data[0] * mat.data[10] * mat.data[13] - mat.data[ 8] * mat.data[ 1] * mat.data[14] + mat.data[ 8] * mat.data[ 2] * mat.data[13] + mat.data[12] * mat.data[ 1] * mat.data[10] - mat.data[12] * mat.data[ 2] * mat.data[ 9],
mat.data[ 1] * mat.data[ 6] * mat.data[15] - mat.data[1] * mat.data[ 7] * mat.data[14] - mat.data[ 5] * mat.data[ 2] * mat.data[15] + mat.data[ 5] * mat.data[ 3] * mat.data[14] + mat.data[13] * mat.data[ 2] * mat.data[ 7] - mat.data[13] * mat.data[ 3] * mat.data[ 6],
-mat.data[ 0] * mat.data[ 6] * mat.data[15] + mat.data[0] * mat.data[ 7] * mat.data[14] + mat.data[ 4] * mat.data[ 2] * mat.data[15] - mat.data[ 4] * mat.data[ 3] * mat.data[14] - mat.data[12] * mat.data[ 2] * mat.data[ 7] + mat.data[12] * mat.data[ 3] * mat.data[ 6],
mat.data[ 0] * mat.data[ 5] * mat.data[15] - mat.data[0] * mat.data[ 7] * mat.data[13] - mat.data[ 4] * mat.data[ 1] * mat.data[15] + mat.data[ 4] * mat.data[ 3] * mat.data[13] + mat.data[12] * mat.data[ 1] * mat.data[ 7] - mat.data[12] * mat.data[ 3] * mat.data[ 5],
-mat.data[ 0] * mat.data[ 5] * mat.data[14] + mat.data[0] * mat.data[ 6] * mat.data[13] + mat.data[ 4] * mat.data[ 1] * mat.data[14] - mat.data[ 4] * mat.data[ 2] * mat.data[13] - mat.data[12] * mat.data[ 1] * mat.data[ 6] + mat.data[12] * mat.data[ 2] * mat.data[ 5],
-mat.data[ 1] * mat.data[ 6] * mat.data[11] + mat.data[1] * mat.data[ 7] * mat.data[10] + mat.data[ 5] * mat.data[ 2] * mat.data[11] - mat.data[ 5] * mat.data[ 3] * mat.data[10] - mat.data[ 9] * mat.data[ 2] * mat.data[ 7] + mat.data[ 9] * mat.data[ 3] * mat.data[ 6],
mat.data[ 0] * mat.data[ 6] * mat.data[11] - mat.data[0] * mat.data[ 7] * mat.data[10] - mat.data[ 4] * mat.data[ 2] * mat.data[11] + mat.data[ 4] * mat.data[ 3] * mat.data[10] + mat.data[ 8] * mat.data[ 2] * mat.data[ 7] - mat.data[ 8] * mat.data[ 3] * mat.data[ 6],
-mat.data[ 0] * mat.data[ 5] * mat.data[11] + mat.data[0] * mat.data[ 7] * mat.data[ 9] + mat.data[ 4] * mat.data[ 1] * mat.data[11] - mat.data[ 4] * mat.data[ 3] * mat.data[ 9] - mat.data[ 8] * mat.data[ 1] * mat.data[ 7] + mat.data[ 8] * mat.data[ 3] * mat.data[ 5],
mat.data[ 0] * mat.data[ 5] * mat.data[10] - mat.data[0] * mat.data[ 6] * mat.data[ 9] - mat.data[ 4] * mat.data[ 1] * mat.data[10] + mat.data[ 4] * mat.data[ 2] * mat.data[ 9] + mat.data[ 8] * mat.data[ 1] * mat.data[ 6] - mat.data[ 8] * mat.data[ 2] * mat.data[ 5]);
float determinant = mat.data[0] * inverted.data[0] + mat.data[1] * inverted.data[4] + mat.data[2] * inverted.data[8] + mat.data[3] * inverted.data[12];
if (determinant != 0.0f)
{
inverted *= 1.0f / determinant;
}
else
{
inverted = identity();
}
return inverted;
}
Matrix4 Matrix4::transpose(const Matrix4 &mat)
{
return Matrix4(mat.data[ 0], mat.data[ 1], mat.data[ 2], mat.data[ 3],
mat.data[ 4], mat.data[ 5], mat.data[ 6], mat.data[ 7],
mat.data[ 8], mat.data[ 9], mat.data[10], mat.data[11],
mat.data[12], mat.data[13], mat.data[14], mat.data[15]);
}
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);
}
Vector3 Matrix4::transform(const Matrix4 &mat, const Vector4 &pt)
{
Vector4 transformed = Vector4::normalize(mat * pt);
return Vector3(transformed.x, transformed.y, transformed.z);
}
Matrix4 operator*(const Matrix4 &a, const Matrix4 &b)
{
return Matrix4(a.data[ 0] * b.data[ 0] + a.data[ 4] * b.data[ 1] + a.data[ 8] * b.data[ 2] + a.data[12] * b.data[ 3],
a.data[ 0] * b.data[ 4] + a.data[ 4] * b.data[ 5] + a.data[ 8] * b.data[ 6] + a.data[12] * b.data[ 7],
a.data[ 0] * b.data[ 8] + a.data[ 4] * b.data[ 9] + a.data[ 8] * b.data[10] + a.data[12] * b.data[11],
a.data[ 0] * b.data[12] + a.data[ 4] * b.data[13] + a.data[ 8] * b.data[14] + a.data[12] * b.data[15],
a.data[ 1] * b.data[ 0] + a.data[ 5] * b.data[ 1] + a.data[ 9] * b.data[ 2] + a.data[13] * b.data[ 3],
a.data[ 1] * b.data[ 4] + a.data[ 5] * b.data[ 5] + a.data[ 9] * b.data[ 6] + a.data[13] * b.data[ 7],
a.data[ 1] * b.data[ 8] + a.data[ 5] * b.data[ 9] + a.data[ 9] * b.data[10] + a.data[13] * b.data[11],
a.data[ 1] * b.data[12] + a.data[ 5] * b.data[13] + a.data[ 9] * b.data[14] + a.data[13] * b.data[15],
a.data[ 2] * b.data[ 0] + a.data[ 6] * b.data[ 1] + a.data[10] * b.data[ 2] + a.data[14] * b.data[ 3],
a.data[ 2] * b.data[ 4] + a.data[ 6] * b.data[ 5] + a.data[10] * b.data[ 6] + a.data[14] * b.data[ 7],
a.data[ 2] * b.data[ 8] + a.data[ 6] * b.data[ 9] + a.data[10] * b.data[10] + a.data[14] * b.data[11],
a.data[ 2] * b.data[12] + a.data[ 6] * b.data[13] + a.data[10] * b.data[14] + a.data[14] * b.data[15],
a.data[ 3] * b.data[ 0] + a.data[ 7] * b.data[ 1] + a.data[11] * b.data[ 2] + a.data[15] * b.data[ 3],
a.data[ 3] * b.data[ 4] + a.data[ 7] * b.data[ 5] + a.data[11] * b.data[ 6] + a.data[15] * b.data[ 7],
a.data[ 3] * b.data[ 8] + a.data[ 7] * b.data[ 9] + a.data[11] * b.data[10] + a.data[15] * b.data[11],
a.data[ 3] * b.data[12] + a.data[ 7] * b.data[13] + a.data[11] * b.data[14] + a.data[15] * b.data[15]);
}
Matrix4 &operator*=(Matrix4 &a, const Matrix4 &b)
{
a = a * b;
return a;
}
Matrix4 operator*(const Matrix4 &a, float b)
{
Matrix4 ret(a);
for (size_t i = 0; i < 16; i++)
{
ret.data[i] *= b;
}
return ret;
}
Matrix4 &operator*=(Matrix4 &a, float b)
{
for (size_t i = 0; i < 16; i++)
{
a.data[i] *= b;
}
return a;
}
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);
}
bool operator==(const Matrix4 &a, const Matrix4 &b)
{
for (size_t i = 0; i < 16; i++)
{
if (a.data[i] != b.data[i])
{
return false;
}
}
return true;
}
bool operator!=(const Matrix4 &a, const Matrix4 &b)
{
return !(a == b);
}

Просмотреть файл

@ -1,145 +0,0 @@
//
// 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.
//
#include "geometry_utils.h"
#define _USE_MATH_DEFINES
#include <math.h>
void CreateSphereGeometry(size_t sliceCount, float radius, SphereGeometry *result)
{
size_t parellelCount = sliceCount / 2;
size_t vertexCount = (parellelCount + 1) * (sliceCount + 1);
size_t indexCount = parellelCount * sliceCount * 6;
float angleStep = static_cast<float>(2.0f * M_PI) / sliceCount;
result->positions.resize(vertexCount);
result->normals.resize(vertexCount);
for (size_t i = 0; i < parellelCount + 1; i++)
{
for (size_t j = 0; j < sliceCount + 1; j++)
{
Vector3 direction(sinf(angleStep * i) * sinf(angleStep * j),
cosf(angleStep * i),
sinf(angleStep * i) * cosf(angleStep * j));
size_t vertexIdx = i * (sliceCount + 1) + j;
result->positions[vertexIdx] = direction * radius;
result->normals[vertexIdx] = direction;
}
}
result->indices.clear();
result->indices.reserve(indexCount);
for (size_t i = 0; i < parellelCount; i++)
{
for (size_t j = 0; j < sliceCount; j++)
{
result->indices.push_back(static_cast<unsigned short>( i * (sliceCount + 1) + j ));
result->indices.push_back(static_cast<unsigned short>((i + 1) * (sliceCount + 1) + j ));
result->indices.push_back(static_cast<unsigned short>((i + 1) * (sliceCount + 1) + (j + 1)));
result->indices.push_back(static_cast<unsigned short>( i * (sliceCount + 1) + j ));
result->indices.push_back(static_cast<unsigned short>((i + 1) * (sliceCount + 1) + (j + 1)));
result->indices.push_back(static_cast<unsigned short>( i * (sliceCount + 1) + (j + 1)));
}
}
}
void GenerateCubeGeometry(float radius, CubeGeometry *result)
{
result->positions.resize(24);
result->positions[ 0] = Vector3(-radius, -radius, -radius);
result->positions[ 1] = Vector3(-radius, -radius, radius);
result->positions[ 2] = Vector3( radius, -radius, radius);
result->positions[ 3] = Vector3( radius, -radius, -radius);
result->positions[ 4] = Vector3(-radius, radius, -radius);
result->positions[ 5] = Vector3(-radius, radius, radius);
result->positions[ 6] = Vector3( radius, radius, radius);
result->positions[ 7] = Vector3( radius, radius, -radius);
result->positions[ 8] = Vector3(-radius, -radius, -radius);
result->positions[ 9] = Vector3(-radius, radius, -radius);
result->positions[10] = Vector3( radius, radius, -radius);
result->positions[11] = Vector3( radius, -radius, -radius);
result->positions[12] = Vector3(-radius, -radius, radius);
result->positions[13] = Vector3(-radius, radius, radius);
result->positions[14] = Vector3( radius, radius, radius);
result->positions[15] = Vector3( radius, -radius, radius);
result->positions[16] = Vector3(-radius, -radius, -radius);
result->positions[17] = Vector3(-radius, -radius, radius);
result->positions[18] = Vector3(-radius, radius, radius);
result->positions[19] = Vector3(-radius, radius, -radius);
result->positions[20] = Vector3( radius, -radius, -radius);
result->positions[21] = Vector3( radius, -radius, radius);
result->positions[22] = Vector3( radius, radius, radius);
result->positions[23] = Vector3( radius, radius, -radius);
result->normals.resize(24);
result->normals[ 0] = Vector3( 0.0f, -1.0f, 0.0f);
result->normals[ 1] = Vector3( 0.0f, -1.0f, 0.0f);
result->normals[ 2] = Vector3( 0.0f, -1.0f, 0.0f);
result->normals[ 3] = Vector3( 0.0f, -1.0f, 0.0f);
result->normals[ 4] = Vector3( 0.0f, 1.0f, 0.0f);
result->normals[ 5] = Vector3( 0.0f, 1.0f, 0.0f);
result->normals[ 6] = Vector3( 0.0f, 1.0f, 0.0f);
result->normals[ 7] = Vector3( 0.0f, 1.0f, 0.0f);
result->normals[ 8] = Vector3( 0.0f, 0.0f, -1.0f);
result->normals[ 9] = Vector3( 0.0f, 0.0f, -1.0f);
result->normals[10] = Vector3( 0.0f, 0.0f, -1.0f);
result->normals[11] = Vector3( 0.0f, 0.0f, -1.0f);
result->normals[12] = Vector3( 0.0f, 0.0f, 1.0f);
result->normals[13] = Vector3( 0.0f, 0.0f, 1.0f);
result->normals[14] = Vector3( 0.0f, 0.0f, 1.0f);
result->normals[15] = Vector3( 0.0f, 0.0f, 1.0f);
result->normals[16] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[17] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[18] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[19] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[20] = Vector3( 1.0f, 0.0f, 0.0f);
result->normals[21] = Vector3( 1.0f, 0.0f, 0.0f);
result->normals[22] = Vector3( 1.0f, 0.0f, 0.0f);
result->normals[23] = Vector3( 1.0f, 0.0f, 0.0f);
result->texcoords.resize(24);
result->texcoords[ 0] = Vector2(0.0f, 0.0f);
result->texcoords[ 1] = Vector2(0.0f, 1.0f);
result->texcoords[ 2] = Vector2(1.0f, 1.0f);
result->texcoords[ 3] = Vector2(1.0f, 0.0f);
result->texcoords[ 4] = Vector2(1.0f, 0.0f);
result->texcoords[ 5] = Vector2(1.0f, 1.0f);
result->texcoords[ 6] = Vector2(0.0f, 1.0f);
result->texcoords[ 7] = Vector2(0.0f, 0.0f);
result->texcoords[ 8] = Vector2(0.0f, 0.0f);
result->texcoords[ 9] = Vector2(0.0f, 1.0f);
result->texcoords[10] = Vector2(1.0f, 1.0f);
result->texcoords[11] = Vector2(1.0f, 0.0f);
result->texcoords[12] = Vector2(0.0f, 0.0f);
result->texcoords[13] = Vector2(0.0f, 1.0f);
result->texcoords[14] = Vector2(1.0f, 1.0f);
result->texcoords[15] = Vector2(1.0f, 0.0f);
result->texcoords[16] = Vector2(0.0f, 0.0f);
result->texcoords[17] = Vector2(0.0f, 1.0f);
result->texcoords[18] = Vector2(1.0f, 1.0f);
result->texcoords[19] = Vector2(1.0f, 0.0f);
result->texcoords[20] = Vector2(0.0f, 0.0f);
result->texcoords[21] = Vector2(0.0f, 1.0f);
result->texcoords[22] = Vector2(1.0f, 1.0f);
result->texcoords[23] = Vector2(1.0f, 0.0f);
result->indices.resize(36);
result->indices[ 0] = 0; result->indices[ 1] = 2; result->indices[ 2] = 1;
result->indices[ 3] = 0; result->indices[ 4] = 3; result->indices[ 5] = 2;
result->indices[ 6] = 4; result->indices[ 7] = 5; result->indices[ 8] = 6;
result->indices[ 9] = 4; result->indices[10] = 6; result->indices[11] = 7;
result->indices[12] = 8; result->indices[13] = 9; result->indices[14] = 10;
result->indices[15] = 8; result->indices[16] = 10; result->indices[17] = 11;
result->indices[18] = 12; result->indices[19] = 15; result->indices[20] = 14;
result->indices[21] = 12; result->indices[22] = 14; result->indices[23] = 13;
result->indices[24] = 16; result->indices[25] = 17; result->indices[26] = 18;
result->indices[27] = 16; result->indices[28] = 18; result->indices[29] = 19;
result->indices[30] = 20; result->indices[31] = 23; result->indices[32] = 22;
result->indices[33] = 20; result->indices[34] = 22; result->indices[35] = 21;
}

Просмотреть файл

@ -34,14 +34,8 @@
], ],
'sources': 'sources':
[ [
'sample_util/Matrix.cpp',
'sample_util/Matrix.h',
'sample_util/SampleApplication.cpp', 'sample_util/SampleApplication.cpp',
'sample_util/SampleApplication.h', 'sample_util/SampleApplication.h',
'sample_util/Vector.cpp',
'sample_util/Vector.h',
'sample_util/geometry_utils.cpp',
'sample_util/geometry_utils.h',
'sample_util/texture_utils.cpp', 'sample_util/texture_utils.cpp',
'sample_util/texture_utils.h', 'sample_util/texture_utils.h',
'sample_util/tga_utils.cpp', 'sample_util/tga_utils.cpp',

302
util/Matrix.cpp Normal file
Просмотреть файл

@ -0,0 +1,302 @@
//
// 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.
//
// Matrix:
// Helper class for doing matrix math.
//
#include "Matrix.h"
#define _USE_MATH_DEFINES
#include <math.h>
#include <cstddef>
Matrix4::Matrix4()
{
data[0] = 1.0f;
data[4] = 0.0f;
data[8] = 0.0f;
data[12] = 0.0f;
data[1] = 0.0f;
data[5] = 1.0f;
data[9] = 0.0f;
data[13] = 0.0f;
data[2] = 0.0f;
data[6] = 0.0f;
data[10] = 1.0f;
data[14] = 0.0f;
data[3] = 0.0f;
data[7] = 0.0f;
data[11] = 0.0f;
data[15] = 1.0f;
}
Matrix4::Matrix4(float m00,
float m01,
float m02,
float m03,
float m10,
float m11,
float m12,
float m13,
float m20,
float m21,
float m22,
float m23,
float m30,
float m31,
float m32,
float m33)
{
data[0] = m00;
data[4] = m01;
data[8] = m02;
data[12] = m03;
data[1] = m10;
data[5] = m11;
data[9] = m12;
data[13] = m13;
data[2] = m20;
data[6] = m21;
data[10] = m22;
data[14] = m23;
data[3] = m30;
data[7] = m31;
data[11] = m32;
data[15] = m33;
}
Matrix4 Matrix4::identity()
{
return Matrix4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f);
}
Matrix4 Matrix4::rotate(float angle, const Vector3 &p)
{
Vector3 u = Vector3::normalize(p);
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);
}
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);
}
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);
}
Matrix4 Matrix4::frustum(float l, float r, float b, float t, float n, float f)
{
return Matrix4((2.0f * n) / (r - l), 0.0f, (r + l) / (r - l), 0.0f, 0.0f, (2.0f * n) / (t - b),
(t + b) / (t - b), 0.0f, 0.0f, 0.0f, -(f + n) / (f - n),
-(2.0f * f * n) / (f - n), 0.0f, 0.0f, -1.0f, 0.0f);
}
Matrix4 Matrix4::perspective(float fovY, float aspectRatio, float nearZ, float farZ)
{
const float frustumHeight = tanf(static_cast<float>(fovY / 360.0f * M_PI)) * nearZ;
const float frustumWidth = frustumHeight * aspectRatio;
return frustum(-frustumWidth, frustumWidth, -frustumHeight, frustumHeight, nearZ, farZ);
}
Matrix4 Matrix4::ortho(float l, float r, float b, float t, float n, float f)
{
return Matrix4(2.0f / (r - l), 0.0f, 0.0f, -(r + l) / (r - l), 0.0f, 2.0f / (t - b), 0.0f,
-(t + b) / (t - b), 0.0f, 0.0f, -2.0f / (f - n), -(f + n) / (f - n), 0.0f, 0.0f,
0.0f, 1.0f);
}
Matrix4 Matrix4::rollPitchYaw(float roll, float pitch, float yaw)
{
return rotate(yaw, Vector3(0, 0, 1)) * rotate(pitch, Vector3(0, 1, 0)) *
rotate(roll, Vector3(1, 0, 0));
}
Matrix4 Matrix4::invert(const Matrix4 &mat)
{
Matrix4 inverted(
mat.data[5] * mat.data[10] * mat.data[15] - mat.data[5] * mat.data[11] * mat.data[14] -
mat.data[9] * mat.data[6] * mat.data[15] + mat.data[9] * mat.data[7] * mat.data[14] +
mat.data[13] * mat.data[6] * mat.data[11] - mat.data[13] * mat.data[7] * mat.data[10],
-mat.data[4] * mat.data[10] * mat.data[15] + mat.data[4] * mat.data[11] * mat.data[14] +
mat.data[8] * mat.data[6] * mat.data[15] - mat.data[8] * mat.data[7] * mat.data[14] -
mat.data[12] * mat.data[6] * mat.data[11] + mat.data[12] * mat.data[7] * mat.data[10],
mat.data[4] * mat.data[9] * mat.data[15] - mat.data[4] * mat.data[11] * mat.data[13] -
mat.data[8] * mat.data[5] * mat.data[15] + mat.data[8] * mat.data[7] * mat.data[13] +
mat.data[12] * mat.data[5] * mat.data[11] - mat.data[12] * mat.data[7] * mat.data[9],
-mat.data[4] * mat.data[9] * mat.data[14] + mat.data[4] * mat.data[10] * mat.data[13] +
mat.data[8] * mat.data[5] * mat.data[14] - mat.data[8] * mat.data[6] * mat.data[13] -
mat.data[12] * mat.data[5] * mat.data[10] + mat.data[12] * mat.data[6] * mat.data[9],
-mat.data[1] * mat.data[10] * mat.data[15] + mat.data[1] * mat.data[11] * mat.data[14] +
mat.data[9] * mat.data[2] * mat.data[15] - mat.data[9] * mat.data[3] * mat.data[14] -
mat.data[13] * mat.data[2] * mat.data[11] + mat.data[13] * mat.data[3] * mat.data[10],
mat.data[0] * mat.data[10] * mat.data[15] - mat.data[0] * mat.data[11] * mat.data[14] -
mat.data[8] * mat.data[2] * mat.data[15] + mat.data[8] * mat.data[3] * mat.data[14] +
mat.data[12] * mat.data[2] * mat.data[11] - mat.data[12] * mat.data[3] * mat.data[10],
-mat.data[0] * mat.data[9] * mat.data[15] + mat.data[0] * mat.data[11] * mat.data[13] +
mat.data[8] * mat.data[1] * mat.data[15] - mat.data[8] * mat.data[3] * mat.data[13] -
mat.data[12] * mat.data[1] * mat.data[11] + mat.data[12] * mat.data[3] * mat.data[9],
mat.data[0] * mat.data[9] * mat.data[14] - mat.data[0] * mat.data[10] * mat.data[13] -
mat.data[8] * mat.data[1] * mat.data[14] + mat.data[8] * mat.data[2] * mat.data[13] +
mat.data[12] * mat.data[1] * mat.data[10] - mat.data[12] * mat.data[2] * mat.data[9],
mat.data[1] * mat.data[6] * mat.data[15] - mat.data[1] * mat.data[7] * mat.data[14] -
mat.data[5] * mat.data[2] * mat.data[15] + mat.data[5] * mat.data[3] * mat.data[14] +
mat.data[13] * mat.data[2] * mat.data[7] - mat.data[13] * mat.data[3] * mat.data[6],
-mat.data[0] * mat.data[6] * mat.data[15] + mat.data[0] * mat.data[7] * mat.data[14] +
mat.data[4] * mat.data[2] * mat.data[15] - mat.data[4] * mat.data[3] * mat.data[14] -
mat.data[12] * mat.data[2] * mat.data[7] + mat.data[12] * mat.data[3] * mat.data[6],
mat.data[0] * mat.data[5] * mat.data[15] - mat.data[0] * mat.data[7] * mat.data[13] -
mat.data[4] * mat.data[1] * mat.data[15] + mat.data[4] * mat.data[3] * mat.data[13] +
mat.data[12] * mat.data[1] * mat.data[7] - mat.data[12] * mat.data[3] * mat.data[5],
-mat.data[0] * mat.data[5] * mat.data[14] + mat.data[0] * mat.data[6] * mat.data[13] +
mat.data[4] * mat.data[1] * mat.data[14] - mat.data[4] * mat.data[2] * mat.data[13] -
mat.data[12] * mat.data[1] * mat.data[6] + mat.data[12] * mat.data[2] * mat.data[5],
-mat.data[1] * mat.data[6] * mat.data[11] + mat.data[1] * mat.data[7] * mat.data[10] +
mat.data[5] * mat.data[2] * mat.data[11] - mat.data[5] * mat.data[3] * mat.data[10] -
mat.data[9] * mat.data[2] * mat.data[7] + mat.data[9] * mat.data[3] * mat.data[6],
mat.data[0] * mat.data[6] * mat.data[11] - mat.data[0] * mat.data[7] * mat.data[10] -
mat.data[4] * mat.data[2] * mat.data[11] + mat.data[4] * mat.data[3] * mat.data[10] +
mat.data[8] * mat.data[2] * mat.data[7] - mat.data[8] * mat.data[3] * mat.data[6],
-mat.data[0] * mat.data[5] * mat.data[11] + mat.data[0] * mat.data[7] * mat.data[9] +
mat.data[4] * mat.data[1] * mat.data[11] - mat.data[4] * mat.data[3] * mat.data[9] -
mat.data[8] * mat.data[1] * mat.data[7] + mat.data[8] * mat.data[3] * mat.data[5],
mat.data[0] * mat.data[5] * mat.data[10] - mat.data[0] * mat.data[6] * mat.data[9] -
mat.data[4] * mat.data[1] * mat.data[10] + mat.data[4] * mat.data[2] * mat.data[9] +
mat.data[8] * mat.data[1] * mat.data[6] - mat.data[8] * mat.data[2] * mat.data[5]);
float determinant = mat.data[0] * inverted.data[0] + mat.data[1] * inverted.data[4] +
mat.data[2] * inverted.data[8] + mat.data[3] * inverted.data[12];
if (determinant != 0.0f)
{
inverted *= 1.0f / determinant;
}
else
{
inverted = identity();
}
return inverted;
}
Matrix4 Matrix4::transpose(const Matrix4 &mat)
{
return Matrix4(mat.data[0], mat.data[1], mat.data[2], mat.data[3], mat.data[4], mat.data[5],
mat.data[6], mat.data[7], mat.data[8], mat.data[9], mat.data[10], mat.data[11],
mat.data[12], mat.data[13], mat.data[14], mat.data[15]);
}
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);
}
Vector3 Matrix4::transform(const Matrix4 &mat, const Vector4 &pt)
{
Vector4 transformed = Vector4::normalize(mat * pt);
return Vector3(transformed.x, transformed.y, transformed.z);
}
Matrix4 operator*(const Matrix4 &a, const Matrix4 &b)
{
return Matrix4(a.data[0] * b.data[0] + a.data[4] * b.data[1] + a.data[8] * b.data[2] +
a.data[12] * b.data[3],
a.data[0] * b.data[4] + a.data[4] * b.data[5] + a.data[8] * b.data[6] +
a.data[12] * b.data[7],
a.data[0] * b.data[8] + a.data[4] * b.data[9] + a.data[8] * b.data[10] +
a.data[12] * b.data[11],
a.data[0] * b.data[12] + a.data[4] * b.data[13] + a.data[8] * b.data[14] +
a.data[12] * b.data[15],
a.data[1] * b.data[0] + a.data[5] * b.data[1] + a.data[9] * b.data[2] +
a.data[13] * b.data[3],
a.data[1] * b.data[4] + a.data[5] * b.data[5] + a.data[9] * b.data[6] +
a.data[13] * b.data[7],
a.data[1] * b.data[8] + a.data[5] * b.data[9] + a.data[9] * b.data[10] +
a.data[13] * b.data[11],
a.data[1] * b.data[12] + a.data[5] * b.data[13] + a.data[9] * b.data[14] +
a.data[13] * b.data[15],
a.data[2] * b.data[0] + a.data[6] * b.data[1] + a.data[10] * b.data[2] +
a.data[14] * b.data[3],
a.data[2] * b.data[4] + a.data[6] * b.data[5] + a.data[10] * b.data[6] +
a.data[14] * b.data[7],
a.data[2] * b.data[8] + a.data[6] * b.data[9] + a.data[10] * b.data[10] +
a.data[14] * b.data[11],
a.data[2] * b.data[12] + a.data[6] * b.data[13] + a.data[10] * b.data[14] +
a.data[14] * b.data[15],
a.data[3] * b.data[0] + a.data[7] * b.data[1] + a.data[11] * b.data[2] +
a.data[15] * b.data[3],
a.data[3] * b.data[4] + a.data[7] * b.data[5] + a.data[11] * b.data[6] +
a.data[15] * b.data[7],
a.data[3] * b.data[8] + a.data[7] * b.data[9] + a.data[11] * b.data[10] +
a.data[15] * b.data[11],
a.data[3] * b.data[12] + a.data[7] * b.data[13] + a.data[11] * b.data[14] +
a.data[15] * b.data[15]);
}
Matrix4 &operator*=(Matrix4 &a, const Matrix4 &b)
{
a = a * b;
return a;
}
Matrix4 operator*(const Matrix4 &a, float b)
{
Matrix4 ret(a);
for (size_t i = 0; i < 16; i++)
{
ret.data[i] *= b;
}
return ret;
}
Matrix4 &operator*=(Matrix4 &a, float b)
{
for (size_t i = 0; i < 16; i++)
{
a.data[i] *= b;
}
return a;
}
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);
}
bool operator==(const Matrix4 &a, const Matrix4 &b)
{
for (size_t i = 0; i < 16; i++)
{
if (a.data[i] != b.data[i])
{
return false;
}
}
return true;
}
bool operator!=(const Matrix4 &a, const Matrix4 &b)
{
return !(a == b);
}

Просмотреть файл

@ -3,9 +3,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Matrix:
// Helper class for doing matrix math.
//
#ifndef SAMPLE_UTIL_MATRIX_H #ifndef UTIL_MATRIX_H
#define SAMPLE_UTIL_MATRIX_H #define UTIL_MATRIX_H
#include "Vector.h" #include "Vector.h"
@ -14,10 +17,22 @@ struct Matrix4
float data[16]; float data[16];
Matrix4(); Matrix4();
Matrix4(float m00, float m01, float m02, float m03, Matrix4(float m00,
float m10, float m11, float m12, float m13, float m01,
float m20, float m21, float m22, float m23, float m02,
float m30, float m31, float m32, float m33); float m03,
float m10,
float m11,
float m12,
float m13,
float m20,
float m21,
float m22,
float m23,
float m30,
float m31,
float m32,
float m33);
static Matrix4 identity(); static Matrix4 identity();
static Matrix4 rotate(float angle, const Vector3 &p); static Matrix4 rotate(float angle, const Vector3 &p);
@ -43,4 +58,4 @@ Vector4 operator*(const Matrix4 &a, const Vector4 &b);
bool operator==(const Matrix4 &a, const Matrix4 &b); bool operator==(const Matrix4 &a, const Matrix4 &b);
bool operator!=(const Matrix4 &a, const Matrix4 &b); bool operator!=(const Matrix4 &a, const Matrix4 &b);
#endif // SAMPLE_UTIL_MATRIX_H #endif // UTIL_MATRIX_H

Просмотреть файл

@ -3,20 +3,19 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Vector:
// Vector class for linear math.
//
#include "Vector.h" #include "Vector.h"
#include <math.h> #include <math.h>
Vector2::Vector2() Vector2::Vector2() : x(0.0), y(0.0)
: x(0.0),
y(0.0)
{ {
} }
Vector2::Vector2(float x, float y) Vector2::Vector2(float x, float y) : x(x), y(y)
: x(x),
y(y)
{ {
} }
@ -28,8 +27,7 @@ float Vector2::length(const Vector2 &vec)
float Vector2::lengthSquared(const Vector2 &vec) float Vector2::lengthSquared(const Vector2 &vec)
{ {
return vec.x * vec.x + return vec.x * vec.x + vec.y * vec.y;
vec.y * vec.y;
} }
Vector2 Vector2::normalize(const Vector2 &vec) Vector2 Vector2::normalize(const Vector2 &vec)
@ -39,23 +37,17 @@ Vector2 Vector2::normalize(const Vector2 &vec)
if (len != 0.0f) if (len != 0.0f)
{ {
float invLen = 1.0f / len; float invLen = 1.0f / len;
ret.x = vec.x * invLen; ret.x = vec.x * invLen;
ret.y = vec.y * invLen; ret.y = vec.y * invLen;
} }
return ret; return ret;
} }
Vector3::Vector3() Vector3::Vector3() : x(0.0), y(0.0), z(0.0)
: x(0.0),
y(0.0),
z(0.0)
{ {
} }
Vector3::Vector3(float x, float y, float z) Vector3::Vector3(float x, float y, float z) : x(x), y(y), z(z)
: x(x),
y(y),
z(z)
{ {
} }
@ -67,9 +59,7 @@ float Vector3::length(const Vector3 &vec)
float Vector3::lengthSquared(const Vector3 &vec) float Vector3::lengthSquared(const Vector3 &vec)
{ {
return vec.x * vec.x + return vec.x * vec.x + vec.y * vec.y + vec.z * vec.z;
vec.y * vec.y +
vec.z * vec.z;
} }
Vector3 Vector3::normalize(const Vector3 &vec) Vector3 Vector3::normalize(const Vector3 &vec)
@ -79,82 +69,58 @@ Vector3 Vector3::normalize(const Vector3 &vec)
if (len != 0.0f) if (len != 0.0f)
{ {
float invLen = 1.0f / len; float invLen = 1.0f / len;
ret.x = vec.x * invLen; ret.x = vec.x * invLen;
ret.y = vec.y * invLen; ret.y = vec.y * invLen;
ret.z = vec.z * invLen; ret.z = vec.z * invLen;
} }
return ret; return ret;
} }
float Vector3::dot(const Vector3 &a, const Vector3 &b) float Vector3::dot(const Vector3 &a, const Vector3 &b)
{ {
return a.x * b.x + return a.x * b.x + a.y * b.y + a.z * b.z;
a.y * b.y +
a.z * b.z;
} }
Vector3 Vector3::cross(const Vector3 &a, const Vector3 &b) Vector3 Vector3::cross(const Vector3 &a, const Vector3 &b)
{ {
return Vector3(a.y * b.z - a.z * b.y, 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);
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
} }
Vector3 operator*(const Vector3 &a, const Vector3 &b) Vector3 operator*(const Vector3 &a, const Vector3 &b)
{ {
return Vector3(a.x * b.x, return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
a.y * b.y,
a.z * b.z);
} }
Vector3 operator*(const Vector3 &a, const float& b) Vector3 operator*(const Vector3 &a, const float &b)
{ {
return Vector3(a.x * b, return Vector3(a.x * b, a.y * b, a.z * b);
a.y * b,
a.z * b);
} }
Vector3 operator/(const Vector3 &a, const Vector3 &b) Vector3 operator/(const Vector3 &a, const Vector3 &b)
{ {
return Vector3(a.x / b.x, return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
a.y / b.y,
a.z / b.z);
} }
Vector3 operator/(const Vector3 &a, const float& b) Vector3 operator/(const Vector3 &a, const float &b)
{ {
return Vector3(a.x / b, return Vector3(a.x / b, a.y / b, a.z / b);
a.y / b,
a.z / b);
} }
Vector3 operator+(const Vector3 &a, const Vector3 &b) Vector3 operator+(const Vector3 &a, const Vector3 &b)
{ {
return Vector3(a.x + b.x, return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
a.y + b.y,
a.z + b.z);
} }
Vector3 operator-(const Vector3 &a, const Vector3 &b) Vector3 operator-(const Vector3 &a, const Vector3 &b)
{ {
return Vector3(a.x - b.x, return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
a.y - b.y,
a.z - b.z);
} }
Vector4::Vector4() Vector4::Vector4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f)
: x(0.0f),
y(0.0f),
z(0.0f),
w(0.0f)
{ {
} }
Vector4::Vector4(float x, float y, float z, float w) Vector4::Vector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w)
: x(x),
y(y),
z(z),
w(w)
{ {
} }
@ -166,10 +132,7 @@ float Vector4::length(const Vector4 &vec)
float Vector4::lengthSquared(const Vector4 &vec) float Vector4::lengthSquared(const Vector4 &vec)
{ {
return vec.x * vec.x + return vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w;
vec.y * vec.y +
vec.z * vec.z +
vec.w * vec.w;
} }
Vector4 Vector4::normalize(const Vector4 &vec) Vector4 Vector4::normalize(const Vector4 &vec)
@ -178,17 +141,14 @@ Vector4 Vector4::normalize(const Vector4 &vec)
if (vec.w != 0.0f) if (vec.w != 0.0f)
{ {
float invLen = 1.0f / vec.w; float invLen = 1.0f / vec.w;
ret.x = vec.x * invLen; ret.x = vec.x * invLen;
ret.y = vec.y * invLen; ret.y = vec.y * invLen;
ret.z = vec.z * invLen; ret.z = vec.z * invLen;
} }
return ret; return ret;
} }
float Vector4::dot(const Vector4 &a, const Vector4 &b) float Vector4::dot(const Vector4 &a, const Vector4 &b)
{ {
return a.x * b.x + return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
a.y * b.y +
a.z * b.z +
a.w * b.w;
} }

Просмотреть файл

@ -3,21 +3,15 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Vector:
// Vector class for linear math.
//
#ifndef SAMPLE_UTIL_VECTOR_H #ifndef UTIL_VECTOR_H
#define SAMPLE_UTIL_VECTOR_H #define UTIL_VECTOR_H
struct Vector2 struct Vector2
{ {
union
{
struct
{
float x, y;
};
float data[2];
};
Vector2(); Vector2();
Vector2(float x, float y); Vector2(float x, float y);
@ -25,19 +19,15 @@ struct Vector2
static float lengthSquared(const Vector2 &vec); static float lengthSquared(const Vector2 &vec);
static Vector2 normalize(const Vector2 &vec); static Vector2 normalize(const Vector2 &vec);
float *data() { return &x; }
const float *data() const { return &x; }
float x, y;
}; };
struct Vector3 struct Vector3
{ {
union
{
struct
{
float x, y, z;
};
float data[3];
};
Vector3(); Vector3();
Vector3(float x, float y, float z); Vector3(float x, float y, float z);
@ -48,26 +38,22 @@ struct Vector3
static float dot(const Vector3 &a, const Vector3 &b); static float dot(const Vector3 &a, const Vector3 &b);
static Vector3 cross(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;
}; };
Vector3 operator*(const Vector3 &a, const Vector3 &b); Vector3 operator*(const Vector3 &a, const Vector3 &b);
Vector3 operator*(const Vector3 &a, const float& b); Vector3 operator*(const Vector3 &a, const float &b);
Vector3 operator/(const Vector3 &a, const Vector3 &b); Vector3 operator/(const Vector3 &a, const Vector3 &b);
Vector3 operator/(const Vector3 &a, const float& b); Vector3 operator/(const Vector3 &a, const float &b);
Vector3 operator+(const Vector3 &a, const Vector3 &b); Vector3 operator+(const Vector3 &a, const Vector3 &b);
Vector3 operator-(const Vector3 &a, const Vector3 &b); Vector3 operator-(const Vector3 &a, const Vector3 &b);
struct Vector4 struct Vector4
{ {
union
{
struct
{
float x, y, z, w;
};
float data[4];
};
Vector4(); Vector4();
Vector4(float x, float y, float z, float w); Vector4(float x, float y, float z, float w);
@ -77,6 +63,11 @@ struct Vector4
static Vector4 normalize(const Vector4 &vec); static Vector4 normalize(const Vector4 &vec);
static float dot(const Vector4 &a, const Vector4 &b); 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 // SAMPLE_UTIL_VECTOR_H #endif // UTIL_VECTOR_H

173
util/geometry_utils.cpp Normal file
Просмотреть файл

@ -0,0 +1,173 @@
//
// 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.
//
// geometry_utils:
// Helper library for generating certain sets of geometry.
//
#include "geometry_utils.h"
#define _USE_MATH_DEFINES
#include <math.h>
void CreateSphereGeometry(size_t sliceCount, float radius, SphereGeometry *result)
{
size_t parellelCount = sliceCount / 2;
size_t vertexCount = (parellelCount + 1) * (sliceCount + 1);
size_t indexCount = parellelCount * sliceCount * 6;
float angleStep = static_cast<float>(2.0f * M_PI) / sliceCount;
result->positions.resize(vertexCount);
result->normals.resize(vertexCount);
for (size_t i = 0; i < parellelCount + 1; i++)
{
for (size_t j = 0; j < sliceCount + 1; j++)
{
Vector3 direction(sinf(angleStep * i) * sinf(angleStep * j), cosf(angleStep * i),
sinf(angleStep * i) * cosf(angleStep * j));
size_t vertexIdx = i * (sliceCount + 1) + j;
result->positions[vertexIdx] = direction * radius;
result->normals[vertexIdx] = direction;
}
}
result->indices.clear();
result->indices.reserve(indexCount);
for (size_t i = 0; i < parellelCount; i++)
{
for (size_t j = 0; j < sliceCount; j++)
{
result->indices.push_back(static_cast<unsigned short>(i * (sliceCount + 1) + j));
result->indices.push_back(static_cast<unsigned short>((i + 1) * (sliceCount + 1) + j));
result->indices.push_back(
static_cast<unsigned short>((i + 1) * (sliceCount + 1) + (j + 1)));
result->indices.push_back(static_cast<unsigned short>(i * (sliceCount + 1) + j));
result->indices.push_back(
static_cast<unsigned short>((i + 1) * (sliceCount + 1) + (j + 1)));
result->indices.push_back(static_cast<unsigned short>(i * (sliceCount + 1) + (j + 1)));
}
}
}
void GenerateCubeGeometry(float radius, CubeGeometry *result)
{
result->positions.resize(24);
result->positions[0] = Vector3(-radius, -radius, -radius);
result->positions[1] = Vector3(-radius, -radius, radius);
result->positions[2] = Vector3(radius, -radius, radius);
result->positions[3] = Vector3(radius, -radius, -radius);
result->positions[4] = Vector3(-radius, radius, -radius);
result->positions[5] = Vector3(-radius, radius, radius);
result->positions[6] = Vector3(radius, radius, radius);
result->positions[7] = Vector3(radius, radius, -radius);
result->positions[8] = Vector3(-radius, -radius, -radius);
result->positions[9] = Vector3(-radius, radius, -radius);
result->positions[10] = Vector3(radius, radius, -radius);
result->positions[11] = Vector3(radius, -radius, -radius);
result->positions[12] = Vector3(-radius, -radius, radius);
result->positions[13] = Vector3(-radius, radius, radius);
result->positions[14] = Vector3(radius, radius, radius);
result->positions[15] = Vector3(radius, -radius, radius);
result->positions[16] = Vector3(-radius, -radius, -radius);
result->positions[17] = Vector3(-radius, -radius, radius);
result->positions[18] = Vector3(-radius, radius, radius);
result->positions[19] = Vector3(-radius, radius, -radius);
result->positions[20] = Vector3(radius, -radius, -radius);
result->positions[21] = Vector3(radius, -radius, radius);
result->positions[22] = Vector3(radius, radius, radius);
result->positions[23] = Vector3(radius, radius, -radius);
result->normals.resize(24);
result->normals[0] = Vector3(0.0f, -1.0f, 0.0f);
result->normals[1] = Vector3(0.0f, -1.0f, 0.0f);
result->normals[2] = Vector3(0.0f, -1.0f, 0.0f);
result->normals[3] = Vector3(0.0f, -1.0f, 0.0f);
result->normals[4] = Vector3(0.0f, 1.0f, 0.0f);
result->normals[5] = Vector3(0.0f, 1.0f, 0.0f);
result->normals[6] = Vector3(0.0f, 1.0f, 0.0f);
result->normals[7] = Vector3(0.0f, 1.0f, 0.0f);
result->normals[8] = Vector3(0.0f, 0.0f, -1.0f);
result->normals[9] = Vector3(0.0f, 0.0f, -1.0f);
result->normals[10] = Vector3(0.0f, 0.0f, -1.0f);
result->normals[11] = Vector3(0.0f, 0.0f, -1.0f);
result->normals[12] = Vector3(0.0f, 0.0f, 1.0f);
result->normals[13] = Vector3(0.0f, 0.0f, 1.0f);
result->normals[14] = Vector3(0.0f, 0.0f, 1.0f);
result->normals[15] = Vector3(0.0f, 0.0f, 1.0f);
result->normals[16] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[17] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[18] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[19] = Vector3(-1.0f, 0.0f, 0.0f);
result->normals[20] = Vector3(1.0f, 0.0f, 0.0f);
result->normals[21] = Vector3(1.0f, 0.0f, 0.0f);
result->normals[22] = Vector3(1.0f, 0.0f, 0.0f);
result->normals[23] = Vector3(1.0f, 0.0f, 0.0f);
result->texcoords.resize(24);
result->texcoords[0] = Vector2(0.0f, 0.0f);
result->texcoords[1] = Vector2(0.0f, 1.0f);
result->texcoords[2] = Vector2(1.0f, 1.0f);
result->texcoords[3] = Vector2(1.0f, 0.0f);
result->texcoords[4] = Vector2(1.0f, 0.0f);
result->texcoords[5] = Vector2(1.0f, 1.0f);
result->texcoords[6] = Vector2(0.0f, 1.0f);
result->texcoords[7] = Vector2(0.0f, 0.0f);
result->texcoords[8] = Vector2(0.0f, 0.0f);
result->texcoords[9] = Vector2(0.0f, 1.0f);
result->texcoords[10] = Vector2(1.0f, 1.0f);
result->texcoords[11] = Vector2(1.0f, 0.0f);
result->texcoords[12] = Vector2(0.0f, 0.0f);
result->texcoords[13] = Vector2(0.0f, 1.0f);
result->texcoords[14] = Vector2(1.0f, 1.0f);
result->texcoords[15] = Vector2(1.0f, 0.0f);
result->texcoords[16] = Vector2(0.0f, 0.0f);
result->texcoords[17] = Vector2(0.0f, 1.0f);
result->texcoords[18] = Vector2(1.0f, 1.0f);
result->texcoords[19] = Vector2(1.0f, 0.0f);
result->texcoords[20] = Vector2(0.0f, 0.0f);
result->texcoords[21] = Vector2(0.0f, 1.0f);
result->texcoords[22] = Vector2(1.0f, 1.0f);
result->texcoords[23] = Vector2(1.0f, 0.0f);
result->indices.resize(36);
result->indices[0] = 0;
result->indices[1] = 2;
result->indices[2] = 1;
result->indices[3] = 0;
result->indices[4] = 3;
result->indices[5] = 2;
result->indices[6] = 4;
result->indices[7] = 5;
result->indices[8] = 6;
result->indices[9] = 4;
result->indices[10] = 6;
result->indices[11] = 7;
result->indices[12] = 8;
result->indices[13] = 9;
result->indices[14] = 10;
result->indices[15] = 8;
result->indices[16] = 10;
result->indices[17] = 11;
result->indices[18] = 12;
result->indices[19] = 15;
result->indices[20] = 14;
result->indices[21] = 12;
result->indices[22] = 14;
result->indices[23] = 13;
result->indices[24] = 16;
result->indices[25] = 17;
result->indices[26] = 18;
result->indices[27] = 16;
result->indices[28] = 18;
result->indices[29] = 19;
result->indices[30] = 20;
result->indices[31] = 23;
result->indices[32] = 22;
result->indices[33] = 20;
result->indices[34] = 22;
result->indices[35] = 21;
}

Просмотреть файл

@ -3,9 +3,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// geometry_utils:
// Helper library for generating certain sets of geometry.
//
#ifndef SAMPLE_UTIL_GEOMETRY_UTILS_H #ifndef UTIL_GEOMETRY_UTILS_H
#define SAMPLE_UTIL_GEOMETRY_UTILS_H #define UTIL_GEOMETRY_UTILS_H
#include <cstddef> #include <cstddef>
#include <vector> #include <vector>
@ -32,4 +35,4 @@ struct CubeGeometry
void GenerateCubeGeometry(float radius, CubeGeometry *result); void GenerateCubeGeometry(float radius, CubeGeometry *result);
#endif // SAMPLE_UTIL_GEOMETRY_UTILS_H #endif // UTIL_GEOMETRY_UTILS_H

Просмотреть файл

@ -11,6 +11,8 @@
[ [
'com_utils.h', 'com_utils.h',
'keyboard.h', 'keyboard.h',
'geometry_utils.cpp',
'geometry_utils.h',
'mouse.h', 'mouse.h',
'random_utils.cpp', 'random_utils.cpp',
'random_utils.h', 'random_utils.h',
@ -20,10 +22,14 @@
'Event.h', 'Event.h',
'EGLWindow.cpp', 'EGLWindow.cpp',
'EGLWindow.h', 'EGLWindow.h',
'Matrix.cpp',
'Matrix.h',
'OSPixmap.h', 'OSPixmap.h',
'OSWindow.cpp', 'OSWindow.cpp',
'OSWindow.h', 'OSWindow.h',
'Timer.h', 'Timer.h',
'Vector.cpp',
'Vector.h',
], ],
'util_win32_sources': 'util_win32_sources':
[ [