1

my game currently uses Graphics2D for a lot of the drawing and text rendering but I would like to use some of the functions available in LWJGL like GL11.glBegin(...) to make custom shapes and maybe use some of the cool texture functions as well.

Is it possible to do this? and if so how what code would I need to add from LWJGL to get its functions working along side what I already have?

genpfault
  • 51,148
  • 11
  • 85
  • 139
bigbass1997
  • 175
  • 1
  • 2
  • 10

1 Answers1

1

It is possible to do but that is not a straightforward way.Graphics2D is a part of Java drawing API while LWJGL is just a Java wrapper for OpenGL API.What I mean is that at the end of the day OpenGL expects the input to be vertices and indices which are assembled into triangles on GPU.I have written several tools to convert vector graphics to polygons in the past so here is basically what you can do:

1)Record Graphics2D shapes paths into a data structure.Then convert those into poly outlines points sets(arrays) which must be consecutive.So for example , if your 2d shape is comprised of 2 straight line edges and of 2 bezier lines you can record the start/end points of straight lines as they are and use this method to decompose the bezier to segments(straight lines).Why do you need it all to be segments?Because later,you are going to triangulate these polygons.

2)next part is to triangulate the poly sets you created in the previous stage.Here you can use one of many triangulation libraries: Poly2Tri - is a robust lib ,also having Java port which triangulates both convex and concave poly sets.Also deals with holes. Another option is to use Glu Tessellator if you are using old (fixed) OpenGL pipeline.In such a case you even don't need to seek any external libs as glu is a part of LWJGL API.

Basically that is what you need.But that is really a very primitive setup.For example , one of the wide spread problems in rendering 2D graphics on GPU is co-planar surfaces.So if you have depth buffer on and a couple of co-planar planes you are going to get "Z-fighting" artifacts.To solve this you should either disable depth write (which is unlikely what you want to do in 3d engine),or using stencil test (there are more hacks with depth/color writes ping-pong but it is complex).But another more "geometrical approach" option is doing polygons clipping operation between overlapped polys before triangulating those.Here you can use libs like Polygon Clipper.

So ,as you can see, what you want to do is not a matter of minutes and there are many more challenges on the way like performance , geometry and uv mapping issues etc.But it is absolutely doable.

Michael IV
  • 11,016
  • 12
  • 92
  • 223
  • I dont really understand anything you just said. To me you basicly said its not possible without hours and hours of work and I dont even know where to start. Do you have any example code or maybe a simpler explanation? – bigbass1997 Dec 23 '12 at 19:49
  • 1
    I think my answer is quite basic.Google about polygon clipping and triangulation methodology. There is a lot of material on the topic in the web. – Michael IV Dec 23 '12 at 21:34