use of com.badlogic.gdx.graphics.g3d.Material in project libgdx by libgdx.
the class SoftBodyTest method create.
@Override
public void create() {
super.create();
world.add("ground", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 1f);
float x0 = -2f, y0 = 6f, z0 = -2f;
float x1 = 8f, y1 = 6f, z1 = 8f;
Vector3 patch00 = new Vector3(x0, y0, z0);
Vector3 patch10 = new Vector3(x1, y1, z0);
Vector3 patch01 = new Vector3(x0, y0, z1);
Vector3 patch11 = new Vector3(x1, y1, z1);
softBody = btSoftBodyHelpers.CreatePatch(worldInfo, patch00, patch10, patch01, patch11, 15, 15, 15, false);
softBody.takeOwnership();
softBody.setTotalMass(100f);
((btSoftRigidDynamicsWorld) (world.collisionWorld)).addSoftBody(softBody);
final int vertCount = softBody.getNodeCount();
final int faceCount = softBody.getFaceCount();
mesh = new Mesh(false, vertCount, faceCount * 3, new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
final int vertSize = mesh.getVertexSize() / 4;
mesh.getVerticesBuffer().position(0);
mesh.getVerticesBuffer().limit(vertCount * vertSize);
mesh.getIndicesBuffer().position(0);
mesh.getIndicesBuffer().limit(faceCount * 3);
softBody.getVertices(mesh.getVerticesBuffer(), vertCount, mesh.getVertexSize(), 0);
softBody.getIndices(mesh.getIndicesBuffer(), faceCount);
final float[] verts = new float[vertCount * vertSize];
final int uvOffset = mesh.getVertexAttribute(Usage.TextureCoordinates).offset / 4;
final int normalOffset = mesh.getVertexAttribute(Usage.Normal).offset / 4;
mesh.getVertices(verts);
for (int i = 0; i < vertCount; i++) {
verts[i * vertSize + normalOffset] = 0f;
verts[i * vertSize + normalOffset + 1] = 1f;
verts[i * vertSize + normalOffset + 2] = 0f;
verts[i * vertSize + uvOffset] = (verts[i * vertSize] - x0) / (x1 - x0);
verts[i * vertSize + uvOffset + 1] = (verts[i * vertSize + 2] - z0) / (z1 - z0);
}
mesh.setVertices(verts);
texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
ModelBuilder builder = new ModelBuilder();
builder.begin();
builder.part(new MeshPart("", mesh, 0, mesh.getNumIndices(), GL20.GL_TRIANGLES), new Material(TextureAttribute.createDiffuse(texture), ColorAttribute.createSpecular(Color.WHITE), FloatAttribute.createShininess(64f), IntAttribute.createCullFace(0)));
model = builder.end();
instance = new ModelInstance(model);
world.add(new BulletEntity(instance, null));
}
use of com.badlogic.gdx.graphics.g3d.Material in project libgdx by libgdx.
the class Animation3DTest method create.
@Override
public void create() {
super.create();
lights = new Environment();
lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
lights.add((shadowLight = new DirectionalShadowLight(1024, 1024, 30f, 30f, 1f, 100f)).set(0.8f, 0.8f, 0.8f, -.4f, -.4f, -.4f));
lights.shadowMap = shadowLight;
inputController.rotateLeftKey = inputController.rotateRightKey = inputController.forwardKey = inputController.backwardKey = 0;
cam.position.set(25, 25, 25);
cam.lookAt(0, 0, 0);
cam.update();
modelsWindow.setVisible(false);
assets.load("data/g3d/skydome.g3db", Model.class);
assets.load("data/g3d/concrete.png", Texture.class);
assets.load("data/tree.png", Texture.class);
assets.load("data/g3d/ship.obj", Model.class);
loading = true;
trForward.translation.set(0, 0, 8f);
trBackward.translation.set(0, 0, -8f);
trLeft.rotation.setFromAxis(Vector3.Y, 90);
trRight.rotation.setFromAxis(Vector3.Y, -90);
ModelBuilder builder = new ModelBuilder();
builder.begin();
builder.node().id = "floor";
MeshPartBuilder part = builder.part("floor", GL20.GL_TRIANGLES, Usage.Position | Usage.TextureCoordinates | Usage.Normal, new Material("concrete"));
((MeshBuilder) part).ensureRectangles(1600);
for (float x = -200f; x < 200f; x += 10f) {
for (float z = -200f; z < 200f; z += 10f) {
part.rect(x, 0, z + 10f, x + 10f, 0, z + 10f, x + 10f, 0, z, x, 0, z, 0, 1, 0);
}
}
builder.node().id = "tree";
part = builder.part("tree", GL20.GL_TRIANGLES, Usage.Position | Usage.TextureCoordinates | Usage.Normal, new Material("tree"));
part.rect(0f, 0f, -10f, 10f, 0f, -10f, 10f, 10f, -10f, 0f, 10f, -10f, 0, 0, 1f);
part.setUVRange(1, 0, 0, 1);
part.rect(10f, 0f, -10f, 0f, 0f, -10f, 0f, 10f, -10f, 10f, 10f, -10f, 0, 0, -1f);
floorModel = builder.end();
shadowBatch = new ModelBatch(new DepthShaderProvider());
}
use of com.badlogic.gdx.graphics.g3d.Material in project libgdx by libgdx.
the class BaseG3dTest method createAxes.
private void createAxes() {
ModelBuilder modelBuilder = new ModelBuilder();
modelBuilder.begin();
MeshPartBuilder builder = modelBuilder.part("grid", GL20.GL_LINES, Usage.Position | Usage.ColorUnpacked, new Material());
builder.setColor(Color.LIGHT_GRAY);
for (float t = GRID_MIN; t <= GRID_MAX; t += GRID_STEP) {
builder.line(t, 0, GRID_MIN, t, 0, GRID_MAX);
builder.line(GRID_MIN, 0, t, GRID_MAX, 0, t);
}
builder = modelBuilder.part("axes", GL20.GL_LINES, Usage.Position | Usage.ColorUnpacked, new Material());
builder.setColor(Color.RED);
builder.line(0, 0, 0, 100, 0, 0);
builder.setColor(Color.GREEN);
builder.line(0, 0, 0, 0, 100, 0);
builder.setColor(Color.BLUE);
builder.line(0, 0, 0, 0, 0, 100);
axesModel = modelBuilder.end();
axesInstance = new ModelInstance(axesModel);
}
use of com.badlogic.gdx.graphics.g3d.Material in project libgdx by libgdx.
the class OcclusionCullingTest method create.
@Override
public void create() {
Gdx.input.setOnscreenKeyboardVisible(true);
super.create();
GLProfiler.enable();
StringBuilder sb = new StringBuilder();
sb.append("Swipe for next test\n");
sb.append("Long press to toggle debug mode\n");
sb.append("Ctrl+drag to rotate\n");
sb.append("Scroll to zoom\n");
sb.append("Tap to spawn dynamic entity, press\n");
sb.append("'0' to spawn ").append(KEY_SPAWN_OCCLUDEE_AMOUNT).append(" static entities\n");
sb.append("'1' to set normal/disabled/occlusion-culling\n");
sb.append("'2' to change camera\n");
sb.append("'3' to toggle camera movement\n");
sb.append("'4' to cycle occlusion buffer sizes\n");
sb.append("'5' to toggle occlusion buffer image\n");
sb.append("'6' to toggle shadows\n");
instructions = sb.toString();
AssetManager assets = new AssetManager();
disposables.add(assets);
for (String modelName : OCCLUDEE_PATHS_DYNAMIC) assets.load(modelName, Model.class);
assets.load(DEFAULT_TEX_PATH, Texture.class);
Camera shadowCamera = ((DirectionalShadowLight) light).getCamera();
shadowCamera.viewportWidth = shadowCamera.viewportHeight = 120;
// User controlled camera
overviewCam = camera;
overviewCam.position.set(overviewCam.direction).nor().scl(-100);
overviewCam.lookAt(Vector3.Zero);
overviewCam.far = camera.far *= 2;
overviewCam.update(true);
// Animated frustum camera model
frustumCam = new PerspectiveCamera(FRUSTUM_CAMERA_FOV, camera.viewportWidth, camera.viewportHeight);
frustumCam.far = FRUSTUM_CAMERA_FAR;
frustumCam.update(true);
final Model frustumModel = FrustumCullingTest.createFrustumModel(frustumCam.frustum.planePoints);
frustumModel.materials.first().set(new ColorAttribute(ColorAttribute.AmbientLight, Color.WHITE));
disposables.add(frustumModel);
frustumInstance = new ModelInstance(frustumModel);
spriteBatch = new SpriteBatch();
disposables.add(spriteBatch);
shapeRenderer = new ShapeRenderer();
disposables.add(shapeRenderer);
oclBuffer = new OcclusionBuffer(OCL_BUFFER_EXTENTS[0], OCL_BUFFER_EXTENTS[0]);
disposables.add(oclBuffer);
occlusionCuller = new OcclusionCuller() {
@Override
public boolean isOccluder(btCollisionObject object) {
return (object.getCollisionFlags() & CF_OCCLUDER_OBJECT) != 0;
}
@Override
public void onObjectVisible(btCollisionObject object) {
visibleEntities.add(world.entities.get(object.getUserValue()));
}
};
disposables.add(occlusionCuller);
// Add occluder walls
final Model occluderModel = modelBuilder.createBox(OCCLUDER_DIM.x, OCCLUDER_DIM.y, OCCLUDER_DIM.z, new Material(ColorAttribute.createDiffuse(Color.WHITE)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
disposables.add(occluderModel);
world.addConstructor("wall", new BulletConstructor(occluderModel, 0, new btBoxShape(tmpV1.set(OCCLUDER_DIM).scl(0.5f))));
float y = OCCLUDER_DIM.y * 0.5f;
addOccluder("wall", 0, tmpV1.set(20, y, 0));
addOccluder("wall", -60, tmpV1.set(10, y, 20));
addOccluder("wall", 60, tmpV1.set(10, y, -20));
addOccluder("wall", 0, tmpV1.set(-20, y, 0));
addOccluder("wall", 60, tmpV1.set(-10, y, 20));
addOccluder("wall", -60, tmpV1.set(-10, y, -20));
// Add ground
final Model groundModel = modelBuilder.createBox(GROUND_DIM.x, GROUND_DIM.y, GROUND_DIM.z, new Material(ColorAttribute.createDiffuse(Color.WHITE)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
btCollisionShape groundShape = new btBoxShape(tmpV1.set(GROUND_DIM).scl(0.5f));
world.addConstructor("big_ground", new BulletConstructor(groundModel, 0, groundShape));
BulletEntity e = world.add("big_ground", 0, -GROUND_DIM.y * 0.5f, 0f);
e.body.setFriction(1f);
e.setColor(Color.FOREST);
// Occludee entity constructors. Scale models uniformly and set a default diffuse texture.
BoundingBox bb = new BoundingBox();
assets.finishLoadingAsset(DEFAULT_TEX_PATH);
TextureAttribute defaultTexture = new TextureAttribute(TextureAttribute.Diffuse, assets.get(DEFAULT_TEX_PATH, Texture.class));
for (int i = 0; i < OCCLUDEE_PATHS_DYNAMIC.length; i++) {
String modelPath = OCCLUDEE_PATHS_DYNAMIC[i];
OCCLUDEE_PATHS_STATIC[i] = "static" + modelPath;
assets.finishLoadingAsset(modelPath);
Model model = assets.get(modelPath, Model.class);
if (!model.materials.first().has(TextureAttribute.Diffuse))
model.materials.first().set(defaultTexture);
Vector3 dim = model.calculateBoundingBox(bb).getDimensions(tmpV1);
float scaleFactor = OCCLUDEE_MAX_EXTENT / Math.max(dim.x, Math.max(dim.y, dim.z));
for (Node node : model.nodes) node.scale.scl(scaleFactor);
btCollisionShape shape = new btBoxShape(dim.scl(scaleFactor * 0.5f));
world.addConstructor(modelPath, new BulletConstructor(model, 1, shape));
world.addConstructor(OCCLUDEE_PATHS_STATIC[i], new BulletConstructor(model, 0, shape));
}
// Add occludees
for (int i = 0; i < STARTING_OCCLUDEE_AMOUNT; i++) addRandomOccludee(false);
}
use of com.badlogic.gdx.graphics.g3d.Material in project libgdx by libgdx.
the class ConstraintsTest method create.
@Override
public void create() {
super.create();
final Model barModel = modelBuilder.createBox(10f, 1f, 1f, new Material(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)), Usage.Position | Usage.Normal);
disposables.add(barModel);
// mass = 0: static body
world.addConstructor("bar", new BulletConstructor(barModel, 0f));
// Create the entities
world.add("ground", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 1f);
BulletEntity bar = world.add("bar", 0f, 7f, 0f);
bar.setColor(0.75f + 0.25f * (float) Math.random(), 0.75f + 0.25f * (float) Math.random(), 0.75f + 0.25f * (float) Math.random(), 1f);
BulletEntity box1 = world.add("box", -4.5f, 6f, 0f);
box1.setColor(0.5f + 0.5f * (float) Math.random(), 0.5f + 0.5f * (float) Math.random(), 0.5f + 0.5f * (float) Math.random(), 1f);
btPoint2PointConstraint constraint = new btPoint2PointConstraint((btRigidBody) bar.body, (btRigidBody) box1.body, tmpV1.set(-5, -0.5f, -0.5f), tmpV2.set(-0.5f, 0.5f, -0.5f));
((btDynamicsWorld) world.collisionWorld).addConstraint(constraint, false);
constraints.add(constraint);
BulletEntity box2 = null;
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
box2 = world.add("box", -3.5f + (float) i, 6f, 0f);
box2.setColor(0.5f + 0.5f * (float) Math.random(), 0.5f + 0.5f * (float) Math.random(), 0.5f + 0.5f * (float) Math.random(), 1f);
constraint = new btPoint2PointConstraint((btRigidBody) box1.body, (btRigidBody) box2.body, tmpV1.set(0.5f, -0.5f, 0.5f), tmpV2.set(-0.5f, -0.5f, 0.5f));
} else {
box1 = world.add("box", -3.5f + (float) i, 6f, 0f);
box1.setColor(0.5f + 0.5f * (float) Math.random(), 0.5f + 0.5f * (float) Math.random(), 0.5f + 0.5f * (float) Math.random(), 1f);
constraint = new btPoint2PointConstraint((btRigidBody) box2.body, (btRigidBody) box1.body, tmpV1.set(0.5f, 0.5f, -0.5f), tmpV2.set(-0.5f, 0.5f, -0.5f));
}
((btDynamicsWorld) world.collisionWorld).addConstraint(constraint, false);
constraints.add(constraint);
}
constraint = new btPoint2PointConstraint((btRigidBody) bar.body, (btRigidBody) box1.body, tmpV1.set(5f, -0.5f, -0.5f), tmpV2.set(0.5f, 0.5f, -0.5f));
((btDynamicsWorld) world.collisionWorld).addConstraint(constraint, false);
constraints.add(constraint);
}
Aggregations