1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.opengl;
18
19import java.io.DataInputStream;
20import java.io.IOException;
21import javax.microedition.khronos.opengles.GL10;
22
23/**
24 * {@hide}
25 */
26public class Material {
27
28    private Object3D parent;
29    private String name;
30    private String map_kd;
31    private float[] ka = new float[4];
32    private float[] kd = new float[4];
33    private float[] ks = new float[4];
34    private float ns;
35    private int illum;
36    private float d;
37
38    private static float[] black = { 0.0f, 0.0f, 0.0f, 1.0f };
39
40    public Material(Object3D parent) {
41        this.parent = parent;
42    }
43
44    public String getName() {
45        return name;
46    }
47
48    public String getMap_Kd() {
49        return map_kd;
50    }
51
52    public void setMaterialParameters(GL10 gl) {
53        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, kd, 0);
54        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_DIFFUSE, kd, 0);
55        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, ks, 0);
56        gl.glMaterialf(gl.GL_FRONT_AND_BACK, gl.GL_SHININESS,
57                Math.min(Math.max(ns, 0), 128));
58
59//      if (illum == 0) {
60//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, kd, 0);
61//      } else {
62//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, ka, 0);
63//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_DIFFUSE, kd, 0);
64//      }
65
66//      if (illum > 1) {
67//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, ks, 0);
68//      gl.glMaterialf(gl.GL_FRONT_AND_BACK, gl.GL_SHININESS,
69//      Math.min(Math.max(ns, 0), 128));
70//      } else {
71//      gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, black, 0);
72//      }
73    }
74
75    public void load(DataInputStream dis) throws IOException {
76        dis.readInt(); // name length
77        this.name = dis.readUTF();
78
79        dis.readInt(); // map_kdLength
80        this.map_kd = dis.readUTF();
81
82        if (parent.hasTexcoords() && map_kd.length() > 0) {
83            parent.loadTexture(map_kd);
84        }
85
86        this.ka[0] = dis.readFloat();
87        this.ka[1] = dis.readFloat();
88        this.ka[2] = dis.readFloat();
89        this.ka[3] = dis.readFloat();
90
91        this.kd[0] = dis.readFloat();
92        this.kd[1] = dis.readFloat();
93        this.kd[2] = dis.readFloat();
94        this.kd[3] = dis.readFloat();
95
96        this.ks[0] = dis.readFloat();
97        this.ks[1] = dis.readFloat();
98        this.ks[2] = dis.readFloat();
99        this.ks[3] = dis.readFloat();
100
101        this.ns = dis.readFloat();
102        this.illum = dis.readInt();
103        this.d = dis.readFloat();
104    }
105
106    public String toString() {
107        return "Material[" +
108        "name=\"" + name + "\"," +
109        "ka={" + ka[0] + "," + ka[1] + "," + ka[2] + "}," +
110        "kd={" + kd[0] + "," + kd[1] + "," + kd[2] + "}," +
111        "ks={" + ks[0] + "," + ks[1] + "," + ks[2] + "}," +
112        "ns=" + ns + "," +
113        "map_kd=\"" +
114        (map_kd == null ? "" : map_kd) +
115        "\"," +
116        "illum=" + illum + "," +
117        "d=" + d +
118        "]";
119    }
120}
121