在 OPENGL 屏幕上添加按钮
我在 Android 中借助 OPENGL 创建了一个旋转立方体,现在我想添加一个侦听器来处理立方体的旋转,即当我单击按钮时,立方体必须开始旋转,反之亦然。我如何在OPENGL 屏幕?
I have created a rotating CUBE with the help of OPENGL in Android now i want to add a listener to handle the rotation of the cube i.e. When i click button then CUBE must start rotating and vice versa..How can i add a button on an OPENGL Screen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有两个选项:
OnClickListener
并让侦听器调用渲染器中的方法来执行旋转。这几乎是实现此功能的最简单方法,但按钮的样式会根据操作系统版本和供应商而变化。如果您想对按钮有更多控制,可以尝试其他选项。glOrtho
),并使用按钮图像绘制纹理矩形(您也可以像 Android 一样缩放按钮图像)通过使用 9 个矩形来保持角和边缘锐利,并仅缩放图像的内部部分(通常是渐变或纯色)。您必须将渲染器设置为接收触摸输入,并执行简单的遏制检查以确保按下的点位于按钮的范围内。如果是,那么您可以调用处理旋转的方法。如果您想要一个成熟的 UI 库,我确信 Android 上有几个适用于 OpenGL ES 的库,其中大多数可能是大型游戏引擎的一部分,该引擎可以为您处理大部分工作。如果您只想要单个按钮,我会推荐第一个选项,因为它是最简单的。
There are two options here:
GLSurfaceView
and whatever buttons/settings you want. Use the standardOnClickListener
for the button and have the listener call a method in your renderer to do the rotations. This is pretty much the easiest way to get this working, but the style of the button will change based on OS version and vendor. If you want to have more control over the button, you can try the other option.glOrtho
) with the same dimensions as your screen and draw a textured rectangle with your button image (You can also scale your button image like Android does by using 9 rectangles to keep the corners and edges sharp and just scale the inner part of the image which is usually a gradient or solid color). You'll have to set your renderer up to receive touch input and do a simple containment check that the pressed point is within the bounds of the button. If it is then you can call the method that handles rotation.If you want a full-blown UI library, I'm sure there are several out there that exist for OpenGL ES on Android, and most of them would probably be part of larger game engines, which would handle most of the work for you. If you just want the single button though, I would recommend the first option as it's the simplest.
您可以绘制一个按钮对象(矩形),该对象始终与相机保持垂直,以便
按钮的视觉效果。每次相机变换时,按钮对象也应该相对变换。
点击部分需要单独处理彻底的拾取,即您需要从相机中心投射光线并查看它是否与您的 3D 按钮对象相交
另外,如果您在桌面上执行此操作,您将搜索“覆盖 GUI”图书馆喜欢
CEGUI里面还是蛮不错的
http://www.cegui.org.uk/wiki/index.php/Main_Page
或者您也可以使用 Navilibrary 和 SDL
http://princeofcode.com/navilibrary.php
如果是 android,您可以探索上述库代码(开源)并尝试从那里获取想法。
You can draw a Button object (rectangle) which remains always perpendicular to the camera for the
visual effect of a button. Everytime camera gets transformed the button object should also be relatively transformed.
The click part needs to be handled seperately thorough picking, i.e. you need to cast a ray from the centre of the camera and see if it intersects your 3D button object
Also if you were doing this on desktop you would have searched for "overlay GUI" libraries like
CEGUI which is quite good
http://www.cegui.org.uk/wiki/index.php/Main_Page
or else you could also use Navilibrary and SDL
http://princeofcode.com/navilibrary.php
In case of android you can explore the above libraries code (open source) and try to get the ideas from there.