2016-11-26 18:26:10 -06:00
|
|
|
// Procedural City Generator
|
|
|
|
// Alex Huddleston
|
|
|
|
// 2016
|
2016-11-24 12:33:20 -06:00
|
|
|
|
|
|
|
#include <iostream>
|
2016-11-26 18:26:10 -06:00
|
|
|
#include <string.h>
|
2016-11-24 12:33:20 -06:00
|
|
|
#include <vector>
|
|
|
|
#include "../include/CompFab.h"
|
|
|
|
#include "../include/Mesh.h"
|
2016-12-05 11:01:12 -06:00
|
|
|
#include "../include/ppm.h"
|
|
|
|
#include "../include/PerlinNoise.h"
|
|
|
|
|
2016-11-27 01:12:12 -06:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#define PI 3.14159265
|
2016-11-24 12:33:20 -06:00
|
|
|
|
2016-12-05 11:01:12 -06:00
|
|
|
|
|
|
|
double distanceA2B(double ax, double ay, double bx, double by){
|
|
|
|
return sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay));
|
|
|
|
}
|
|
|
|
|
|
|
|
double jerfunc(double x, double y, PerlinNoise pnoise){
|
|
|
|
double n = 0;
|
|
|
|
double m = 0;
|
|
|
|
|
|
|
|
//perlin noise
|
|
|
|
n = 20 * pnoise.noise(x,y,0.8);
|
|
|
|
n = n - floor(n);
|
|
|
|
|
|
|
|
//Central distrobution
|
|
|
|
//distance from point to center
|
|
|
|
m = distanceA2B(x,y,0.5,0.5);
|
|
|
|
|
|
|
|
return (m*0.15)+(n*0.85);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//A function to find the X and Y dimensions of the template obj
|
2016-11-24 12:33:20 -06:00
|
|
|
void findLW(Mesh &m, double &l, double &w)
|
|
|
|
{
|
|
|
|
double minl, maxl, minw, maxw;
|
|
|
|
for(int i = 0; i < m.t.size(); i++)
|
|
|
|
{
|
|
|
|
if(m.v[i].m_x < minl)
|
|
|
|
{
|
|
|
|
minl = m.v[i].m_x;
|
|
|
|
}
|
|
|
|
if(m.v[i].m_x > maxl)
|
|
|
|
{
|
|
|
|
maxl = m.v[i].m_x;
|
|
|
|
}
|
|
|
|
if(m.v[i].m_y < minw)
|
|
|
|
{
|
2016-11-27 01:12:12 -06:00
|
|
|
minw = m.v[i].m_y;
|
2016-11-24 12:33:20 -06:00
|
|
|
}
|
|
|
|
if(m.v[i].m_y > maxw)
|
|
|
|
{
|
2016-11-27 01:12:12 -06:00
|
|
|
maxw = m.v[i].m_y;
|
2016-11-24 12:33:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
l = maxl - minl;
|
|
|
|
w = maxw - minw;
|
|
|
|
}
|
|
|
|
|
2016-11-27 02:52:25 -06:00
|
|
|
// Calculate translation matrices and output them as a vector of Vec3s.
|
2016-11-28 18:50:39 -06:00
|
|
|
std::vector<CompFab::Vec3> createVec3d(std::vector<CompFab::Vec3> &t, int layers, double spacing, double length, double width)
|
2016-11-27 01:12:12 -06:00
|
|
|
{
|
|
|
|
std::vector<CompFab::Vec3> *output = new std::vector<CompFab::Vec3>();
|
|
|
|
|
2016-11-27 02:52:25 -06:00
|
|
|
double ls = length + spacing;
|
|
|
|
double ws = width + spacing;
|
|
|
|
|
|
|
|
// Will be used later to determine the direction of the translation matrix.
|
|
|
|
double angle = 0.0;
|
|
|
|
|
|
|
|
CompFab::Vec3 *temp = new CompFab::Vec3(-ls, -ws, 0);
|
|
|
|
|
|
|
|
// Vec3 to hold our current translation matrix.
|
2016-12-05 11:14:33 -06:00
|
|
|
<<<<<<< HEAD
|
2016-11-30 16:21:08 -06:00
|
|
|
CompFab::Vec3 *trans = new CompFab::Vec3(ls, ws, 0);
|
2016-11-28 19:49:37 -06:00
|
|
|
|
2016-11-30 16:21:08 -06:00
|
|
|
//Vec3 to hold our current height multiplication materx.
|
2016-11-28 19:49:37 -06:00
|
|
|
CompFab::Vec3 *mult = new CompFab::Vec3(1,1,1);
|
|
|
|
|
2016-11-30 16:21:08 -06:00
|
|
|
//push_back initial templat's mesh, multiplied by the height multiplication matrix.
|
2016-11-28 19:49:37 -06:00
|
|
|
for(int i = 0; i < t.size(); i++)
|
|
|
|
{
|
|
|
|
output->push_back(mmult(*mult, t[i]));
|
|
|
|
}
|
2016-12-05 11:14:33 -06:00
|
|
|
=======
|
2016-12-05 11:01:12 -06:00
|
|
|
CompFab::Vec3 *trans = new CompFab::Vec3(0, spacing, 0);
|
|
|
|
|
|
|
|
CompFab::Vec3 *coord = new CompFab::Vec3(0.5,0.5,0);
|
2016-12-05 11:14:33 -06:00
|
|
|
>>>>>>> master
|
2016-11-27 01:12:12 -06:00
|
|
|
|
2016-11-27 02:52:25 -06:00
|
|
|
// cl for current layer.
|
2016-11-27 01:12:12 -06:00
|
|
|
for(int cl = 1; cl < layers; cl++)
|
|
|
|
{
|
2016-11-30 16:21:08 -06:00
|
|
|
// Constructor used to re-initialize temp.
|
2016-11-27 02:52:25 -06:00
|
|
|
*temp = CompFab::Vec3(-ls*cl, -ws*cl, 0);
|
2016-11-30 16:21:08 -06:00
|
|
|
|
|
|
|
// Used to change the multiplciation matrix per layer.
|
|
|
|
mult->m_z = 1.0/(cl +1);
|
2016-11-28 19:49:37 -06:00
|
|
|
|
2016-11-27 01:12:12 -06:00
|
|
|
|
|
|
|
for(int c = 0; c < cl*8; c++)
|
|
|
|
{
|
2016-11-27 02:52:25 -06:00
|
|
|
angle = (c/(2*cl))*(0.5*PI);
|
|
|
|
*trans = CompFab::Vec3(ls*cos(angle), ws*sin(angle), 0);
|
2016-12-05 11:14:33 -06:00
|
|
|
<<<<<<< HEAD
|
2016-11-28 18:50:39 -06:00
|
|
|
*temp += *trans;
|
|
|
|
for(int j = 0; j < t.size(); j++)
|
|
|
|
{
|
2016-11-28 19:49:37 -06:00
|
|
|
output->push_back(mmult(*mult, t[j]) + *temp);
|
2016-11-28 18:50:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *output;
|
|
|
|
}
|
|
|
|
std::vector<CompFab::Vec3i> createVec3id(std::vector<CompFab::Vec3i> &t, std::vector<CompFab::Vec3> &v, int layers)
|
|
|
|
{
|
|
|
|
std::vector<CompFab::Vec3i> *output = new std::vector<CompFab::Vec3i>();
|
|
|
|
*output = t;
|
|
|
|
|
|
|
|
for(int n = 1; n < pow((2*layers - 1), 2); n++)
|
|
|
|
{
|
|
|
|
CompFab::Vec3i *offset = new CompFab::Vec3i(v.size()*n);
|
|
|
|
for(int k = 0; k < t.size(); k++)
|
|
|
|
{
|
|
|
|
output->push_back(t[k] + *offset);
|
2016-12-05 11:14:33 -06:00
|
|
|
=======
|
2016-11-27 01:12:12 -06:00
|
|
|
*temp = *temp + *trans;
|
|
|
|
|
|
|
|
output->push_back(*temp);
|
2016-12-05 11:01:12 -06:00
|
|
|
|
|
|
|
*coord = *coord + mmult(trans, Vec3(1/(layers*2-1)/2),1/(layers*2-1)/2,0);
|
|
|
|
|
|
|
|
|
|
|
|
*temp=mmult(temp,Vec3(1,1,n));
|
2016-12-05 11:14:33 -06:00
|
|
|
>>>>>>> master
|
2016-11-27 01:12:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *output;
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:33:20 -06:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
|
2016-11-27 01:12:12 -06:00
|
|
|
// Error checking.
|
2016-11-26 18:26:10 -06:00
|
|
|
if(argc < 3)
|
2016-11-24 12:33:20 -06:00
|
|
|
{
|
2016-11-30 16:21:08 -06:00
|
|
|
std::cout << "Usage: [executable] [template].obj output.obj [mode (-g, etc.)] [optional: -d for debugging output]" << std::endl;
|
2016-11-26 18:26:10 -06:00
|
|
|
std::exit(1);
|
2016-11-24 12:33:20 -06:00
|
|
|
}
|
2016-11-27 01:12:12 -06:00
|
|
|
|
2016-11-30 16:21:08 -06:00
|
|
|
// Initialization of needed output variables.
|
|
|
|
std::vector<CompFab::Vec3> vv;
|
|
|
|
std::vector<CompFab::Vec3i> tt;
|
|
|
|
Mesh *output = new Mesh();
|
2016-11-26 18:26:10 -06:00
|
|
|
|
|
|
|
// Debugging
|
|
|
|
if(argc > 3)
|
|
|
|
{
|
2016-12-05 11:14:33 -06:00
|
|
|
<<<<<<< HEAD
|
2016-11-30 16:21:08 -06:00
|
|
|
if(strcmp(argv[3], "-g") == 0 || strcmp(argv[3], "-d") == 0)
|
|
|
|
{
|
|
|
|
// TODO: Modularize these.
|
|
|
|
int layers = 10;
|
|
|
|
double spacing = 1;
|
|
|
|
|
|
|
|
// Create Mesh object from file, output to manipulate from template Mesh.
|
|
|
|
Mesh *test = new Mesh(argv[1], false);
|
|
|
|
|
|
|
|
double l = 0, w = 0;
|
|
|
|
double *length = &l, *width = &w;
|
|
|
|
|
|
|
|
// Find the X and Y dimensions for the mesh. Assumes the mesh is facing upright.
|
|
|
|
findLW(*test, *length, *width);
|
|
|
|
|
|
|
|
// Calculate the translation matrices needed and apply them.
|
|
|
|
vv = createVec3d(test->v, layers, spacing, *length, *width);
|
|
|
|
|
|
|
|
// Copying needed triangle data.
|
|
|
|
tt = createVec3id(test->t, test->v, layers);
|
|
|
|
|
|
|
|
// Using contructor to create new output Mesh.
|
|
|
|
*output = Mesh(vv, tt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strcmp(argv[argc - 1], "-d") == 0)
|
2016-11-24 12:33:20 -06:00
|
|
|
{
|
2016-12-05 11:14:33 -06:00
|
|
|
=======
|
2016-12-05 11:01:12 -06:00
|
|
|
|
|
|
|
// {
|
2016-12-05 11:14:33 -06:00
|
|
|
>>>>>>> master
|
2016-11-26 18:26:10 -06:00
|
|
|
for(int j = 0; j < output->v.size(); j++)
|
2016-11-24 12:33:20 -06:00
|
|
|
{
|
2016-11-26 18:26:10 -06:00
|
|
|
std::cout << output->v[j].m_x << " " << output->v[j].m_y << " " << output->v[j].m_z << std::endl;
|
2016-11-28 18:50:39 -06:00
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
for(int k = 0; k < output->t.size(); k++)
|
|
|
|
{
|
|
|
|
std::cout << output->t[k].m_x << " " << output->t[k].m_y << " " << output->t[k].m_z << std::endl;
|
2016-11-24 12:33:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-26 18:26:10 -06:00
|
|
|
output->save(argv[2]);
|
|
|
|
|
|
|
|
std::cout << "Success." << std::endl;
|
2016-11-24 12:33:20 -06:00
|
|
|
}
|