/* * Leo Chan * * October 1995 * * ----------- * * Sven Goethel * September 1997 * * @version 1.0, 23. JUNI 1998 */ import GL4Java.*; import java.awt.*; import java.awt.event.*; class testGL1 extends GLFrame implements ActionListener, ComponentListener, MouseListener, Runnable { protected double FramesPerSec=20; protected long fps_count=0; protected long fps_start=0; protected Thread killme = null; protected int FrameStep =0; protected boolean inAnApplet = true; float lmodel_twoside[] = null; float lmodel_local[] = null; float lmodel_ambient[] = null; InfoWin iw=null; public static void main( String args[] ) { GLFrame.glNativeDebug = false; GLFrame.glClassDebug = false; testGL1 tt = new testGL1(); tt.inAnApplet=false; } public testGL1() { super( "Star GL4Java", 300, 250 ); setVisible(true); } Frame ctrl = null; Button ButtonFps; TextField TextFps; public void init() { ctrl = new Frame ("Features"); ctrl.setLayout(new GridLayout(1,2)); ctrl.add ( ButtonFps=new Button("Frames per secound") ); ctrl.add ( TextFps=new TextField() ); ctrl.pack(); ctrl.setVisible( true ); ButtonFps.addMouseListener(this); TextFps.addActionListener(this); ctrl.addWindowListener(this); iw = new InfoWin(this); iw.pack(); iw.show(); iw.addWindowListener(this); lmodel_twoside= new float[4]; lmodel_twoside[0]=0f; lmodel_twoside[1]=0f; lmodel_twoside[2]=0f; lmodel_twoside[3]=0f; lmodel_local= new float[4]; lmodel_local[0]=0f; lmodel_local[1]=0f; lmodel_local[2]=0f; lmodel_local[3]=0f; lmodel_ambient= new float[4]; lmodel_ambient[0]=0f; lmodel_ambient[1]=0f; lmodel_ambient[2]=0f; lmodel_ambient[3]=0f; /* Standard GL4Java Init */ if( gljUse() == false ) { System.out.println("problem in use() method"); return; } /* initialize the widget */ glClearColor( 0.8f, 0.8f, 1.0f, 1.0f ); glFrontFace( GL_CW ); glEnable( GL_DEPTH_TEST ); int width = gljGetWidth(); int height = gljGetHeight(); glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); double fov = 45.0, aspect = width / height, near = 1.0, far = 200.0; gluPerspective( fov, aspect, near, far ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); /* render three rotated cylinders */ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); float lightArr[] = new float[4]; float farr[] = new float[4]; /* enable lighting */ lightArr[0] = 1.0f; lightArr[1] = 1.0f; lightArr[2] = 1.0f; lightArr[3] = 1.0f; glLightfv( GL_LIGHT0, GL_DIFFUSE, lightArr ); lightArr[0] = 90.0f; lightArr[1] = 90.0f; lightArr[2] = 0.0f; lightArr[3] = 0.0f; glLightfv( GL_LIGHT0, GL_POSITION, lightArr ); lightArr[0] = 0.1f; lightArr[1] = 0.1f; lightArr[2] = 0.1f; lightArr[3] = 1.0f; glLightfv( GL_LIGHT0, GL_AMBIENT, lightArr ); glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, 1 ); glEnable( GL_LIGHT0 ); /* glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); */ glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1); glEnable( GL_LIGHTING ); glMaterialf( GL_FRONT, GL_SHININESS, 30.0f ); farr[0] = 0.0f; farr[1] = 0.0f; farr[2] = 0.0f; farr[3] = 1.0f; glMaterialfv( GL_FRONT, GL_SPECULAR, farr ); farr[0] = 0.0f; farr[1] = 1.0f; farr[2] = 0.0f; farr[3] = 1.0f; glMaterialfv( GL_FRONT, GL_DIFFUSE, farr ); glMaterialf( GL_BACK, GL_SHININESS, 50.0f ); farr[0] = 0.0f; farr[1] = 0.0f; farr[2] = 1.0f; farr[3] = 1.0f; glMaterialfv( GL_BACK, GL_SPECULAR, farr ); farr[0] = 1.0f; farr[1] = 1.0f; farr[2] = 0.0f; farr[3] = 1.0f; glMaterialfv( GL_BACK, GL_DIFFUSE, farr ); start(); } public void start() { if(killme == null) { killme = new Thread(this,"star"); killme.start(); fps_count=0; fps_start=System.currentTimeMillis(); } } public void stop() { killme.stop(); killme = null; } public void run() { while (killme!=null) { /* DRAW THE TINGS .. */ repaint(); try {Thread.sleep((int)((1.0/FramesPerSec)*1000.0));} catch (InterruptedException e){} //sDisplay(); FrameStep+=1; if(FrameStep>100000) FrameStep=0; //Thread.yield(); } } public void display() { /* Standard GL4Java Init */ if( gljUse() == false ) { System.out.println("problem in use() method"); return; } glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); glTranslatef( 0.0f, 0.0f, -65.0f ); glRotatef( 40.0f+(2f*FrameStep), 0.0f, 0.0f, 1.0f ); glRotatef( 190.0f+(2f*FrameStep), 0.0f, 1.0f, 0.0f ); glRotatef( 200.0f+(FrameStep*2f), 1.0f, 0.0f, 0.0f ); glPushMatrix(); glScalef( 1.0f, 1.0f, 10.0f ); drawCylinder(); glPopMatrix(); glPushMatrix(); glRotatef( 90.0f, 0.0f, 1.0f, 0.0f ); glScalef( 1.0f, 1.0f, 10.0f ); drawCylinder(); glPopMatrix(); glPushMatrix(); glRotatef( 90.0f, 1.0f, 0.0f, 0.0f ); glScalef( 1.0f, 1.0f, 10.0f ); drawCylinder(); glPopMatrix(); glPopMatrix(); gljSwap(); gljCheckGL(); fps_count++; } public void drawCylinder() { glBegin( GL_TRIANGLE_STRIP ); glNormal3f ( 1.0f, 0.0f, -5.0f); glVertex3f ( 1.0f, 0.0f, -5.0f); glNormal3f ( 1.0f, 0.0f, -5.0f); glVertex3f (1.000000f, 0.000000f, 5.000000f); glNormal3f (0.707107f, 0.707107f, -5.000000f); glVertex3f (0.707107f, 0.707107f, -5.000000f); glNormal3f (0.707107f, 0.707107f, -5.000000f); glVertex3f (0.707107f, 0.707107f, 5.000000f); glNormal3f (0.000000f, 1.000000f, -5.000000f); glVertex3f (0.000000f, 1.000000f, -5.000000f); glNormal3f (0.000000f, 1.000000f, -5.000000f); glVertex3f (0.000000f, 1.000000f, 5.000000f); glNormal3f (-0.707107f, 0.707107f, -5.00000f); glVertex3f (-0.707107f, 0.707107f, -5.00000f); glNormal3f (-0.707107f, 0.707107f, -5.00000f); glVertex3f (-0.707107f, 0.707107f, 5.00000f); glNormal3f (-1.000000f, 0.000000f, -5.000000f); glVertex3f (-1.000000f, 0.000000f, -5.000000f); glNormal3f (-1.000000f, 0.000000f, -5.000000f); glVertex3f (-1.000000f, 0.000000f, 5.00000f); glNormal3f (-0.707107f, -0.707107f, -5.000000f); glVertex3f (-0.707107f, -0.707107f, -5.000000f); glNormal3f (-0.707107f, -0.707107f, -5.000000f); glVertex3f (-0.707107f, -0.707107f, 5.00000f); glNormal3f (0.000000f, -1.000000f, -5.000000f); glVertex3f (0.000000f, -1.000000f, -5.000000f); glNormal3f (0.000000f, -1.000000f, -5.000000f); glVertex3f (0.000000f, -1.000000f, 5.00000f); glNormal3f (0.707107f, -0.707107f, -5.00000f); glVertex3f (0.707107f, -0.707107f, -5.00000f); glNormal3f (0.707107f, -0.707107f, -5.00000f); glVertex3f (0.707107f, -0.707107f, 5.000000f); glNormal3f (1.000000f, 0.000000f, -5.00000f); glVertex3f (1.000000f, 0.000000f, -5.00000f); glNormal3f (1.000000f, 0.000000f, -5.00000f); glVertex3f (1.000000f, 0.000000f, 5.00000f); glEnd(); } // Methods required for the implementation of MouseListener public void mouseClicked( MouseEvent evt ) { Component comp = evt.getComponent(); if( gljUse() == false ) { System.out.println("CtrlWin: problem in use() method"); return; } glFinish(); if(comp.equals(ButtonFps)) { long fps_end=System.currentTimeMillis(); long timed= (fps_end-fps_start)/1000; if(timed==0) timed=1; TextFps.setText(String.valueOf(fps_count/timed)); System.out.println("calculated fps: "+TextFps.getText()); fps_count=0; fps_start=System.currentTimeMillis(); } glFinish(); gljFree(); if ( killme==null || ! Thread.currentThread().equals(killme) ) repaint(); } public void mouseEntered( MouseEvent evt ) { } public void mouseExited( MouseEvent evt ) { } public void mousePressed( MouseEvent evt ) { } public void mouseReleased( MouseEvent evt ) { } public void windowClosing(WindowEvent e) { Component comp = e.getComponent(); if(ctrl!=null && ( comp.equals(ctrl) || comp.equals(this) ) ) { ctrl.dispose(); } if(iw!=null && ( comp.equals(iw) || comp.equals(this) ) ) { iw.dispose(); } if(killme!=null && comp.equals(this) ) { ctrl.dispose(); iw.dispose(); super.windowClosing(e); killme.stop(); } } public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if ( source == TextFps) { try { FramesPerSec=Double.valueOf(TextFps.getText()).doubleValue(); System.out.println("trying "+FramesPerSec+" fps."); } catch (NumberFormatException s) { System.out.println("wrong fps format, use float .."); } fps_count=0; fps_start=System.currentTimeMillis(); } } public class InfoWin extends Frame implements ActionListener { GLFrame glFrame = null; TextArea output = null; MenuItem miGLInfo = null; public InfoWin(GLFrame glf) { glFrame=glf; setTitle("Info Window"); output = new TextArea(5, 30); output.setEditable(false); setLayout(new BorderLayout()); //give max space to the output add("Center", output); MenuBar mb = new MenuBar(); setMenuBar( mb ); Menu info = new Menu( "Info"); info.add( (miGLInfo=new MenuItem( "about GL/GLU"))); mb.add( info ); miGLInfo.addActionListener(this); } public void actionPerformed(ActionEvent event) { String str = new String(); if (event.getSource().equals(miGLInfo)) { if( gljUse() == false ) { str=str+"problem in use() method\n"; } else { String glVen = glGetString(GL_VENDOR); String glRen = glGetString(GL_RENDERER); String glVer = glGetString(GL_VERSION); String glExt = glGetString(GL_EXTENSIONS); String gluVer = gluGetString(GLU_VERSION); String gluExt = gluGetString(GLU_EXTENSIONS); str=str+"GL VENDOR: "+glVen+"\n"; str=str+"GL RENDERER: "+glRen+"\n"; str=str+"GL VERSION: "+glVer+"\n"; str=str+"GL EXTENSIONS: "+glExt+"\n"; str=str+"GLU VERSION: "+gluVer+"\n"; str=str+"GLU EXTENSIONS: "+gluExt+"\n"; gljFree(); } } str += "\n\n"; output.append(str); } } }