Skip to content

Commit 7410460

Browse files
committed
Moved instance creation into its own class
To clean up main.cpp
1 parent 972302e commit 7410460

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

instance.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "instance.h"
2+
3+
using namespace std;
4+
using namespace glm;
5+
6+
inline float Random(float min, float max)
7+
{
8+
return float(rand()) / float(RAND_MAX) * (max - min) + min;
9+
}
10+
11+
void Instance::DefineInstances(vector<Instance> & instances, int n)
12+
{
13+
float d = 8.0f;
14+
15+
for (int i = 0; i < n; i++)
16+
{
17+
mat4 m = rotate(mat4(), Random(0.0f, pi<float>() * 2.0f), vec3(0.0f, 1.0f, 0.0f));
18+
m = rotate(m, Random(0.0f, pi<float>() * 2.0f), vec3(1.0f, 0.0f, 0.0f));
19+
float z = Random(-8.0f, 8.0f);
20+
z += (z > 0) ? 2.0f : -2.0f;
21+
22+
vec4 p = m * vec4(0.0f, 0.0f, z, 1.0f);
23+
24+
vec3 diffuse(Random(0.2f, 1.0f), Random(0.2f, 1.0f), Random(0.2f, 1.0f));
25+
instances.push_back(Instance(
26+
vec3(p),
27+
Random(0.0f, pi<float>() * 2.0f),
28+
Random(-360.0f, 360.0f),
29+
diffuse));
30+
}
31+
}

instance.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include <vector>
3+
#include <glm/glm.hpp>
4+
#include <glm/gtc/matrix_transform.hpp>
5+
#include <glm/gtc/type_ptr.hpp>
6+
#include <glm/gtc/constants.hpp>
7+
8+
class Instance
9+
{
10+
public:
11+
Instance(glm::vec3 p, float o, float r, glm::vec3 d) : position(p), offset(o), rate(r), diffuse(d) {};
12+
static void DefineInstances(std::vector<Instance> & instances, int n = 32);
13+
14+
glm::vec3 position;
15+
float offset;
16+
float rate;
17+
glm::vec3 diffuse;
18+
};

0 commit comments

Comments
 (0)