1 /*
2  * Copyright (C) 2011 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 
17 package com.android.scenegraph;
18 
19 import java.lang.Math;
20 import java.util.ArrayList;
21 
22 import android.renderscript.Float3;
23 import android.renderscript.Float4;
24 import android.renderscript.Matrix4f;
25 import android.renderscript.RenderScriptGL;
26 import android.util.Log;
27 
28 /**
29  * @hide
30  */
31 public abstract class LightBase extends SceneGraphBase {
32     static final int RS_LIGHT_POINT = 0;
33     static final int RS_LIGHT_DIRECTIONAL = 1;
34 
35     ScriptField_Light_s mField;
36     ScriptField_Light_s.Item mFieldData;
37     Transform mTransform;
38     Float4 mColor;
39     float mIntensity;
LightBase()40     public LightBase() {
41         mColor = new Float4(0.0f, 0.0f, 0.0f, 0.0f);
42         mIntensity = 1.0f;
43     }
44 
setTransform(Transform t)45     public void setTransform(Transform t) {
46         mTransform = t;
47         updateRSData();
48     }
49 
setColor(float r, float g, float b)50     public void setColor(float r, float g, float b) {
51         mColor.x = r;
52         mColor.y = g;
53         mColor.z = b;
54         updateRSData();
55     }
56 
setColor(Float3 c)57     public void setColor(Float3 c) {
58         setColor(c.x, c.y, c.z);
59     }
60 
setIntensity(float i)61     public void setIntensity(float i) {
62         mIntensity = i;
63         updateRSData();
64     }
65 
setName(String n)66     public void setName(String n) {
67         super.setName(n);
68         updateRSData();
69     }
70 
updateRSData()71     protected void updateRSData() {
72         if (mField == null) {
73             return;
74         }
75         RenderScriptGL rs = SceneManager.getRS();
76         mFieldData.transformMatrix = mTransform.getRSData().getAllocation();
77         mFieldData.name = getNameAlloc(rs);
78         mFieldData.color = mColor;
79         mFieldData.intensity = mIntensity;
80 
81         initLocalData();
82 
83         mField.set(mFieldData, 0, true);
84     }
85 
initLocalData()86     abstract void initLocalData();
87 
getRSData()88     ScriptField_Light_s getRSData() {
89         if (mField != null) {
90             return mField;
91         }
92 
93         RenderScriptGL rs = SceneManager.getRS();
94         if (rs == null) {
95             return null;
96         }
97         if (mField == null) {
98             mField = new ScriptField_Light_s(rs, 1);
99             mFieldData = new ScriptField_Light_s.Item();
100         }
101 
102         updateRSData();
103 
104         return mField;
105     }
106 }
107 
108 
109 
110 
111 
112