-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
182 lines (146 loc) · 5.75 KB
/
Main.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
171
172
173
174
175
176
177
178
179
180
181
182
from TGAImage import TGAImage, Format
from Color import Color
from Model import Model
from Vector import Vector
from random import randint
from math import fabs
import sys
red = Color(255, 0, 0, 255)
green = Color(0, 255, 0, 255)
blue = Color(0, 0, 255, 255)
white = Color(255, 255, 255, 255)
width = 700
height = 700
depth = 255
light_intensity_vector = Vector(0, 0, 1.0)
def main():
model = Model("obj/african_head.obj")
image = TGAImage(width, height, Format.RGB)
# model.write("info.txt")
zbuffer = []
for i in range(0, image.width * image.height):
zbuffer.append(-sys.maxint - 1)
for i in range(0, len(model.faces)):
screen_coord = [Vector(), Vector(), Vector()]
world_coords = [Vector(), Vector(), Vector()]
for j in range(0, 3):
world_coord_vector = model.get_vert(i, j)
screen_coord[j].x = int((world_coord_vector.x + 1.) * width / 2)
screen_coord[j].y = int((world_coord_vector.y + 1.) * height / 2)
screen_coord[j].z = int((world_coord_vector.z + 1.) * depth / 2)
world_coords[j] = world_coord_vector
# n = (world_coords[2] - world_coords[0]) ^ (world_coords[1] - world_coords[0])
# norm = n.normalize()
# intensity = norm.mul(light_intensity_vector)
# if intensity < 0: # skip invisible triangle
# continue
uv = [Vector(), Vector(), Vector()]
for k in range(0, 3):
uv[k] = model.get_uv(i, k)
norms = [Vector(), Vector(), Vector()]
for k in range(0, 3):
norms[k] = model.get_norm(i, k)
# random_color = Color(randint(0, 255), randint(0, 255), randint(0, 255), 255)
# color = Color(int(intensity * 255), int(intensity * 255), int(intensity * 255), 255)
triangle(screen_coord[0], screen_coord[1], screen_coord[2], uv[0], uv[1], uv[2], norms[0], norms[1], norms[2], image, zbuffer, model)
image.write("output.tga")
# scene = TGAImage(width + 1, height + 1, Format.RGBA)
# line_by_vector(Vector(20, 34), Vector(644, 400), scene, red)
# line_by_vector(Vector(120, 434), Vector(444, 400), scene, green)
# line_by_vector(Vector(330, 463), Vector(594, 200), scene, blue)
#
# line_by_vector(Vector(10, 10), Vector(690, 10), scene, white)
#
# scene.write("scene.tga")
#
# render = TGAImage(width, 16, Format.RGBA)
# buffer = []
# for i in range(0, width):
# buffer.append(-sys.maxint - 1)
#
# rasterise(Vector(20, 34), Vector(644, 400), render, red, buffer)
# rasterise(Vector(120, 434), Vector(444, 400), render, green, buffer)
# rasterise(Vector(330, 463), Vector(594, 200), render, blue, buffer)
#
# render.write("render.tga")
def rasterise(point_vect0, point_vect1, image, color, ybuffer):
p0 = point_vect0
p1 = point_vect1
if p0.x > p1.x:
p0, p1 = p1, p0
deltaX = float(p1.x - p0.x)
for x in range(p0.x, p1.x, 1):
t = (x - p0.x) / deltaX
y = p0.y * (1 - t) + p1.y * t
if ybuffer[x] < y:
ybuffer[x] = y
for i in range(0, image.height):
image.set(x, i, color)
def triangle(v0, v1, v2, uv0, uv1, uv2, n0, n1, n2, image, zbuffer, model):
# line_by_vector(v1, v2, image, red)
# line_by_vector(v1, v3, image, red)
# line_by_vector(v2, v3, image, red)
if v0.y > v1.y:
v0, v1 = v1, v0
uv0, uv1 = uv1, uv0
n0, n1 = n1, n0
if v0.y > v2.y:
v0, v2 = v2, v0
uv0, uv2 = uv2, uv0
n0, n2 = n2, n0
if v1.y > v2.y:
v1, v2 = v2, v1
uv1, uv2 = uv2, uv1
n1, n2 = n2, n1
total_height = int(v2.y - v0.y)
for y in range(0, total_height):
is_second_half = y > v1.y - v0.y or v1.y == v0.y
segment_height = v2.y - v1.y if is_second_half else v1.y - v0.y
alpha = float(y) / total_height
delta = (v1.y - v0.y) if is_second_half else 0
beta = float(y - delta) / segment_height
a = v0 + (v2 - v0) * alpha
b = v1 + (v2 - v1) * beta if is_second_half else v0 + (v1 - v0) * beta
uvA = uv0 + (uv2 - uv0) * alpha
uvB = uv1 + (uv2 - uv1) * beta if is_second_half else uv0 + (uv1 - uv0)*beta
nA = n0 + (n2 - n0) * alpha
nB = n1 + (n2 - n1) * beta if is_second_half else n0 + (n1 - n0) * beta
if a.x > b.x:
a, b = b, a
uvA, uvB = uvB, uvA
nA, nB = nB, nA
for i in range(int(a.x), int(b.x)):
phi = 1. if a.x == b.x else float((i - a.x) / float(b.x - a.x))
p = a + (b - a) * phi
uvP = uvA + (uvB - uvA) * phi
nP = nA + (nB - nA) * phi
idx = int(p.x + p.y * image.width)
if zbuffer[idx] < p.z:
zbuffer[idx] = p.z
color = model.diffuse(uvP)
intensity = nP.mul(light_intensity_vector)
if intensity < 0: # skip invisible triangle
continue
color_intensity = Color(int(color.r() * intensity), int(color.g() * intensity), int(color.b() * intensity))
image.set(int(p.x), int(p.y), color_intensity)
def line_by_vector(v1, v2, image, color):
line(v1.x, v1.y, v2.x, v2.y, image, color)
def line(x0, y0, x1, y1, image, color):
steep = None
if abs(x1 - x0) < abs(y1 - y0):
y0, x0 = x0, y0
y1, x1 = x1, y1
steep = True
if x0 > x1:
x0, x1 = x1, x0
y0, y1 = y1, y0
for x in range(x0, x1, 1):
t = (x - x0) / float(x1 - x0)
y = y0 * (1 - t) + y1 * t
if steep:
image.set(int(y), int(x), color)
else:
image.set(int(x), int(y), color)
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()