-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeightMapSampler.cpp
More file actions
135 lines (125 loc) · 5.64 KB
/
HeightMapSampler.cpp
File metadata and controls
135 lines (125 loc) · 5.64 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "HeightMapSampler.h"
#include "lodepng/lodepng.h"
// this function sets the region to sample from.
void SF_Heightmap::setSpecs(const ivec3& from, const ivec3& to){
minBound = from;
maxBound = to;
}
void SF_Heightmap::loadPNG(const ivec2& _dim, const char* fileName){
dimension = _dim;
std::vector<unsigned char> pngData; //the raw pixels
unsigned int width = dimension.x;
unsigned int height = dimension.y;
lodepng::load_file(pngData, fileName);
unsigned int error = lodepng::decode(image, width, height, pngData);
assert(error == 0);
imageBuffer = image.data();
}
unsigned char SF_Heightmap::materialFunc(const ivec3& pos, const int cellSize, vec3* intersects, vec3* outNormal0, vec3* outNormal1, vec3* outNormal2){
vec3 floatPos(pos);
ivec2 i2pos(ivec2(pos.x, pos.z));
ivec2 i2posXEnd(ivec2(pos.x + 1, pos.z));
ivec2 i2posYEnd(ivec2(pos.x, pos.z + 1));
//floatPos *= sampleScale;
unsigned char material = 0;
float heightOrigin = getHeightSimple(i2pos);
if (floatPos.y <= heightOrigin){
material = 1;
}
if (floatPos.y <= heightOrigin && floatPos.y + sampleScale >= heightOrigin){
// crossing on the y axis.
intersects->y = (heightOrigin - floatPos.y) / sampleScale;
// calculate normal
*outNormal1 = getNormal2(vec2(floatPos.x, floatPos.z), heightOrigin);
}
//float intersectionHeight;
float heightXEnd = getHeightSimple(i2posXEnd);
vec3 normal = getNormal(vec2(floatPos.x, floatPos.z), heightOrigin);
if (floatPos.y > heightOrigin && floatPos.y <= heightXEnd){
// on an incline on the x axis.
intersects->x = (floatPos.y - heightOrigin) * sampleScale / ((floatPos.y - heightOrigin) + (heightXEnd - floatPos.y));
//intersectionHeight = getHeight(vec2(floatPos.x + intersects->x, floatPos.z));
//*outNormal0 = getNormal(vec2(floatPos.x + intersects->x, floatPos.z), intersectionHeight);
*outNormal0 = normal;
}
else if (floatPos.y <= heightOrigin && floatPos.y > heightXEnd){
// on a decline on the x axis.
intersects->x = (floatPos.y - heightOrigin) * sampleScale / ((floatPos.y - heightOrigin) + (heightXEnd - floatPos.y));
//intersectionHeight = getHeight(vec2(floatPos.x + intersects->x, floatPos.z));
//*outNormal0 = getNormal(vec2(floatPos.x + intersects->x, floatPos.z), intersectionHeight);
*outNormal0 = normal;
}
float heightZEnd = getHeightSimple(i2posYEnd);
if (floatPos.y > heightOrigin && floatPos.y < heightZEnd){
// on an incline on the z axis.
intersects->z = (floatPos.y - heightOrigin) * sampleScale / ((floatPos.y - heightOrigin) + (heightZEnd - floatPos.y));
//intersectionHeight = getHeight(vec2(floatPos.x, floatPos.z + intersects->z));
//*outNormal2 = getNormal(vec2(floatPos.x, floatPos.z + intersects->z), intersectionHeight);
*outNormal2 = normal;
}
else if (floatPos.y < heightOrigin && floatPos.y > heightZEnd){
// on a decline on the z axis.
intersects->z = (floatPos.y - heightOrigin) * sampleScale / ((floatPos.y - heightOrigin) + (heightZEnd - floatPos.y));
//intersectionHeight = getHeight(vec2(floatPos.x, floatPos.z + intersects->z));
//*outNormal2 = getNormal(vec2(floatPos.x, floatPos.z + intersects->z), intersectionHeight);
*outNormal2 = normal;
}
return material;
}
float SF_Heightmap::getHeight(vec2 pos){
ivec2 floorPos = glm::floor(pos);
vec2 remainderPos = pos - vec2(floorPos);
float val0 = getRGBSum(floorPos);
float val1 = getRGBSum(floorPos + ivec2(1, 0));
float val2 = getRGBSum(floorPos + ivec2(0, 1));
float val3 = getRGBSum(floorPos + ivec2(1, 1));
// use a simple sampling method for four-point sampling.
float bottom = val0 + remainderPos.x * (val1 - val0);
float top = val2 + remainderPos.x * (val3 - val2);
float height = bottom + remainderPos.y * (top - bottom);
return height;
}
float SF_Heightmap::getHeightSimple(const ivec2& pos){
float ret = getRGBSum(pos);
return ret;
}
vec3 SF_Heightmap::getNormal(vec2 pos, float height){
/* use this simple method to calculate the normal.
sample the heights of the four adjacent points. if it's at the boundary,
arbitrarily set the normal to pointing upwards, for now.
*/
if (pos.x == 0 || pos.y == 0 || pos.x == dimension.x - 1 || pos.y == dimension.y - 1)
return vec3(0, 1, 0);
// otherwise, sample the four adjacent points.
float heightPosX = getHeight(vec2(pos.x + sampleScale, pos.y));
float heightPosY = getHeight(vec2(pos.x, pos.y + sampleScale));
vec3 origin(pos.x, height, pos.y);
vec3 xpos(pos.x + sampleScale, heightPosX, pos.y);
vec3 ypos(pos.x, heightPosY, pos.y + sampleScale);
vec3 seg0 = glm::normalize(xpos - origin);
vec3 seg1 = glm::normalize(ypos - origin);
vec3 ret(glm::normalize(glm::cross(seg1, seg0)));
return ret;
}
// for sampling the normal at the tip, we need a different method.
vec3 SF_Heightmap::getNormal2(vec2 pos, float height){
/* use this simple method to calculate the normal.
sample the heights of the four adjacent points. if it's at the boundary,
arbitrarily set the normal to pointing upwards, for now.
*/
if (pos.x == 0 || pos.y == 0 || pos.x == dimension.x - 1 || pos.y == dimension.y - 1)
return vec3(0, 1, 0);
// otherwise, sample the four adjacent points.
float heightNegX = getHeight(vec2(pos.x - sampleScale, pos.y));
float heightPosX = getHeight(vec2(pos.x + sampleScale, pos.y));
float heightNegY = getHeight(vec2(pos.x, pos.y - sampleScale));
float heightPosY = getHeight(vec2(pos.x, pos.y + sampleScale));
vec3 xneg(pos.x - sampleScale, heightNegX, pos.y);
vec3 yneg(pos.x, heightNegY, pos.y - sampleScale);
vec3 xpos(pos.x + sampleScale, heightPosX, pos.y);
vec3 ypos(pos.x, heightPosY, pos.y + sampleScale);
vec3 seg0 = glm::normalize(xpos - xneg);
vec3 seg1 = glm::normalize(ypos - yneg);
vec3 ret(glm::normalize(glm::cross(seg1, seg0)));
return ret;
}