Hi,
Is it possile to create a .obj file and write some Object3D in it ?
Thanks.
Is it possile to create a .obj file and write some Object3D in it ?
Thanks.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu
public Object3D createBox(final float width, final float depth,
final float height, final int textureId) {
final Object3D box = new Object3D(12);
final float x = width / 2;
final float y = depth / 2;
final float z = height / 2;
final int textureFactor = 64;
final SimpleVector upperLeftFront = new SimpleVector(-x, -y, -z);
final SimpleVector upperRightFront = new SimpleVector(x, -y, -z);
final SimpleVector lowerLeftFront = new SimpleVector(-x, y, -z);
final SimpleVector lowerRightFront = new SimpleVector(x, y, -z);
final SimpleVector upperLeftBack = new SimpleVector(-x, -y, z);
final SimpleVector upperRightBack = new SimpleVector(x, -y, z);
final SimpleVector lowerLeftBack = new SimpleVector(-x, y, z);
final SimpleVector lowerRightBack = new SimpleVector(x, y, z);
// Upper
box.addTriangle(lowerLeftFront, x / textureFactor, 0, lowerRightFront,
0, 0, upperLeftFront, x / textureFactor, y / textureFactor,
textureId);
box.addTriangle(lowerRightFront, 0, 0, upperRightFront, 0, y
/ textureFactor, upperLeftFront, x / textureFactor, y
/ textureFactor, textureId);
// Lower
box.addTriangle(upperLeftBack, x / textureFactor, 0, upperRightBack, 0,
0, lowerLeftBack, x / textureFactor, z / textureFactor,
textureId);
box.addTriangle(upperRightBack, 0, 0, lowerRightBack, 0, z
/ textureFactor, lowerLeftBack, x / textureFactor, z
/ textureFactor, textureId);
// Front
box.addTriangle(upperLeftFront, x / textureFactor, z / textureFactor,
upperRightBack, 0, 0, upperLeftBack, x / textureFactor, 0,
textureId);
box.addTriangle(upperLeftFront, x / textureFactor, z / textureFactor,
upperRightFront, 0, z / textureFactor, upperRightBack, 0, 0,
textureId);
// Back
box.addTriangle(lowerLeftBack, x / textureFactor, 0, lowerRightBack, 0,
0, lowerLeftFront, x / textureFactor, z / textureFactor,
textureId);
box.addTriangle(lowerRightBack, 0, 0, lowerRightFront, 0, z
/ textureFactor, lowerLeftFront, x / textureFactor, z
/ textureFactor, textureId);
// Left
box.addTriangle(lowerRightBack, y / textureFactor, 0, upperRightBack,
0, 0, lowerRightFront, y / textureFactor, z / textureFactor,
textureId);
box.addTriangle(upperRightBack, 0, 0, upperRightFront, 0, z
/ textureFactor, lowerRightFront, y / textureFactor, z
/ textureFactor, textureId);
// Right
box.addTriangle(upperLeftBack, y / textureFactor, 0, lowerLeftBack, 0,
0, upperLeftFront, y / textureFactor, z / textureFactor,
textureId);
box.addTriangle(lowerLeftBack, 0, 0, lowerLeftFront, 0, z
/ textureFactor, upperLeftFront, y / textureFactor, z
/ textureFactor, textureId);
return box;
}
final Light sun = new Light(world);
sun.setIntensity(50, 55, 60);
sun.setDiscardDistance(3000);
sun.setPosition(new SimpleVector(500, 500, 2700));
Config.lightMul = 1;
Config.fadeoutLight = false;
world.getLights().setRGBScale(Lights.RGB_SCALE_2X);
world.setAmbientLight(131, 138, 154);
world.getLights().setOverbrightLighting(Lights.OVERBRIGHT_LIGHTING_DISABLED);
wall.build();
wall.setSpecularLighting(true);
wall.setLighting(Object3D.LIGHTING_ALL_ENABLED);
public static Object3D generateFlatModel(final List<Object3D> inPoints,
final float height, final int groundTextureId,
final int roofTextureId) {
final Object3D plan = new Object3D(1000);
final List<Object3D> points = new ArrayList<Object3D>(inPoints);
int i = 0;
// The angle to know if a corner is in or out the shape.
final float innerAngle = ModelComposerImpl.calcInnerAngle(inPoints);
// The vector to lift the plan by the given value.
final SimpleVector liftVector = new SimpleVector(0, 0, -(inPoints
.get(0).getOrigin().z - height));
final float textureRatio = 128;
// While we can make triangles
while (points.size() > 2) {
final int size = points.size();
// Get the angle of the corner
final float angle = ModelComposerImpl.getAngle(points.get(i % size)
.getOrigin(), points.get((i + 1) % size).getOrigin(),
points.get((i + 2) % size).getOrigin());
// If the corner is an obtuse angle
if (angle <= 0 && innerAngle > 0 || angle >= 0 && innerAngle < 0) {
++i;
continue;
}
boolean intersected = false;
// Check if the triangle overlaps another side of the shape
for (int j = 0; j < inPoints.size() && !intersected; ++j) {
intersected = ModelComposerImpl.isLineCrossing(
points.get(i % size).getOrigin(),
points.get((i + 2) % size).getOrigin(),
inPoints.get(j % inPoints.size()).getOrigin(), inPoints
.get((j + 1) % inPoints.size()).getOrigin());
}
// If the triangle does not overlaps another side of the shape,
// create the triangle.
if (!intersected) {
final SimpleVector point1 = points.get((i + 2) % size)
.getOrigin().calcAdd(liftVector);
final SimpleVector point2 = points.get((i + 1) % size)
.getOrigin().calcAdd(liftVector);
final SimpleVector point3 = points.get(i % size).getOrigin()
.calcAdd(liftVector);
// Upper face
plan.addTriangle(point1, point1.x / textureRatio, point1.y
/ textureRatio, point2, point2.x / textureRatio,
point2.y / textureRatio, point3, point3.x
/ textureRatio, point3.y / textureRatio,
innerAngle > 0 ? groundTextureId : roofTextureId);
// Lower face
plan.addTriangle(point3, point3.x / textureRatio, point3.y
/ textureRatio, point2, point2.x / textureRatio,
point2.y / textureRatio, point1, point1.x
/ textureRatio, point1.y / textureRatio,
innerAngle > 0 ? roofTextureId : groundTextureId);
points.remove((i + 1) % size);
} else {
++i;
}
}
return plan;
}
public static float getAngle(final SimpleVector side1,
final SimpleVector center, final SimpleVector side2) {
final SimpleVector side1Vector = side1.calcSub(center);
final SimpleVector side2Vector = side2.calcSub(center);
float angle = side1.calcSub(center).calcAngle(side2.calcSub(center));
// If the angle is an obtuse angle, subtract PI to the angle
if (side1Vector.x < side2Vector.x && side1Vector.y > side2Vector.y
&& side1Vector.y > 0 && side2Vector.x > 0) {
angle -= Math.PI;
}
if (side1Vector.x < side2Vector.x && side1Vector.y < side2Vector.y
&& side1Vector.x < 0 && side2Vector.y > 0) {
angle -= Math.PI;
}
if (side1Vector.x > side2Vector.x && side1Vector.y < side2Vector.y
&& side1Vector.y < 0 && side2Vector.x < 0) {
angle -= Math.PI;
}
if (side1Vector.x > side2Vector.x && side1Vector.y > side2Vector.y
&& side1Vector.x > 0 && side2Vector.y < 0) {
angle -= Math.PI;
}
return angle;
}
public static boolean isLineCrossing(final SimpleVector line1Origin,
final SimpleVector line1End, final SimpleVector line2Origin,
final SimpleVector line2End) {
// If the lines have a same point , return false.
if (line1Origin.distance(line2Origin) == 0
|| line1Origin.distance(line2End) == 0
|| line1End.distance(line2Origin) == 0
|| line1End.distance(line2End) == 0) {
return false;
}
double sx;
double sy;
if (line1Origin.x == line1End.x) {
if (line2Origin.x == line2End.x) {
return false;
} else {
final double pCD = (line2Origin.y - line2End.y)
/ (line2Origin.x - line2End.x);
sx = line1Origin.x;
sy = pCD * (line1Origin.x - line2Origin.x) + line2Origin.y;
}
} else {
if (line2Origin.x == line2End.x) {
final double pAB = (line1Origin.y - line1End.y)
/ (line1Origin.x - line1End.x);
sx = line2Origin.x;
sy = pAB * (line2Origin.x - line1Origin.x) + line1Origin.y;
} else {
final double pCD = (line2Origin.y - line2End.y)
/ (line2Origin.x - line2End.x);
final double pAB = (line1Origin.y - line1End.y)
/ (line1Origin.x - line1End.x);
if (pAB == pCD) {
// If the vectors are collinear, we don't need to consider that they colliding.
return false;
}
final double oCD = line2Origin.y - pCD * line2Origin.x;
final double oAB = line1Origin.y - pAB * line1Origin.x;
sx = (oAB - oCD) / (pCD - pAB);
sy = pCD * sx + oCD;
}
}
// if sx and sy are not in the lines, return false.
if ((sx < line1Origin.x && sx < line1End.x)
| (sx > line1Origin.x && sx > line1End.x)
| (sx < line2Origin.x && sx < line2End.x)
| (sx > line2Origin.x && sx > line2End.x)
| (sy < line1Origin.y && sy < line1End.y)
| (sy > line1Origin.y && sy > line1End.y)
| (sy < line2Origin.y && sy < line2End.y)
| (sy > line2Origin.y && sy > line2End.y)) {
return false;
}
return true;
}
public static float calcInnerAngle(final List<Object3D> points) {
float angle = 0;
final int size = points.size();
for (int i = 0; i < points.size(); ++i) {
angle += ModelComposerImpl.getAngle(points.get(i).getOrigin(),
points.get((i + 1) % size).getOrigin(),
points.get((i + 2) % size).getOrigin());
}
return angle / 4;
}
Page created in 0.020 seconds with 10 queries.