home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-01-30 | 4.4 KB | 179 lines |
- /*
- * @(#)HawkRay.java 1.0 98/04/12
- *
- * Copyright (c) 1998, David Griffiths. All Rights Reserved.
- *
- * This software is the proprietary information of David Griffiths.
- * This source code may not be published or redistributed without the
- * express permission of the author.
- *
- * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
- * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- */
-
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
- import java.awt.image.PixelGrabber;
- import java.awt.image.MemoryImageSource;
-
- public class HawkRay extends Applet implements Runnable {
- protected int w, h, dispx = 0, dispz = 0, m_sw, m_sh, texel[], m_cos1, m_sin1, xres, yres;
- protected int m_alti = 120, d = 1, viewA = 180, dViewA = 3;
- protected int screenPixel[];
- protected MemoryImageSource screenMem;
- protected Image screenImage;
-
- private int garbageCnt = 0;
- private Thread theThread;
- private boolean m_tAnimate = true;
-
- // Initialisation code
-
- public void init() {
- addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent e){
- m_tAnimate = !m_tAnimate;
- }
- });
-
- MediaTracker tracker = new MediaTracker(this);
- Image piccy = getImage(getDocumentBase(), getParameter("image"));
- m_sh = getSize().height << 2; m_sw = getSize().width;
- screenPixel = new int[m_sw * m_sh >> 2];
- screenMem = new MemoryImageSource(m_sw, m_sh >> 2, screenPixel, 0, m_sw);
- tracker.addImage(piccy, 0);
- try {
- tracker.waitForID(0);
- }
- catch(Exception e) {}
- w = piccy.getWidth(this); h = piccy.getHeight(this);
- texel = new int[w * h];
- PixelGrabber pixelGrabber = new PixelGrabber(piccy.getSource(),
- 0, 0, w, h, texel, 0, w);
- try {
- pixelGrabber.grabPixels();
- }
- catch(Exception e) {
- return;
- }
- piccy = null;
- }
-
- // Running section
-
- public void start() {
- (theThread = new Thread(this)).start();
- }
-
- public void stop() {
- if (theThread != null) {
- theThread = null;
- }
- }
-
- public void run() {
- Graphics myGraphics = getGraphics();
-
- // Initialize the timer
- Timer.sampleStart();
-
- while ((theThread != null) && m_tAnimate) {
-
- // Start of timed section
- Timer.start();
-
- if (viewA > 720)
- dViewA = -1;
- else if (viewA < 360)
- dViewA = 1;
- viewA += dViewA;
-
- renderImage();
-
- Thread.yield();
- paint(myGraphics);
-
- if (garbageCnt++ > 120) {
- System.gc ();
- garbageCnt = 0;
- }
-
- // End of timed section
- Timer.stop();
- }
-
- // Stop timing session and report results
- Timer.sampleEnd();
- Timer.report();
- }
-
- // Painting section
-
- public void update(Graphics g) {
- paint(g);
- }
-
- public void paint(Graphics g) {
- if (screenImage != null)
- g.drawImage(screenImage, 0, 0, this);
- }
-
- // Rendering section
-
- protected void renderImage() {
- int x1, y1, x2, y2, x3, y3, p = 0, xinc, yinc;
- int localW = w;
- int localH = h;
- int localSWM1 = m_sw - 1;
- int localSW = m_sw;
- int localDispX = dispx;
- int localDispZ = dispz;
- int ly = ((3 * m_sh) >> 2) + 1;
- int uy = m_sh;
- double localSH = (double)m_sh;
- int wb = w << 10;
- int hb = h << 10;
- int m_alti = (int)(240.0 - 120.0 * Math.sin((double)viewA * 0.0174432925));
- int m_sin1 = (int)((1 << 10) * Math.sin((double)viewA * 0.0174432925));
- int m_cos1 = (int)((1 << 10) * Math.cos((double)viewA * 0.0174432925));
- dispz += m_cos1 << 3;
- dispx += m_sin1 << 3;
- for (int y = ly; y <= uy; y++) {
-
- double xd1, yd1, yd2, xd2;
- yd1 = ((double)(y << 1) / localSH) - 1.0;
- yd2 = (double)((m_alti - yd1)) / yd1;
- xd2 = -(yd2 + 1) / 2;
-
- x1 = (int)(m_cos1 * xd2 + m_sin1 * yd2) + localDispX;
- y1 = (int)(-m_sin1 * xd2 + m_cos1 * yd2) + localDispZ;
-
- x2 = (int)(-m_cos1 * xd2 + m_sin1 * yd2) + localDispX;
- y2 = (int)(m_sin1 * xd2 + m_cos1 * yd2) + localDispZ;
-
- xinc = (x2 - x1) / localSWM1;
- yinc = (y2 - y1) / localSWM1;
- xinc = ((xinc % wb) + wb) % wb;
- yinc = ((yinc % hb) + hb) % hb;
-
- for (int x = 0; x < localSW; x++) {
- x3 = ((localW - x1) >> 10) % localW;
- if (x3 < 0)
- x3 += localW;
- y3 = (y1 >> 10) % localH;
- if (y3 < 0)
- y3 += localH;
- screenPixel[p++] = texel[y3 * localW + x3];
- x1 += xinc; y1 += yinc;
- }
- }
- screenImage = createImage(screenMem);
- }
- }
-