#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())));
}
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(Vec2(sinf(uv.x*2.0f*(float)M_PI), cosf(uv.y*2.0f*(float)M_PI)));
}
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) {
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;
}
}
#endif // PRIMITIVES_H