From ee69f1f9c76f13eb2f69b61cc2b6e68a7ab2c380 Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 16 Jun 2020 10:04:05 +0200 Subject: [PATCH 1/4] 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 a87c394ef6f44fa1c0d2b30930523a844b6e70ff Mon Sep 17 00:00:00 2001 From: Leo Date: Thu, 18 Jun 2020 16:17:07 +0200 Subject: [PATCH 2/4] 22.) Cleanup example3 --- examples/example3.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/examples/example3.py b/examples/example3.py index 2f736cc..42434b0 100644 --- a/examples/example3.py +++ b/examples/example3.py @@ -13,30 +13,28 @@ class MainApp(App): """ def build(self): + root = FloatLayout() + renderer = Renderer() + scene = Scene() + camera = PerspectiveCamera(15, 1, 1, 1000) # load obj file loader = OBJMTLLoader() obj_path = os.path.join(os.path.dirname(__file__), "./testnurbs.obj") obj = loader.load(obj_path, "./testnurbs.mtl") - scene = Scene() scene.add(*obj.children) for obj in scene.children: obj.pos.z = -20 - camera = PerspectiveCamera(15, 1, 1, 1000) - - renderer = self.renderer = Renderer() - renderer.bind(size=self._adjust_aspect) renderer.render(scene, camera) - - root = FloatLayout() 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 + def _adjust_aspect(inst, val): + rsize = renderer.size + aspect = rsize[0] / float(rsize[1]) + renderer.camera.aspect = aspect + renderer.bind(size=_adjust_aspect) + return root if __name__ == "__main__": From 4c8199ac9cd15d54f89e2b5ccd99f37d4a8b1b80 Mon Sep 17 00:00:00 2001 From: Leo Date: Fri, 17 Jul 2020 09:46:56 +0200 Subject: [PATCH 3/4] make _adjust_aspect class member --- examples/example3.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/example3.py b/examples/example3.py index 42434b0..e423f69 100644 --- a/examples/example3.py +++ b/examples/example3.py @@ -14,7 +14,7 @@ class MainApp(App): def build(self): root = FloatLayout() - renderer = Renderer() + renderer = self.renderer = Renderer() scene = Scene() camera = PerspectiveCamera(15, 1, 1, 1000) # load obj file @@ -29,13 +29,14 @@ def build(self): renderer.render(scene, camera) root.add_widget(renderer) - 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) return root + def _adjust_aspect(self, inst, val): + rsize = self.renderer.size + aspect = rsize[0] / float(rsize[1]) + self.renderer.camera.aspect = aspect + if __name__ == "__main__": MainApp().run() From 6ce38d8a4153724a36e1b4dafa70bc78fa323da6 Mon Sep 17 00:00:00 2001 From: Leo Date: Mon, 20 Jul 2020 13:55:00 +0200 Subject: [PATCH 4/4] revert sphere cleanup --- examples/sphere/main.py | 55 ++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/examples/sphere/main.py b/examples/sphere/main.py index d8dcccb..8c88ccf 100644 --- a/examples/sphere/main.py +++ b/examples/sphere/main.py @@ -1,32 +1,28 @@ +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 SphereExample(App): - """This is a basic example showcasing the Sphere Geometry. - """ - +class MainApp(App): def build(self): - renderer = Renderer(shader_file=shader_file) - renderer.set_clear_color((0.2, 0.2, 0.2, 1.0)) - + self.renderer = Renderer(shader_file=shader_file) + scene = Scene() camera = PerspectiveCamera(45, 1, 0.1, 2500) + self.renderer.set_clear_color((0.2, 0.2, 0.2, 1.0)) - renderer.main_light.intensity = 5000 + self.camera = camera + self.renderer.main_light.intensity = 5000 + root = ObjectTrackball(camera, 10) geometry = SphereGeometry(radius=1) material = Material( @@ -37,6 +33,7 @@ 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) @@ -48,24 +45,20 @@ def build(self): ) lines = Lines(geometry, material) lines.rotation.x = 90 - - scene = Scene() - scene.add(obj) scene.add(lines) - 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) + self.renderer.render(scene, camera) + self.renderer.main_light.intensity = 500 - root = ObjectTrackball(camera, 10) - root.add_widget(renderer) + root.add_widget(self.renderer) + self.renderer.bind(size=self._adjust_aspect) 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): @@ -113,4 +106,4 @@ def do_rotate(self, touch): if __name__ == "__main__": - SphereExample().run() + MainApp().run()