2021年5月6日星期四

How can I add the Z component to my graphics engine

Im writing a Graphics engine in python for learning but I am unable to actually get the z component to work. Any help would be amazing. I would prefer not to have to import anything.

#!/usr/bin/env python3  import turtle  t=turtle.Pen()  t.speed(0)  turtle.tracer(0, 0)  import math  #points = [(-1,-1,-1),(-1,-1,1),(-1,1,1),(-1,1,-1),(1,-1,-1),(1,-1,1),(1,1,1),(1,1,-1)]  #triangles = [(0,1,2),(0,2,3), (2,3,7),(2,7,6), (1,2,5),(2,5,6), (0,1,4),(1,4,5), (4,5,6),(4,6,7), (3,7,4),(4,3,0)]  points = []  triangles = []  modelName = "Ship"  with open(modelName+"V.txt", 'r') as f:      lines = f.readlines()      for line in lines:          coords = line[:-2].split(' ')          points.append([float(coords[0]), float(coords[1]), float(coords[2])])      f.close()    with open(modelName+"T.txt", 'r') as f:      lines = f.readlines()      for line in lines:          coords = line[:-2].split(' ')          newCoords = []          for coord in coords[1:4]:              newCoords.append(int(coord))          triangles.append(newCoords)      f.close()  def draw():      for i in range(len(triangles)):          poly = [points[triangles[i][0]][0],points[triangles[i][0]][1],points[triangles[i][1]][0],points[triangles[i][1]][1],points[triangles[i][2]][0],points[triangles[i][2]][1]]          for a in range(len(poly)):              #print(points[triangles[i][a]][2])              #z = points[triangles[i][a%3]][2]              poly[a] *= -0.5              #print(z)              #poly[a] =poly[a]/(z)              #if a%2 !=1:              #poly[a] =poly[a]*1/math.tan(90)#/points[triangles[i][a%3]][2]              #poly[a] =poly[a]/(z)                                    #print(poly)          #print("new",[100*points[triangles[i][0]][0],100*points[triangles[i][0]][1],100*points[triangles[i][1]][0],100*points[triangles[i][1]][1], 100*points[triangles[i][2]][0],100*points[triangles[i][2]][1]])            for q in range(0, len(poly), 4):              #print(q)              #print(poly[q],poly[q-1])              t.up()              t.goto(poly[q-2],poly[q-1])              t.down()              t.goto(poly[q],poly[q+1])      turtle.update()            t.goto(0,0)  draw()  

the actual files it is reading from are really huge so I have this example setup which is commented out as points and triangles.

https://stackoverflow.com/questions/67428499/how-can-i-add-the-z-component-to-my-graphics-engine May 07, 2021 at 11:04AM

没有评论:

发表评论