testStelVertexBuffer.cpp   testStelVertexArray.cpp 
/* /*
* Stellarium * Stellarium
* Copyright (C) Stellarium Team * Copyright (C) 2009 Fabien Chereau
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 * as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. * of the License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/ */
#include "tests/testStelVertexBuffer.hpp" #include "tests/testStelVertexArray.hpp"
QTEST_MAIN(TestStelVertexBuffer) QTEST_MAIN(TestStelVertexArray)
void TestStelVertexBuffer::initTestCase() void TestStelVertexArray::initTestCase()
{ {
} QVector<Vec3d> vertices;
QVector<Vec2f> textureCoords;
//! Used to generate random vertices for tests. Not really random, either.
float uniform(float min = 0.0f, float max = 100.0f)
{
return min + ((max - min) * static_cast<float>(rand()) / RAND_MAX);
}
//! Simple test vertex with only a position.
struct TestVertex1
{
Vec3f vertex;
TestVertex1() : vertex(uniform(), uniform(), uniform()) {}
bool operator == (const TestVertex1& rhs) const {return vertex == rh
s.vertex;}
VERTEX_ATTRIBUTES(Vec3f Position); for (int i = 0; i < 1000; ++i)
};
//! Test vertex with a position and a texcoord.
struct TestVertex2
{
Vec3f vertex;
Vec2f texCoord;
TestVertex2()
: vertex(uniform(), uniform(), uniform())
, texCoord(uniform(0.0f, 1.0f), uniform(0.0f, 1.0f))
{ {
} Vec3d v(i+1, i+1, i+1);
v.normalize();
vertices.append(v);
bool operator == (const TestVertex2& rhs) const Vec2f t(i, i);
{ textureCoords.append(t);
return vertex == rhs.vertex && texCoord == rhs.texCoord;
} }
VERTEX_ATTRIBUTES(Vec3f Position, Vec2f TexCoord); array = StelVertexArray(vertices, StelVertexArray::TriangleStrip, te
}; xtureCoords);
}
struct TestVertex3 struct EmptyVisitor
{ {
Vec3f vertex; inline void operator()(const Vec3d* , const Vec3d* , const Vec3d* ,
Vec2f texCoord; const Vec2f* , const Vec2
Vec3f normal; f* , const Vec2f* ,
Vec4f color; unsigned int , unsigned i
nt , unsigned int )
TestVertex3()
: vertex(uniform(), uniform(), uniform())
, texCoord(uniform(0.0f, 1.0f), uniform(0.0f, 1.0f))
, normal(uniform(), uniform(), uniform())
, color(uniform(0.0f, 1.0f), uniform(0.0f, 1.0f), uniform(0.
0f, 1.0f), uniform(0.0f, 1.0f))
{ {
}
bool operator == (const TestVertex3& rhs) const
{
return vertex == rhs.vertex &&
texCoord == rhs.texCoord &&
normal == rhs.normal &&
color == rhs.color;
} }
VERTEX_ATTRIBUTES(Vec3f Position, Vec2f TexCoord, Vec3f Normal, Vec4
f Color);
}; };
template<class BufferBackend, class Vertex> void TestStelVertexArray::benchmarkForeachTriangleNoOp()
void TestStelVertexBuffer::testVertexBuffer()
{ {
StelVertexBuffer<Vertex>* buffer = QBENCHMARK {
new StelVertexBuffer<Vertex>(new BufferBackend(PrimitiveType array.foreachTriangle(EmptyVisitor());
_Triangles, Vertex::attributes()),
PrimitiveType_Triangles);
// Make sure the buffer was initialized correctly.
QCOMPARE(buffer->locked(), false);
QCOMPARE(buffer->length(), 0);
// Vertices we'll be adding (random generated).
QVector<Vertex> vertexVector;
for(int v = 0; v < 10042; ++v)
{
vertexVector.append(Vertex());
}
// Add vertices to the buffer.
foreach(Vertex vertex, vertexVector)
{
buffer->addVertex(vertex);
} }
}
// Verify buffer length. struct VerticesVisitor
QCOMPARE(buffer->length(), vertexVector.size()); {
VerticesVisitor(const VerticesVisitor& rst) : sum(rst.sum) {}
// Verify buffer contents (and that getVertex works). VerticesVisitor() : sum(0, 0, 0) {}
for(int v = 0; v < buffer->length(); ++v) inline void operator()(const Vec3d* , const Vec3d* v1, const Vec3d*
v2,
const Vec2f* , const Vec2
f* , const Vec2f* ,
unsigned int , unsigned i
nt , unsigned int )
{ {
QCOMPARE(buffer->getVertex(v), vertexVector[v]); sum += *v1 + *v2;
} }
// Test setVertex by setting the vertices in reverse order. Vec3d sum;
for(int v = 0; v < buffer->length(); ++v) };
{
buffer->setVertex(buffer->length() - v - 1, vertexVector[v])
;
}
// Verify that the vertices have been set as expected. void TestStelVertexArray::benchmarkForeachTriangle()
for(int v = 0; v < buffer->length(); ++v) {
{ Vec3d sum(0, 0, 0);
QCOMPARE(buffer->getVertex(buffer->length() - v - 1), vertex QBENCHMARK {
Vector[v]); VerticesVisitor result = array.foreachTriangle(VerticesVisit
or());
sum = result.sum;
} }
qDebug() << sum.toString();
delete buffer;
} }
void TestStelVertexBuffer::testStelQGL2ArrayVertexBuffer() void TestStelVertexArray::benchmarkForeachTriangleDirect()
{ {
testVertexBuffer<StelQGL2ArrayVertexBufferBackend, TestVertex1>(); // Now we do the same thing "manually"
testVertexBuffer<StelQGL2ArrayVertexBufferBackend, TestVertex2>(); Vec3d sum(0, 0, 0);
testVertexBuffer<StelQGL2ArrayVertexBufferBackend, TestVertex3>(); QBENCHMARK {
sum = Vec3d(0, 0, 0);
for (int i = 2; i < array.vertex.size(); ++i)
{
if ((i % 2) == 0)
{
sum += array.vertex.at(i-1) + array.vertex.a
t(i);
}
else
{
sum += array.vertex.at(i-2) + array.vertex.a
t(i);
}
}
}
qDebug() << sum.toString();
} }
 End of changes. 25 change blocks. 
105 lines changed or deleted 66 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/