我想更改此代码以添加一个on_touch_up事件,该事件将告知是否单击了“猴子头”。但是我很难弄清楚坐标系是如何工作的。我知道可变触摸返回鼠标单击的坐标,左下角为(0,0)。如果我检查模型(猴子头)的位置,它将返回(0,0)。有人可以帮助我解决如何解决这个问题的提示吗?预先感谢。
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.resources import resource_find
from kivy.graphics.transformation import Matrix
from kivy.graphics.opengl import *
from kivy.graphics import *
from objloader import ObjFile
class Renderer(Widget):
def __init__(self, **kwargs):
self.canvas = RenderContext(compute_normal_mat=True)
self.canvas.shader.source = resource_find('simple.glsl')
self.scene = ObjFile(resource_find("monkey.obj"))
super(Renderer, self).__init__(**kwargs)
with self.canvas:
self.cb = Callback(self.setup_gl_context)
PushMatrix()
self.setup_scene()
PopMatrix()
self.cb = Callback(self.reset_gl_context)
Clock.schedule_interval(self.update_glsl, 1 / 60.)
def setup_gl_context(self, *args):
glEnable(GL_DEPTH_TEST)
def reset_gl_context(self, *args):
glDisable(GL_DEPTH_TEST)
def update_glsl(self, delta):
asp = self.width / float(self.height)
proj = Matrix().view_clip(-asp, asp, -1, 1, 1, 100, 1)
self.canvas['projection_mat'] = proj
self.canvas['diffuse_light'] = (1.0, 1.0, 0.8)
self.canvas['ambient_light'] = (0.1, 0.1, 0.1)
self.rot.angle += delta * 100
def setup_scene(self):
Color(1, 1, 1, 1)
PushMatrix()
Translate(0, 0, -3)
self.rot = Rotate(1, 0, 1, 0)
m = list(self.scene.objects.values())[0]
UpdateNormalMatrix()
self.mesh = Mesh(
vertices=m.vertices,
indices=m.indices,
fmt=m.vertex_format,
mode='triangles',
)
PopMatrix()
def on_touch_up(self, touch):
print "touch: ({}, {})".format(touch.pos[0], touch.pos[1])
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'right':
self.update_glsl(1/60.)
elif keycode[1] == 'left':
self.rot.angle -= 1/60. * 100
class RendererApp(App):
def build(self):
return Renderer()
if __name__ == "__main__":
RendererApp().run()