home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-01-30 | 4.2 KB | 161 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();
- while ((theThread != null) && m_tAnimate) {
- 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;
- }
- }
- }
-
- // 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() {
- double x1, y1, x2, y2, x3, y3, x4, y4, va, xinc, yinc;
- int p = 0;
- m_alti = (int)(240.0 - 120.0 * Math.sin((double)viewA * 3.1415926 / 180.0));
- m_sin1 = (int)((1 << 10) * Math.sin((double)viewA * 3.1415926 / 180.0));
- m_cos1 = (int)((1 << 10) * Math.cos((double)viewA * 3.1415926 / 180.0));
- va = (w * (viewA % 360) / 360);
- dispz += m_cos1 << 3;
- dispx += m_sin1 << 3;
- for (int y = ((3 * m_sh) >> 2) + 1; y <= m_sh; y++) {
- planePoint (0, y); x1 = xres; y1 = yres;
- planePoint (m_sw - 1, y); x2 = xres; y2 = yres;
- xinc = (x2 - x1) / (m_sw - 1);
- yinc = (y2 - y1) / (m_sw - 1);
- xinc = ((xinc % (w << 10)) + (w << 10)) % (w << 10);
- yinc = ((yinc % (h << 10)) + (h << 10)) % (h << 10);
- for (int x = 0; x < m_sw; x++) {
- setTexel(p++, (int)x1, (int)y1);
- x1 += xinc; y1 += yinc;
- }
- }
- screenImage = createImage(screenMem);
- }
-
- void setTexel(int p, int u, int v) {
- int x3 = modIt((w - u) >> 10, w);
- int y3 = modIt(v >> 10, h);
- screenPixel[p] = texel[y3 * w + x3];
- }
-
- int modIt(int a, int b) {
- int result = (a) % b;
- if (result < 0)
- result += b;
- return result;
- }
-
- protected void planePoint (int i, int j) {
- double x1, y1, y2, x2;
- y1 = ((double)(j << 1) / (double)m_sh) - 1.0;
- y2 = (double)(d * (m_alti - y1)) / y1;
- x1 = (double)(i - (m_sw >> 1)) / (double)m_sw;
- x2 = x1 * (y2 + d) / (double)d;
- xres = (int)(m_cos1 * x2 + m_sin1 * y2) + dispx;
- yres = (int)(-m_sin1 * x2 + m_cos1 * y2) + dispz;
- }
- }
-