From ee69f1f9c76f13eb2f69b61cc2b6e68a7ab2c380 Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 16 Jun 2020 10:04:05 +0200 Subject: [PATCH 1/2] 15.) Cleanup sphere example --- examples/sphere/main.py | 55 +++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/examples/sphere/main.py b/examples/sphere/main.py index 8c88ccf..d8dcccb 100644 --- a/examples/sphere/main.py +++ b/examples/sphere/main.py @@ -1,28 +1,32 @@ -import os -import math from kivy.app import App -from kivy.clock import Clock - -from kivy3 import Scene, Renderer, PerspectiveCamera, Material, Mesh -from kivy3.extras.geometries import GridGeometry, SphereGeometry from kivy.uix.floatlayout import FloatLayout +from kivy3 import Material +from kivy3 import Mesh +from kivy3 import PerspectiveCamera +from kivy3 import Renderer +from kivy3 import Scene +from kivy3.extras.geometries import GridGeometry +from kivy3.extras.geometries import SphereGeometry from kivy3.objects.lines import Lines +import math +import os # Resource paths _this_path = os.path.dirname(os.path.realpath(__file__)) shader_file = os.path.join(_this_path, "./blinnphong.glsl") -class MainApp(App): +class SphereExample(App): + """This is a basic example showcasing the Sphere Geometry. + """ + def build(self): - self.renderer = Renderer(shader_file=shader_file) - scene = Scene() + renderer = Renderer(shader_file=shader_file) + renderer.set_clear_color((0.2, 0.2, 0.2, 1.0)) + camera = PerspectiveCamera(45, 1, 0.1, 2500) - self.renderer.set_clear_color((0.2, 0.2, 0.2, 1.0)) - self.camera = camera - self.renderer.main_light.intensity = 5000 - root = ObjectTrackball(camera, 10) + renderer.main_light.intensity = 5000 geometry = SphereGeometry(radius=1) material = Material( @@ -33,7 +37,6 @@ def build(self): transparency=0.8, ) obj = Mesh(geometry, material) - scene.add(obj) # create a grid on the xz plane geometry = GridGeometry(size=(30, 30), spacing=1) @@ -45,19 +48,23 @@ def build(self): ) lines = Lines(geometry, material) lines.rotation.x = 90 + + scene = Scene() + scene.add(obj) scene.add(lines) - self.renderer.render(scene, camera) - self.renderer.main_light.intensity = 500 + renderer.render(scene, camera) + renderer.main_light.intensity = 500 - root.add_widget(self.renderer) - self.renderer.bind(size=self._adjust_aspect) - return root + def _adjust_aspect(inst, val): + rsize = renderer.size + aspect = rsize[0] / float(rsize[1]) + renderer.camera.aspect = aspect + renderer.bind(size=_adjust_aspect) - def _adjust_aspect(self, inst, val): - rsize = self.renderer.size - aspect = rsize[0] / float(rsize[1]) - self.renderer.camera.aspect = aspect + root = ObjectTrackball(camera, 10) + root.add_widget(renderer) + return root class ObjectTrackball(FloatLayout): @@ -106,4 +113,4 @@ def do_rotate(self, touch): if __name__ == "__main__": - MainApp().run() + SphereExample().run() From 1dc5ebeb99aeaf9aa23d8f69c7ec7e35f2f98808 Mon Sep 17 00:00:00 2001 From: Leo Date: Mon, 20 Jul 2020 12:55:09 +0200 Subject: [PATCH 2/2] make _adjust_aspect class member --- examples/sphere/main.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/sphere/main.py b/examples/sphere/main.py index d8dcccb..ebdb750 100644 --- a/examples/sphere/main.py +++ b/examples/sphere/main.py @@ -21,7 +21,7 @@ class SphereExample(App): """ def build(self): - renderer = Renderer(shader_file=shader_file) + renderer = self.renderer = Renderer(shader_file=shader_file) renderer.set_clear_color((0.2, 0.2, 0.2, 1.0)) camera = PerspectiveCamera(45, 1, 0.1, 2500) @@ -55,17 +55,17 @@ def build(self): renderer.render(scene, camera) renderer.main_light.intensity = 500 - - def _adjust_aspect(inst, val): - rsize = renderer.size - aspect = rsize[0] / float(rsize[1]) - renderer.camera.aspect = aspect - renderer.bind(size=_adjust_aspect) + renderer.bind(size=self._adjust_aspect) root = ObjectTrackball(camera, 10) root.add_widget(renderer) return root + def _adjust_aspect(self, inst, val): + rsize = self.renderer.size + aspect = rsize[0] / float(rsize[1]) + self.renderer.camera.aspect = aspect + class ObjectTrackball(FloatLayout): def __init__(self, camera, radius, *args, **kw):