-
Notifications
You must be signed in to change notification settings - Fork 32
/
material.cpp
34 lines (29 loc) · 1008 Bytes
/
material.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "material.h"
#include <sstream>
int hex2int(std::string str)
{
std::stringstream ss;
ss << std::hex << str;
int x;
ss >> x;
return x;
}
vec3f hex2Colour(std::string str) // e.g. "#00FF00";
{
if (str[0] == '#')
str = str.substr(1);
if (str.length() != 6)
std::cout << "Error: badly formed hex colour value!\n";
return vec3f(hex2int(str.substr(0, 2)) / 255.f,
hex2int(str.substr(2, 2)) / 255.f,
hex2int(str.substr(4, 2)) / 255.f);
}
material::material(Json::Value root)
{
name = root.get("name", "Unspecified").asString();
mass = root.get("mass", 1).asDouble();
strength = root.get("strength", 1).asDouble() / mass * 1000;
colour = hex2Colour((root.isMember("colour") ? root["colour"] : root["color"]).asString()); // may as well account for American spelling...
isHull = root["isHull"].asBool();
std::cout << "Adding new material: \"" << name << "\" " << colour.toString() << "\n";
}