Package edu.worcester.subter

Source Code of edu.worcester.subter.Subter

/*
* Copyright 2010 Ian Bollinger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package edu.worcester.subter;

import javax.media.j3d.AmbientLight;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.PointLight;
import javax.media.j3d.SpotLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;

import edu.worcester.subter.j3d.Transforms;

public final class Subter {
    private static final int BOUNDING_RADIUS = 100;
    private static final double BACK_CLIP_DISTANCE = 20.0;
    private static final double FRONT_CLIP_DISTANCE = 0.05;
    private static final double FIELD_OF_VIEW = Math.toRadians(90.0);

    public static void main(String[] args) {
        final MazeBuilder mazeManager = new MazeBuilder(
                "src/main/resources/maze.txt");
        new Subter(mazeManager);
    }

    private final SimpleUniverse simpleUniverse;
    private final MazeBuilder mazeManager;
    private final BranchGroup sceneBranchGroup;
    private final BoundingSphere bounds = new BoundingSphere(new Point3d(0, 0, 0), BOUNDING_RADIUS);

    public Subter(MazeBuilder newMazeManager) {
        super();
        mazeManager = newMazeManager;
        simpleUniverse = new SimpleUniverse();
        prepareViewpoint();
        initializeViewPosition();
        sceneBranchGroup = new BranchGroup();
        setKeyBehavior();
        simpleUniverse.addBranchGraph(createSceneGraph());
    }

    private BranchGroup createSceneGraph() {
        sceneBranchGroup.addChild(new TexturedFloor());
        sceneBranchGroup.addChild(mazeManager.getMaze());
        final AmbientLight light = new AmbientLight();
        light.setInfluencingBounds(bounds);
        sceneBranchGroup.addChild(light);
        sceneBranchGroup.compile();
        return sceneBranchGroup;
    }

    private void initializeViewPosition() {
        final TransformGroup steerTransformGroup = simpleUniverse
                .getViewingPlatform().getViewPlatformTransform();
        final Transform3D transform = Transforms
                .getTransform(steerTransformGroup);
        transform.mul(Transforms.rotY(-Math.PI));
        transform.setTranslation(mazeManager.getStartPosition());
        steerTransformGroup.setTransform(transform);
    }

    private void prepareViewpoint() {
        final View userView = simpleUniverse.getViewer().getView();
        userView.setFieldOfView(FIELD_OF_VIEW);
        userView.setBackClipDistance(BACK_CLIP_DISTANCE);
        userView.setFrontClipDistance(FRONT_CLIP_DISTANCE);
    }

    private void setKeyBehavior() {
        final ViewingPlatform vp = simpleUniverse.getViewingPlatform();
        final KeyBehavior keyBehavior = new KeyBehavior(mazeManager.getMaze());
        keyBehavior.setSchedulingBounds(bounds);
        vp.setViewPlatformBehavior(keyBehavior);
    }
}
TOP

Related Classes of edu.worcester.subter.Subter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.