use of com.ardor3d.renderer.state.TextureState in project energy3d by concord-consortium.
the class TriangleMeshLib method findGroups.
private static ArrayList<GroupData> findGroups(final Mesh mesh, final boolean redoNormal) {
final FloatBuffer vertexBuffer = mesh.getMeshData().getVertexBuffer();
vertexBuffer.rewind();
FloatBuffer normalBuffer = mesh.getMeshData().getNormalBuffer();
if (normalBuffer == null) {
normalBuffer = BufferUtils.createFloatBuffer(vertexBuffer.limit());
} else {
normalBuffer.rewind();
}
final TextureState textureState = (TextureState) mesh.getLocalRenderState(StateType.Texture);
FloatBuffer textureBuffer = mesh.getMeshData().getTextureBuffer(0);
if (textureBuffer != null) {
textureBuffer.rewind();
} else {
textureBuffer = BufferUtils.createFloatBuffer(vertexBuffer.limit() / 3 * 2);
}
final Vector3 v1 = new Vector3();
final Vector3 v2 = new Vector3();
final Vector3 normal = new Vector3();
final ArrayList<GroupData> groups = new ArrayList<GroupData>();
for (int i = 0; i < vertexBuffer.limit() / 9; i++) {
final Vector3 p1 = new Vector3(vertexBuffer.get(), vertexBuffer.get(), vertexBuffer.get());
final Vector3 p2 = new Vector3(vertexBuffer.get(), vertexBuffer.get(), vertexBuffer.get());
final Vector3 p3 = new Vector3(vertexBuffer.get(), vertexBuffer.get(), vertexBuffer.get());
p2.subtract(p1, v1);
p3.subtract(p1, v2);
v1.cross(v2, normal);
// as the vertex buffer can be relative to the node, apply the world transform to get the absolute normal
mesh.getWorldTransform().applyForwardVector(normal);
normal.normalizeLocal();
Vector3 firstNormal = null;
if (!redoNormal) {
firstNormal = new Vector3(normalBuffer.get(), normalBuffer.get(), normalBuffer.get());
if (Double.isNaN(firstNormal.length())) {
continue;
}
}
final GroupData group = new GroupData();
group.key.set(normal);
groups.add(group);
if (textureState != null && textureState.getTexture() != null) {
group.textureImage = textureState.getTexture().getImage();
}
group.vertices.add(p1);
group.vertices.add(p2);
group.vertices.add(p3);
if (redoNormal) {
group.normals.add(group.key.clone());
group.normals.add(group.key.clone());
group.normals.add(group.key.clone());
} else {
group.normals.add(firstNormal);
group.normals.add(new Vector3(normalBuffer.get(), normalBuffer.get(), normalBuffer.get()));
group.normals.add(new Vector3(normalBuffer.get(), normalBuffer.get(), normalBuffer.get()));
}
// texture is 2D, vertex is 3D
group.textures.add(new Vector2(textureBuffer.get(), textureBuffer.get()));
group.textures.add(new Vector2(textureBuffer.get(), textureBuffer.get()));
group.textures.add(new Vector2(textureBuffer.get(), textureBuffer.get()));
}
MeshLib.combineGroups(groups);
return groups;
}
use of com.ardor3d.renderer.state.TextureState in project energy3d by concord-consortium.
the class TriangleMeshLib method createMeshes.
private static List<Mesh> createMeshes(final ArrayList<GroupData> groups) {
final List<Mesh> results = new ArrayList<Mesh>();
for (final GroupData group : groups) {
final Mesh mesh = new Mesh();
mesh.setUserData(group.key);
mesh.setModelBound(new BoundingBox());
results.add(mesh);
final FloatBuffer vertexBuffer = BufferUtils.createVector3Buffer(group.vertices.size());
mesh.getMeshData().setVertexBuffer(vertexBuffer);
for (final ReadOnlyVector3 v : group.vertices) {
vertexBuffer.put(v.getXf()).put(v.getYf()).put(v.getZf());
}
final FloatBuffer normalBuffer = BufferUtils.createFloatBuffer(vertexBuffer.limit());
mesh.getMeshData().setNormalBuffer(normalBuffer);
for (final ReadOnlyVector3 v : group.normals) {
normalBuffer.put(v.getXf()).put(v.getYf()).put(v.getZf());
}
if (!group.textures.isEmpty()) {
final FloatBuffer textureBuffer = BufferUtils.createVector2Buffer(group.textures.size());
mesh.getMeshData().setTextureBuffer(textureBuffer, 0);
for (final ReadOnlyVector2 v : group.textures) {
textureBuffer.put(v.getXf()).put(v.getYf());
}
if (group.textureImage != null) {
final Texture texture = TextureManager.loadFromImage(group.textureImage, Texture.MinificationFilter.Trilinear, TextureStoreFormat.GuessNoCompressedFormat);
final TextureState ts = new TextureState();
ts.setTexture(texture);
mesh.setRenderState(ts);
}
}
mesh.updateModelBound();
}
return results;
}
use of com.ardor3d.renderer.state.TextureState in project energy3d by concord-consortium.
the class Foundation method updateTextureAndColor.
@Override
public void updateTextureAndColor() {
for (final Mesh mesh : sideMesh) {
mesh.setDefaultColor(Scene.getInstance().getTextureMode() == TextureMode.Full ? ColorRGBA.GRAY : (getColor() == null ? Scene.getInstance().getFoundationColor() : getColor()));
updateTextureAndColor(mesh, getColor() == null ? Scene.getInstance().getFoundationColor() : getColor());
}
updateTextureAndColor(mesh, getColor() == null ? Scene.getInstance().getFoundationColor() : getColor());
if (!SceneManager.getInstance().getSolarHeatMap() && importedNodes != null) {
final int n = importedNodes.size();
if (n > 0) {
Node ni;
for (int i = 0; i < n; i++) {
ni = importedNodes.get(i);
if (root.getChildren().contains(ni)) {
for (final Spatial s : ni.getChildren()) {
if (s instanceof Mesh) {
if (s instanceof Line) {
continue;
}
final Mesh m = (Mesh) s;
final UserData ud = (UserData) m.getUserData();
final TextureState ts = (TextureState) m.getLocalRenderState(StateType.Texture);
if (ts == null || ts.getTexture() == null) {
m.clearRenderState(StateType.Texture);
// m.setDefaultColor(nis.getDefaultColor());
} else {
if (ud.getTextureBuffer() == null || Util.isZero(ud.getTextureBuffer())) {
m.clearRenderState(StateType.Texture);
// m.setDefaultColor(nis.getDefaultColor());
} else {
m.getMeshData().setTextureBuffer(ud.getTextureBuffer(), 0);
m.setRenderState(ud.getRenderState());
}
}
}
}
}
}
}
}
}
use of com.ardor3d.renderer.state.TextureState in project energy3d by concord-consortium.
the class HousePart method updateTextureAndColor.
protected void updateTextureAndColor(final Mesh mesh, final ReadOnlyColorRGBA defaultColor, final TextureMode textureMode) {
if (this instanceof Tree) {
// special treatment because the same mesh of a tree has two textures (shed or not)
final TextureState ts = new TextureState();
final Texture texture = getTexture(getTextureFileName(), textureMode == TextureMode.Simple, defaultColor, lockEdit);
ts.setTexture(texture);
mesh.setRenderState(ts);
} else {
if (SceneManager.getInstance().getSolarHeatMap()) {
if (isDrawable()) {
if (this instanceof Foundation || this instanceof Wall || this instanceof Roof || this instanceof Floor) {
SolarRadiation.getInstance().initMeshTextureData(mesh, mesh, this instanceof Roof ? (ReadOnlyVector3) mesh.getParent().getUserData() : getNormal());
}
}
} else if (textureMode == TextureMode.None || getTextureFileName() == null) {
mesh.clearRenderState(StateType.Texture);
mesh.setDefaultColor(defaultColor);
} else {
final TextureState ts = new TextureState();
final Texture texture = getTexture(getTextureFileName(), textureMode == TextureMode.Simple, defaultColor, false);
ts.setTexture(texture);
mesh.setRenderState(ts);
mesh.setDefaultColor(ColorRGBA.WHITE);
}
}
}
Aggregations