/*
Copyright (c) 2004, Bradley Jones
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the nondot.org nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package org.nondot.mesh;
import java.awt.Color;
import java.util.*;
public class SimpleTriMeshBuilder implements MeshBuilder {
VertexDescription vertexDesc;
Vector vertices;
Vector indices;
Mesh mesh;
public SimpleTriMeshBuilder() {
vertexDesc = VertexDescription.createUntransformedVertexDescription();
}
public SimpleTriMeshBuilder(VertexDescription vd) {
vertexDesc = vd;
}
public void addFace(int v1, int v2, int v3) {
indices.add(new Integer(v1));
indices.add(new Integer(v2));
indices.add(new Integer(v3));
}
public void addVertex(float x, float y, float z, float w) {
Vertex v = vertexDesc.createVertex();
v.setPosition(x,y,z,w);
vertices.add(v);
}
public void addVertex(float x, float y, float z) {
Vertex v = vertexDesc.createVertex();
v.setPosition(x,y,z);
vertices.add(v);
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#endMesh()
*/
public void endMesh() {
Vertex[] vertVertices = new Vertex[vertices.size()];
for(int i = 0; i < vertices.size(); ++i) {
vertVertices[i] = (Vertex)vertices.get(i);
}
vertices = null;
int[] intIndices = new int[indices.size()];
for(int i = 0; i < indices.size(); ++i) {
intIndices[i] = ((Integer)indices.get(i)).intValue();
}
indices = null;
mesh = new SimpleTriMesh(vertVertices,intIndices);
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#getMesh()
*/
public Mesh getMesh() {
return mesh;
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#setDiffuseColor(java.awt.Color)
*/
public void setDiffuseColor(Color color) {
((Vertex)vertices.lastElement()).setDiffuseColor(color);
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#setNormal(float, float, float)
*/
public void setNormal(float x, float y, float z) {
((Vertex)vertices.lastElement()).setNormal(x,y,z);
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#setPointSize(float)
*/
public void setPointSize(float x) {
((Vertex)vertices.lastElement()).setPointSize(x);
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#setSpecularColor(java.awt.Color)
*/
public void setSpecularColor(Color color) {
((Vertex)vertices.lastElement()).setSpecularColor(color);
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#setTextureCoordinate(int, float, float, float, float)
*/
public void setTextureCoordinate(
int set,
float r,
float s,
float t,
float u) {
((Vertex)vertices.lastElement()).setTextureCoordinate(set,r,s,t,u);
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#setTextureCoordinate(int, float, float, float)
*/
public void setTextureCoordinate(int set, float r, float s, float t) {
((Vertex)vertices.lastElement()).setTextureCoordinate(set,r,s,t);
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#setTextureCoordinate(int, float, float)
*/
public void setTextureCoordinate(int set, float r, float s) {
((Vertex)vertices.lastElement()).setTextureCoordinate(set,r,s);
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#setTextureCoordinate(int, float)
*/
public void setTextureCoordinate(int set, float r) {
((Vertex)vertices.lastElement()).setTextureCoordinate(set,r);
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#startMesh()
*/
public void startMesh() {
vertices = new Vector();
indices = new Vector();
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#addTriFan(int[])
*/
public void addTriFan(int[] fan) {
Util.linearizeFan(this,fan);
}
/* (non-Javadoc)
* @see org.nondot.mesh.MeshBuilder#addTriStrip(int[])
*/
public void addTriStrip(int[] strip) {
Util.linearizeStrip(this,strip);
}
}
|