Skip to content

Commit

Permalink
switch workplace.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonvy committed Apr 12, 2012
1 parent a424ca9 commit 40f91e9
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 72 deletions.
5 changes: 5 additions & 0 deletions src/ja3d/collisions/EllipsoidCollider.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class EllipsoidCollider {
private Transform3D matrix = new Transform3D();
private Transform3D inverseMatrix = new Transform3D();

// when preparing, object.collectGeometry will invoke this.addGeometry to fill this.
// After collision detected, these pools will be cleared.
private List<Geometry> geometries = new ArrayList<Geometry>();
private List<Transform3D> transforms = new ArrayList<Transform3D>();

Expand Down Expand Up @@ -209,6 +211,9 @@ private void prepare(Vector3D source, Vector3D displacement,
// Offset by number of vertices
mapOffset += geometry.getNumVertices();
}

geometries.clear();
transforms.clear();
}

public boolean getCollision(Vector3D source, Vector3D displacement,
Expand Down
2 changes: 1 addition & 1 deletion src/ja3d/core/Object3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class Object3D {

private String name;
private boolean visible;
// private boolean visible;

private BoundBox boundBox;

Expand Down
7 changes: 0 additions & 7 deletions src/ja3d/core/Resource.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
package ja3d.core;

public class Resource {

public Boolean isUploaded() {
return false;
}

public void dispose() {
}
}
14 changes: 7 additions & 7 deletions src/ja3d/loaders/collada/DaeElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class DaeElement {

// cached attributes, if not set in the element, the value is empty instead of null.
private String _id;
private String _sid;
// private String _sid;
private String _name;

// -1 - not parsed, 0 - parsed with error, 1 - parsed without error.
Expand Down Expand Up @@ -62,12 +62,12 @@ public String id() {
return _id;
}

public String sid() {
if (_sid == null) {
_sid = XmlPath.attribute(data, ".@sid[0]");
}
return _sid;
}
// public String sid() {
// if (_sid == null) {
// _sid = XmlPath.attribute(data, ".@sid[0]");
// }
// return _sid;
// }

public String name() {
if (_name == null) {
Expand Down
10 changes: 6 additions & 4 deletions src/ja3d/loaders/collada/DaeGeometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ja3d.resources.Geometry;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.w3c.dom.Element;
Expand Down Expand Up @@ -44,7 +45,7 @@ protected boolean parseImplementation() {
parsePrimitives();

vertices.parse();
int numVertices = vertices.positions.numbers.size() / vertices.positions.stride;
int numVertices = vertices.getNumVertices();
geometry = new Geometry();
geometryVertices = new ArrayList<DaeVertex>(numVertices);
int channels = 0;
Expand Down Expand Up @@ -94,9 +95,9 @@ protected boolean parseImplementation() {
ByteArray data = new ByteArray(4 * numMappings * numVertices);

for (DaeVertex vertex : geometryVertices) {
data.writeFloat(vertex.x);
data.writeFloat(vertex.y);
data.writeFloat(vertex.z);
data.writeFloat(vertex.position.x);
data.writeFloat(vertex.position.y);
data.writeFloat(vertex.position.z);
if (vertex.normal != null) {
data.writeFloat(vertex.normal.x);
data.writeFloat(vertex.normal.y);
Expand Down Expand Up @@ -145,6 +146,7 @@ private void parsePrimitives() {
}
}
}
primitives = Collections.unmodifiableList(primitives);
}

// Creates geometry and returns it as mesh.
Expand Down
16 changes: 8 additions & 8 deletions src/ja3d/loaders/collada/DaeInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DaeInput extends DaeElement {
private String _semantic;
private String _source;
private int _offset = -1;
private int _set = -1;
// private int _set = -1;

public DaeInput(Element data, DaeDocument document) {
super(data, document);
Expand Down Expand Up @@ -38,13 +38,13 @@ public int offset() {
return _offset;
}

public int setNum() {
if (_set < 0) {
String o = XmlPath.attribute(data, ".@set[0]");
_set = o.length() > 0 ? parseInt(o) : 0;
}
return _set;
}
// public int setNum() {
// if (_set < 0) {
// String o = XmlPath.attribute(data, ".@set[0]");
// _set = o.length() > 0 ? parseInt(o) : 0;
// }
// return _set;
// }

public DaeSource prepareSource(int minComponents) {
DaeSource source = document.findSource(this.source());
Expand Down
39 changes: 21 additions & 18 deletions src/ja3d/loaders/collada/DaePrimitive.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ja3d.resources.Geometry;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.w3c.dom.Element;
Expand All @@ -13,19 +14,20 @@ class DaePrimitive extends DaeElement {

public static final int NORMALS = 1;
public static final int TANGENT4 = 2;
public static final int[] TEXCOORDS ={8, 16, 32, 64, 128, 256, 512, 1024};
public static final int[] TEXCOORDS = {8, 16, 32, 64, 128, 256, 512, 1024};

private DaeInput verticesInput;
private List<DaeInput> texCoordsInputs;
private DaeInput normalsInput;
private List<DaeInput> biNormalsInputs;
private List<DaeInput> tangentsInputs;

private List<Integer> indices;
private int[] indices;
// Indices are organized in groups, each group has inputsStride items.
private int inputsStride;

int indexBegin;
int numTriangles;
private int indexBegin;
private int numTriangles;

public DaePrimitive(Element data, DaeDocument document) {
super(data, document);
Expand Down Expand Up @@ -72,7 +74,6 @@ private void parseInputs() {
}

private void parseIndices() {
indices = new ArrayList<Integer>();
List<Integer> vcount = new ArrayList<Integer>();

String localName = data.getTagName();
Expand All @@ -87,12 +88,15 @@ private void parseIndices() {
// we do not accept other formats.
throw new IllegalStateException();

} else if ("triangles".equals(localName)) {
} else

if ("triangles".equals(localName)) {
List<Element> pList = XmlPath.list(data, ".p");
for (Element element : pList) {
String[] array = parseStringArray(element);
for (String item : array) {
indices.add(parseInt(item));
indices = new int[array.length];
for (int i = 0; i < array.length; i++) {
indices[i] = parseInt(array[i]);
}
// if (!vcount.isEmpty()) {
// indices = triangulate(indices, vcount);
Expand Down Expand Up @@ -146,7 +150,7 @@ public int fillGeometry(Geometry geometry, List<DaeVertex> vertices) {
}
verticesInput.parse();

int numIndices = indices.size();
int numIndices = indices.length;

DaeVertices daeVertices = document.findVertices(verticesInput.source());
if (daeVertices == null) {
Expand Down Expand Up @@ -206,7 +210,7 @@ public int fillGeometry(Geometry geometry, List<DaeVertex> vertices) {
// Make geometry data
indexBegin = geometry._indices.size();
for (int i = 0; i < numIndices; i+= inputsStride) {
int index = indices.get(i + mainInput.offset());
int index = indices[i + mainInput.offset()];

DaeVertex vertex = index < vertices.size() ? vertices.get(index) : null;
// not exist
Expand All @@ -220,28 +224,27 @@ public int fillGeometry(Geometry geometry, List<DaeVertex> vertices) {
index = vertices.size() - 1;
}

vertex.vertexInIndex = indices.get(i + verticesInput.offset());
vertex.vertexInIndex = indices[i + verticesInput.offset()];
vertex.addPosition(positionSource.numbers, vertex.vertexInIndex, positionSource.stride, document.unitScaleFactor);

if (normalSource != null) {
vertex.addNormal(normalSource.numbers, indices.get(i + normalsInput.offset()), normalSource.stride);
vertex.addNormal(normalSource.numbers, indices[i + normalsInput.offset()], normalSource.stride);
}

if (tangentSource != null) {
vertex.addTangentBiDirection(
tangentSource.numbers, indices.get(i + tangentsInputs.get(0).offset()), tangentSource.stride,
binormalSource.numbers, indices.get(i + biNormalsInputs.get(0).offset()), binormalSource.stride
tangentSource.numbers, indices[i + tangentsInputs.get(0).offset()], tangentSource.stride,
binormalSource.numbers, indices[i + biNormalsInputs.get(0).offset()], binormalSource.stride
);
}

for (int j = 0; j < textureSources.size(); j++) {
vertex.appendUV(textureSources.get(j).numbers, indices.get(i + texCoordsInputs.get(j).offset()), textureSources.get(j).stride);
vertex.appendUV(textureSources.get(j).numbers, indices[i + texCoordsInputs.get(j).offset()], textureSources.get(j).stride);
}
}

vertex.vertexOutIndex = index;
geometry._indices.add(index);

}

numTriangles = (geometry._indices.size() - indexBegin)/3;
Expand All @@ -250,9 +253,9 @@ public int fillGeometry(Geometry geometry, List<DaeVertex> vertices) {

// check whether the given vertex is equal to the vertex starting from index in indices/
// Assume both vertices have the same offsets.
private boolean isEqual(DaeVertex vertex, List<Integer> indices, int index, List<Integer> offsets) {
private boolean isEqual(DaeVertex vertex, int[] indices, int index, List<Integer> offsets) {
for (int j = 0; j < offsets.size(); j++) {
if (vertex.indices.get(j) != indices.get(index + offsets.get(j))) {
if (vertex.indices.get(j) != indices[index + offsets.get(j)]) {
return false;
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/ja3d/loaders/collada/DaeSource.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ja3d.loaders.collada;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
Expand All @@ -17,7 +16,7 @@ class DaeSource extends DaeElement {
// private static final String NAME_ARRAY = "Name_array";

// union {
List<Double> numbers;
double[] numbers;
// List<Integer> ints;
// List<String> names;
// }
Expand Down Expand Up @@ -97,13 +96,12 @@ private int numValidParams(List<Element> params) {
// Assumed no such items, the method just parse the array and output the items in float type.
private int parseArray(int offset, int count, int stride, String[] array, String type) {
List<Element> params = XmlPath.list(this.accessor(), ".param");
assert(numValidParams(params) != stride);
assert(numValidParams(params) == stride);

if (FLOAT_ARRAY.equals(type)) {
numbers = new ArrayList<Double>(stride * count);
for (String item : array) {
double v = parseFloat(item);
numbers.add(v);
numbers = new double[stride * count];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = parseFloat(array[offset + i]);
}
} else {
throw new IllegalStateException();
Expand Down
39 changes: 19 additions & 20 deletions src/ja3d/loaders/collada/DaeVertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,32 @@ class DaeVertex {

List<Integer> indices = new ArrayList<Integer>();

double x;
double y;
double z;
Vector3D position;

List<Double> uvs = new ArrayList<Double>();

Vector3D normal;
Vector3D tangent;

public void addPosition(List<Double> data, int dataIndex, int stride, float unitScaleFactor) {
public void addPosition(double[] data, int dataIndex, int stride, float unitScaleFactor) {
assert(stride == 3);
indices.add(dataIndex);
int offset = stride * dataIndex;
x = data.get(offset) * unitScaleFactor;
y = data.get(offset + 1) * unitScaleFactor;
z = data.get(offset + 2) * unitScaleFactor;
position = new Vector3D();
position.x = data[offset + 0] * unitScaleFactor;
position.y = data[offset + 1] * unitScaleFactor;
position.z = data[offset + 2] * unitScaleFactor;
}

public void addNormal(List<Double> data, int dataIndex, int stride) {
public void addNormal(double[] data, int dataIndex, int stride) {
assert(stride == 3);
indices.add(dataIndex);
int offset = stride * dataIndex;
normal = new Vector3D(data.get(offset), data.get(offset + 1), data.get(offset + 2));
normal = new Vector3D(data[offset], data[offset + 1], data[offset + 2]);
}

public void addTangentBiDirection(List<Double> tangentData, int tangentDataIndex, int tangentStride,
List<Double> biNormalData, int biNormalDataIndex, int biNormalStride) {
public void addTangentBiDirection(double[] tangentData, int tangentDataIndex, int tangentStride,
double[] biNormalData, int biNormalDataIndex, int biNormalStride) {

indices.add(tangentDataIndex);
indices.add(biNormalDataIndex);
Expand All @@ -47,15 +46,15 @@ public void addTangentBiDirection(List<Double> tangentData, int tangentDataIndex
int biNormalOffset = biNormalStride * biNormalDataIndex;

Vector3D biNormal = new Vector3D(
biNormalData.get(biNormalOffset),
biNormalData.get(biNormalOffset + 1),
biNormalData.get(biNormalOffset + 2)
biNormalData[biNormalOffset + 0],
biNormalData[biNormalOffset + 1],
biNormalData[biNormalOffset + 2]
);

tangent = new Vector3D(
tangentData.get(tangentOffset),
tangentData.get(tangentOffset + 1),
tangentData.get(tangentOffset + 2)
tangentData[tangentOffset + 0],
tangentData[tangentOffset + 1],
tangentData[tangentOffset + 2]
);

Vector3D cross = new Vector3D();
Expand All @@ -65,10 +64,10 @@ public void addTangentBiDirection(List<Double> tangentData, int tangentDataIndex
tangent.w = dot < 0 ? -1 : 1;
}

public void appendUV(List<Double> data, int dataIndex, int stride) {
public void appendUV(double[] data, int dataIndex, int stride) {
indices.add(dataIndex);
int offset = stride * dataIndex;
uvs.add(data.get(offset));
uvs.add(1 - data.get(offset + 1));
uvs.add(data[offset]);
uvs.add(1 - data[offset + 1]);
}
}
8 changes: 8 additions & 0 deletions src/ja3d/loaders/collada/DaeVertices.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ protected boolean parseImplementation() {

return false;
}

// How many vertices are in this pool?
public int getNumVertices() {
if (positions != null) {
return positions.numbers.length / positions.stride;
}
return 0;
}
}

0 comments on commit 40f91e9

Please sign in to comment.