Skip to content

Commit

Permalink
Gerando a imagem
Browse files Browse the repository at this point in the history
  • Loading branch information
beacarvalho4 committed Jun 15, 2021
1 parent 18b38d6 commit 1b64d31
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
17 changes: 17 additions & 0 deletions Cenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ def transforma_cena(self):
objeto.transformacao = multMatriz(self.transformacao, objeto.transformacao)
objeto.transformar()

def translacao(self, offset_x=0, offset_y=0, offset_z=0):
"""Move um objeto na cena. Deve ser passado a variação a ser realizada nos eixos."""
matriz = [[1, 0, 0, offset_x],
[0, 1, 0, offset_y],
[0, 0, 1, offset_z],
[0, 0, 0, 1]]
self.transformacao = multMatriz(matriz, self.transformacao)

def escala(self, prop_x=1, prop_y=1, prop_z=1):
"""Expande ou contrai um objeto na cena. Deve ser passado a variação a ser realizada nos eixos."""
matriz = [[prop_x, 0, 0, 0],
[0, prop_y, 0, 0],
[0, 0, prop_z, 0],
[0, 0, 0, 1]]
self.transformacao = multMatriz(matriz, self.transformacao)


def salvar_cenario(self):
k = 0
for objeto in self.cena:
Expand Down
42 changes: 42 additions & 0 deletions ImageGenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from PIL import Image, ImageColor


def gerar_imagem(cenario, largura, altura):
im = Image.new('RGB', (largura, altura)) # create the Image of size 1 pixel
color = ImageColor.getrgb('#634525')

for objeto in cenario.cena:
for face in objeto.faces:
p1_idx, p2_idx, p3_idx = face
p1 = objeto.vertices[p1_idx - 1]
p2 = objeto.vertices[p2_idx - 1]
p3 = objeto.vertices[p3_idx - 1]

inc_line(im, int(p1[0]), int(p1[1]), int(p2[0]), int(p2[1]), color)
inc_line(im, int(p2[0]), int(p2[1]), int(p3[0]), int(p3[1]), color)
inc_line(im, int(p1[0]), int(p1[1]), int(p3[0]), int(p3[1]), color)

im.save('teste.png') # or any image format


def inc_line(image, x1, y1, x2, y2, color):
dx = x2 - x1
dy = y2 - y1
d = 2 * dy - dx
inc_e = 2 * dy
inc_ne = 2 * (dy - dx)
x = x1
y = y1
if x > 0 and y > 0:
image.putpixel((x, y), color)
while x < x2:
if d <= 0:
d = d + inc_e
x = x + 1
else:
d = d + inc_ne
x = x + 1
y = y + 1

if x > 0 and y > 0:
image.putpixel((x, y), color)
12 changes: 9 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ImageGenerator import gerar_imagem
from Object import Object
from Cenario import Cenario

Expand All @@ -11,8 +12,13 @@
vaso.translacao(offset_x=22.31, offset_z=4.02, offset_y=4.13)

cenario = Cenario()
cenario.cena = [vaso, mesa]
cenario.setCamera(e=[21, 4.625, 15], g=[21, 4.625, 3.85], t=[0, 1, 0])
cenario.cena = [mesa, vaso]
cenario.setCamera(e=[21, 10, 15], g=[21, 4.625, 3.85], t=[0, 1, 0])
cenario.setProjecao(fov=60, ratio=1, z_near=0, z_far=15)
cenario.translacao(1, 1, 1)
cenario.escala(500, 500)
cenario.transforma_cena()
cenario.salvar_cenario()

gerar_imagem(cenario, 1000, 1000)

cenario.salvar_cenario()

0 comments on commit 1b64d31

Please sign in to comment.