简体   繁体   中英

Java JOGL: How can create an image using both GL and Graphics?

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? Ie 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM