This repository was archived by the owner on Apr 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.py
More file actions
73 lines (54 loc) · 2.12 KB
/
utility.py
File metadata and controls
73 lines (54 loc) · 2.12 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
import numpy as np
import pygame
from pygame.locals import *
# treat sphere intersection as a quadratic function to solve, f = ax^2 + bx + c
# https://en.wikipedia.org/wiki/Quadratic_equation#Quadratic_formula_and_its_derivation
def solve_quadratic_equation(ray, sphere):
rD = ray.direction
rO = ray.origin
sC = sphere.origin
sR = sphere.radius
rOsC = rO - sC # origin - sphere center
#a = length(rD)
b = 2 * np.dot(rD, rOsC) # b = 2 * rD*rOsC
c = length(rOsC) ** 2 - (sR ** 2) # (rO - sC)^2 - sR^2,
discriminant = (b**2) - (4*c)
if discriminant > 0:
# solutions found
sqrt = np.sqrt(discriminant)
d1 = (-b + sqrt) / (2)
d2 = (-b - sqrt) / (2)
if d1 > 0.001 and d2 > 0.001: # prevent shadow acne by checking above 0.001
return min(d1, d2)
return None
def normalize(vec):
# divides each component of vec with its length, normalizing it
return vec / np.linalg.norm(vec)
def length(vec):
return np.linalg.norm(vec)
def clamp(a, min_val, max_val):
if (a < min_val): return min_val
if (a > max_val): return max_val
return a
# get a value between a and b depending on t (or just get a or b)
def lerp(a, b, t):
return (1-t) * a + t * b
# Returns a normalized reflected ray
def reflect(vector, normal):
return vector - 2*np.dot(vector,normal) * normal
def progress_text(y_index, font, width, height):
percentage = ((y_index+1) / (height)) * 100
progress = "Progress: " + "%.2f" % percentage + "%"
color = (lerp(0, 255, y_index/(width-1)), 0, 255)
text = font.render(progress, False, color, (0,0,0))
textrect = text.get_rect(left=10, top=50)
return (text, textrect)
def print_camera_info(camera, font):
color = (255, 255, 255)
formatter = {'float_kind': lambda x: "%.2f" % x}
position = np.array2string(camera.position, precision=4, separator=',', formatter=formatter)
look_to = np.array2string(camera.look_to, precision=4, separator=',', formatter=formatter)
info = "Position: " + position + ", Looking at: " + look_to
text = font.render(info, False, color, (0,0,0))
textpos = text.get_rect(left=0, top=0)
return (text, textpos)