import java.awt.*; import gl4java.*; import gl4java.awt.*; import gl4java.drawable.*; /** Tests the GLComponentFactory/GLEventListener mechanism via a port of the OpenGL logo applet. */ public class TestListener implements GLEventListener, GLEnum { GLFunc gl; GLUFunc glu; static final int RAND_MAX = 100; /* some math.h's don't define M_PI */ static final float M_PI = 3.14159265359f; static final int ACC = 8; static final int ACC2 = 16; static final int ACC3 = 48; static final int ACC4 = 24; static final float THLD = 0.6f; static final float THLD2 = 0.8f; int angle, rotating; float TRANS[][]; float ROTAXIS[][]; float ROT[]; float char_El[][][]; float normal_El[][][]; float char_O[][][]; float normal_O[][][]; float char_P[][][]; float normal_P[][][]; float char_G[][][]; float normal_G[][][]; float accSIN[], accCOS[]; float difmat4[] = { 0.425f, 0.570f, 0.664f, 1.0f }; float difamb4[] = { 0.425f, 0.570f, 0.664f, 1.0f }; float matspec4[] = { 0.174f, 0.174f, 0.174f, 1.0f }; float lightpos[] = { 1.0f, 1.0f, 1.0f, 0.0f }; float lightamb[] = { 0.3f, 0.3f, 0.3f, 1.0f }; float lightdif[] = { 0.8f, 0.8f, 0.8f, 1.0f }; float speed=0f, progress = 1f; public void init(GLDrawable drawable) { gl = drawable.getGL(); glu = drawable.getGLU(); /* here we should add and initialize our JAVA components */ TRANS = new float[7][3]; ROTAXIS = new float[7][3]; ROT = new float[7]; char_El=new float[ACC3+1][ACC+1][3]; normal_El=new float[ACC3+1][ACC+1][3]; char_O=new float[ACC4][ACC+1][3]; normal_O=new float[ACC4][ACC+1][3]; char_P=new float[ACC2][ACC+1][3]; normal_P=new float[ACC2][ACC+1][3]; char_G=new float[ACC4][ACC+1][3]; normal_G=new float[ACC4][ACC+1][3]; accSIN=new float[ACC+1]; accCOS=new float[ACC+1]; randomize(); gl.glShadeModel (GL_SMOOTH); gl.glEnable(GL_DEPTH_TEST); gl.glLightfv(GL_LIGHT0, GL_POSITION, lightpos); gl.glLightfv(GL_LIGHT0, GL_AMBIENT, lightamb); gl.glLightfv(GL_LIGHT0, GL_DIFFUSE, lightdif); gl.glEnable(GL_LIGHTING); gl.glEnable(GL_LIGHT0); gl.glColor3f(1.0f, 1.0f, 1.0f); gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); gl.glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); gl.glEnable(GL_NORMALIZE); def_logo(); gl.glCullFace(GL_BACK); gl.glEnable(GL_CULL_FACE); } public void preDisplay(GLDrawable drawable) { } public void display(GLDrawable drawable) { int i; // just render it gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); setCamera(); draw_logo(); } public void postDisplay(GLDrawable drawable) { // // computation for an animation sequence ... // speed = -0.95f*speed + progress*0.05f; if (progress > 0.0f && speed < 0.0003f) speed = 0.0003f; if (speed > 0.01f) speed = 0.01f; progress = progress - speed; } public void reshape(GLDrawable drawable, int width, int height) { gl.glMatrixMode (GL_MODELVIEW); gl.glViewport (0, 0, width, height); gl.glLoadIdentity(); setCamera(); } public void cleanup(GLDrawable drawable) { } void setCamera() { gl.glMatrixMode (GL_PROJECTION); gl.glLoadIdentity (); gl.glFrustum (-0.1333f, 0.1333f, -0.1f, 0.1f, 0.2f, 150.0f); gl.glMatrixMode(GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt(0,1.5f,2, 0,1.5f,0, 0,1,0); gl.glTranslatef(0.0f, -8.0f, -45.0f); gl.glRotatef(-progress*720f, 0.0f, 1.0f, 0.0f); } int rnd(int i) { return ( (int)((float)Math.random()*RAND_MAX) )%i; } void groen_texture() { gl.glMaterialfv(GL_FRONT, GL_DIFFUSE, difmat4); gl.glMaterialfv(GL_FRONT, GL_AMBIENT, difamb4); gl.glMaterialfv(GL_FRONT, GL_SPECULAR, matspec4); gl.glMaterialf(GL_FRONT, GL_SHININESS, 35.0f); } void def_O() { float a, s, c, ln; int i,j,k,l,m,n; float dx, dy; float dx1, dx2, dy1, dy2, dz1, dz2; float center_O[][]; float width_O[]; center_O=new float[ACC4+4][3]; width_O=new float[ACC4+4]; for (i=0;i1.01f?0.8f:0)) + 0.8f; center_O[i][2] = 0.0f; width_O[i] = 0.6f; } /* I should be able to generalise this. oh well */ for (i=0;iTHLD?THLD:s)); } } for (i=0;i1.01f?0.7f:0)) + 0.7f; center_P[i][2] = 0.0f; width_P[i] = 0.5f; } for (i=0;iTHLD?THLD:s)); } } for (i=0;iTHLD2?THLD2:s)); } } for (i=0;i1.01f?0.8f:0)) + 0.8f; center_G[i][2] = 0.0f; width_G[i] = 0.9f; if (i>ACC4*3/4) width_G[i] = 0.9f - ((i-ACC4*3/4)*0.9f)/ACC; } for (i=0;iTHLD?THLD:s)); } } for (i=0;i=ACC) k = 0; gl.glBegin(GL_QUAD_STRIP); for (j=0;j=ACC) k = 0; gl.glBegin(GL_QUAD_STRIP); for (j=0;j=ACC) k = 0; gl.glBegin(GL_QUAD_STRIP); gl.glNormal3f(normal_P[0][k][0], normal_P[0][k][1], normal_P[0][k][2]); gl.glVertex3f(char_P[0][k][0], char_P[0][k][1]+0.0f, char_P[0][k][2]); gl.glNormal3f(normal_P[0][i][0], normal_P[0][i][1], normal_P[0][i][2]); gl.glVertex3f(char_P[0][i][0], char_P[0][i][1]+0.0f, char_P[0][i][2]); for (j=1;j=ACC) k = 0; gl.glBegin(GL_QUAD_STRIP); for (j=0;j<=ACC3;j++) { gl.glNormal3f(normal_El[j][k][0], normal_El[j][k][1], normal_El[j][k][2]); gl.glVertex3f(char_El[j][k][0], char_El[j][k][1], char_El[j][k][2]); gl.glNormal3f(normal_El[j][i][0], normal_El[j][i][1], normal_El[j][i][2]); gl.glVertex3f(char_El[j][i][0], char_El[j][i][1], char_El[j][i][2]); } gl.glEnd(); } } void draw_N() { int i,j,k; for (i=0;i=ACC) k = 0; gl.glBegin(GL_QUAD_STRIP); for (j=0;j<=ACC2/2;j++) { gl.glNormal3f(normal_P[j][k][0], normal_P[j][k][1], normal_P[j][k][2]); gl.glVertex3f(char_P[j][k][0], char_P[j][k][1], char_P[j][k][2]); gl.glNormal3f(normal_P[j][i][0], normal_P[j][i][1], normal_P[j][i][2]); gl.glVertex3f(char_P[j][i][0], char_P[j][i][1], char_P[j][i][2]); } gl.glEnd(); } j = 0; gl.glBegin(GL_QUAD_STRIP); for (i=0;i=ACC) k = 0; gl.glBegin(GL_QUAD_STRIP); gl.glNormal3f(normal_G[0][k][0], normal_G[0][k][1], normal_G[0][k][2]); gl.glVertex3f(char_G[0][k][0], char_G[0][k][1]+1.2f, char_G[0][k][2]); gl.glNormal3f(normal_G[0][i][0], normal_G[0][i][1], normal_G[0][i][2]); gl.glVertex3f(char_G[0][i][0], char_G[0][i][1]+1.2f, char_G[0][i][2]); for (j=1;j=0;i--) gl.glVertex3f(5.6f, 0.9f+0.9f*accSIN[i], 0.9f*accCOS[i]); gl.glVertex3f(5.6f, 0.9f+0.9f*accSIN[ACC-1], 0.9f*accCOS[ACC-1]); gl.glEnd(); } void draw_part(int i) { gl.glPushMatrix(); gl.glTranslatef(TRANS[i][0]*progress, TRANS[i][1]*progress, TRANS[i][2]*progress); gl.glRotatef(ROT[i]*progress, ROTAXIS[i][0], ROTAXIS[i][1], ROTAXIS[i][2]); switch(i) { case 0: draw_El(); break; case 1: draw_O(); break; case 2: draw_P(); break; case 3: draw_E(); break; case 4: draw_N(); break; case 5: draw_G(); break; case 6: draw_L(); break; } gl.glPopMatrix(); } void draw_logo() { groen_texture(); gl.glEnable(GL_CULL_FACE); gl.glTranslatef(-2.8f, 0.0f, 0.0f); draw_part(0); gl.glTranslatef(-12.0f, 4.3f, 0.0f); draw_part(1); gl.glTranslatef(7.3f, 0.0f, 0.0f); draw_part(2); gl.glTranslatef(5.4f, 0.0f, 0.0f); draw_part(3); gl.glTranslatef(5.4f, 0.0f, 0.0f); draw_part(4); gl.glTranslatef(7.4f, 0.0f, 0.0f); draw_part(5); gl.glTranslatef(6.8f, 0.0f, 0.0f); draw_part(6); } void go() { GLCapabilities caps = new GLCapabilities(); /* caps.setDoubleBuffered(true); caps.setTrueColor(true); caps.setStereo(false); caps.setDepthBits(32); caps.setRedBits(5); caps.setGreenBits(5); caps.setBlueBits(5); */ GLAnimCanvas canvas = GLDrawableFactory.getFactory().createGLAnimCanvas(caps, 400, 400); if (canvas == null) { throw new RuntimeException("Unable to match required GLCapabilities"); } canvas.addGLEventListener(this); canvas.start(); Frame frame = new Frame(); frame.add(canvas); frame.setSize(400, 400); frame.pack(); frame.show(); } public static void main(String[] args) { GLContext.gljNativeDebug = true; GLContext.gljThreadDebug = false; GLContext.gljClassDebug = true; new TestListener().go(); } }