Remove cpp impl of normal calculation

This commit is contained in:
hodasemi 2018-11-28 11:00:08 +01:00
parent 624c3a4997
commit 5cbd74c93d

View file

@ -164,7 +164,8 @@ std::string vec4_to_str(Eigen::Vector4f &v)
return ss.str();
}
std::string vec3_to_str(Eigen::Vector3f& v) {
std::string vec3_to_str(Eigen::Vector3f &v)
{
std::stringstream ss;
ss << "(" << v.x() << ", "
@ -174,129 +175,6 @@ std::string vec3_to_str(Eigen::Vector3f& v) {
return ss.str();
}
float length(Eigen::Vector2f v) {
return sqrt(v.x() * v.x() + v.y() * v.y());
}
float dot(Eigen::Vector2f v1, Eigen::Vector2f v2) {
return (v1.x() * v2.x() + v1.y() * v2.y()) / length(v1) * length(v2);
}
float mix(float f1, float f2, float value) {
float difference = f1 - f2;
return f1 + difference * value;
}
//source: https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
float rand(Eigen::Vector2f c)
{
double dummy;
return 2 * modf(sin(dot(c, Eigen::Vector2f(12.9898f, 78.233f))) * 43758.5453, &dummy) - 1;
}
Eigen::Vector2f mul(Eigen::Vector2f v1, Eigen::Vector2f v2) {
return Eigen::Vector2f(v1.x() * v2.x(), v1.y() * v2.y());
}
float perlinNoise(Eigen::Vector2f p)
{
Eigen::Vector2f ij = Eigen::Vector2f(floor(p.x()), floor(p.y()));
Eigen::Vector2f xy = p - ij;
xy = (3.0f * mul(xy, xy)) - (2.0f * mul(mul(xy, xy), xy));
//xy = 0.5f * (1.0f - cos(3.1415926 * xy));
float a = rand((ij + Eigen::Vector2f(0.0f, 0.0f)));
float b = rand((ij + Eigen::Vector2f(1.0f, 0.0f)));
float c = rand((ij + Eigen::Vector2f(0.0f, 1.0f)));
float d = rand((ij + Eigen::Vector2f(1.0f, 1.0f)));
float x1 = mix(a, b, xy.x());
float x2 = mix(c, d, xy.x());
return mix(x1, x2, xy.y());
}
//based on https://www.seedofandromeda.com/blogs/58-procedural-heightmap-terrain-generation
float getTerrainHeight(Eigen::Vector2f p)
{
float total = 0.0;
float maxAmplitude = 0.0;
float amplitude = 1.0;
float frequency = 0.02;
for (int i = 0; i < 11; i++)
{
total += ((1.0 - abs(perlinNoise(p * frequency))) * 2.0 - 1.0) * amplitude;
frequency *= 2.0;
maxAmplitude += amplitude;
amplitude *= 0.45;
}
return 15 * total / maxAmplitude;
}
Eigen::Vector3f vec4_to_vec3(Eigen::Vector4f v) {
return Eigen::Vector3f(v.x(), v.y(), v.z());
}
Eigen::Vector3f calculate_gpu_normal(Eigen::Vector4f pos, int gl_VertexID) {
const Eigen::Vector2f offsets[6] = {
Eigen::Vector2f(0.0, 0.0),
Eigen::Vector2f(0.0, 1.0),
Eigen::Vector2f(1.0, 0.0),
Eigen::Vector2f(0.0, 1.0),
Eigen::Vector2f(1.0, 0.0),
Eigen::Vector2f(1.0, 1.0)
};
int offset_index = gl_VertexID % 6;
Eigen::Vector4f base_vertex = Eigen::Vector4f(pos.x() - offsets[offset_index].x(), pos.y(), pos.z() - offsets[offset_index].y(), pos.w());
// the other 2 points from the triangle
int points_index = 0;
Eigen::Vector4f points[2];
int iterator_offset = 0;
// second triangle offset
if (offset_index > 2) {
iterator_offset = 3;
}
for (int i = iterator_offset; i < (3 + iterator_offset); i++) {
// skip for current vertex
if (i == offset_index) {
continue;
}
Eigen::Vector2f new_pos = Eigen::Vector2f(base_vertex.x(), base_vertex.z()) + offsets[i];
float y = getTerrainHeight(new_pos);
points[points_index] = Eigen::Vector4f(new_pos.x(), y, new_pos.y(), base_vertex.w());
points_index++;
}
// create connection vectors
Eigen::Vector3f v1 = vec4_to_vec3(points[0] - pos);
Eigen::Vector3f v2 = vec4_to_vec3(points[1] - pos);
v1.normalize();
v2.normalize();
Eigen::Vector3f normal = v1.cross(v2).normalized();
return normal;
}
Eigen::Vector3f calculate_reference_normal(Eigen::Vector4f p1, Eigen::Vector4f p2, Eigen::Vector4f p3) {
Eigen::Vector3f v1 = vec4_to_vec3(p1 - p2);
Eigen::Vector3f v2 = vec4_to_vec3(p1 - p3);
v1.normalize();
v2.normalize();
Eigen::Vector3f normal = v1.cross(v2).normalized();
return normal;
}
void Viewer::CreateGeometry()
{
//empty VAO for sky
@ -357,7 +235,7 @@ void Viewer::CreateGeometry()
{
for (int j = 0; j < PATCH_SIZE; j++)
{
ref_pos.emplace_back((float)j, getTerrainHeight(Eigen::Vector2f((float)j, (float)i)), (float)i, 1.0f);
ref_pos.emplace_back((float)j, 0.0f, (float)i, 1.0f);
}
}
@ -382,67 +260,6 @@ void Viewer::CreateGeometry()
}
}
// --------------------------------------------------
// ---------------- Debugging Area ------------------
// --------------------------------------------------
/*
for (int i = 0; i < ref_ind.size(); i+=6) {
struct Point {
Eigen::Vector4f position;
Eigen::Vector3f normal;
};
std::vector<Point> gpu_points;
std::vector<Point> ref_points;
for (int j = i; j < i+6; j++) {
// gpu
{
auto position = ref_pos[j];
auto normal = calculate_gpu_normal(position, j);
gpu_points.emplace_back(Point { position, normal });
}
// reference
{
int index = j % 6;
auto position = ref_pos[j];
Eigen::Vector3f normal;
if (index < 3) {
normal = calculate_reference_normal(ref_pos[i], ref_pos[i+1], ref_pos[i+2]);
} else {
normal = calculate_reference_normal(ref_pos[i+3], ref_pos[i+4], ref_pos[i+5]);
}
ref_points.emplace_back(Point { position, normal });
}
}
std::string gpu_output = "gpu_points: \n";
std::string ref_output = "ref_points: \n";
for (Point& point : gpu_points) {
gpu_output += "\tpos: " + vec4_to_str(point.position) + "\tnormal: " + vec3_to_str(point.normal) + "\n";
}
for (Point& point : ref_points) {
ref_output += "\tpos: " + vec4_to_str(point.position) + "\tnormal: " + vec3_to_str(point.normal) + "\n";
}
std::cout << gpu_output << std::endl;
std::cout << ref_output << std::endl;
abort();
}
*/
// --------------------------------------------------
// -------------- Debugging Area End ----------------
// --------------------------------------------------
referenceVB.uploadData(ref_pos)
.bindToAttribute("position");
referenceIB.uploadData(ref_ind.size() * sizeof(uint32_t), ref_ind.data());