Java JOGL:如何同时使用 GL 和 Graphics 创建图像?
基本上我正在使用 JOGL 学习 openGL,作为一个帮助我学习的项目,我将制作一个简单的游戏,但在此之前我想绘制一个正方形并使用箭头键移动相机。
因为我对 openGL 的了解不是很好,所以我制作了一个小菜单,它使用 awt.Graphics 和缓冲图像将其绘制到 GLCanvas 上。然后,当我选择一个按钮时,它会使用 javax.media.opengl.GL 渲染游戏(当前只是正方形)。
这个设置工作得很好,可以在两种渲染 GLCanvas 的方法之间切换。其中之一可以绘制到画布上,使我能够利用我当前的 Java 图形知识来创建复杂的 2D 菜单,并切换到 JOGL GL 来学习 3D 图形。
但是,这是一个或另一个完全占据了屏幕,
所以问题是如何在屏幕上创建一个混合了两者的图像?即我怎样才能让我的 3D 立方体在屏幕上快乐地旋转,并在顶部有一个由 Java Graphics 绘制的菜单栏?我如何让它们绘制到相同的对象/事物,所以我只是将菜单的 2D 图像粘贴到 3D 上
如果这不可能,我将简单地使用键盘命令,直到我对 GL 有足够的信心来创建菜单直接它。
如果您不想看到我的任何代码,我可以编辑这篇文章,但我不知道它有什么帮助(但您可以,所以请询问)
非常感谢
Basically I am learning openGL using JOGL, as a project to help me learn I am going to make a simple game, but before that I want to draw a square and move the camera around with the arrow keys.
Because I am not very well accomplished with openGL I have made a little menu which uses awt.Graphics and Buffered images to draw it onto a GLCanvas. Then when I select a button it renders the game (currently just the square) using javax.media.opengl.GL.
This set up works perfectly, with switching between the two methods of rendering to the GLCanvas. One OR the other can draw to the canvas allowing me to utilise both my current knowledge of Java Graphics to create a complex 2D menu, and switch to JOGL GL to learn 3D Graphics.
BUT, it is one OR the other that completely hogs the screen,
So the question is how can I create an image on screen which is a mixture of both? I.e how can I have my 3D cube rotating happily on screen and have a menu bar at the top which is drawn by Java Graphics? How do I get them to draw to the same Object/thing so I am just pasting the 2D images of the menu over the 3D
If this is not possible I will simply use keyboard commands until I am confident enough with GL to create the menu with it directly.
If you wan't to see any of my code I can edit this post, but I don't see how it could be helpful (but you may, so please ask)
Many Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在单个面板中混合 JOGL 和 Java2D,这是绝对正确的。所以你必须采取一些技巧。
如果两者占据屏幕的不同区域(例如菜单和动画画布),您可能只需创建两个单独的面板 - 一个用于 Java2D,一个用于 JOGL。它们不能重叠,但可以彼此相邻,看起来就像是同一窗口的一部分。
如果您需要在同一面板中混合 JOGL 和 Graphics2D 重叠,最简单的方法是使用 GLPBuffer。 GLPBuffer 是一个属于 GLDrawable 的屏幕外区域。您将 JOGL 渲染给它,然后从中创建一个位图,并使用 Java2D 将位图绘制到窗口。有关详细信息,请参阅此问题。这样做会对性能造成轻微影响,但可能没有您想象的那么严重。
如果您绝对需要在同一面板中混合 2D 和 3D 并具有最终的快速性能,您将必须使用 JOGL 来绘制所有内容。您必须重新学习如何使用 JOGL 而不是 Java2D 绘制菜单。从好的方面来说,您可能会看到性能略有提高。您将成为为数不多的深入了解 OpenGL 的酷人之一。
You are absolutely right that you can't mix JOGL and Java2D in a single panel. So you are going to have to do some kind of trick.
If the two occupy separate areas of the screen, like a menu and an animation canvas, you might be able to just create two separate panels - one for Java2D and one for JOGL. They can't overlap, but they can sit next to each other and look like they are part of the same window.
If you need to mix JOGL and Graphics2D overlapping in the same panel, the easiest way is to use a GLPBuffer. A GLPBuffer is an offscreen area that is a GLDrawable. You render JOGL to it and then create a bitmap from it, and draw the bitmap to the window using Java2D. See this question for more information. You will take a slight performance hit doing this, but probably not as much as you think.
If you absolutely need to mix 2D and 3D in the same panel with ultimately fast performance you're going to have to use JOGL to draw everything. You'll have to relearn how to draw menus with JOGL rather than Java2D. On the upside you will probably see a slight performance improvement. And you'll be one of the few really cool people who know in-depth OpenGL.