Android Open Source - Schooner-3D Normals






From Project

Back to project page Schooner-3D.

License

The source code is released under:

Apache License

If you think the Android project Schooner-3D listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2012 Dan Mercer//from  w w  w.j a va2s .co m
 * 
 * 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 com.supermercerbros.gameengine.engine;

import java.util.Arrays;

import com.supermercerbros.gameengine.objects.GameObject;
import com.supermercerbros.gameengine.util.Utils;

public class Normals {

  public static void calculate(GameObject object) {
    if (object.normals == null || object.normals.length != object.verts.length) {
      throw new IllegalStateException("object.normals is not equal in length to object.verts");
    } else {
      Arrays.fill(object.normals, 0.0f);
    }
    final float[] normals = object.normals, verts = object.verts;
    final short[] indices = object.indices;
    final short[][] doubles = object.doubles;

    final float[] vectors = new float[9];

    for (int faceIndex = 0; faceIndex < indices.length; faceIndex += 3) {
      // For each face...
      
      final int index1 = indices[faceIndex + 1] * 3;
      final float cX = verts[index1    ];
      final float cY = verts[index1 + 1];
      final float cZ = verts[index1 + 2];

      final int index2 = indices[faceIndex + 2] * 3;
      vectors[0] = verts[index2    ] - cX;
      vectors[1] = verts[index2 + 1] - cY;
      vectors[2] = verts[index2 + 2] - cZ;

      final int index0 = indices[faceIndex    ] * 3;
      vectors[3] = verts[index0    ] - cX;
      vectors[4] = verts[index0 + 1] - cY;
      vectors[5] = verts[index0 + 2] - cZ;
      
      // Compute cross product
      vectors[6] = vectors[1] * vectors[5]
          - vectors[2] * vectors[4];
      vectors[7] = vectors[2] * vectors[3]
          - vectors[0] * vectors[5];
      vectors[8] = vectors[0] * vectors[4]
          - vectors[1] * vectors[3];
      
      // Normalize result
      float length = Utils.pythagF(vectors[6],
          vectors[7], vectors[8]);
      vectors[6] /= length;
      vectors[7] /= length;
      vectors[8] /= length;
      
      // Add result to normals of vertices of face      
      normals[index0    ] += vectors[6];
      normals[index0 + 1] += vectors[7];
      normals[index0 + 2] += vectors[8];
      
      normals[index1    ] += vectors[6];
      normals[index1 + 1] += vectors[7];
      normals[index1 + 2] += vectors[8];
      
      normals[index2    ] += vectors[6];
      normals[index2 + 1] += vectors[7];
      normals[index2 + 2] += vectors[8];
    }
    
    if (doubles != null) {
      for (int i = 0; i < doubles[0].length; i++) {
        int indexA = doubles[0][i] * 3, indexB = doubles[1][i] * 3;
        normals[indexB    ] = (normals[indexA    ] = normals[indexA    ] + normals[indexB    ]);
        normals[indexB + 1] = (normals[indexA + 1] = normals[indexA + 1] + normals[indexB + 1]);
        normals[indexB + 2] = (normals[indexA + 2] = normals[indexA + 2] + normals[indexB + 2]);
      }
    }
    
    for (int i = 0; i < normals.length / 3; i++) {
      float length = Utils.pythagF(normals[i * 3    ],
          normals[i * 3 + 1], normals[i * 3 + 2]);
      normals[i * 3    ] /= length; // Normalize our new vector components.
      normals[i * 3 + 1] /= length;
      normals[i * 3 + 2] /= length;
    }
  }
}




Java Source Code List

com.supermercerbros.gameengine.GameActivity.java
com.supermercerbros.gameengine.GameView.java
com.supermercerbros.gameengine.Schooner3D.java
com.supermercerbros.gameengine.TestActivity.java
com.supermercerbros.gameengine.TestMaterials.java
com.supermercerbros.gameengine.TestObjects.java
com.supermercerbros.gameengine.animation.AnimationData.java
com.supermercerbros.gameengine.animation.Keyframe.java
com.supermercerbros.gameengine.animation.MeshAnimation.java
com.supermercerbros.gameengine.armature.ActionData.java
com.supermercerbros.gameengine.armature.Action.java
com.supermercerbros.gameengine.armature.BinarySkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Bone.java
com.supermercerbros.gameengine.armature.SkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Skeleton.java
com.supermercerbros.gameengine.collision.Bounds.java
com.supermercerbros.gameengine.collision.Collider.java
com.supermercerbros.gameengine.collision.CollisionDetector.java
com.supermercerbros.gameengine.collision.Collision.java
com.supermercerbros.gameengine.collision.DebugListener.java
com.supermercerbros.gameengine.collision.Edge.java
com.supermercerbros.gameengine.collision.Face.java
com.supermercerbros.gameengine.collision.Feature.java
com.supermercerbros.gameengine.collision.Intersection.java
com.supermercerbros.gameengine.collision.Line.java
com.supermercerbros.gameengine.collision.LocalDistMinimum.java
com.supermercerbros.gameengine.collision.Matrix.java
com.supermercerbros.gameengine.collision.OnCollisionCheckFinishedListener.java
com.supermercerbros.gameengine.collision.Plane.java
com.supermercerbros.gameengine.collision.Point.java
com.supermercerbros.gameengine.collision.Polyhedron.java
com.supermercerbros.gameengine.collision.SphereBounds.java
com.supermercerbros.gameengine.collision.Vector.java
com.supermercerbros.gameengine.collision.Vertex.java
com.supermercerbros.gameengine.debug.JankCatcher.java
com.supermercerbros.gameengine.debug.LoopLog.java
com.supermercerbros.gameengine.engine.Camera.java
com.supermercerbros.gameengine.engine.DataPipe.java
com.supermercerbros.gameengine.engine.EGLContextLostHandler.java
com.supermercerbros.gameengine.engine.Engine.java
com.supermercerbros.gameengine.engine.GameRenderer.java
com.supermercerbros.gameengine.engine.Light.java
com.supermercerbros.gameengine.engine.Normals.java
com.supermercerbros.gameengine.engine.RenderData.java
com.supermercerbros.gameengine.engine.Scene.java
com.supermercerbros.gameengine.engine.Time.java
com.supermercerbros.gameengine.engine.shaders.Material.java
com.supermercerbros.gameengine.engine.shaders.Program.java
com.supermercerbros.gameengine.engine.shaders.ShaderLib.java
com.supermercerbros.gameengine.engine.shaders.Shader.java
com.supermercerbros.gameengine.engine.shaders.VertexModifier.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteDispatcher.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteListener.java
com.supermercerbros.gameengine.hud.CoordsConverter.java
com.supermercerbros.gameengine.hud.GameHud.java
com.supermercerbros.gameengine.hud.HudElement.java
com.supermercerbros.gameengine.material.CelShadedMaterial.java
com.supermercerbros.gameengine.material.TexturedMaterial.java
com.supermercerbros.gameengine.math.BezierCurve.java
com.supermercerbros.gameengine.math.Curve.java
com.supermercerbros.gameengine.math.MatrixUtils.java
com.supermercerbros.gameengine.math.Quaternion.java
com.supermercerbros.gameengine.motion.CurveMovement.java
com.supermercerbros.gameengine.motion.MovementData.java
com.supermercerbros.gameengine.motion.Movement.java
com.supermercerbros.gameengine.objects.AnimatedMeshObject.java
com.supermercerbros.gameengine.objects.BasicMaterial.java
com.supermercerbros.gameengine.objects.BonedObject.java
com.supermercerbros.gameengine.objects.GameObject.java
com.supermercerbros.gameengine.objects.Metadata.java
com.supermercerbros.gameengine.parsers.ConstantCurve.java
com.supermercerbros.gameengine.parsers.GameFactory.java
com.supermercerbros.gameengine.parsers.PreBoneData.java
com.supermercerbros.gameengine.parsers.PreObjectData.java
com.supermercerbros.gameengine.parsers.Sch3D.java
com.supermercerbros.gameengine.render.Compositor.java
com.supermercerbros.gameengine.shaders.ProgramSource.java
com.supermercerbros.gameengine.texture.BitmapTexture.java
com.supermercerbros.gameengine.texture.ETC1CompressedTexture.java
com.supermercerbros.gameengine.texture.Texture.java
com.supermercerbros.gameengine.util.BetterDataInputStream.java
com.supermercerbros.gameengine.util.DelayedRunnable.java
com.supermercerbros.gameengine.util.GLES2.java
com.supermercerbros.gameengine.util.IPO.java
com.supermercerbros.gameengine.util.LoopingThread.java
com.supermercerbros.gameengine.util.Toggle.java
com.supermercerbros.gameengine.util.Utils.java