- gp->tex_coords[index].x = gp->dots[index].x * 0.125 + 0.5
- * (1.0 + 0.25 * asin(gp->normals[index].x) / (0.5 * PI));
- gp->tex_coords[index].y = -gp->dots[index].y * 0.125 - 0.5
- * (1.0 + 0.25 * asin(gp->normals[index].y) / (0.5 * PI));
+ const float cube_size = 100.0;
+ Vector3D eye = {0.0, 0.0, 50.0};
+ Vector3D eye_r = normalise(subtract(gp->dots[index], eye));
+ Vector3D reference = subtract(eye_r, scale(gp->normals[index], 2.0 * dot(eye_r, gp->normals[index])));
+ double x = 0.0;
+ double y = 0.0;
+ double n, n_min = 10000.0, sign = 1.0;
+ if (fabs(reference.z) > 1e-9)
+ {
+ n = (cube_size - gp->dots[index].z) / reference.z;
+ if (n < 0.0)
+ {
+ n = (-cube_size - gp->dots[index].z) / reference.z;
+ sign = 3.0;
+ }
+ if (n > 0.0)
+ {
+ x = sign * (gp->dots[index].x + n * reference.x);
+ y = sign * (gp->dots[index].y + n * reference.y);
+ n_min = n;
+ }
+ }
+ if (fabs(reference.x) > 1e-9)
+ {
+ n = (cube_size - gp->dots[index].x) / reference.x;
+ sign = 1.0;
+ if (n < 0.0)
+ {
+ n = (-cube_size - gp->dots[index].x) / reference.x;
+ sign = -1.0;
+ }
+ if ((n > 0.0) && (n < n_min))
+ {
+ x = sign * (2.0 * cube_size - (gp->dots[index].z + n * reference.z));
+ y = sign * x * (gp->dots[index].y + n * reference.y) / cube_size;
+ n_min = n;
+ }
+ }
+ if (fabs(reference.y) > 1e-9)
+ {
+ n = (cube_size - gp->dots[index].y) / reference.y;
+ sign = 1.0;
+ if (n < 0.0)
+ {
+ n = (-cube_size - gp->dots[index].y) / reference.y;
+ sign = -1.0;
+ }
+ if ((n > 0.0) && (n < n_min))
+ {
+ y = sign * (2.0 * cube_size -( gp->dots[index].z + n * reference.z));
+ x = sign * y * (gp->dots[index].x + n * reference.x) / cube_size;
+ }
+ }
+
+ gp->tex_coords[index].x = 0.5 + x / (cube_size * 6.0);
+ gp->tex_coords[index].y = 0.5 - y / (cube_size * 6.0);