1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| public class CubeChase extends Panel implements GLEventListener, Runnable {
GLU glu = new GLU(); GLCanvas canvas; Cube cube; float x = 0.0f; private int[] textures = new int[3]; private float[] lightAmbient = {0.5f, 0.5f, 0.5f, 1.0f}; private float[] lightDiffuse = {1.0f, 1.0f, 1.0f, 1.0f}; private float[] lightPosition = {0.0f, 0.0f, 0.0f, -6.0f}; public CubeChase() { GLCapabilities capabilities = new GLCapabilities(); canvas = GLDrawableFactory.getFactory().createGLCanvas(capabilities); canvas.addGLEventListener(this); setLayout(new BorderLayout()); add(canvas); Point3D centerPoint = new Point3D(0,0,0); cube = new Cube(centerPoint,2.0); }
private boolean loadGLTextures(GLAutoDrawable gldrawable) { TextureReader.Texture texture = null; try { texture = TextureReader.readTexture("data/glass.png"); } catch (IOException e) { return false; }
GL gl = gldrawable.getGL(); gl.glGenTextures(3, textures,0); gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 3, texture.getWidth(), texture.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, texture.getPixels());
return true; }
public void init(GLAutoDrawable drawable) { if (!loadGLTextures(drawable)) { System.out.println("Unable to load textures,Bailing!"); System.exit(0); } GL gl = drawable.getGL(); gl.glEnable(GL.GL_BLEND); gl.glDisable(GL.GL_DEPTH_TEST); gl.glEnable(GL.GL_LIGHTING); gl.glEnable(GL.GL_TEXTURE_2D); gl.glShadeModel(GL.GL_SMOOTH); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClearDepth(1.0); gl.glEnable(GL.GL_DEPTH_TEST); gl.glDepthFunc(GL.GL_LEQUAL); gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); gl.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, lightAmbient,0); gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, lightDiffuse,0); gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, lightPosition,0); gl.glEnable(GL.GL_LIGHT1); gl.glColor4f(1.0f, 1.0f, 1.0f, 0.5f); gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE); }
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0.0f,0.0f,-5.0f);
gl.glRotatef(x, 1.0f, 1.0f, 0.0f); gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]); Rectangle3D[] faces = cube.getFaces(); for ( int i=0; i<faces.length; i++) { gl.glBegin(GL.GL_QUADS); Point3D p1 = faces[i].getP1(); Point3D p2 = faces[i].getP2(); Point3D p3 = faces[i].getP3(); Point3D p4 = faces[i].getP4(); Point3D normal = findNormal(p3,p1); gl.glNormal3d(normal.x,normal.y,normal.z); gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3d(p1.x,p1.y,p1.z); gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3d(p2.x,p2.y,p2.z); gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3d(p3.x,p3.y,p3.z); gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3d(p4.x,p4.y,p4.z); gl.glEnd(); } } public static double dotProduct(Point3D p1, Point3D p2) { return (p1.x*p2.x)+(p1.y*p2.y)+(p1.y*p2.y); } public static Point3D findNormal (Point3D p1, Point3D p2) { double x = (p1.y * p2.z) - (p2.y * p1.z); double y = (p1.z * p2.x) - (p2.z * p1.x); double z = (p1.x * p2.y) - (p2.x * p1.y); return new Point3D(x,y,z); }
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL(); ; if (height == 0) height = 1; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0f, width / (height*1.0f), 0.1f, 100.0f); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); }
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { } public void run () { while (true) { try { Thread.sleep(5); } catch (InterruptedException e) { } x+=0.05f; canvas.repaint(0); } } public static void main ( String[] args ) { CubeChase cubeChase = new CubeChase(); Frame f = new Frame(); f.add(cubeChase); f.setBounds(50,50,400,300); f.setVisible(true); (new Thread(cubeChase)).run(); } } |