⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 model3d.java

📁 Java 3D Game SDK.老外做的.
💻 JAVA
字号:
/*
 * Model3D.java
 */

package org.java3dgamesdk.graphics;

import javax.media.j3d.*;
import javax.vecmath.*;

/**
 * Base class of all 3D models in the engine.
 *
 * @author  Norbert Nopper
 */
public abstract class Model3D {
    
    /**
     * x position of the model.
     */
    public double                           x;

    /**
     * y position of the model.
     */
    public double                           y;
    
    /**
     * z position of the model.
     */
    public double                           z;
    
    /**
     * Vector for translating the position.
     */
    private Vector3d                        position;

    /**
     * world x position of the model.
     */
    public double                           worldX;

    /**
     * world y position of the model.
     */
    public double                           worldY;
    
    /**
     * world z position of the model.
     */
    public double                           worldZ;
    
    /**
     * Vector for translating the world position.
     */
    private Vector3d                        worldPosition;
        
    /**
     * x axis rotation.
     */
    public double                           rotX;

    /**
     * y axis rotation.
     */
    public double                           rotY;
    
    /**
     * z axis rotation.
     */
    public double                           rotZ;
    
    /**
     * Complete rotation matrix.
     */
    private Matrix3d                        rotation;

    /**
     * world x axis rotation.
     */
    public double                           worldRotX;
    
    /**
     * world y axis rotation.
     */
    public double                           worldRotY;

    /**
     * world z axis rotation.
     */
    public double                           worldRotZ;
        
    /**
     * World rotation matrix.
     */
    private Matrix3d                        worldRotation;
        
    /**
     * Scale of the model.
     */
    public double                           scale;
    
    /**
     * Object for transforming.
     */
    private Transform3D                     transformOne;

    /**
     * Object for the transforming.
     */
    private Transform3D                     transformTwo;

    /**
     * Object for transforming.
     */
    private Transform3D                     transformThree;
        
    /**
     * Constructor initializes the object with the default values.
     */
    public Model3D() {
        
        this.transformOne = new Transform3D();
        this.transformTwo = new Transform3D();
        this.transformThree = new Transform3D();
        
        this.x = 0.0;
        this.y = 0.0;
        this.z = 0.0;
        this.position = new Vector3d(x, y, z);
        
        this.rotX = 0.0;
        this.rotY = 0.0;
        this.rotZ = 0.0;
        this.rotation = new Matrix3d( ( Math.cos(rotY) * Math.cos(rotZ) ), ( - Math.cos(rotY) * Math.sin(rotZ) ), ( Math.sin(rotY) ),
        ( Math.cos(rotZ) * Math.sin(rotX) * Math.sin(rotY) + Math.cos(rotX) * Math.sin(rotZ) ), ( Math.cos(rotX) * Math.cos(rotZ) - Math.sin(rotX) * Math.sin(rotY) * Math.sin(rotZ) ), ( - Math.cos(rotY) * Math.sin(rotX) ),
        ( - Math.cos(rotX) * Math.cos(rotZ) * Math.sin(rotY) + Math.sin(rotX) * Math.sin(rotZ) ), ( Math.cos(rotZ) * Math.sin(rotX) + Math.cos(rotX) * Math.sin(rotY) * Math.sin(rotZ) ), ( Math.cos(rotX) * Math.cos(rotY) )
        );

        this.scale = 1.0;
        
        
        this.worldX = 0.0;
        this.worldY = 0.0;
        this.worldZ = 0.0;
        this.worldPosition = new Vector3d(worldX, worldY, worldZ);
                
        this.worldRotX = 0.0;
        this.worldRotY = 0.0;
        this.worldRotZ = 0.0;
        this.worldRotation = new Matrix3d( ( Math.cos(worldRotY) * Math.cos(worldRotZ) ), ( - Math.cos(worldRotY) * Math.sin(worldRotZ) ), ( Math.sin(worldRotY) ),
        ( Math.cos(worldRotZ) * Math.sin(worldRotX) * Math.sin(worldRotY) + Math.cos(worldRotX) * Math.sin(worldRotZ) ), ( Math.cos(worldRotX) * Math.cos(worldRotZ) - Math.sin(worldRotX) * Math.sin(worldRotY) * Math.sin(worldRotZ) ), ( - Math.cos(worldRotY) * Math.sin(worldRotX) ),
        ( - Math.cos(worldRotX) * Math.cos(worldRotZ) * Math.sin(worldRotY) + Math.sin(worldRotX) * Math.sin(worldRotZ) ), ( Math.cos(worldRotZ) * Math.sin(worldRotX) + Math.cos(worldRotX) * Math.sin(worldRotY) * Math.sin(worldRotZ) ), ( Math.cos(worldRotX) * Math.cos(worldRotY) )
        );

    }
    
    /**
     * This method updates the model depending on the passed time. Generally
     * needed for animations.
     *
     * @param time the passed time in milliseconds
     */
    protected abstract void updateModel(long time);

    /**
     * This method renders the model.
     *
     * @param gc3D the graphics contect 3D to render on
     */
    protected abstract void drawModel(GraphicsContext3D gc3D);
    
    /**
     * This method calls the abstract method for updating the model.
     *
     * @param time the passed time in milliseconds
     */
    final public void update(long time) {
        updateModel(time);
    }
    
    /**
     * This method sets the current transformation and renders the model.
     *
     * @param gc3D the graphics contect 3D to render on
     */
    final public void draw(GraphicsContext3D gc3D) {

        transformOne.setIdentity();
        
        position.x = x;
        position.y = y;
        position.z = z;        
        transformOne.setTranslation(position);
        
        //
        
        transformTwo.setIdentity();
        
        rotation.m00 = ( Math.cos(rotY) * Math.cos(rotZ) );
        rotation.m01 = ( - Math.cos(rotY) * Math.sin(rotZ) );
        rotation.m02 = ( Math.sin(rotY) );
        rotation.m10 = ( Math.cos(rotZ) * Math.sin(rotX) * Math.sin(rotY) + Math.cos(rotX) * Math.sin(rotZ) );
        rotation.m11 = ( Math.cos(rotX) * Math.cos(rotZ) - Math.sin(rotX) * Math.sin(rotY) * Math.sin(rotZ) );
        rotation.m12 = ( - Math.cos(rotY) * Math.sin(rotX) );
        rotation.m20 = ( - Math.cos(rotX) * Math.cos(rotZ) * Math.sin(rotY) + Math.sin(rotX) * Math.sin(rotZ) );
        rotation.m21 = ( Math.cos(rotZ) * Math.sin(rotX) + Math.cos(rotX) * Math.sin(rotY) * Math.sin(rotZ) );
        rotation.m22 = ( Math.cos(rotX) * Math.cos(rotY) );
        transformTwo.setRotation(rotation);

        worldPosition.x = worldX;
        worldPosition.y = worldY;
        worldPosition.z = worldZ;
        transformTwo.setTranslation(worldPosition);
        
        //
        
        transformThree.setIdentity();
                
        worldRotation.m00 = ( Math.cos(worldRotY) * Math.cos(worldRotZ) );
        worldRotation.m01 = ( - Math.cos(worldRotY) * Math.sin(worldRotZ) );
        worldRotation.m02 = ( Math.sin(worldRotY) );
        worldRotation.m10 = ( Math.cos(worldRotZ) * Math.sin(worldRotX) * Math.sin(worldRotY) + Math.cos(worldRotX) * Math.sin(worldRotZ) );
        worldRotation.m11 = ( Math.cos(worldRotX) * Math.cos(worldRotZ) - Math.sin(worldRotX) * Math.sin(worldRotY) * Math.sin(worldRotZ) );
        worldRotation.m12 = ( - Math.cos(worldRotY) * Math.sin(worldRotX) );
        worldRotation.m20 = ( - Math.cos(worldRotX) * Math.cos(worldRotZ) * Math.sin(worldRotY) + Math.sin(worldRotX) * Math.sin(worldRotZ) );
        worldRotation.m21 = ( Math.cos(worldRotZ) * Math.sin(worldRotX) + Math.cos(worldRotX) * Math.sin(worldRotY) * Math.sin(worldRotZ) );
        worldRotation.m22 = ( Math.cos(worldRotX) * Math.cos(worldRotY) );
        transformThree.setRotation(worldRotation);

        //
        
        transformTwo.mul(transformOne);
        transformThree.mul(transformTwo);
        
        transformThree.setScale(scale);
                
        gc3D.setModelTransform(transformThree);
        
        drawModel(gc3D);        
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -