Example usage for com.badlogic.gdx.graphics.g3d.attributes ColorAttribute Diffuse

List of usage examples for com.badlogic.gdx.graphics.g3d.attributes ColorAttribute Diffuse

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g3d.attributes ColorAttribute Diffuse.

Prototype

long Diffuse

To view the source code for com.badlogic.gdx.graphics.g3d.attributes ColorAttribute Diffuse.

Click Source Link

Usage

From source file:com.badlogic.gdx.ai.tests.utils.bullet.BaseEntity.java

License:Apache License

public void setColor(float r, float g, float b, float a) {
    color.set(r, g, b, a);/*from   w ww .ja v  a 2  s.co  m*/
    if (modelInstance != null) {
        for (Material m : modelInstance.materials) {
            ColorAttribute ca = (ColorAttribute) m.get(ColorAttribute.Diffuse);
            if (ca != null)
                ca.color.set(r, g, b, a);
        }
    }
}

From source file:com.badlogic.gdx.tests.g3d.voxel.VoxelWorld.java

License:Apache License

public VoxelWorld(TextureRegion[] tiles, int chunksX, int chunksY, int chunksZ) {
    this.tiles = tiles;
    this.chunks = new VoxelChunk[chunksX * chunksY * chunksZ];
    this.chunksX = chunksX;
    this.chunksY = chunksY;
    this.chunksZ = chunksZ;
    this.numChunks = chunksX * chunksY * chunksZ;
    this.voxelsX = chunksX * CHUNK_SIZE_X;
    this.voxelsY = chunksY * CHUNK_SIZE_Y;
    this.voxelsZ = chunksZ * CHUNK_SIZE_Z;
    int i = 0;/*from  w  ww.j a va2 s .c om*/
    for (int y = 0; y < chunksY; y++) {
        for (int z = 0; z < chunksZ; z++) {
            for (int x = 0; x < chunksX; x++) {
                VoxelChunk chunk = new VoxelChunk(CHUNK_SIZE_X, CHUNK_SIZE_Y, CHUNK_SIZE_Z);
                chunk.offset.set(x * CHUNK_SIZE_X, y * CHUNK_SIZE_Y, z * CHUNK_SIZE_Z);
                chunks[i++] = chunk;
            }
        }
    }
    int len = CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z * 6 * 6 / 3;
    short[] indices = new short[len];
    short j = 0;
    for (i = 0; i < len; i += 6, j += 4) {
        indices[i + 0] = (short) (j + 0);
        indices[i + 1] = (short) (j + 1);
        indices[i + 2] = (short) (j + 2);
        indices[i + 3] = (short) (j + 2);
        indices[i + 4] = (short) (j + 3);
        indices[i + 5] = (short) (j + 0);
    }
    this.meshes = new Mesh[chunksX * chunksY * chunksZ];
    for (i = 0; i < meshes.length; i++) {
        meshes[i] = new Mesh(true, CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z * 6 * 4,
                CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z * 36 / 3, VertexAttribute.Position(),
                VertexAttribute.Normal());
        meshes[i].setIndices(indices);
    }
    this.dirty = new boolean[chunksX * chunksY * chunksZ];
    for (i = 0; i < dirty.length; i++)
        dirty[i] = true;

    this.numVertices = new int[chunksX * chunksY * chunksZ];
    for (i = 0; i < numVertices.length; i++)
        numVertices[i] = 0;

    this.vertices = new float[VoxelChunk.VERTEX_SIZE * 6 * CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
    this.materials = new Material[chunksX * chunksY * chunksZ];
    for (i = 0; i < materials.length; i++) {
        materials[i] = new Material(new ColorAttribute(ColorAttribute.Diffuse, MathUtils.random(0.5f, 1f),
                MathUtils.random(0.5f, 1f), MathUtils.random(0.5f, 1f), 1));
    }
}

From source file:com.badlogic.invaders.simulation.Simulation.java

License:Apache License

private void populate() {
    ObjLoader objLoader = new ObjLoader();
    shipModel = objLoader.loadModel(Gdx.files.internal("data/ship.obj"));
    invaderModel = objLoader.loadModel(Gdx.files.internal("data/invader.obj"));
    blockModel = objLoader.loadModel(Gdx.files.internal("data/block.obj"));
    shotModel = objLoader.loadModel(Gdx.files.internal("data/shot.obj"));

    final Texture shipTexture = new Texture(Gdx.files.internal("data/ship.png"), Format.RGB565, true);
    shipTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
    final Texture invaderTexture = new Texture(Gdx.files.internal("data/invader.png"), Format.RGB565, true);
    invaderTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
    shipModel.materials.get(0).set(TextureAttribute.createDiffuse(shipTexture));
    invaderModel.materials.get(0).set(TextureAttribute.createDiffuse(invaderTexture));

    ((ColorAttribute) blockModel.materials.get(0).get(ColorAttribute.Diffuse)).color.set(0, 0, 1, 0.5f);
    blockModel.materials.get(0).set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA));

    shotModel.materials.get(0).set(ColorAttribute.createDiffuse(1, 1, 0, 1f));

    final Texture explosionTexture = new Texture(Gdx.files.internal("data/explode.png"), Format.RGBA4444, true);
    explosionTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);

    final Mesh explosionMesh = new Mesh(true, 4 * 16, 6 * 16,
            new VertexAttribute(Usage.Position, 3, "a_position"),
            new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0"));

    float[] vertices = new float[4 * 16 * (3 + 2)];
    short[] indices = new short[6 * 16];
    int idx = 0;//www .  j a  v a  2  s. c om
    int index = 0;
    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 4; column++) {
            vertices[idx++] = 1;
            vertices[idx++] = 1;
            vertices[idx++] = 0;
            vertices[idx++] = 0.25f + column * 0.25f;
            vertices[idx++] = 0 + row * 0.25f;

            vertices[idx++] = -1;
            vertices[idx++] = 1;
            vertices[idx++] = 0;
            vertices[idx++] = 0 + column * 0.25f;
            vertices[idx++] = 0 + row * 0.25f;

            vertices[idx++] = -1;
            vertices[idx++] = -1;
            vertices[idx++] = 0;
            vertices[idx++] = 0f + column * 0.25f;
            vertices[idx++] = 0.25f + row * 0.25f;

            vertices[idx++] = 1;
            vertices[idx++] = -1;
            vertices[idx++] = 0;
            vertices[idx++] = 0.25f + column * 0.25f;
            vertices[idx++] = 0.25f + row * 0.25f;

            final int t = (4 * row + column) * 4;
            indices[index++] = (short) (t);
            indices[index++] = (short) (t + 1);
            indices[index++] = (short) (t + 2);
            indices[index++] = (short) (t);
            indices[index++] = (short) (t + 2);
            indices[index++] = (short) (t + 3);
        }
    }

    explosionMesh.setVertices(vertices);
    explosionMesh.setIndices(indices);

    explosionModel = ModelBuilder.createFromMesh(explosionMesh, GL20.GL_TRIANGLES,
            new Material(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA),
                    TextureAttribute.createDiffuse(explosionTexture)));

    ship = new Ship(shipModel);
    ship.transform.rotate(0, 1, 0, 180);

    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 8; column++) {
            Invader invader = new Invader(invaderModel, -PLAYFIELD_MAX_X / 2 + column * 2f, 0,
                    PLAYFIELD_MIN_Z + row * 2f);
            invaders.add(invader);
        }
    }

    for (int shield = 0; shield < 3; shield++) {
        blocks.add(new Block(blockModel, -10 + shield * 10 - 1, 0, -2));
        blocks.add(new Block(blockModel, -10 + shield * 10 - 1, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 0, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 1, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 1, 0, -2));
    }
}

From source file:com.github.fauu.helix.core.GeneralShader.java

License:Open Source License

@Override
public void render(Renderable renderable) {
    program.setUniformMatrix(u_worldTrans, renderable.worldTransform);

    final Color color = ((ColorAttribute) renderable.material.get(ColorAttribute.Diffuse)).color;
    program.setUniformf(u_color, color.r, color.g, color.b, color.a);

    final TextureDescriptor textureDescriptor = ((TextureAttribute) renderable.material
            .get(TextureAttribute.Diffuse)).textureDescription;
    program.setUniformi(u_texture, context.textureBinder.bind(textureDescriptor));

    if (!(renderable.mesh instanceof MapRegionMesh)) {
        program.setAttributef("a_color", 1, 1, 1, 1);
    }//w  ww . java 2s  .c o  m

    renderable.mesh.render(program, renderable.primitiveType, renderable.meshPartOffset,
            renderable.meshPartSize, true);
}

From source file:com.github.fauu.helix.core.MapRegion.java

License:Open Source License

public void create(Vector2 size, Tile[] tiles, Array<Object> objects, TextureAtlas textureAtlas,
        GeometrySet geometrySet) {//w ww.j ava2s  .  c o  m
    this.size = size;
    this.textureAtlas = textureAtlas;
    this.geometrySet = geometrySet;
    this.objects = objects;

    if (tiles != null) {
        this.tiles = tiles;
    } else {
        final int tilesLength = (int) (size.x * size.y);

        tiles = new Tile[tilesLength];
        for (int i = 0; i < tilesLength; i++) {
            final Tile.Builder tileBuilder = new Tile.Builder();
            final int tileX = i % (int) size.x;
            final int tileY = (int) Math.floor(i / (tilesLength / size.y));

            tiles[i] = tileBuilder.setNo(i).setPosition(new Vector2(tileX, tileY)).setElevation(0)
                    .setTextureId(0).setGeometryId(0).setFacing(Direction.SOUTH).build();
        }
    }

    this.tiles = tiles;

    mesh = new MapRegionMesh(tiles, geometrySet, textureAtlas);

    renderable = new Renderable();
    renderable.mesh = mesh;
    renderable.material = new Material(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE),
            new TextureAttribute(TextureAttribute.Diffuse, textureAtlas.getTextures().first()));
    renderable.meshPartOffset = 0;
    renderable.meshPartSize = mesh.getNumVertices();
    renderable.primitiveType = GL20.GL_TRIANGLES;
    renderable.worldTransform.idt();
}

From source file:com.github.fauu.helix.core.WaterShader.java

License:Open Source License

@Override
public void render(Renderable renderable) {
    program.setUniformMatrix(u_worldTrans, renderable.worldTransform);

    final Color color = ((ColorAttribute) renderable.material.get(ColorAttribute.Diffuse)).color;
    program.setUniformf(u_color, color.r, color.g, color.b, color.a);

    final TextureDescriptor textureDescriptor = ((TextureAttribute) renderable.material
            .get(TextureAttribute.Diffuse)).textureDescription;
    program.setUniformi(u_texture, context.textureBinder.bind(textureDescriptor));

    WaterData waterData = (WaterData) renderable.userData;

    final TextureDescriptor reflectionTextureDescriptor = new TextureDescriptor(waterData.reflectionTexture);
    program.setUniformi(u_reflectionTexture, context.textureBinder.bind(reflectionTextureDescriptor));

    program.setUniformf(u_waveData, waterData.waveAmplitude, waterData.waveAngle);

    //    final Vector3 trn = new Vector3();
    //    renderable.worldTransform.getTranslation(trn);
    ///*w  ww.j  a  v  a  2  s.  c o m*/
    //    final float diffX = trn.x - ((float[]) renderable.userData)[2];
    //    final float posCoeffX = (float) ((diffX / ((float[]) renderable.userData)[3]) * 2 * Math.PI);
    //
    //    final float diffZ = trn.z - ((float[]) renderable.userData)[4];
    //    final float posCoeffZ = (float) ((diffZ / ((float[]) renderable.userData)[5]) * 2 * Math.PI);
    //
    //    program.setUniformf(u_waveData, ((float[]) renderable.userData)[0], ((float[]) renderable.userData)[1],
    //        posCoeffX, posCoeffZ);

    renderable.mesh.render(program, renderable.primitiveType, renderable.meshPartOffset,
            renderable.meshPartSize, true);
}

From source file:com.mbrlabs.mundus.commons.assets.MaterialAsset.java

License:Apache License

/**
 * Applies this material asset to the libGDX material.
 *
 * @param material/*from w w  w  .j  a v a2 s.c  om*/
 * @return
 */
public Material applyToMaterial(Material material) {
    if (diffuseColor != null) {
        material.set(new ColorAttribute(ColorAttribute.Diffuse, diffuseColor));
    }
    if (diffuseTexture != null) {
        material.set(new TextureAttribute(TextureAttribute.Diffuse, diffuseTexture.getTexture()));
    } else {
        material.remove(TextureAttribute.Diffuse);
    }
    material.set(new FloatAttribute(FloatAttribute.Shininess, shininess));

    return material;
}

From source file:com.mbrlabs.mundus.commons.model.MModelInstance.java

License:Apache License

public void applyModelMaterial() {
    Model m = model.getModel();/*from   ww w.  j a  va 2  s .  c  om*/
    for (int i = 0; i < m.materials.size; i++) {
        Material modelMat = m.materials.get(i);
        Material instanceMat = modelInstance.materials.get(i);

        // diffuse texture
        Attribute diffuseTex = modelMat.get(TextureAttribute.Diffuse);
        if (diffuseTex != null) {
            instanceMat.set(diffuseTex);
        } else {
            instanceMat.remove(TextureAttribute.Diffuse);
        }
        // diffuse color
        Attribute diffuseColor = modelMat.get(ColorAttribute.Diffuse);
        if (diffuseColor != null) {
            instanceMat.set(diffuseColor);
        }

        // TODO other attributes
    }
}

From source file:com.mbrlabs.mundus.commons.shaders.EntityShader.java

License:Apache License

@Override
public void render(Renderable renderable) {
    final MundusEnvironment env = (MundusEnvironment) renderable.environment;

    setLights(env);//from  w w  w .  ja  v a2 s  .c  om
    set(UNIFORM_TRANS_MATRIX, renderable.worldTransform);

    // texture uniform
    TextureAttribute diffuseTexture = ((TextureAttribute) (renderable.material.get(TextureAttribute.Diffuse)));
    ColorAttribute diffuseColor = ((ColorAttribute) (renderable.material.get(ColorAttribute.Diffuse)));

    if (diffuseTexture != null) {
        set(UNIFORM_MATERIAL_DIFFUSE_TEXTURE, diffuseTexture.textureDescription.texture);
        set(UNIFORM_MATERIAL_DIFFUSE_USE_TEXTURE, 1);
    } else {
        set(UNIFORM_MATERIAL_DIFFUSE_COLOR, diffuseColor.color);
        set(UNIFORM_MATERIAL_DIFFUSE_USE_TEXTURE, 0);
    }

    // Fog
    final Fog fog = env.getFog();
    if (fog == null) {
        set(UNIFORM_FOG_DENSITY, 0f);
        set(UNIFORM_FOG_GRADIENT, 0f);
    } else {
        set(UNIFORM_FOG_DENSITY, fog.density);
        set(UNIFORM_FOG_GRADIENT, fog.gradient);
        set(UNIFORM_FOG_COLOR, fog.color);
    }

    // bind attributes, bind mesh & render; then unbinds everything
    renderable.meshPart.render(program);
}

From source file:com.mbrlabs.mundus.commons.shaders.ModelShader.java

License:Apache License

@Override
public void render(Renderable renderable) {
    final MundusEnvironment env = (MundusEnvironment) renderable.environment;

    setLights(env);/*  www . j  a  v  a 2 s.c  o  m*/
    set(UNIFORM_TRANS_MATRIX, renderable.worldTransform);

    // texture uniform
    TextureAttribute diffuseTexture = ((TextureAttribute) (renderable.material.get(TextureAttribute.Diffuse)));
    ColorAttribute diffuseColor = ((ColorAttribute) (renderable.material.get(ColorAttribute.Diffuse)));

    if (diffuseTexture != null) {
        set(UNIFORM_MATERIAL_DIFFUSE_TEXTURE, diffuseTexture.textureDescription.texture);
        set(UNIFORM_MATERIAL_DIFFUSE_USE_TEXTURE, 1);
    } else {
        set(UNIFORM_MATERIAL_DIFFUSE_COLOR, diffuseColor.color);
        set(UNIFORM_MATERIAL_DIFFUSE_USE_TEXTURE, 0);
    }

    // shininess
    float shininess = ((FloatAttribute) renderable.material.get(FloatAttribute.Shininess)).value;
    set(UNIFORM_MATERIAL_SHININESS, shininess);

    // Fog
    final Fog fog = env.getFog();
    if (fog == null) {
        set(UNIFORM_FOG_DENSITY, 0f);
        set(UNIFORM_FOG_GRADIENT, 0f);
    } else {
        set(UNIFORM_FOG_DENSITY, fog.density);
        set(UNIFORM_FOG_GRADIENT, fog.gradient);
        set(UNIFORM_FOG_COLOR, fog.color);
    }

    // bind attributes, bind mesh & render; then unbinds everything
    renderable.meshPart.render(program);
}