forked from Felipetoledo4815/Ucc-RayTracing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
170 lines (128 loc) · 4.58 KB
/
camera.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# camera.py
#
# Copyright 2017 John Coppens <john@jcoppens.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf, GLib
from rt_math import *
class Hit():
""" Cada impacto registra 3 datos:
spot float distancia del origen del rayo
normal Vec3 normal en el punto de impacto
obj object referencia al objeto impactado
"""
def __init__(self, t, normal, obj):
self.t = t
self.normal = normal
self.obj = obj
class Hit_list():
""" Lista de impactos """
def __init__(self):
self.hits = []
def __str__(self):
s = ""
for hit in self.hits:
s += "{:12.6f} {:s}\n".format(hit.t, str(hit.normal))
return s
def nearest(self):
""" Busca al impacto mas cercano """
dist = 1e99
if len(self.hits) == 0:
return None
for hit in self.hits:
if hit.t < dist:
nearest_hit = hit
dist = hit.t
return nearest_hit
def append(self, new_hits):
""" Agrega un impacto a la lista """
self.hits += new_hits
class Picture():
def __init__(self, w, h):
self.pixels = bytearray(w * h * 3)
self.w = w
self.h = h
self.rowstride = w * 3
def set_pixel(self, x, y, rgb):
pixel_offset = (self.h - 1 - y) * self.rowstride + x * 3
self.pixels[pixel_offset + 0] = rgb[0]
self.pixels[pixel_offset + 1] = rgb[1]
self.pixels[pixel_offset + 2] = rgb[2]
def make_pixbuf(self):
return GdkPixbuf.Pixbuf.new_from_bytes(
GLib.Bytes(self.pixels),
GdkPixbuf.Colorspace.RGB,
False, 8, self.w, self.h, self.rowstride)
class Camera():
def __init__(self, toplevel, props, objects, lights):
self.props = props
self.toplevel = toplevel
self.objects = objects
self.lights = lights
self.width = int(self.props["width"])
self.height = int(self.props["height"])
self.begcol = int(self.props["begcol"])
self.endcol = int(self.props["endcol"])
self.begrow = int(self.props["begrow"])
self.endrow = int(self.props["endrow"])
self.aspect = self.height/self.width
self.fov_y = float(self.props["fov_y"])
self.pixels = Picture(self.width, self.height)
def render(self):
cte_x = self.width/2 - 0.5
cte_y = self.height/2 - 0.5
scale = 2/self.height
for y in range(self.begrow, self.endrow+1):
for x in range(self.begcol, self.endcol+1):
ray = Ray(Vec3(0, 0, 0),
Vec3((x - cte_x) * scale,
(y - cte_y) * scale,
1).normalize())
self.pixels.set_pixel(x, y, self.tracer(ray))
#~ pdb.set_trace()
self.toplevel.viewer.update(self.pixels.make_pixbuf())
def tracer(self, ray):
""" Tracer
- sigue el ray <ray> y determina impactos
- determina el impacto mas cercano
- devuelve el color correspondiente como (R, G, B)
"""
hits = Hit_list()
for obj in self.objects:
new_hits = obj.intersection(ray)
hits.append(new_hits)
nearest_hit = hits.nearest()
if nearest_hit == None:
return (0, 0, 0)
else:
#~ pdb.set_trace()
amb_col = nearest_hit.obj.ambient
cos_ang = abs(nearest_hit.normal.dot(ray.direct))
dif_col = nearest_hit.obj.diffuse.scale(cos_ang)
col = Vec3(amb_col).add(dif_col)
return col.as_RGB()
def main(args):
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))