alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (Material.java)

This example Android source code file (Material.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

io, ioexception, material, object3d, string

The Material.java Android example source code

/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.opengl;

import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.khronos.opengles.GL10;

/**
 * {@hide}
 */
public class Material {

    private Object3D parent;
    private String name;
    private String map_kd;
    private float[] ka = new float[4];
    private float[] kd = new float[4];
    private float[] ks = new float[4];
    private float ns;
    private int illum;
    private float d;

    private static float[] black = { 0.0f, 0.0f, 0.0f, 1.0f };

    public Material(Object3D parent) {
        this.parent = parent;
    }

    public String getName() {
        return name;
    }

    public String getMap_Kd() {
        return map_kd;
    }

    public void setMaterialParameters(GL10 gl) {
        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, kd, 0);
        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_DIFFUSE, kd, 0);
        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, ks, 0);
        gl.glMaterialf(gl.GL_FRONT_AND_BACK, gl.GL_SHININESS,
                Math.min(Math.max(ns, 0), 128));

//      if (illum == 0) {
//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, kd, 0);
//      } else {
//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, ka, 0);
//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_DIFFUSE, kd, 0);
//      }

//      if (illum > 1) {
//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, ks, 0);
//      gl.glMaterialf(gl.GL_FRONT_AND_BACK, gl.GL_SHININESS,
//      Math.min(Math.max(ns, 0), 128));
//      } else {
//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, black, 0);
//      }
    }

    public void load(DataInputStream dis) throws IOException {
        dis.readInt(); // name length
        this.name = dis.readUTF();

        dis.readInt(); // map_kdLength
        this.map_kd = dis.readUTF();

        if (parent.hasTexcoords() && map_kd.length() > 0) {
            parent.loadTexture(map_kd);
        }

        this.ka[0] = dis.readFloat();
        this.ka[1] = dis.readFloat();
        this.ka[2] = dis.readFloat();
        this.ka[3] = dis.readFloat();

        this.kd[0] = dis.readFloat();
        this.kd[1] = dis.readFloat();
        this.kd[2] = dis.readFloat();
        this.kd[3] = dis.readFloat();

        this.ks[0] = dis.readFloat();
        this.ks[1] = dis.readFloat();
        this.ks[2] = dis.readFloat();
        this.ks[3] = dis.readFloat();

        this.ns = dis.readFloat();
        this.illum = dis.readInt();
        this.d = dis.readFloat();
    }

    public String toString() {
        return "Material[" +
        "name=\"" + name + "\"," +
        "ka={" + ka[0] + "," + ka[1] + "," + ka[2] + "}," +
        "kd={" + kd[0] + "," + kd[1] + "," + kd[2] + "}," +
        "ks={" + ks[0] + "," + ks[1] + "," + ks[2] + "}," +
        "ns=" + ns + "," +
        "map_kd=\"" + 
        (map_kd == null ? "" : map_kd) +
        "\"," +
        "illum=" + illum + "," +
        "d=" + d +
        "]";
    }
}

Other Android examples (source code examples)

Here is a short list of links related to this Android Material.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.