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.

Thanks

Recommended Answers

All 7 Replies

Hard to say really, especially not seeing your custom wrapper code. I have only used JOGL with Swing/AWT, so I'm not familiar with any of the issues related to its use with SWT, but since you say the code works fine without your abstractions I would look to those first and make sure you aren't losing the context or canvas somewhere in there.

On that note, I have another question. How exactly are the context, data, and canvas related? So far, all my abstraction classes do are set up and maintain relationships between these three classes, nothing else at all. I have checked and double checked, and I am 99% positive that my classes are not messing up in any obvious way.

The only thing I can think of, is that the relationship between these guys is not what I think it is, and that is how I am breaking the code.

This is the best succinct explanation that I could find

The JSR-231 APIs specify interfaces two low-level OpenGL abstractions: drawables and contexts. An OpenGL drawable is effectively a surface upon which OpenGL rendering will be performed. In order to perform rendering, an OpenGL rendering context is needed. Contexts and drawables typically go hand-in-hand. More than one context may be created for a particular drawable. In the JSR-231 abstractions, a context is always associated with exactly one drawable

a context is always associated with exactly one drawable

And that was the answer I needed. Thank you very much Ezzaral :)

I did not realize that a GLContext went hand in hand with a GLCanvas (the drawable I use), and luck would have it that the examples I was working with only ever had one instance of GLCanvas at a time. So, me making my assumptions, I thought GLContext was only good for retrieving a GL instance, and used one GLContext instance across several GLCanvas instances.

I found this...

OpenGL rendering semantics specify that only one context may be current on the current thread at any given time, and also that a given context may be current on only one thread at any given time.

... and I also know that JOGL creates many different threads for synchronization reasons, and I bet that is where my problems began; I may have been breaking both rules. (I didnt do any test, so I dont really know for sure)

Thanks again

Glad you found the issue.

I hate to embarrass myself, but it turns out my problem was a pure newb mistake, and not as described in my last post.

Although I was incorrectly using the GLContext, my problem was coming from somewhere else. My misuse of GLContext could have caused more issues later, but this was not the reason I was getting a blank screen.

My first attempts with JOGL were simply to wrap up all of the little pieces you need to get the grand finale. My mistake was that I did not adequately understand those little pieces, and with all of the examples I was working with, I was mixing and matching a lot more than I realized. After I got all of my examples working WITH my new wrappers, I started leaving Java/JOGL documentation, and started digging into more straight-up OpenGL (many good documentations use C in the examples).

After a couple of hours in the OpenGL Red Book, http://www.glprogramming.com/red/ , I found this interesting little tid-bit.

It's pretty easy to get a camera pointed in the right direction, but in computer graphics, you have to specify position and direction with coordinates and angles. As we can attest, it's all too easy to achieve the well-known black-screen effect. Although any number of things can go wrong, often you get this effect - which results in absolutely nothing being drawn in the window you open on the screen - from incorrectly aiming the "camera" and taking a picture with the model behind you. A similar problem arises if you don't choose a field of view that's wide enough to view your objects but narrow enough so they appear reasonably large.

I stared at that paragraph for about 5 minutes, laughed, and all I could think was son of a b****.

Hehe, yes I have botched initial calculations on such things before too and it can cause a bit of frustration if you don't immediately realize it. Even more so in the early stages of working with OpenGL when you aren't very familiar with any of what you are coding. It does start to snap into place if you keep after it though :)

(Watch things like polygon winding too, as that can bite you in some unexpected ways if you are not attentive to it)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.