#ifndef PRIMITIVES_H
#define PRIMITIVES_H
#include "utils.h"
#include "vecmath.h"
#include <cmath>
#include <vector>
namespace bex {
const std::basic_string<TCHAR>& name,
const std::basic_string<TCHAR>& materialName,
int segmentsU,
int segmentsV,
std::vector<float>* colors = 0) {
const float PI = 3.141592f;
if(segmentsU < 3 || segmentsV < 3) {
throw std::runtime_error("Invalid segment setup");
}
for(int v = 0; v < segmentsV; ++v) {
float angleTh = (static_cast<float>(v) / static_cast<float>(segmentsV - 1)) * PI;
std::vector<Vec3> positions;
std::vector<Vec3> normals;
positions.reserve(segmentsU);
normals.reserve(segmentsU);
for(int u = 0; u <= segmentsU; ++u) {
float anglePh = static_cast<float>(u) * 2.0f * PI / static_cast<float>(segmentsU);
float x = cosf(anglePh) * sinf(angleTh);
float y = cosf(angleTh);
float z = sinf(anglePh) * sinf(angleTh);
positions.push_back(Vec3(x, y, z));
normals.push_back(normalize(Vec3(x, y, z)));
}
apiCall(
ILBAddVertexData(mesh, &positions[0], &normals[0], static_cast<int32>(positions.size())));
}
for(int v = 0; v < segmentsV - 1; ++v) {
std::vector<int32> indices;
indices.reserve(segmentsU * 6);
for(int u = 0; u < segmentsU; ++u) {
int a = u + v * (segmentsU + 1);
int b = (u + 1) + v * (segmentsU + 1);
int c = (u + 1) + (v + 1) * (segmentsU + 1);
int d = u + (v + 1) * (segmentsU + 1);
if (v > 0)
{
indices.push_back(a);
indices.push_back(b);
indices.push_back(c);
}
if (v < segmentsV - 2)
{
indices.push_back(a);
indices.push_back(c);
indices.push_back(d);
}
}
}
for(int v = 0; v < segmentsV; ++v) {
std::vector<Vec2> uvs;
uvs.reserve(segmentsU);
for(int u = 0; u <= segmentsU; ++u) {
Vec2 uv;
uv.y = static_cast<float>(v) / static_cast<float>(segmentsV-1);
if (v == 0 || v == (segmentsV-1)) {
uv.x = 0.5f;
} else {
uv.x = static_cast<float>(u) / static_cast<float>(segmentsU);
}
uvs.push_back(uv);
}
apiCall(
ILBAddUVData(mesh, &uvs[0], static_cast<int32>(uvs.size())));
}
if (colors && colors->size() == (segmentsU + 1) * segmentsV * 4) {
} else {
std::vector<ColorRGBA> randomColors;
randomColors.reserve(segmentsU * segmentsV);
for(int v = 0; v < segmentsV * (segmentsU+1); ++v) {
randomColors.push_back(randomRGBA(1.0f));
}
apiCall(
ILBAddColorData(mesh, &randomColors[0], static_cast<int32>(randomColors.size())));
}
return mesh;
}
const std::basic_string<TCHAR>& name,
const std::basic_string<TCHAR>& materialName) {
Vec3 A(-1,-1,-1);
Vec3 B(-1,-1, 1);
Vec3 C( 1,-1, 1);
Vec3 D( 1,-1,-1);
Vec3 E(-1, 1,-1);
Vec3 F( 1, 1,-1);
Vec3 G( 1, 1, 1);
Vec3 H(-1, 1, 1);
Vec2 UVD(0.f, 0.f);
Vec2 UVC(1.f/3.f,0.f);
Vec2 UVB(2.f/3.f,0.f);
Vec2 UVA(1.f,0.f);
Vec2 UVH(0.f,1.f/3.f);
Vec2 UVG(1.f/3.f,1.f/3.f);
Vec2 UVF(2.f/3.f,1.f/3.f);
Vec2 UVE(1.f,1.f/3.f);
Vec2 UVL(0.f,2.f/3.f);
Vec2 UVK(1.f/3.f,2.f/3.f);
Vec2 UVJ(2.f/3.f,2.f/3.f);
Vec2 UVI(1.f,2.f/3.f);
const int vertexCount = 20;
std::vector<Vec3> positions;
std::vector<Vec3> normals;
std::vector<ILBLinearRGBA> colors;
std::vector<Vec2> uv;
std::vector<Vec3> tangents;
std::vector<Vec3> bitangents;
positions.push_back(A);
positions.push_back(B);
positions.push_back(C);
positions.push_back(D);
uv.push_back(UVA);
uv.push_back(UVB);
uv.push_back(UVF);
uv.push_back(UVE);
normals.insert(normals.end(), 4, Vec3(0,1,0));
colors.insert(colors.end(), 4, white);
tangents.insert(tangents.end(), 4, Vec3(1,0,0));
bitangents.insert(bitangents.end(), 4, Vec3(0,0,-1));
positions.push_back(E);
positions.push_back(F);
positions.push_back(G);
positions.push_back(H);
uv.push_back(UVB);
uv.push_back(UVC);
uv.push_back(UVG);
uv.push_back(UVF);
normals.insert(normals.end(), 4, Vec3(0,-1,0));
colors.insert(colors.end(), 4, white);
tangents.insert(tangents.end(), 4, Vec3(1,0,0));
bitangents.insert(bitangents.end(), 4, Vec3(0,0,1));
positions.push_back(H);
positions.push_back(G);
positions.push_back(C);
positions.push_back(B);
uv.push_back(UVC);
uv.push_back(UVD);
uv.push_back(UVH);
uv.push_back(UVG);
normals.insert(normals.end(), 4, Vec3(0,0,-1));
colors.insert(colors.end(), 4, white);
tangents.insert(tangents.end(), 4, Vec3(1,0,0));
bitangents.insert(bitangents.end(), 4, Vec3(0,-1,0));
positions.push_back(E);
positions.push_back(H);
positions.push_back(B);
positions.push_back(A);
uv.push_back(UVE);
uv.push_back(UVF);
uv.push_back(UVJ);
uv.push_back(UVI);
normals.insert(normals.end(), 4, Vec3(1,0,0));
colors.insert(colors.end(), 4, red);
tangents.insert(tangents.end(), 4, Vec3(0,0,1));
bitangents.insert(bitangents.end(), 4, Vec3(0,-1,0));
positions.push_back(G);
positions.push_back(F);
positions.push_back(D);
positions.push_back(C);
uv.push_back(UVF);
uv.push_back(UVG);
uv.push_back(UVK);
uv.push_back(UVJ);
normals.insert(normals.end(), 4, Vec3(-1,0,0));
colors.insert(colors.end(), 4, blue);
tangents.insert(tangents.end(), 4, Vec3(0,0,-1));
bitangents.insert(bitangents.end(), 4, Vec3(0,-1,0));
std::vector<int> triangles;
for (int i = 0; i < 5; i++) {
int base = i * 4;
triangles.push_back(base + 0);
triangles.push_back(base + 1);
triangles.push_back(base + 2);
triangles.push_back(base + 2);
triangles.push_back(base + 3);
triangles.push_back(base + 0);
}
return mesh;
}
const std::basic_string<TCHAR>& name,
const std::basic_string<TCHAR>& materialName) {
const int vertexCount = 4;
Vec3 normal(0.0f, 1.0f, 0.0f);
std::vector<Vec3> normals(vertexCount, normal);
std::vector<Vec3> positions(vertexCount);
for(int i = 0; i < vertexCount; ++i) {
float x = (i < 2) ? -1.0f : 1.0f;
float z = ((i == 1)||(i == 2)) ? 1.0f : -1.0f;
positions[i] = Vec3(x, 0.0f, z);
}
std::vector<int32> tris(6);
tris[0] = 0;
tris[1] = 1;
tris[2] = 2;
tris[3] = 0;
tris[4] = 2;
tris[5] = 3;
std::vector<Vec2> uvData(vertexCount);
std::vector<Vec3> tangents(vertexCount);
std::vector<Vec3> bitangents(vertexCount);
for(int i = 0; i < vertexCount; ++i) {
int idx = i & 3;
float u = (idx < 2) ? 0.0f : 1.0f;
float v = ((idx == 1)||(idx == 2)) ? 1.0f : 0.0f;
uvData[i] = Vec2(u, v);
tangents[i] = Vec3(1.0f, 0.0f, 0.0f);
bitangents[i] = Vec3(0.0f, 0.0f, -1.0f);
}
#if 0
#endif
return mesh;
}
inline void createPlaneVertices(unsigned int xres,
unsigned int yres,
std::vector<Vec3>& vertices) {
const int vertexCount = xres * yres;
vertices.resize(vertexCount);
unsigned int vertexIndex = 0;
const float x_rcp = 2.0f / ((float)xres-1.0f);
const float y_rcp = 2.0f / ((float)yres-1.0f);
for (unsigned int y = 0; y < yres; y++) {
float yp = (float)y * y_rcp - 1.0f;
for (unsigned int x = 0; x < xres; x++) {
float xp = (float)x * x_rcp - 1.0f;
vertices[vertexIndex++] = Vec3(xp, 0.0f, yp);
}
}
}
const std::basic_string<TCHAR>& name,
unsigned int xres,
unsigned int yres,
bex::Matrix4x4& mtx) {
const int vertexCount = xres * yres;
Vec3 normal(0.0f, 1.0f, 0.0f);
std::vector<Vec3> normals(vertexCount, normal);
std::vector<Vec3> positions;
createPlaneVertices(xres, yres, positions);
for (size_t i = 0; i < positions.size(); i++) {
positions[i] = mulPoint(mtx, positions[i]);
}
return pc;
}
const std::basic_string<TCHAR>& name,
const std::basic_string<TCHAR>& materialName,
unsigned int xres,
unsigned int yres,
std::vector<float>* colors = 0) {
const int vertexCount = xres * yres;
const int triangleCount = (xres-1) * (yres-1) * 2;
Vec3 normal(0.0f, 1.0f, 0.0f);
Vec3 tangent(1.0f, 0.0f, 0.0f);
Vec3 bitangent(0.0f, 0.0f, -1.0f);
std::vector<Vec3> normals(vertexCount, normal);
std::vector<Vec3> positions;
std::vector<int32> tris(triangleCount * 3);
std::vector<Vec2> uvData(vertexCount);
std::vector<Vec3> tangents(vertexCount, tangent);
std::vector<Vec3> bitangents(vertexCount, bitangent);
unsigned int vertexIndex = 0;
unsigned int triangleIndex = 0;
createPlaneVertices(xres, yres, positions);
for (unsigned int y = 0; y < yres; y++) {
for (unsigned int x = 0; x < xres; x++) {
uvData[vertexIndex] = Vec2(positions[vertexIndex].x * 0.5f + 0.5f, positions[vertexIndex].z * 0.5f + 0.5f);
vertexIndex++;
if (x != xres-1 && y != yres-1) {
unsigned int indexV1 = y * xres + x;
unsigned int indexV2 = y * xres + x + 1;
unsigned int indexV3 = y * xres + x + xres;
unsigned int indexV4 = y * xres + x + xres + 1;
tris[triangleIndex++] = indexV1;
tris[triangleIndex++] = indexV4;
tris[triangleIndex++] = indexV2;
tris[triangleIndex++] = indexV1;
tris[triangleIndex++] = indexV3;
tris[triangleIndex++] = indexV4;
}
}
}
if (colors && colors->size() == vertexCount*4) {
}
return mesh;
}
inline ILBMeshHandle createPlaneMultiUV(
ILBManagerHandle bm,
const std::basic_string<TCHAR>& name,
const std::basic_string<TCHAR>& materialName) {
const int vertexCount = 4;
Vec3 normal(0.0f, 1.0f, 0.0f);
std::vector<Vec3> normals(vertexCount, normal);
std::vector<Vec3> positions(vertexCount);
for(int i = 0; i < vertexCount; ++i) {
float x = (i < 2) ? -1.0f : 1.0f;
float z = ((i == 1)||(i == 2)) ? 1.0f : -1.0f;
positions[i] = Vec3(x, 0.0f, z);
}
std::vector<int32> tris(6);
tris[0] = 0;
tris[1] = 1;
tris[2] = 2;
tris[3] = 0;
tris[4] = 2;
tris[5] = 3;
std::vector<Vec2> uvData1(vertexCount);
std::vector<Vec2> uvData2(vertexCount);
std::vector<Vec2> uvData3(vertexCount);
std::vector<Vec2> uvData4(vertexCount);
std::vector<Vec3> tangents(vertexCount);
std::vector<Vec3> bitangents(vertexCount);
for(int i = 0; i < vertexCount; ++i) {
int idx = i & 3;
float u = (idx < 2) ? 0.0f : 0.5f;
float v = ((idx == 1)||(idx == 2)) ? 0.0f : 0.5f;
uvData1[i] = Vec2(u, v);
uvData2[i] = Vec2(u+0.5f, v);
uvData3[i] = Vec2(u, v+0.5f);
uvData4[i] = Vec2(u+0.5f, v+0.5f);
tangents[i] = Vec3(1.0f, 0.0f, 0.0f);
bitangents[i] = Vec3(0.0f, 0.0f, -1.0f);
}
return mesh;
}
const std::basic_string<TCHAR>& name,
int32 count) {
if (count < 2) {
return 0;
}
std::vector<ILBVec3> normals;
normals.resize(count,
ILBVec3(1, 0, 0));
ILBVec3 sideLength(maxCorner.
x - minCorner.
x, maxCorner.
y - minCorner.
y, maxCorner.
z - minCorner.
z);
for (int32 z = 0; z < count; z++) {
for (int32 y = 0; y < count; y++) {
std::vector<ILBVec3> points;
points.reserve(count);
for(int x = 0; x < count; ++x) {
float xx = minCorner.
x + sideLength.
x * (float)x / (
float)(count-1);
float yy = minCorner.
y + sideLength.
y * (float)y / (
float)(count-1);
float zz = minCorner.
z + sideLength.
z * (float)z / (
float)(count-1);
points.push_back(
ILBVec3(xx, yy, zz));
}
}
}
return pch;
}
}
#endif // PRIMITIVES_H