From: Kevin N. <kn...@ca...> - 2001-12-01 00:54:34
|
Hey all. I'm new to OpenGL and GL4Java, so please bear with me. I am having problems getting GL4Java to perform quite as well as I = expected it to. I created a small demo with a triangle that you can fly = around the screen. It's just a single non-textured, non-lit polygon. = But I can't seem to get a FPS higher than 83 or so on my Athlon 1.4 = Geforce3 system (at 85Hz refresh and with VSYNC on). Also, every so = often it'll just skip for a second, and the ship will appear somewhere = else. If I turn VSYNC off the skipping is MUCH worse, but I get around = 99.4 fps. On my friends' computer running Win98, it runs so crappily = you can barely tell where the ship is at any given point. I created the GLAnimCanvas using GLDrawableFactory as suggested on this = list, and called canvas.setUseRepaint(false), = canvas.setUseFpsSleep(false), canvas.setUseYield(false) on it. Possible explanations... 1) I'm feeding GL4Java such a tiny rendering job that it finished too = quickly and is having timing problems? However, I don't think this is = the case, because when I call setUseFpsSleep(true) and = setAnimateFps(60), I get a frame rate of around 34. With VSYNC off and = setUseFpsSleep(true) and setAnimateFps(85) at 85Hz, I get 83fps, but = then I have other problems. 2) I am calculating my FPS wrong? This is how I currently do it. Every = time display() is called, I get the amount of time between this tick and = last tick by calling ((new Date()).getTime() - lastTick)/1000.0, which = should give me a double that is the amount of time in seconds since the = last tick. However, this "delta-time" is ALWAYS a multiple of 10 = milliseconds. I don't understand that. Is GL4Java somehow locked at = 100 fps max? Even that wouldn't explain it, because I should be able to = get 11ms delta-times, and 12, 13, etc. But I always get 10s, 20s, and = 30s. Which really sucks. Also, is Date.getTime the best way to handle ticking? There must be a = better way out there (maybe something like the windows API high = performance counter?)... My code is included...it is all in one file, so it should be easy to = read. Thanks much in advance for any help! -Kevin ******************************************************************* import gl4java.*; import gl4java.awt.*; import gl4java.drawable.*; import java.awt.*; import java.util.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import java.net.*; public class GL4JavaTest implements GLEventListener, MouseListener, = KeyListener { private static class MovingTriangle { // physics private double x; private double y; private double rotation; private double vx; private double vy; =20 // display private static float[][][] mesh; private static int displayList =3D -1; =20 // input private boolean bRotRight =3D false; private boolean bRotLeft =3D false; private boolean bAccel =3D false; private boolean bReverse =3D false; =20 // default non-static parameters private double rotRate; private double accelRate; private double maxRate; =20 // default static initialization static { float[][][] newMesh =3D { { { 3.0f, 0.0f, 0.0f }, { -2.598f, 1.5f, 0.0f }, { -2.598f,-1.5f, 0.0f } } }; mesh =3D newMesh; } =20 // default non-static initialization { rotRate =3D 270.0; accelRate =3D 80.0; maxRate =3D 35.0; } =20 public MovingTriangle() { this(0.0, 0.0, 0.0, 0.0, 90.0); } =20 public MovingTriangle(double locX, double locY, double velX, = double velY, double rot) { x =3D locX; y =3D locY; vx =3D velX; vy =3D velY; rotation =3D rot; } =20 private void createDisplayList(GLAnimCanvas glc) { displayList =3D glc.gl.glGenLists(1); glc.gl.glNewList(displayList, GL_COMPILE); glc.gl.glBegin(GL_TRIANGLES); for (int i =3D 0; i < mesh.length; i++) { glc.gl.glVertex3f(mesh[i][0][0], mesh[i][0][1], = mesh[i][0][2]); glc.gl.glVertex3f(mesh[i][1][0], mesh[i][1][1], = mesh[i][1][2]); glc.gl.glVertex3f(mesh[i][2][0], mesh[i][2][1], = mesh[i][2][2]); } glc.gl.glEnd(); glc.gl.glEndList(); } =20 public void tick(double deltaTime) { x +=3D vx * deltaTime; y +=3D vy * deltaTime; =20 if (bRotRight) addRotation(-rotRate * deltaTime); else if (bRotLeft) addRotation(rotRate * deltaTime); =20 if (bAccel) accelerate(accelRate * deltaTime); } =20 public void render(GLAnimCanvas glc) { if (displayList =3D=3D -1) createDisplayList(glc); glc.gl.glTranslated(x , y, -100.0); glc.gl.glRotated(rotation, 0.0, 0.0, 1.0); glc.gl.glCallList(displayList); } =20 public void addRotation(double addRot) { rotation =3D (rotation + addRot) % 360.0; if (rotation < 0.0) rotation =3D 360.0 + (rotation % 360.0); } =20 public void accelerate(double deltaTime) { vx +=3D deltaTime * Math.cos(Math.toRadians(rotation)); vy +=3D deltaTime * Math.sin(Math.toRadians(rotation)); double length =3D Math.sqrt(vx*vx + vy*vy); if (length > maxRate) { vx =3D (vx / length) * maxRate; vy =3D (vy / length) * maxRate; } } =20 public void setInput(boolean bRR, boolean bRL, boolean bA) { bRotRight =3D bRR; bRotLeft =3D bRL; bAccel =3D bA; } =20 } =20 private static GLAnimCanvas canvas; private static GL4JavaTest gl4JavaTest; private static GraphicsEnvironment ge =3D = GraphicsEnvironment.getLocalGraphicsEnvironment(); private static GraphicsDevice gd =3D ge.getDefaultScreenDevice(); private static int frameCount =3D 0; private static MovingTriangle myTriangle =3D new MovingTriangle(); private static boolean[] keys =3D new boolean[255]; private static long lastTick; private static long numFrames =3D 0; private static long elapsedTime =3D 0; private static final int WIDTH =3D 1280; private static final int HEIGHT =3D 1024; =20 public static void main(String args[]) { gl4JavaTest =3D new GL4JavaTest(); Frame glWindow =3D new Frame("Java Test - Moving Triangle!"); GraphicsEnvironment ge =3D = GraphicsEnvironment.getLocalGraphicsEnvironment(); Cursor hiddenCursor =3D null; GLCapabilities glCaps =3D new GLCapabilities(); =20 glWindow.setUndecorated(true); glWindow.setLayout(new BorderLayout()); glWindow.setIgnoreRepaint(true); =20 glWindow.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { gd.setFullScreenWindow(null); System.exit(0); } }); =20 canvas =3D = GLDrawableFactory.getFactory().createGLAnimCanvas(glCaps, WIDTH, = HEIGHT); canvas.addGLEventListener((GLEventListener)gl4JavaTest); canvas.addKeyListener(gl4JavaTest); canvas.setUseRepaint(false); canvas.setUseFpsSleep(false); canvas.setUseYield(false); //canvas.setAnimateFps(85); =20 glWindow.add("Center", canvas); glWindow.pack(); glWindow.setVisible(true); canvas.setVisible(true); canvas.setIgnoreRepaint(true); canvas.repaint(); canvas.start(); =20 gd.setFullScreenWindow(glWindow); gd.setDisplayMode(new DisplayMode(WIDTH, HEIGHT, 32, 60)); canvas.requestFocus(); } =20 public void reshape (GLDrawable glDrawable, int w, int h) { GLAnimCanvas glc =3D (GLAnimCanvas)glDrawable; glc.gl.glViewport(0,0,WIDTH,HEIGHT); // Reset The Current = Viewport glc.gl.glMatrixMode(GL_PROJECTION); // Select The = Projection Matrix glc.gl.glLoadIdentity(); // Reset The Projection = Matrix = glc.glu.gluPerspective(45.0f,(float)WIDTH/(float)HEIGHT,0.1f,100.0f); glc.gl.glMatrixMode(GL_MODELVIEW); // Select The = Modelview Matrix glc.gl.glLoadIdentity(); } =20 public void display (GLDrawable glDrawable) { long newTick; double deltaTime; GLAnimCanvas glc =3D (GLAnimCanvas)glDrawable; glc.gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); myTriangle.setInput(keys[KeyEvent.VK_NUMPAD6], = keys[KeyEvent.VK_NUMPAD4], keys[KeyEvent.VK_NUMPAD8]); if (keys[KeyEvent.VK_ESCAPE]) { gd.setFullScreenWindow(null); System.exit(0); } newTick =3D (new Date()).getTime(); deltaTime =3D (newTick - lastTick)/1000.0; myTriangle.tick(deltaTime); glc.gl.glLoadIdentity(); myTriangle.render(glc); =20 /* REMOVE ME */ elapsedTime +=3D newTick - lastTick; /* REMOVE ME */ numFrames++; System.out.println("Average FPS: " + = numFrames/(elapsedTime/1000.0)); System.out.println("Num Frames: " + numFrames); System.out.println("Elapsed Time: " + (elapsedTime/1000.0)); System.out.println("Num MS in last frame: " + (newTick - = lastTick)); try { while((new Date()).getTime() - lastTick < 10000) wait(1); } catch(Exception e) { } lastTick =3D newTick; } =20 public void init (GLDrawable glDrawable) { GLAnimCanvas glc =3D (GLAnimCanvas)glDrawable; glc.gl.glShadeModel(GL_SMOOTH); // Enable Smooth = Shading =20 glc.gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black = Background glc.gl.glClearDepth(1.0f); // Depth Buffer Setup glc.gl.glEnable(GL_DEPTH_TEST); // Enables Depth = Testing glc.gl.glDepthFunc(GL_LEQUAL); // The Type Of Depth = Testing To Do glc.gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// = Really Nice Perspective Calculations =20 lastTick =3D (new Date()).getTime(); } =20 public void preDisplay(GLDrawable canvas) {} public void postDisplay(GLDrawable canvas) {} public void cleanup(GLDrawable canvas) {} =20 public void keyPressed (KeyEvent keyEvent) { keys[keyEvent.getKeyCode()] =3D true; } =20 public void keyReleased (KeyEvent keyEvent) { keys[keyEvent.getKeyCode()] =3D false; } =20 public void keyTyped (KeyEvent keyEvent) { } =20 public void mouseExited (MouseEvent mouseEvent) { } =20 public void mouseReleased (MouseEvent mouseEvent) { }=20 =20 public void mousePressed (MouseEvent mouseEvent) { } =20 public void mouseClicked (MouseEvent mouseEvent) { } =20 public void mouseEntered (MouseEvent mouseEvent) { } } |