GL4Java

Is anyone familiar with GL4Java? If so I have a problem I could really use some assistance with. I’m trying to do something as basic as rendering a polygon, however, when I run I am unable to see object just black background. Also I have a menu bar (through SWING) at the top of the frame and it click on certains items the list is hidden behind the black canvas. Can anyone assist. Below is my code for the polygon

import gl4java.GLContext;
import gl4java.awt.GLAnimCanvas;

public class gci_glcode extends GLAnimCanvas {

public gci_glcode(int w, int h) {
super(w,h);
}
public void preInit()
{
doubleBuffer = true;
stereoView = false; // buffering but not stereoview
}

  public void init(){
  float width =  (float)getSize().width;
  float height = (float)getSize().height;

  gl.glShadeModel(GL_SMOOTH);                       gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);               //This Will Clear The Background Color To Black
       gl.glClearDepth(1.0);                                  //Enables Clearing Of The Depth Buffer
       gl.glEnable(GL_DEPTH_TEST);                            //Enables Depth Testing
       gl.glDepthFunc(GL_LEQUAL);                             //The Type Of Depth Test To Do
       gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective

  /*qobj = (int)glu.gluNewQuadric();
  glu.gluQuadricNormals(qobj, GLU_SMOOTH);       // Create Smooth Normals ( NEW )
  glu.gluQuadricTexture(qobj, GL_TRUE);*/
 }
 public void doCleanup(){
        stop();
 }

 public void reshape(int width, int height){
   //Reset The Current Viewport And Perspective Transformation

   System.out.println("Width : "+width+" Height: "+height);
   if(height==0)height=1;
   gl.glViewport(0, 0, width, height);                       // Reset The Current Viewport And Perspective Transformation
   gl.glMatrixMode(GL_PROJECTION);                           // Select The Projection Matrix
   gl.glLoadIdentity();                                      // Reset The Projection Matrix
   glu.gluPerspective(45.0f, width / height, 0.1f, 100.0f);  // Calculate The Aspect Ratio Of The Window
   gl.glMatrixMode(GL_MODELVIEW);                            // Select The Modelview Matrix
   gl.glLoadIdentity();                                      // Reset The ModalView Matrix


 }


 public void display(){
  /*if(!cvsIsInit()  | |  !glj.gljMakeCurrent())

{
System.out.println(“The program is broken; didn’t initialize”);
}*/
if(glj.gljMakeCurrent()== false) return;
gl.glMatrixMode(GL_MODELVIEW);
gl.glLoadIdentity();

gl.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

 /*gl.glPushMatrix();
 gl.glTranslated(-100,0,0);
 glu.gluSphere(qobj, 20,20,20);
 gl.glPopMatrix();

gl.glFlush();*/

gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
gl.glClear(GL_COLOR_BUFFER_BIT);
gl.glColor3f(1.0f,1.0f,1.0f);
gl.glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
gl.glBegin(GL_POLYGON);
gl.glVertex3f(-1.0f,1.0f,0.0f);
gl.glVertex3f(1.0f,1.0f,0.0f);
gl.glVertex3f(1.0f,-1.0f,0.0f);
gl.glVertex3f(-1.0f,-1.0f,0.0f);
gl.glEnd();
gl.glFlush();

glj.gljSwap(); //Swap buffers
glj.gljFree(); // release GL
}
}

Chapter 3 of the Red Book has a section called “Troubleshooting Transformations” that lists a lot of reasons why you’re screen may be blank.
http://fly.cc.fer.hr/~unreal/theredbook/chapter03.html

After a quick glance at your code, do you really want to be translating 100 units down negative X? The default “camera” is situated at the origin pointing down the negative Z axis.

The reason why some of your menu bars appear to be below the gl4java canvas is due to your mixing of lightweight and heavyweight components.

The hardware acceleration path brings with it its own problems though, mainly centred around the mixing of lightweight and heavyweight components. A heavyweight component is one that is based on the older abstract windowing toolkit (AWT) java classes and a lightweight is one that is based upon the newer swing classes. AWT based components are not 100% java based, using an underlay of C/C++ code that is dependable on the operating system. This means that the same application may look totally different on different operating systems. AWT based components also have their own z-ordering scheme whereas swing does not, we will see why this is important in a moment.

Swing on the other hand is more or less 100% java based, this means that an application developed using swing classes will look the same no matter what operating system it is running on. Whereas AWT components are drawn using peer counterparts from the operating system, swing components are just drawn/painted to blank windows. As mentioned before, swing does not have any z-ordering scheme, which means that an AWT component will ALWAYS be drawn on top of it. This was the main problem that we encountered when we mixed the heavyweight OpenGL component with the lightweight container and information components.

The solution to this problem was to develop a lightweight version of the OpenGL accelerated panel. The panel would have to be reworked from scratch to make use of the lightweight swing classes. During the development of the new OpenGL panel, further major bugs were encountered. The frame rate of the panel had decreased to a tenth of what is used to be when using the AWT classes. This resultant frame rate was unacceptable, and after spending a great deal of time debugging the problem we finally got the frame rate back up to somewhere near what it used to be.

“A heavyweight component is one that is based on the older abstract windowing toolkit (AWT) java classes and a lightweight is one that is based upon the newer swing classes.”
No true. A heavyweight control is one that has its own native peer, and such relies upon the underlying OS for its display. A lightweight control is drawn an managed by java code. While AWT is made of heavyweight controls, not all of Swing is lightweight (such as JFrame) and some are able to operate in both methods (menu popups for example).

“swing does not have any z-ordering scheme”
No, Swing has it’s own ordering system, but AWT controls - being native controls - will not respect this ordering.

j3d.org has a good article on mixing a heavyweight canvas (a J3d one or GL4Java one) with lightweight controls.