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 64d8c94032c2100d0f983099bf912c906387292d Mon Sep 17 00:00:00 2001 From: Leo Date: Thu, 18 Jun 2020 16:08:01 +0200 Subject: [PATCH 2/4] 18.) Cleanup trackball example --- examples/trackball/main.py | 70 +++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/examples/trackball/main.py b/examples/trackball/main.py index 52b2856..bc0be55 100644 --- a/examples/trackball/main.py +++ b/examples/trackball/main.py @@ -1,16 +1,48 @@ -import os -import math from kivy.app import App -from kivy.clock import Clock -from kivy3 import Scene, Renderer, PerspectiveCamera -from kivy3.loaders import OBJLoader from kivy.uix.floatlayout import FloatLayout +from kivy3 import PerspectiveCamera +from kivy3 import Renderer +from kivy3 import Scene +from kivy3.loaders import OBJLoader +import math +import os # Resources pathes _this_path = os.path.dirname(os.path.realpath(__file__)) obj_file = os.path.join(_this_path, "./MQ-27.obj") +class TrackballExample(App): + """This example shows how to use the Trackball-Navigation. + """ + + def build(self): + renderer = Renderer() + + loader = OBJLoader() + obj = loader.load(obj_file) + + camera = PerspectiveCamera(15, 1, 100, 2500) + + root = ObjectTrackball(camera, 1500) + + scene = Scene() + scene.add(obj) + + renderer.render(scene, camera) + renderer.main_light.intensity = 500 + + 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) + + return root + + class ObjectTrackball(FloatLayout): def __init__(self, camera, radius, *args, **kw): super(ObjectTrackball, self).__init__(*args, **kw) @@ -56,31 +88,5 @@ def do_rotate(self, touch): self.camera.look_at((0, 0, 0)) -class MainApp(App): - def build(self): - self.renderer = Renderer() - scene = Scene() - camera = PerspectiveCamera(15, 1, 100, 2500) - loader = OBJLoader() - obj = loader.load(obj_file) - self.obj3d = obj - self.camera = camera - root = ObjectTrackball(camera, 1500) - - scene.add(obj) - - self.renderer.render(scene, camera) - self.renderer.main_light.intensity = 500 - - 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 - - if __name__ == "__main__": - MainApp().run() + TrackballExample().run() From 902888be91253803a0a88b0a8b7cd7f313b31970 Mon Sep 17 00:00:00 2001 From: Leo Date: Mon, 20 Jul 2020 13:04:18 +0200 Subject: [PATCH 3/4] make _adjust_aspect class member --- examples/trackball/main.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/trackball/main.py b/examples/trackball/main.py index bc0be55..3e7c032 100644 --- a/examples/trackball/main.py +++ b/examples/trackball/main.py @@ -17,7 +17,7 @@ class TrackballExample(App): """ def build(self): - renderer = Renderer() + renderer = self.renderer = Renderer() loader = OBJLoader() obj = loader.load(obj_file) @@ -31,17 +31,17 @@ def build(self): renderer.render(scene, camera) renderer.main_light.intensity = 500 + renderer.bind(size=self._adjust_aspect) 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) - 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): From 7f842e529e693ce24358c7fbf8835db120cd6f73 Mon Sep 17 00:00:00 2001 From: Leo Date: Mon, 20 Jul 2020 13:42:46 +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()