Some noobish GL4Java questions

Hi,

after playing a while with Java and the creation of a very basic tile-engine using java 1.4.0 with it’s VolatileImage where I allready got an application into exclusive fullscreenmode I now started to play around with GL4Java and the NeHe ports to Java. But I have no idea get the sample Applets turned into applications which maybe also should run in fullscreen.

Anyone here can help me please?

[quote] But I have no idea get the sample Applets turned into applications which maybe also should run in fullscreen.
[/quote]
I had the same problem some days ago…
Here’s my solution (without fullscreen)


// create a panel which supports OpenGL (true for double buffering)
GLJPanel thePanel = new GLJPanel(true);
JFrame theFrame = new JFrame();

// insert frame init code like setting size etc.

// put the openGL panel into the frame
theFrame.getContentPane().add(thePanel);
            theFrame.setVisible(true);

// add an opengl listener
thePanel.addGLEventListener( new GLEventListener() {
      /** @see gl4java.drawable.GLEventListener#init(GLDrawable) */
      public void init(GLDrawable arg0) {
      }

      /** @see gl4java.drawable.GLEventListener#preDisplay(GLDrawable) */
      public void preDisplay(GLDrawable in_Draw) {
            in_Draw.getGL().glMatrixMode(GLEnum.GL_PROJECTION_MATRIX);
            in_Draw.getGL().glLoadIdentity();
                                    
            in_Draw.getGLU().gluPerspective(45.0f, 
                  (float)in_Draw.getSize().width / 
                  (float)in_Draw.getSize().height, 
                  0.1f, 100.0f);
      }

      /** @see 4java.drawable.GLEventListener#display(GLDrawable) */
      public void display(GLDrawable arg0) {
            arg0.getGL().glClearColor(0, 0, 0, 0);
            arg0.getGL().glClear(GL_COLOR_BUFFER_BIT);
            // insert your display code here
      }

      /** @see gl4java.drawable.GLEventListener#postDisplay(GLDrawable) */
      public void postDisplay(GLDrawable arg0) {
      }

      /** @see gl4java.drawable.GLEventListener#cleanup(GLDrawable) */
      public void cleanup(GLDrawable arg0) {
      }

      /**@see gl4java.drawable.GLEventListener#reshape(GLDrawable, int, int) */
      public void reshape(GLDrawable arg0, int arg1, int arg2) {
      }
});      

// call this to invoke the paint function
thePanel.paint(theFrame.getContentPane().getGraphics());

This method may be far from optimal (especially placing the matrice setting code in the preDisplay might be nonsense) but at least it works. :slight_smile:

btw: there isn’t any new GLEventAdapter class like these for WindowAdapter etc., is there?

After some trying here my version, contains parts of the tutors #4 and #34 from gametutorials.com to show something and to get the fps…


import java.awt.*;
import java.awt.event.*;

import gl4java.awt.*;
import gl4java.utils.glut.*;
import gl4java.utils.glut.fonts.*;


public class Test01 extends Frame implements Runnable
{
      renderCanvas canvas = null;
      GraphicsDevice       myDevice;
    DisplayMode       oldMode, newMode;
    
      boolean                  fullscreen = false;
      int                   ScreenWidth = 800, ScreenHeight = 600, BitDepth = 32;            
      
      public static void main( String args[] )
      {
          new Test01();
      }
      
      public Test01()
      {
            super("Lalala");
              addWindowListener( new FensterAdapter() );
                          
              setSize(ScreenWidth, ScreenHeight);
            setResizable(false);
            
            myDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
            oldMode = myDevice.getDisplayMode();
            newMode = new DisplayMode(ScreenWidth, ScreenHeight, BitDepth, oldMode.getRefreshRate());
      
              start();
      }

      public void start()
      {
            Thread th = new Thread (this);
            th.start ();
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            
            canvas = new renderCanvas(getSize().width, getSize().height);
        add("Center", canvas);
        
        if (fullscreen) {
                  if(myDevice.isFullScreenSupported()) {
                        try {
                              this.setUndecorated(true);
                              myDevice.setFullScreenWindow(this);
                              myDevice.setDisplayMode(newMode);
                        }
                        catch (Exception e) {
                              myDevice.setDisplayMode(oldMode);
                }
                  }
                  else {
                        System.out.print("Vollbildmodus wird nicht unterstützt");
                        System.exit(0);
                  }
            }
            else {
                  setVisible(true);
            }      
      }
      
      public void run()
      {
            while (true) {
                  repaint();
                  try { Thread.sleep(0); }
                  catch (InterruptedException ex) { }
            }
      }
}

class renderCanvas extends GLAnimCanvas implements KeyListener
{
      boolean[] keys=new boolean[256];                                          // holds information on which keys are held down.
      static float framesPerSecond            = 0.0f;                              // This will store our fps
      static long lastTime                        = 0;                              // This will hold the time from the last frame
      static String strFrameRate;                                                      // We will store the string here for the window title
      static float rotY = 0;                                                            // This is used for the rotation degree
      
      int FONT_HEIGHT = 24;
      protected GLUTFunc glut = null;
      
      public renderCanvas(int w, int h)
      {
            super(w,h);
            addKeyListener(this);
            glut = new GLUTFuncLightImplWithFonts(gl, glu);
      }

      void PositionText( int x, int y )
      {
            gl.glPushAttrib( GL_TRANSFORM_BIT | GL_VIEWPORT_BIT );
            gl.glMatrixMode( GL_PROJECTION );                                    // Set our matrix to our projection matrix
            gl.glPushMatrix();                                                            // Push on a new matrix to work with
            gl.glLoadIdentity();                                                      // reset the matrix
            gl.glMatrixMode( GL_MODELVIEW );                                    // Set our matrix to our model view matrix
            gl.glPushMatrix();                                                            // Push on a new matrix to work with
            gl.glLoadIdentity();                                                      // Reset that matrix
            y = this.getHeight() - FONT_HEIGHT - y;                              // Calculate the weird screen position
            gl.glViewport( x - 1, y - 1, 0, 0 );                              // Create a new viewport to draw into
            gl.glRasterPos4f( 0, 0, 0, 1 );                                          // Set the drawing position
            gl.glPopMatrix();                                                            // Pop the current modelview matrix off the stack
            gl.glMatrixMode( GL_PROJECTION );                                    // Go back into projection mode
            gl.glPopMatrix();                                                            // Pop the projection matrix off the stack
            gl.glPopAttrib();                                                            // This restores our TRANSFORM and VIEWPORT attributes
      }

      void glDrawText(int x, int y, String strString)
      {
            PositionText(x, y);                                                            
            glut.glutBitmapString(glut.GLUT_BITMAP_TIMES_ROMAN_24,strString);
      }

      void CalculateFrameRate()
      {
            long currentTime = System.currentTimeMillis()/1000;                        
            ++framesPerSecond;
            if( currentTime - lastTime > 1.0f )
            {
                  lastTime = currentTime;
                  strFrameRate = "FPS: "+ framesPerSecond;
                  framesPerSecond = 0;
            }
            gl.glColor3f(0f, 1f, 0f);      
            glDrawText(0, 0, strFrameRate);
      }

      public void preInit()
      {
            setUseFpsSleep(false);                                                      //We want to run this animation at full speed. :)
      }

      public void init()
      {
            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);                        //This will clear the background color to Black
            gl.glEnable(GL_DEPTH_TEST);                                                //Enables Depth Testing
            reshape(getSize().width, getSize().height);                        //Reshape the drawing canvas
            start();                                                                        //Start the animation
      }

      public void doCleanup()
      {
            stop();                                                                              //Stop the animation
      }

      public void reshape(int width, int height)
      {
            gl.glViewport(0,0,width,height);                                    // Make our viewport the whole window
            gl.glMatrixMode(GL_PROJECTION);                                          // Select The Projection Matrix
            gl.glLoadIdentity();                                                      // Reset The Projection Matrix
            glu.gluPerspective(45.0f,(float)width/(float)height, 1 ,150.0f);
            gl.glMatrixMode(GL_MODELVIEW);                                          // Select The Modelview Matrix
            gl.glLoadIdentity();                                                      // Reset The Modelview Matrix
      }

      public void display()
      {
            //Ensure GL is initialized correctly
            if (glj.gljMakeCurrent() == false) return;

            gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);      // Clear The Screen And The Depth Buffer
            gl.glLoadIdentity();                                                      // Reset The matrix
            
            //                    Position     View              Up Vector
            glu.gluLookAt(0, 0, 6,     0, 0, 0,     0, 1, 0);            // This determines where the camera's position and view is
            gl.glRotatef(rotY, 0, 1, 0);
            rotY += 2;

            gl.glBegin (GL_TRIANGLES);                                                // This is our BEGIN to draw
                  gl.glColor3f(1, 0, 0);                                                // Make the top vertex RED
                  gl.glVertex3f(0, 1, 0);                                                // Here is the top point of the triangle
                  gl.glColor3f(0, 1, 0);                                                // Make the left vertex GREEN
                  gl.glVertex3f(-1, 0, 0);                                          // Here is the left point of the triangle
                  gl.glColor3f(0, 0, 1);                                                // Make the right vertex BLUE
                  gl.glVertex3f(1, 0, 0);                                                // Here is the right point of the triangle
            gl.glEnd();                                                                        // This is the END of drawing

            CalculateFrameRate();
            ProcessKeyboard();                                                            // Process Keyboard Results

            //Swap buffers, check for errors, and release the drawing context
            glj.gljSwap();
            glj.gljCheckGL();
            glj.gljFree();
      }

      // Process Keyboard Results
      void ProcessKeyboard()                                                                  
      {
            if(keys[KeyEvent.VK_ESCAPE]) {
                  keys[KeyEvent.VK_ESCAPE] = false;
                  doCleanup();
                    System.exit(0);
              }
      }

      // These Methods Override the default implementation of MouseListener in GLAnimCanvas
      public void mouseEntered( MouseEvent evt )
      {
            Component comp = evt.getComponent();
            if( comp.equals(this ) )
            {
                  requestFocus();
            }
      }

      public void mouseClicked( MouseEvent evt )
      { 
            Component comp = evt.getComponent();
            if( comp.equals(this ) )
            {
                  requestFocus();
            }
      }

      // Invoked when a key has been typed. This event occurs when a key press is followed by a key release. 
      public void keyTyped(KeyEvent e)
      {
      }

      // Invoked when a key has been pressed.
      public void keyPressed(KeyEvent e)
      {
            if(e.getKeyCode()<250)            // only interested in first 250 key codes
                  keys[e.getKeyCode()]=true;      
      }

      // Invoked when a key has been released. 
      public void keyReleased(KeyEvent e)
      {
            if(e.getKeyCode()<250)            // only interested in first 250 key codes
                  keys[e.getKeyCode()]=false;      
      }
}

class FensterAdapter extends WindowAdapter
{
      public void windowClosing ( WindowEvent e) 
      { 
            System.exit(0); 
      }
}

The fps on my xp1700+ with gf4 ti4200 are horrible ~180 @80060032 windowed and ~200 in fullscreen.
(Q3 timedemo gives me more then 300fps^^)
Is java so slow? are there any big mistakes that slows everthing down?

The problem is using threads here. That’s just how GL4Java is designed to work though.

Cas :slight_smile:

If I remove my thread it’s even slower.

Can’t think why it should be. I get hundreds of fps using LWJGL…

Cas :slight_smile: