I have recently been trying to work with OpenGL in Java and decided to go with JOGL to start. I am using SWT for my visuals and I am programming inside the Eclipse IDE.
I have found plenty of interesting uses for OpenGL, but once I have more than one GLCanvas going, there is a lot of extra code splashed about (here is one example that compiled and executed wonderfully, but is also a little messy for my liking, http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/drawarotatingtorususingtheJOGLOpenGLbinding.htm). So, I decided to wrap it all up in a couple of classes that just manage the creation and use of any GLCanvas that I want. As soon as I created these classes, all of my code broke!!!
Now, my confusion comes from a couple of sources. One, SWT and JOGL aren't exactly made for each other, so documentation is scarce, and I find myself reading through OpenGL/C++ and JOGL/AWT/Swing examples, and I am getting a lot of mixed input on the *right* way to do things. Second, I have got SWT and JOGL working together in several instances without any problems, but once I add my little layer of abstraction, they quit working properly.
Here is my main display method, which is contained inside of one of my custom classes...
this.canvas.setCurrent(); this.context.makeCurrent(); GL gl = this.context.getGL(); gl.glClearColor(0f, 0f, 0f, 0f); gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glLoadIdentity(); gl.glColor3f(0.5f, 0.5f, 0.5f); gl.glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f); gl.glBegin(GL.GL_POLYGON); gl.glVertex2f(-0.5f, -0.5f); gl.glVertex2f(-0.5f, 0.5f); gl.glVertex2f(0.5f, 0.5f); gl.glVertex2f(0.5f, -0.5f); gl.glEnd(); gl.glFlush(); this.canvas.swapBuffers(); this.context.release();
What is odd is as follows. The first 5 lines work just fine (up until the glLoadIdentity call), but as soon as I try to start drawing any type of point, line, or polygon, I get nothing at all.
The reason I say the first 5 lines work, is because I made the background incrementally change colors (gl.glClearColor) and it worked fine, but I still got nothing on the remainder of the code.
Now, the display method above works find when I slap all the JOGL goodies together (leaving out my classes). It makes no sense to me why I cannot see any of my drawing commands, but the canvas is being cleared to the color I want just fine.
I do not think this is a direct problem with SWT since I have been able to get it to work, but I must be getting in the way of some internal functionality either in SWT or in JOGL. If you see anything immediately wrong with my code, or you have any ideas why I am getting these results, your input would be much appreciated.