def Load3DElement(Path): f = open(Path, "r") Points = [] MeshElement = [] for Line in f: SplitedVar = Line.split(" ") if (Line[0] == "v"): Points.append([ float(SplitedVar[1]), float(SplitedVar[2]), float(SplitedVar[3]) ]) if (Line[0] == "f"): MeshElement.append( matrix.mat3x3([ Points[int(SplitedVar[1]) - 1], Points[int(SplitedVar[2]) - 1], Points[int(SplitedVar[3]) - 1] ])) f.close() return MeshElement
def ProjectTriangle(T): return matrix.mat3x3( [[(T.m[0][0] * Graphics.fovConverterX) / (T.m[0][2] * trsl), (T.m[0][1] * Graphics.fovConverterY) / (T.m[0][2] * trsl), T.m[0][2]], [(T.m[1][0] * Graphics.fovConverterX) / (T.m[1][2] * trsl), (T.m[1][1] * Graphics.fovConverterY) / (T.m[1][2] * trsl), T.m[1][2]], [(T.m[2][0] * Graphics.fovConverterX) / (T.m[2][2] * trsl), (T.m[2][1] * Graphics.fovConverterY) / (T.m[2][2] * trsl), (T.m[2][2])]])
def CalculateTriangle(Point1,Point2,Point3): PointsOutsideOfRay = [] PointsInsideOfRay = [] for El in [Point1,Point2,Point3]: if(El.p[1] < 0): PointsOutsideOfRay.append(El) else: PointsInsideOfRay.append(El) if(len(PointsOutsideOfRay) >= 2): return if(len(PointsOutsideOfRay) == 0): return [matrix.mat3x3([ [Point1.p[0],Point1.p[1],0], [Point2.p[0],Point2.p[1],0], [Point3.p[0],Point3.p[1],0] ])] if(len(PointsOutsideOfRay) == 1): A1 = PointsOutsideOfRay[0].p[1]-PointsInsideOfRay[0].p[1] B1 = PointsOutsideOfRay[0].p[0]-PointsInsideOfRay[0].p[0] C1 = A1 * PointsOutsideOfRay[0].p[0] + B1 * PointsOutsideOfRay[0].p[1] A2 = 0 B2 = 100 C2 = 0 den = A1 * B2 - A2 * B1 IntersectionA = Point([(B2 * C1 - B1 * C2)/den,(A1 * C2 - A2 * C1)/den]) Graphics.drawPoint(IntersectionA,(255,0,0),"") return [matrix.mat3x3([ [Point1.p[0],Point1.p[1],0], [Point2.p[0],Point2.p[1],0], [Point3.p[0],Point3.p[1],0] ])]
def Affichage(phi, HorizontalSpeed, VerticalSpeed): global vLookDir global vForward global Cam global Light global offset global miniOffset global LastCamPos global Size global Step global vTarget vForward = vector([0, VerticalSpeed, HorizontalSpeed]) Cam += vForward #print(Cam) #vTarget = ([0,0,1]) vLookDir = vector([0, math.sin(phi), math.cos(phi)]) #vLookDir = vector([-math.sin(theta),0,math.cos(theta)]) #vLookDir = vector([1,0,0]) vTarget = Cam + vLookDir subMat = PointAt(Cam, vTarget, vUp) mtView = LookAtMatrix(subMat) print(mtView.m) sol = [] #print(offset) if Cam.v[2] > Size + offset - 60: offset = int(Cam.v[2]) miniOffset = Cam.v[2] - offset for i in range(-30, 30, Step): for j in range(offset, Size + offset, Step): sq1 = matrix.mat3x3([[i, 6, j - miniOffset], [i + Step, 6, j - miniOffset], [i, 6, j + Step - miniOffset]]) sol.append(sq1) print() FillMesh(sol, (255, 255, 255), Cam, Light, mtView)
def FillMesh(M, color, Camera, LightSource, matView): #Msh = PaintingAlgorithm(M) Msh = M #Normal = CalculateNormals(Msh) #global Normal for i in range(len(Msh)): M2 = matrix.mat3x3([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) #print("\n\n\n",Msh) """ M2.m[0][0] = -1 * Msh[i].m[0][2] M2.m[0][1] = 1 * Msh[i].m[0][1] M2.m[0][2] = 1 * Msh[i].m[0][0] M2.m[1][0] = -1 * Msh[i].m[1][2] M2.m[1][1] = 1 * Msh[i].m[1][1] M2.m[1][2] = 1 * Msh[i].m[1][0] M2.m[2][0] = -1 * Msh[i].m[2][2] M2.m[2][1] = 1 * Msh[i].m[2][1] M2.m[2][2] = 1 * Msh[i].m[2][0] """ #print(matView) #print(M2.m) M2.m[0][0] = matView.m[0][0] * Msh[i].m[0][0] + matView.m[1][0] * Msh[ i].m[0][1] + matView.m[2][0] * Msh[i].m[0][2] + matView.m[3][0] M2.m[0][1] = matView.m[0][1] * Msh[i].m[0][0] + matView.m[1][1] * Msh[ i].m[0][1] + matView.m[2][1] * Msh[i].m[0][2] + matView.m[3][1] M2.m[0][2] = matView.m[0][2] * Msh[i].m[0][0] + matView.m[1][2] * Msh[ i].m[0][1] + matView.m[2][2] * Msh[i].m[0][2] + matView.m[3][2] M2.m[1][0] = matView.m[0][0] * Msh[i].m[1][0] + matView.m[1][0] * Msh[ i].m[1][1] + matView.m[2][0] * Msh[i].m[1][2] + matView.m[3][0] M2.m[1][1] = matView.m[0][1] * Msh[i].m[1][0] + matView.m[1][1] * Msh[ i].m[1][1] + matView.m[2][1] * Msh[i].m[1][2] + matView.m[3][1] M2.m[1][2] = matView.m[0][2] * Msh[i].m[1][0] + matView.m[1][2] * Msh[ i].m[1][1] + matView.m[2][2] * Msh[i].m[1][2] + matView.m[3][2] M2.m[2][0] = matView.m[0][0] * Msh[i].m[2][0] + matView.m[1][0] * Msh[ i].m[2][1] + matView.m[2][0] * Msh[i].m[2][2] + matView.m[3][0] M2.m[2][1] = matView.m[0][1] * Msh[i].m[2][0] + matView.m[1][1] * Msh[ i].m[2][1] + matView.m[2][1] * Msh[i].m[2][2] + matView.m[3][1] M2.m[2][2] = matView.m[0][2] * Msh[i].m[2][0] + matView.m[1][2] * Msh[ i].m[2][1] + matView.m[2][2] * Msh[i].m[2][2] + matView.m[3][2] #print("after: ",Msh[i]) ProjectedCam = vector([ M2.m[0][0] - Camera.v[0], M2.m[0][1] - Camera.v[1], M2.m[0][2] - Camera.v[2] ]) if (M2.m[0][0] < 1.24 * (M2.m[0][2]) and M2.m[1][0] < 1.24 * (M2.m[1][2]) and M2.m[2][0] < 1.24 * (M2.m[2][2]) and M2.m[0][0] > -1.24 * (M2.m[0][2]) and M2.m[1][0] > -1.24 * (M2.m[1][2]) and M2.m[2][0] > -1.24 * (M2.m[2][2]) and M2.m[0][1] < 0.7 * (M2.m[0][2]) and M2.m[1][1] < 0.7 * (M2.m[1][2]) and M2.m[2][1] < 0.7 * (M2.m[2][2]) and M2.m[0][1] > -0.7 * (M2.m[0][2]) and M2.m[1][1] > -0.7 * (M2.m[1][2]) and M2.m[2][1] > -0.7 * (M2.m[2][2])): #print(M2[i]) #if(MatGraph.DotProduct(ProjectedCam,Normal[i]) <= 0): #Graphics.DrawTriangle(MatGraph.ProjectTriangle(M2[i]),MatGraph.CalculateTriangleColor(Normal[i],LightSource,color),0) #if(M2.m[0][2] >= 0.7 and M2.m[1][2] >= 0.7 and M2.m[2][2] >= 0.7): Graphics.DrawTriangle(MatGraph.ProjectTriangle(M2), (0, 0, 0), 1) #Graphics.DrawTriangle(M2[i],color,0) """
def main(): sys.path.append(".") import DEBUG global Cam global Light #t = threading.Thread(target = ui) #t.start() sol = [] Step = 2 Size = 30 for i in range(-Size, Size, Step): for j in range(-Size, Size, Step): sq1 = matrix.mat3x3([[i, j, 3], [i, j + Step, 3], [i + Step, j, 3]]) """ sq2 = matrix.mat3x3([ [i,j+Step,1], [i+Step,j+Step,1], [i+Step,j,1] ])""" sol.append(sq1) #sol.append(sq2) MatGraph.MultiplyMeshAndMatrix(sol, matrix.RotationMatrix("X", math.pi / 2)) global TranslationMatrix DEBUG.init() Graphics.initDisplayHandler(DEBUG.window, DEBUG.ScreenSize, 100, 15 * math.pi / 180) DEBUG.HandleWindowEvents() theta = 0 phi = 0 vtheta = 0 #for p in sys.path: #Test2 = Load3DElement("GPlaneur/Tests/Graphipsa3D/MeshFiles/GAMIIIING.obj") #MatGraph.MultiplyMeshAndMatrix(Test,matrix.RotationMatrix("Y",theta)) #MatGraph.MultiplyMeshAndMatrix(Test,matrix.RotationMatrix("X",math.pi/6)) #MatGraph.MultiplyMeshAndMatrix(Test,matrix.RotationMatrix("Z",math.pi/6)) #Test2 = Load3DElement("Tests/Graphipsa3D/MeshFiles/teapot.obj") Cam = vector([0, 0, 0]) vUp = vector([0, 1, 0]) vLookDir = vector([0, 0, 1]) vTarget = vector([0, 0, 1]) #print(pygame.display.Info()) Step = 2 Size = 60 offset = 0 LastCamPos = 0 miniOffset = 0 while DEBUG.ISRUNNING: #thetaa += math.pi/512 #Cam.v[2] -= DEBUG.JoystickAxis[3]/16 phi += DEBUG.JoystickAxis[1] / 32 vForward = vector([(-vLookDir.v[0]) * DEBUG.JoystickAxis[3] / 1, (-vLookDir.v[1]) * DEBUG.JoystickAxis[3] / 1, (-vLookDir.v[2]) * DEBUG.JoystickAxis[3] / 1]) Cam += vForward #print(Cam) #vTarget = ([0,0,1]) vLookDir = vector([0, math.sin(phi), math.cos(phi)]) #vLookDir = vector([-math.sin(theta),0,math.cos(theta)]) #vLookDir = vector([1,0,0]) vTarget = Cam + vLookDir subMat = PointAt(Cam, vTarget, vUp) mtView = LookAtMatrix(subMat) #print(vTarget) sol = [] #print(offset) if Cam.v[2] > Size + offset - 60: offset = int(Cam.v[2]) miniOffset = Cam.v[2] - offset for i in range(-30, 30, Step): for j in range(offset, Size + offset, Step): sq1 = matrix.mat3x3([[i, 6, j - miniOffset], [i + Step, 6, j - miniOffset], [i, 6, j + Step - miniOffset]]) sol.append(sq1) #Tst2 = sol.copy() #MatGraph.MultiplyMeshAndMatrix(Tst2,matrix.RotationMatrix("X",math.pi/2)) #MatGraph.AddMeshAndMatrix(Tst2,matrix.mat3x3([[0,3,0],[0,3,0],[0,3,0]])) #MatGraph.MultiplyMeshAndMatrix(Test,matrix.RotationMatrix("Y",-thetaa)) #MatGraph.AddMeshAndMatrix(Test,TranslationMatrix) #MatGraph.MultiplyMeshAndMatrix(Test,matrix.RotationMatrix("X",thetaa)) #MatGraph.MultiplyMeshAndMatrix(Test,matrix.RotationMatrix("X",math.pi/6)) #MatGraph.MultiplyMeshAndMatrix(Test,matrix.RotationMatrix("Z",math.pi/6)) #FillMesh(Test,(255,255,255),Cam,Light,matView) #FillMesh(Tst2,(0,255,0),Cam,Light,matView) FillMesh(sol, (255, 255, 255), Cam, Light, mtView) #DrawMesh(Test,(255,255,255)) DEBUG.HandleWindowEvents() DEBUG.quit()
#if(M2.m[0][2] >= 0.7 and M2.m[1][2] >= 0.7 and M2.m[2][2] >= 0.7): Graphics.DrawTriangle(MatGraph.ProjectTriangle(M2), (0, 0, 0), 1) #Graphics.DrawTriangle(M2[i],color,0) """ else : tri = MatGraph.ProjectTriangle(Msh[i]) Graphics.drawPoint(tri.m[0],(255,255,255)) Graphics.drawPoint(tri.m[1],(255,255,255)) Graphics.drawPoint(tri.m[2],(255,255,255)) """ Light = vector([0, -0.7, -0.7]) TranslationMatrix = matrix.mat3x3([[0, 3, 15], [0, 3, 15], [0, 3, 15]]) def PointAt(pos, target, up): #print(target," ",pos) NewForward = target - pos size = math.sqrt(NewForward.v[0]**2 + NewForward.v[1]**2 + NewForward.v[2]**2) NewForward.v[0] /= size NewForward.v[1] /= size NewForward.v[2] /= size #print(NewForward.v) a = NewForward * MatGraph.DotProduct(up, NewForward)
P = Point([5,3]) CameraPosition = Point([0,0]) LookingVector = vector([1,0]) DownVec = vector([0,1]) Msh = [] Step = 1 Size = 10 for i in range(-Size,Size,Step): for j in range(-Size,Size,Step): sq1 = matrix.mat3x3([ [i,j,0], [i+Step,j,0], [i,j+Step,0] ]) sq2 = matrix.mat3x3([ [i+Step,j,0], [i+Step,j+Step,0], [i,j+Step,0] ]) Msh.append(sq1) Msh.append(sq2) def DotProduct(A,B): return A.v[0]*B.v[0]+A.v[1]*B.v[1]
import Mesh import ConversionsTo3D as Conversions import pygame import matrix import math import copy from dataclasses import dataclass import time from typing import List TranslationMatrix = matrix.mat3x3([ [0,0,8], [0,0,8], [0,0,8] ]) TranslationMatrix2 = matrix.mat3x3([ [-0,0,0], [-0,0,0], [-0,0,0], ]) Cam = matrix.vector([0,0,0]) Light = matrix.vector([0,-0,-1])