Skip to content

Commit

Permalink
switch workplace.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonvy committed Apr 10, 2012
1 parent b639027 commit 729fd53
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 103 deletions.
11 changes: 5 additions & 6 deletions src/ja3d/loaders/collada/DaeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import utils.XmlPath;

// Done
public class DaeArray extends DaeElement {
class DaeArray extends DaeElement {

String[] array;

Expand All @@ -16,16 +16,15 @@ public DaeArray(Element data, DaeDocument document) {
}

public String type() {
return data.getLocalName();
return data.getTagName();
}

@Override
protected boolean parseImplementation() {
array = parseStringArray(data);
String countString = XmlPath.attribute(data, ".@count[0]");
if (countString != null) {
// why it is parse integer?
int count = parseInt(countString);
String countXML = XmlPath.attribute(data, ".@count[0]");
if (countXML.length() > 0) {
int count = parseInt(countXML);
if (array.length < count) {
return false;
} else {
Expand Down
32 changes: 17 additions & 15 deletions src/ja3d/loaders/collada/DaeDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import utils.XmlPath;

// Done
public class DaeDocument {

private DaeVisualScene scene;
Expand All @@ -31,7 +32,8 @@ public DaeDocument(Document document, float units) {

// :version

float colladaUnit = 1; //parseFloat(XmlPath.evaluate(data, "asset[0].unit[0].@meter"));
String unitXML = XmlPath.attribute(data, "asset[0].unit[0].@meter");
float colladaUnit = Float.parseFloat(unitXML);

if (units > 0) {
unitScaleFactor = colladaUnit/units;
Expand Down Expand Up @@ -92,13 +94,13 @@ private void constructScenes() {
}
}
if (vsceneID != null && scene == null) {
// error
throw new IllegalStateException("cannot find scene with name " + vsceneID);
}
}

private String getLocalID(String path) {
if (path.charAt(0) == '#') {
return path.substring(1);
private String getLocalID(String url) {
if (url.charAt(0) == '#') {
return url.substring(1);
} else {
return null;
}
Expand All @@ -109,43 +111,43 @@ public DaeVisualScene scene() {
}

// getter and setter
public DaeArray findArray(String url) {
DaeArray findArray(String url) {
return arrays.get(getLocalID(url));
}

public void addArray(DaeArray o) {
void addArray(DaeArray o) {
arrays.put(o.id(), o);
}

public DaeSource findSource(String url) {
DaeSource findSource(String url) {
return sources.get(getLocalID(url));
}

public void addSource(DaeSource o) {
void addSource(DaeSource o) {
sources.put(o.id(), o);
}

public DaeVertices findVertices(String url) {
DaeVertices findVertices(String url) {
return vertices.get(getLocalID(url));
}

public void addVertices(DaeVertices o) {
void addVertices(DaeVertices o) {
vertices.put(o.id(), o);
}

public DaeGeometry findGeometry(String url) {
DaeGeometry findGeometry(String url) {
return geometries.get(getLocalID(url));
}

public void addGeometry(DaeGeometry o) {
void addGeometry(DaeGeometry o) {
geometries.put(o.id(), o);
}

public DaeNode findNode(String url) {
DaeNode findNode(String url) {
return nodes.get(getLocalID(url));
}

public void addNode(DaeNode o) {
void addNode(DaeNode o) {
nodes.put(o.id(), o);
}
//
Expand Down
27 changes: 14 additions & 13 deletions src/ja3d/loaders/collada/DaeElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import utils.XmlPath;

public abstract class DaeElement {
// Done
abstract class DaeElement {

protected DaeDocument document;
protected Element data;
Expand Down Expand Up @@ -33,6 +34,18 @@ protected boolean parseImplementation() {
return true;
}

protected final static String[] parseStringArray(Element element) {
return element.getTextContent().split(" ");
}

protected final static float parseFloat(String o) {
return Float.parseFloat(o);
}

protected final static int parseInt(String o) {
return Integer.parseInt(o);
}

public String id() {
if (_id == null) {
_id = XmlPath.attribute(data, ".@id[0]");
Expand All @@ -53,16 +66,4 @@ public String name() {
}
return _name;
}

protected static String[] parseStringArray(Element element) {
return element.getTextContent().split(" ");
}

protected static float parseFloat(String o) {
return Float.parseFloat(o);
}

protected static int parseInt(String o) {
return Integer.parseInt(o);
}
}
58 changes: 48 additions & 10 deletions src/ja3d/loaders/collada/DaeGeometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import ja3d.objects.Mesh;
import ja3d.resources.Geometry;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -12,9 +16,10 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import utils.ByteArray;
import utils.XmlPath;

public class DaeGeometry extends DaeElement {
class DaeGeometry extends DaeElement {

private List<DaeVertex> geometryVertices;
private List<DaePrimitive> primitives;
Expand All @@ -32,7 +37,9 @@ private void constructVertices() {

if (e != null) {
vertices = new DaeVertices(e, document);
document.addVertices(vertices);
if (vertices.id().length() > 0) {
document.addVertices(vertices);
}
}
}

Expand All @@ -42,17 +49,16 @@ protected boolean parseImplementation() {
parsePrimitives();

vertices.parse();
int numVertices = vertices.positions.numbers.size()/vertices.positions.stride;
int numVertices = vertices.positions.numbers.size() / vertices.positions.stride;
geometry = new Geometry();
geometryVertices = new ArrayList<DaeVertex>(numVertices);
int channels = 0;
for (DaePrimitive p : primitives) {
p.parse();
if (p.verticesEqual(vertices)) {
numVertices = geometryVertices.size();
channels |= p.fillGeometry(geometry, geometryVertices);
} else {

// Error, Vertices of another geometry can not be used
}
}

Expand Down Expand Up @@ -86,19 +92,51 @@ protected boolean parseImplementation() {

numVertices = geometryVertices.size();

// data
ByteArray data = new ByteArray();

// every attribute is a float of 4 bytes.
int numMappings = attributes.size();
data.setLength(4 * numMappings * numVertices);
data.setEndian(ByteOrder.LITTLE_ENDIAN);

for (DaeVertex vertex : geometryVertices) {
data.writeFloat(vertex.x);
data.writeFloat(vertex.y);
data.writeFloat(vertex.z);
if (vertex.normal != null) {
data.writeFloat(vertex.normal.x);
data.writeFloat(vertex.normal.y);
data.writeFloat(vertex.normal.z);
}
if (vertex.tangent != null) {
data.writeFloat(vertex.tangent.x);
data.writeFloat(vertex.tangent.y);
data.writeFloat(vertex.tangent.z);
data.writeFloat(vertex.tangent.w);
}
for (int j = 0; j < vertex.uvs.size(); j++) {
data.writeFloat(vertex.uvs.get(j));
}
}

geometry._vertexStreams[0].data = data;
geometry._numVertices = numVertices;
return true;
}
return false;
}

private void parsePrimitives() {
primitives = new ArrayList<DaePrimitive>();
NodeList children = XmlPath.element(data, ".mesh[0]").getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if (node instanceof Element) {
Element mesh = XmlPath.element(data, ".mesh");
if (mesh != null) {
NodeList children = mesh.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if (!(node instanceof Element)) {
continue;
}

Element child = (Element) node;
String localName = child.getTagName();
if ("polygons".equals(localName)
Expand Down
33 changes: 26 additions & 7 deletions src/ja3d/loaders/collada/DaeInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,46 @@

import utils.XmlPath;

public class DaeInput extends DaeElement {
// Done
class DaeInput extends DaeElement {

private String _semantic;
private String _source;
private int _offset;
private int _set;

public DaeInput(Element data, DaeDocument document) {
super(data, document);
}

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

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

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

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

public DaeSource prepareSource(int minComponents) {
Expand All @@ -34,6 +52,7 @@ public DaeSource prepareSource(int minComponents) {
source.parse();
if (source.numbers != null && source.stride >= minComponents) {
return source;
} else {
}
} else {
}
Expand Down
8 changes: 5 additions & 3 deletions src/ja3d/loaders/collada/DaePrimitive.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ private void parseIndices() {
String localName = data.getLocalName();
if ("polylist".equals(localName) || "polygons".equals(localName)) {
Element element = XmlPath.element(data, ".vcount[0]");
String[] array = parseStringArray(element);
for (String item : array) {
vcount.add(parseInt(item));
if (element != null) {
String[] array = parseStringArray(element);
for (String item : array) {
vcount.add(parseInt(item));
}
}
} else if ("triangles".equals(localName)) {
List<Element> pList = XmlPath.list(data, ".p");
Expand Down
6 changes: 3 additions & 3 deletions src/ja3d/loaders/collada/DaeSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ protected boolean parseImplementation() {
Element accessor = this.accessor();
if (accessor != null) {
String s = XmlPath.attribute(accessor, ".@source[0]");
DaeArray array = s == null ? null : document.findArray(s);
DaeArray array = document.findArray(s);
if (array != null) {
String countString = XmlPath.attribute(accessor, ".@count[0]");
if (countString != null) {
if (countString.length() > 0) {
int count = parseInt(countString);
String offsetString = XmlPath.attribute(accessor, ".@offset[0]");
String strideString = XmlPath.attribute(accessor, ".@stride[0]");
Expand All @@ -85,7 +85,7 @@ private int numValidParams(NodeList params) {
for (int i = 0; i < params.getLength(); i++) {
Element item = (Element) params.item(i);
String name = XmlPath.attribute(item, ".@name[0]");
if (name != null) {
if (name.length() > 0) {
res ++;
}
}
Expand Down
Loading

0 comments on commit 729fd53

Please sign in to comment.