001 package com.croftsoft.core.media.jogl.render; 002 003 import javax.media.opengl.GL; 004 import javax.media.opengl.glu.GLU; 005 import com.sun.opengl.util.GLUT; 006 007 import com.croftsoft.core.math.MathConstants; 008 import com.croftsoft.core.media.jogl.JoglRenderer; 009 010 /*********************************************************************** 011 * Displays the frame rate. 012 * 013 * @version 014 * $Id: JoglFrameRate.java,v 1.3 2008/02/24 23:54:24 croft Exp $ 015 * @since 016 * 2008-02-15 017 * @author 018 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 019 ***********************************************************************/ 020 021 public final class JoglFrameRate 022 implements JoglRenderer 023 //////////////////////////////////////////////////////////////////////// 024 //////////////////////////////////////////////////////////////////////// 025 { 026 027 private static final int 028 FONT = GLUT.BITMAP_HELVETICA_18; 029 030 private static final double 031 HEIGHT = 1, 032 OFFSET_X = 0, 033 OFFSET_Y = 0, 034 WIDTH = 1; 035 036 private static final long 037 POLLING_PERIOD_NANOS = 10 * MathConstants.NANOSECONDS_PER_SECOND; 038 039 private static final GLU glu = new GLU ( ); 040 041 private static final GLUT glut = new GLUT ( ); 042 043 // 044 045 private int 046 font, 047 frameCount; 048 049 private long 050 pollingPeriodNanos, 051 startTime; 052 053 private double 054 frameRate, 055 height, 056 offsetX, 057 offsetY, 058 width; 059 060 //////////////////////////////////////////////////////////////////////// 061 //////////////////////////////////////////////////////////////////////// 062 063 public JoglFrameRate ( ) 064 //////////////////////////////////////////////////////////////////////// 065 { 066 font = FONT; 067 068 height = HEIGHT; 069 070 offsetX = OFFSET_X; 071 072 offsetY = OFFSET_Y; 073 074 pollingPeriodNanos = POLLING_PERIOD_NANOS; 075 076 width = WIDTH; 077 } 078 079 //////////////////////////////////////////////////////////////////////// 080 // accessor methods 081 //////////////////////////////////////////////////////////////////////// 082 083 public double getFrameRate ( ) { return frameRate; } 084 085 public double getHeight ( ) { return height; } 086 087 public double getWidth ( ) { return width; } 088 089 //////////////////////////////////////////////////////////////////////// 090 // mutator methods 091 //////////////////////////////////////////////////////////////////////// 092 093 public void setFont ( final int font ) 094 //////////////////////////////////////////////////////////////////////// 095 { 096 this.font = font; 097 } 098 099 public void setHeight ( final double height ) 100 //////////////////////////////////////////////////////////////////////// 101 { 102 this.height = height; 103 } 104 105 public void setOffsetX ( final double offsetX ) 106 //////////////////////////////////////////////////////////////////////// 107 { 108 this.offsetX = offsetX; 109 } 110 111 public void setOffsetY ( final double offsetY ) 112 //////////////////////////////////////////////////////////////////////// 113 { 114 this.offsetY = offsetY; 115 } 116 117 public void setPollingPeriodNanos ( final long pollingPeriodNanos ) 118 //////////////////////////////////////////////////////////////////////// 119 { 120 this.pollingPeriodNanos = pollingPeriodNanos; 121 } 122 123 public void setWidth ( final double width ) 124 //////////////////////////////////////////////////////////////////////// 125 { 126 this.width = width; 127 } 128 129 //////////////////////////////////////////////////////////////////////// 130 // interface JoglRenderer methods 131 //////////////////////////////////////////////////////////////////////// 132 133 public void init ( final GL gl ) 134 /////////////////////////////////////////////////////////////////////// 135 { 136 // empty 137 } 138 139 public void destroy ( final GL gl ) 140 /////////////////////////////////////////////////////////////////////// 141 { 142 // empty 143 } 144 145 public void setBounds ( 146 final GL gl, 147 final int x, 148 final int y, 149 final int width, 150 final int height ) 151 /////////////////////////////////////////////////////////////////////// 152 { 153 setHeight ( height ); 154 155 setWidth ( width ); 156 } 157 158 public void render ( final GL gl ) 159 /////////////////////////////////////////////////////////////////////// 160 { 161 final long currentTime = System.nanoTime ( ); 162 163 if ( currentTime >= startTime + pollingPeriodNanos ) 164 { 165 final long deltaTime = currentTime - startTime; 166 167 frameRate = frameCount / 168 ( deltaTime * MathConstants.SECONDS_PER_NANOSECOND ); 169 170 frameRate = Math.round ( frameRate * 10 ) / 10.0; 171 172 startTime = currentTime; 173 174 frameCount = 0; 175 } 176 177 ++frameCount; 178 179 gl.glMatrixMode ( GL.GL_PROJECTION ); 180 181 gl.glPushMatrix ( ); 182 183 { 184 gl.glLoadIdentity ( ); 185 186 glu.gluOrtho2D ( 0, width, 0, height ); 187 188 gl.glMatrixMode ( GL.GL_MODELVIEW ); 189 190 gl.glPushMatrix ( ); 191 192 { 193 gl.glLoadIdentity ( ); 194 195 gl.glPushAttrib ( GL.GL_ALL_ATTRIB_BITS ); 196 197 { 198 gl.glColor4d ( 1, 1, 1, 1 ); 199 200 gl.glRasterPos2d ( offsetX, offsetY ); 201 202 glut.glutBitmapString ( font, Double.toString ( frameRate ) ); 203 } 204 205 gl.glPopAttrib ( ); 206 } 207 208 gl.glPopMatrix ( ); 209 } 210 211 gl.glMatrixMode ( GL.GL_PROJECTION ); 212 213 gl.glPopMatrix ( ); 214 } 215 216 /////////////////////////////////////////////////////////////////////// 217 /////////////////////////////////////////////////////////////////////// 218 }