use of net.drewke.tdme.engine.model.Material in project tdme by andreasdr.
the class PrimitiveModel method setupConvexMeshModel.
/**
* Set up a convex mesh model
* @param model
*/
public static void setupConvexMeshModel(Model model) {
// material
Material material = new Material("tdme.primitive.material");
material.getAmbientColor().set(0.5f, 0.5f, 0.5f, 1.0f);
material.getDiffuseColor().set(1.0f, 0.5f, 0.5f, 0.5f);
material.getSpecularColor().set(0f, 0f, 0f, 1f);
model.getMaterials().put(material.getId(), material);
setupConvexMeshMaterial(model.getSubGroups(), material);
}
use of net.drewke.tdme.engine.model.Material in project tdme by andreasdr.
the class TMReader method read.
/**
* TDME model format reader
* @param path name
* @param file name
* @throws IOException
* @throws ModelIOException
* @return model
*/
public static Model read(String pathName, String fileName) throws IOException, ModelFileIOException {
InputStream is = null;
try {
is = FileSystem.getInstance().getInputStream(pathName, fileName);
// version major.minor = 1.0
String fileId = readString(is);
if (fileId == null || fileId.equals("TDME Model") == false) {
throw new ModelFileIOException("File is not a TDME model file, file id = '" + fileId + "'");
}
byte[] version = new byte[3];
version[0] = readByte(is);
version[1] = readByte(is);
version[2] = readByte(is);
if (version[0] != 1 || version[1] != 0 || version[2] != 0) {
throw new ModelFileIOException("Version mismatch, should be 1.0.0, but is " + version[0] + "." + version[1] + "." + version[2]);
}
// meta data
String name = readString(is);
// up vector, rotation order, bounding box
UpVector upVector = UpVector.valueOf(readString(is));
RotationOrder rotationOrder = RotationOrder.valueOf(readString(is));
BoundingBox boundingBox = new BoundingBox(new Vector3(readFloatArray(is)), new Vector3(readFloatArray(is)));
// create object
Model model = new Model(pathName + File.separator + fileName, fileName, upVector, rotationOrder, boundingBox);
// set additional data
model.setFPS(readFloat(is));
model.getImportTransformationsMatrix().set(readFloatArray(is));
// materials
int materialCount = readInt(is);
for (int i = 0; i < materialCount; i++) {
Material material = readMaterial(is);
model.getMaterials().put(material.getId(), material);
}
// sub groups
readSubGroups(is, model, null, model.getSubGroups());
//
return model;
} catch (IOException ioe) {
throw ioe;
} catch (ModelFileIOException mfioe) {
throw mfioe;
} finally {
if (is != null) {
is.close();
}
}
}
use of net.drewke.tdme.engine.model.Material in project tdme by andreasdr.
the class DAEReader method readMaterial.
/**
* Reads a material
* @param authoring tool
* @param path name
* @param model
* @param xml root
* @param xml node id
* @return material
* @throws Exception
*/
public static Material readMaterial(AuthoringTool authoringTool, String pathName, Model model, Element xmlRoot, String xmlNodeId) throws Exception {
// determine effect id
String xmlEffectId = null;
Element xmlLibraryMaterials = getChildrenByTagName(xmlRoot, "library_materials").get(0);
for (Element xmlMaterial : getChildrenByTagName(xmlLibraryMaterials, "material")) {
if (xmlMaterial.getAttribute("id").equals(xmlNodeId)) {
Element xmlInstanceEffect = getChildrenByTagName(xmlMaterial, "instance_effect").get(0);
xmlEffectId = xmlInstanceEffect.getAttribute("url").substring(1);
}
}
if (xmlEffectId == null) {
System.out.println("Could not determine effect id for '" + xmlNodeId + "'");
return null;
}
// parse effect
Material material = new Material(xmlNodeId);
String xmlDiffuseTextureId = null;
String xmlSpecularTextureId = null;
String xmlBumpTextureId = null;
Element xmlLibraryEffects = getChildrenByTagName(xmlRoot, "library_effects").get(0);
for (Element xmlEffect : getChildrenByTagName(xmlLibraryEffects, "effect")) {
if (xmlEffect.getAttribute("id").equals(xmlEffectId)) {
// diffuse texture
Element xmlProfile = getChildrenByTagName(xmlEffect, "profile_COMMON").get(0);
HashMap<String, String> samplerSurfaceMapping = new HashMap<String, String>();
HashMap<String, String> surfaceImageMapping = new HashMap<String, String>();
for (Element xmlNewParam : getChildrenByTagName(xmlProfile, "newparam")) {
String xmlNewParamSID = xmlNewParam.getAttribute("sid");
for (Element xmlSurface : getChildrenByTagName(xmlNewParam, "surface")) for (Element xmlSurfaceInitFrom : getChildrenByTagName(xmlSurface, "init_from")) {
surfaceImageMapping.put(xmlNewParamSID, xmlSurfaceInitFrom.getTextContent());
}
for (Element xmlSampler2D : getChildrenByTagName(xmlNewParam, "sampler2D")) for (Element xmlSampler2DSource : getChildrenByTagName(xmlSampler2D, "source")) {
samplerSurfaceMapping.put(xmlNewParamSID, xmlSampler2DSource.getTextContent());
}
}
//
for (Element xmlTechnique : getChildrenByTagName(xmlProfile, "technique")) {
NodeList xmlTechniqueNodes = xmlTechnique.getChildNodes();
for (int i = 0; i < xmlTechniqueNodes.getLength(); i++) {
Node xmlTechniqueNode = xmlTechniqueNodes.item(i);
// skip if not an element
if (xmlTechniqueNode.getNodeType() != Node.ELEMENT_NODE)
continue;
// diffuse
for (Element xmlDiffuse : getChildrenByTagName((Element) xmlTechniqueNode, "diffuse")) {
// texture
for (Element xmlTexture : getChildrenByTagName(xmlDiffuse, "texture")) {
xmlDiffuseTextureId = xmlTexture.getAttribute("texture");
String sample2Surface = samplerSurfaceMapping.get(xmlDiffuseTextureId);
String surface2Image = null;
if (sample2Surface != null)
surface2Image = surfaceImageMapping.get(sample2Surface);
if (surface2Image != null)
xmlDiffuseTextureId = surface2Image;
}
// color
for (Element xmlColor : getChildrenByTagName(xmlDiffuse, "color")) {
StringTokenizer t = new StringTokenizer(xmlColor.getTextContent(), " ");
material.getDiffuseColor().set(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()));
}
}
// ambient
for (Element xmlAmbient : getChildrenByTagName((Element) xmlTechniqueNode, "ambient")) {
// color
for (Element xmlColor : getChildrenByTagName(xmlAmbient, "color")) {
StringTokenizer t = new StringTokenizer(xmlColor.getTextContent(), " ");
material.getAmbientColor().set(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()));
}
}
// emission
for (Element xmlEmission : getChildrenByTagName((Element) xmlTechniqueNode, "emission")) {
// color
for (Element xmlColor : getChildrenByTagName(xmlEmission, "color")) {
StringTokenizer t = new StringTokenizer(xmlColor.getTextContent(), " ");
material.getEmissionColor().set(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()));
}
}
// specular
boolean hasSpecularMap = false;
boolean hasSpecularColor = false;
for (Element xmlSpecular : getChildrenByTagName((Element) xmlTechniqueNode, "specular")) {
// texture
for (Element xmlTexture : getChildrenByTagName(xmlSpecular, "texture")) {
xmlSpecularTextureId = xmlTexture.getAttribute("texture");
String sample2Surface = samplerSurfaceMapping.get(xmlSpecularTextureId);
String surface2Image = null;
if (sample2Surface != null)
surface2Image = surfaceImageMapping.get(sample2Surface);
if (surface2Image != null)
xmlSpecularTextureId = surface2Image;
hasSpecularMap = true;
}
// color
for (Element xmlColor : getChildrenByTagName(xmlSpecular, "color")) {
StringTokenizer t = new StringTokenizer(xmlColor.getTextContent(), " ");
material.getSpecularColor().set(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()));
hasSpecularColor = true;
}
}
// set up specular color if not yet done but spec maps is available
if (hasSpecularMap == true && hasSpecularColor == false) {
material.getSpecularColor().set(1f, 1f, 1f, 1f);
}
// shininess
for (Element xmlShininess : getChildrenByTagName((Element) xmlTechniqueNode, "shininess")) {
// color
for (Element xmlFloat : getChildrenByTagName(xmlShininess, "float")) {
material.setShininess(Float.parseFloat(xmlFloat.getTextContent()));
}
}
}
// bump / normal map
for (Element xmlBumpExtra : getChildrenByTagName(xmlTechnique, "extra")) for (Element xmlBumpTechnique : getChildrenByTagName(xmlBumpExtra, "technique")) for (Element xmlBumpTechniqueBump : getChildrenByTagName(xmlBumpTechnique, "bump")) for (Element xmlBumpTexture : getChildrenByTagName(xmlBumpTechniqueBump, "texture")) {
xmlBumpTextureId = xmlBumpTexture.getAttribute("texture");
String sample2Surface = samplerSurfaceMapping.get(xmlBumpTextureId);
String surface2Image = null;
if (sample2Surface != null)
surface2Image = surfaceImageMapping.get(sample2Surface);
if (surface2Image != null)
xmlBumpTextureId = surface2Image;
}
}
}
}
// diffuse texture
String xmlDiffuseTextureFilename = null;
if (xmlDiffuseTextureId != null) {
xmlDiffuseTextureFilename = getTextureFileNameById(xmlRoot, xmlDiffuseTextureId);
// do we have a file name
if (xmlDiffuseTextureFilename != null) {
xmlDiffuseTextureFilename = makeFileNameRelative(xmlDiffuseTextureFilename);
// add texture
material.setDiffuseTexture(pathName, xmlDiffuseTextureFilename);
}
}
// specular texture
String xmlSpecularTextureFilename = null;
if (xmlSpecularTextureId != null) {
xmlSpecularTextureFilename = getTextureFileNameById(xmlRoot, xmlSpecularTextureId);
// do we have a file name
if (xmlSpecularTextureFilename != null) {
xmlSpecularTextureFilename = makeFileNameRelative(xmlSpecularTextureFilename);
// add texture
material.setSpecularTexture(pathName, xmlSpecularTextureFilename);
}
}
// normal map
String xmlBumpTextureFilename = null;
if (xmlBumpTextureId != null) {
xmlBumpTextureFilename = getTextureFileNameById(xmlRoot, xmlBumpTextureId);
// do we have a file name
if (xmlBumpTextureFilename != null) {
xmlBumpTextureFilename = makeFileNameRelative(xmlBumpTextureFilename);
// add texture
material.setNormalTexture(pathName, xmlBumpTextureFilename);
}
}
// determine displacement map file name
String xmlDisplacementFilename = null;
// by diffuse file name
if (xmlDiffuseTextureFilename != null) {
xmlDisplacementFilename = determineDisplacementFilename(pathName, "diffuse", xmlDiffuseTextureFilename);
}
// by normal file name
if (xmlDisplacementFilename == null && xmlBumpTextureFilename != null) {
xmlDisplacementFilename = determineDisplacementFilename(pathName, "normal", xmlBumpTextureFilename);
}
// add texture
if (xmlDisplacementFilename != null) {
material.setDisplacementTexture(pathName, xmlDisplacementFilename);
}
// adjust ambient light with blender
if (authoringTool == AuthoringTool.BLENDER && material.getAmbientColor().equals(BLENDER_AMBIENT_NONE)) {
material.getAmbientColor().set(material.getDiffuseColor().getRed() * BLENDER_AMBIENT_FROM_DIFFUSE_SCALE, material.getDiffuseColor().getGreen() * BLENDER_AMBIENT_FROM_DIFFUSE_SCALE, material.getDiffuseColor().getBlue() * BLENDER_AMBIENT_FROM_DIFFUSE_SCALE, 1.0f);
material.getDiffuseColor().set(material.getDiffuseColor().getRed() * BLENDER_DIFFUSE_SCALE, material.getDiffuseColor().getGreen() * BLENDER_DIFFUSE_SCALE, material.getDiffuseColor().getBlue() * BLENDER_DIFFUSE_SCALE, material.getDiffuseColor().getAlpha());
}
// add material to library
model.getMaterials().put(material.getId(), material);
//
return material;
}
use of net.drewke.tdme.engine.model.Material in project tdme by andreasdr.
the class TMWriter method write.
/**
* TDME model format writer
* @param model
* @param path name
* @param file name
* @throws IOException
* @throws ModelIOException
*/
public static void write(Model model, String pathName, String fileName) throws IOException {
OutputStream os = null;
try {
os = FileSystem.getInstance().getOutputStream(pathName, fileName);
// version major.minor = 1.0
writeString(os, "TDME Model");
writeByte(os, (byte) 1);
writeByte(os, (byte) 0);
writeByte(os, (byte) 0);
// meta data
writeString(os, model.getName());
// up vector, rotation order, bounding box, ...
writeString(os, model.getUpVector().toString());
writeString(os, model.getRotationOrder().toString());
writeFloatArray(os, model.getBoundingBox().getMin().getArray());
writeFloatArray(os, model.getBoundingBox().getMax().getArray());
writeFloat(os, model.getFPS());
writeFloatArray(os, model.getImportTransformationsMatrix().getArray());
// materials
writeInt(os, model.getMaterials().size());
for (Material material : model.getMaterials().getValuesIterator()) {
writeMaterial(os, material);
}
// sub groups
writeSubGroups(os, model.getSubGroups());
} catch (IOException ioe) {
throw ioe;
} finally {
if (os != null) {
os.flush();
os.close();
}
}
}
use of net.drewke.tdme.engine.model.Material in project tdme by andreasdr.
the class WFObjReader method read.
/**
* Reads a wave front object file
* @param path name
* @param file name
* @return model
* @throws IOException
* @throws ModelIOException
*/
public static Model read(String pathName, String fileName) throws IOException, ModelFileIOException {
// create object
Model model = new Model(pathName + File.separator + fileName, fileName, UpVector.Y_UP, RotationOrder.XYZ, null);
ArrayList<Vector3> vertices = new ArrayList<Vector3>();
ArrayList<TextureCoordinate> textureCoordinates = new ArrayList<TextureCoordinate>();
HashMap<String, Material> materials = model.getMaterials();
HashMap<String, Group> subGroups = model.getSubGroups();
HashMap<String, Group> groups = model.getGroups();
// current group
Group group = null;
// model vertices -> group vertices mapping
HashMap<Integer, Integer> modelGroupVerticesMapping = null;
// model texture coordinates -> group texture coordinates mapping
HashMap<Integer, Integer> modelGroupTextureCoordinatesMapping = null;
// current group data
ArrayList<Face> groupFacesEntityFaces = null;
ArrayList<Vector3> groupVertices = null;
ArrayList<Vector3> groupNormals = null;
ArrayList<TextureCoordinate> groupTextureCoordinates = null;
// current group's faces entity
ArrayList<FacesEntity> groupFacesEntities = null;
FacesEntity groupFacesEntity = null;
//
DataInputStream inputStream = new DataInputStream(FileSystem.getInstance().getInputStream(pathName, fileName));
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
//
try {
//
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
// skip on comments
if (line.startsWith("#")) {
continue;
}
// determine index of first ' ' which will separate command from arguments
int commandEndIdx = line.indexOf(' ');
if (commandEndIdx == -1)
commandEndIdx = line.length();
// determine command
String command = line.substring(0, commandEndIdx).trim().toLowerCase();
// determine arguments if any exist
String arguments = command.length() + 1 > line.length() ? "" : line.substring(command.length() + 1);
// parse
if (command.equals("mtllib")) {
String materialFileName = arguments;
materials = WFObjReader.readMaterials(pathName, materialFileName);
} else if (command.equals("v")) {
StringTokenizer t = new StringTokenizer(arguments, " ");
float x = Float.parseFloat(t.nextToken());
float y = Float.parseFloat(t.nextToken());
float z = Float.parseFloat(t.nextToken());
// add vertex
vertices.add(new Vector3(x, y, z));
} else if (command.equals("vt")) {
StringTokenizer t = new StringTokenizer(arguments, " ");
float u = Float.parseFloat(t.nextToken());
float v = Float.parseFloat(t.nextToken());
textureCoordinates.add(new TextureCoordinate(u, v));
} else if (command.equals("f")) {
StringTokenizer t2;
StringTokenizer t = new StringTokenizer(arguments, " ");
int v0 = -1;
int v1 = -1;
int v2 = -1;
int vt0 = -1;
int vt1 = -1;
int vt2 = -1;
// parse vertex index 0, vertex texture index 0
t2 = new StringTokenizer(t.nextToken(), "/");
v0 = Integer.parseInt(t2.nextToken()) - 1;
if (t2.hasMoreTokens()) {
vt0 = Integer.parseInt(t2.nextToken()) - 1;
}
// parse vertex index 1, vertex texture index 1
t2 = new StringTokenizer(t.nextToken(), "/");
v1 = Integer.parseInt(t2.nextToken()) - 1;
if (t2.hasMoreTokens()) {
vt1 = Integer.parseInt(t2.nextToken()) - 1;
}
// parse vertex index 2, vertex texture index 2
t2 = new StringTokenizer(t.nextToken(), "/");
v2 = Integer.parseInt(t2.nextToken()) - 1;
if (t2.hasMoreTokens()) {
vt2 = Integer.parseInt(t2.nextToken()) - 1;
}
// check if triangulated
if (t.hasMoreTokens()) {
throw new ModelFileIOException("We only support triangulated meshes");
}
Integer mappedVertex = null;
// map v0 to group
mappedVertex = modelGroupVerticesMapping.get(v0);
if (mappedVertex == null) {
groupVertices.add(vertices.get(v0).clone());
v0 = groupVertices.size() - 1;
} else {
v0 = mappedVertex.intValue();
}
// map v1 to group
mappedVertex = modelGroupVerticesMapping.get(v1);
if (mappedVertex == null) {
groupVertices.add(vertices.get(v1).clone());
v1 = groupVertices.size() - 1;
} else {
v1 = mappedVertex.intValue();
}
// map v2 to group
mappedVertex = modelGroupVerticesMapping.get(v2);
if (mappedVertex == null) {
groupVertices.add(vertices.get(v2).clone());
v2 = groupVertices.size() - 1;
} else {
v2 = mappedVertex.intValue();
}
//
Integer mappedTextureCoordinate = null;
// map vt0 to group
mappedTextureCoordinate = modelGroupTextureCoordinatesMapping.get(vt0);
if (mappedTextureCoordinate == null) {
groupTextureCoordinates.add(textureCoordinates.get(vt0).clone());
vt0 = groupTextureCoordinates.size() - 1;
} else {
vt0 = mappedTextureCoordinate.intValue();
}
// map vt1 to group
mappedTextureCoordinate = modelGroupTextureCoordinatesMapping.get(vt1);
if (mappedTextureCoordinate == null) {
groupTextureCoordinates.add(textureCoordinates.get(vt1).clone());
vt1 = groupTextureCoordinates.size() - 1;
} else {
vt1 = mappedTextureCoordinate.intValue();
}
// map vt2 to group
mappedTextureCoordinate = modelGroupTextureCoordinatesMapping.get(vt2);
if (mappedTextureCoordinate == null) {
groupTextureCoordinates.add(textureCoordinates.get(vt2).clone());
vt2 = groupTextureCoordinates.size() - 1;
} else {
vt2 = mappedTextureCoordinate.intValue();
}
// compute vertex normal
Vector3[] faceVertexNormals = ModelHelper.computeNormals(new Vector3[] { groupVertices.get(v0), groupVertices.get(v1), groupVertices.get(v2) });
// store group normals
int n0 = groupNormals.size();
groupNormals.add(faceVertexNormals[0]);
int n1 = groupNormals.size();
groupNormals.add(faceVertexNormals[1]);
int n2 = groupNormals.size();
groupNormals.add(faceVertexNormals[2]);
// create face with vertex indices
// we only support triangulated faces
Face face = new Face(group, v0, v1, v2, n0, n1, n2);
if (vt0 != -1 && vt1 != -1 && vt2 != -1) {
// set optional texture coordinate index
face.setTextureCoordinateIndices(vt0, vt1, vt2);
}
groupFacesEntityFaces.add(face);
} else if (command.equals("g")) {
if (group != null) {
// current faces entity
if (groupFacesEntityFaces.isEmpty() == false) {
groupFacesEntity.setFaces(groupFacesEntityFaces);
groupFacesEntities.add(groupFacesEntity);
}
// group
group.setVertices(groupVertices);
group.setNormals(groupNormals);
group.setTextureCoordinates(groupTextureCoordinates);
group.setFacesEntities(groupFacesEntities);
group.determineFeatures();
}
StringTokenizer t = new StringTokenizer(arguments, " ");
String name = t.nextToken();
groupVertices = new ArrayList<Vector3>();
groupNormals = new ArrayList<Vector3>();
groupTextureCoordinates = new ArrayList<TextureCoordinate>();
groupFacesEntityFaces = new ArrayList<Face>();
group = new Group(model, null, name, name);
groupFacesEntity = new FacesEntity(group, name);
groupFacesEntities = new ArrayList<FacesEntity>();
modelGroupVerticesMapping = new HashMap<Integer, Integer>();
modelGroupTextureCoordinatesMapping = new HashMap<Integer, Integer>();
subGroups.put(name, group);
groups.put(name, group);
} else if (command.equals("usemtl")) {
if (group != null) {
// current faces entity
if (groupFacesEntityFaces.isEmpty() == false) {
groupFacesEntity.setFaces(groupFacesEntityFaces);
groupFacesEntities.add(groupFacesEntity);
}
// set up new one
groupFacesEntity = new FacesEntity(group, "#" + groupFacesEntities.size());
groupFacesEntityFaces = new ArrayList<Face>();
}
groupFacesEntity.setMaterial(materials.get(arguments));
} else {
// not supported
}
}
// finish last group
if (group != null) {
// current faces entity
if (groupFacesEntityFaces.isEmpty() == false) {
groupFacesEntity.setFaces(groupFacesEntityFaces);
groupFacesEntities.add(groupFacesEntity);
}
// group
group.setVertices(groupVertices);
group.setNormals(groupNormals);
group.setTextureCoordinates(groupTextureCoordinates);
group.setFacesEntities(groupFacesEntities);
group.determineFeatures();
}
} finally {
// close resouces
reader.close();
inputStream.close();
}
// prepare for indexed rendering
ModelHelper.prepareForIndexedRendering(model);
//
return model;
}
Aggregations