planet rendering finished, front/back planes for occlusion done

This commit is contained in:
Sakimori 2022-01-20 00:06:37 -05:00
parent e522f85e23
commit 57d87459c3
2 changed files with 32 additions and 17 deletions

View file

@ -95,16 +95,16 @@ if __name__=="__main__":
pygame.init() pygame.init()
pygame.display.set_caption("Spinny") pygame.display.set_caption("Spinny")
window = pygame.display.set_mode((1500, 900)) window = pygame.display.set_mode((900, 900))
resolutionDownscaling = 2 resolutionDownscaling = 2
pygame.display.flip() pygame.display.flip()
frameTime = 1/30 #framerate frameTime = 1/144 #framerate
running = True running = True
display = False display = False
thisEarth = deepcopy(Planet.Earth) thisEarth = deepcopy(Planet.Earth)
sat = OrbitingBody(Point(1, config()["earthRadius"] + 2042000, 1), Point(-7400,0,-1200), "BoSLOO", 3, thisEarth) sat = OrbitingBody(Point(0, config()["earthRadius"] + 2042000, config()["earthRadius"] + 3000000), Point(-4800,0,-1800), "BoSLOO", 5, thisEarth)
orbitlines = [] orbitlines = []
renderObjects = [thisEarth, sat, orbitlines] renderObjects = [thisEarth, sat, orbitlines]
imageThread = threading.Thread() imageThread = threading.Thread()

View file

@ -118,7 +118,7 @@ class PlanetSprite(pygame.sprite.Sprite):
class Camera: class Camera:
"""Object which will be used to paint pixels on screen.""" """Object which will be used to paint pixels on screen."""
def __init__(self, surface:pygame.Surface, location:Point, target:"Planet", objects, hFOV = 75): def __init__(self, surface:pygame.Surface, location:Point, target:"Planet", objects, hFOV = 60):
self.surface = surface self.surface = surface
self.objects = objects self.objects = objects
self.location = location self.location = location
@ -143,14 +143,17 @@ class Camera:
"""generates a frame and draws it to the surface. Does not update screen; use pygame.display.flip()""" """generates a frame and draws it to the surface. Does not update screen; use pygame.display.flip()"""
font = pygame.freetype.SysFont("Comic Sans MS", 14) font = pygame.freetype.SysFont("Comic Sans MS", 14)
winWidth, winHeight = self.surface.get_size() winWidth, winHeight = self.surface.get_size()
screenSurface = pygame.Surface((winWidth, winHeight))
screenSurface.fill((10,10,10))
self.spriteGroup.update() frontSurface = pygame.Surface((winWidth, winHeight), pygame.SRCALPHA)
self.spriteGroup.draw(screenSurface) backSurface = pygame.Surface((winWidth, winHeight), pygame.SRCALPHA)
backgroundSurface = pygame.Surface((winWidth, winHeight))
backgroundSurface.fill((15,15,15))
backSurface.fill((0,0,0,0))
frontSurface.fill((0,0,0,0))
orbitlineSurface = pygame.Surface((winWidth, winHeight), pygame.SRCALPHA)
orbitlineSurface.fill((0,0,0,0))
#pygame uses 0,0 as the top left corner #pygame uses 0,0 as the top left corner
for obj in self.objects: for obj in self.objects:
if type(obj).__name__ == "OrbitingBody": if type(obj).__name__ == "OrbitingBody":
@ -160,7 +163,11 @@ class Camera:
intersectPoint.vector[2] = -intersectPoint.vector[2] intersectPoint.vector[2] = -intersectPoint.vector[2]
if intersectPoint is not None: if intersectPoint is not None:
intersectPoint = Point.add(intersectPoint, Point(0, int(winWidth/2), int(winHeight/2))) #x is meaningless here intersectPoint = Point.add(intersectPoint, Point(0, int(winWidth/2), int(winHeight/2))) #x is meaningless here
pygame.draw.circle(screenSurface, (255,255,150), (int(intersectPoint.vector[1]), int(intersectPoint.vector[2])), obj.displaySize) if sat.location.vector[0] < 0:
drawSurface = backSurface
else:
drawSurface = frontSurface
pygame.draw.circle(drawSurface, (255,255,150,255), (int(intersectPoint.vector[1]), int(intersectPoint.vector[2])), obj.displaySize)
elif isinstance(obj, list): elif isinstance(obj, list):
for orbitline in obj: for orbitline in obj:
@ -171,13 +178,17 @@ class Camera:
if intersectPoint is not None: if intersectPoint is not None:
intersectPoint = Point.add(intersectPoint, Point(0, int(winWidth/2), int(winHeight/2))) intersectPoint = Point.add(intersectPoint, Point(0, int(winWidth/2), int(winHeight/2)))
if orbitline.color[3] != 0: if orbitline.color[3] != 0:
pygame.draw.circle(orbitlineSurface, orbitline.color, (int(intersectPoint.vector[1]), int(intersectPoint.vector[2])), 1) if orbitline.location.vector[0] < 0:
drawSurface = backSurface
else:
drawSurface = frontSurface
pygame.draw.circle(drawSurface, orbitline.color, (int(intersectPoint.vector[1]), int(intersectPoint.vector[2])), 1)
#DEBUG DOTS #DEBUG DOTS
#lineToCam = Line(Point.add(self.target.location, Point(0,self.target.radius,0)), self.location) #lineToCam = Line(Point.add(self.target.location, Point(0,self.target.radius,0)), self.location)
#intersectPoint = lineToCam.intersectWithPlane(self.screenPlane) #intersectPoint = lineToCam.intersectWithPlane(self.screenPlane)
#intersectPoint = Point.add(intersectPoint, Point(0, int(winWidth/2), int(winHeight/2))) #intersectPoint = Point.add(intersectPoint, Point(0, int(winWidth/2), int(winHeight/2)))
#pygame.draw.circle(screenSurface, (255,150,150), (int(intersectPoint.vector[1]), int(intersectPoint.vector[2])), 5) #pygame.draw.circle(frontSurface, (255,150,150,255), (int(intersectPoint.vector[1]), int(intersectPoint.vector[2])), 5)
#newLineToCam = Line(Point.add(self.screenPlane.point, Point(0,750,0)), self.location) #newLineToCam = Line(Point.add(self.screenPlane.point, Point(0,750,0)), self.location)
#intersectPoint = newLineToCam.intersectWithPlane(self.screenPlane) #intersectPoint = newLineToCam.intersectWithPlane(self.screenPlane)
@ -192,11 +203,15 @@ class Camera:
0 == 0 0 == 0
#textSurface, rect = font.render(f"Speed: {round(sat.velocity.magnitude())} m/s \nAltitude: {round(rho - target.radius)} m", False, (255,255,255)) #textSurface, rect = font.render(f"Speed: {round(sat.velocity.magnitude())} m/s \nAltitude: {round(rho - target.radius)} m", False, (255,255,255))
font.render_to(screenSurface, (0,0), f"Speed: {round(sat.velocity.magnitude()/1000,3)} km/s", (255,255,255)) font.render_to(backSurface, (0,0), f"Speed: {round(sat.velocity.magnitude()/1000,3)} km/s", (255,255,255))
font.render_to(screenSurface, (0,20), f"Altitude: {round((rho - self.target.radius)/1000)} km", (255,255,255)) font.render_to(backSurface, (0,20), f"Altitude: {round((rho - self.target.radius)/1000)} km", (255,255,255))
self.spriteGroup.update()
self.spriteGroup.draw(backSurface)
self.surface.blit(screenSurface, (0,0)) self.surface.blit(backgroundSurface, (0,0))
self.surface.blit(orbitlineSurface, (0,0)) self.surface.blit(backSurface, (0,0))
self.surface.blit(frontSurface, (0,0))