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 com.android.scenegraph.Scene;
23 import com.android.scenegraph.SceneManager;
24 
25 import android.renderscript.Element;
26 import android.renderscript.Float4;
27 import android.renderscript.Matrix4f;
28 import android.renderscript.ProgramFragment;
29 import android.renderscript.ProgramStore;
30 import android.renderscript.ProgramVertex;
31 import android.renderscript.RenderScriptGL;
32 import android.util.Log;
33 
34 /**
35  * @hide
36  */
37 public class Float4Param extends ShaderParam {
38     private static String TAG = "Float4Param";
39 
40     LightBase mLight;
41 
Float4Param(String name)42     public Float4Param(String name) {
43         super(name);
44     }
45 
Float4Param(String name, float x)46     public Float4Param(String name, float x) {
47         super(name);
48         set(x, 0, 0, 0);
49     }
50 
Float4Param(String name, float x, float y)51     public Float4Param(String name, float x, float y) {
52         super(name);
53         set(x, y, 0, 0);
54     }
55 
Float4Param(String name, float x, float y, float z)56     public Float4Param(String name, float x, float y, float z) {
57         super(name);
58         set(x, y, z, 0);
59     }
60 
Float4Param(String name, float x, float y, float z, float w)61     public Float4Param(String name, float x, float y, float z, float w) {
62         super(name);
63         set(x, y, z, w);
64     }
65 
set(float x, float y, float z, float w)66     void set(float x, float y, float z, float w) {
67         mData.float_value.x = x;
68         mData.float_value.y = y;
69         mData.float_value.z = z;
70         mData.float_value.w = w;
71         if (mField != null) {
72             mField.set_float_value(0, mData.float_value, true);
73         }
74         incTimestamp();
75     }
76 
setValue(Float4 v)77     public void setValue(Float4 v) {
78         set(v.x, v.y, v.z, v.w);
79     }
80 
getValue()81     public Float4 getValue() {
82         return mData.float_value;
83     }
84 
setLight(LightBase l)85     public void setLight(LightBase l) {
86         mLight = l;
87         if (mField != null) {
88             mData.light = mLight.getRSData().getAllocation();
89             mField.set_light(0, mData.light, true);
90         }
91         incTimestamp();
92     }
93 
findLight(String property)94     boolean findLight(String property) {
95         String indexStr = mParamName.substring(property.length() + 1);
96         if (indexStr == null) {
97             Log.e(TAG, "Invalid light index.");
98             return false;
99         }
100         int index = Integer.parseInt(indexStr);
101         if (index == -1) {
102             return false;
103         }
104         Scene parentScene = SceneManager.getInstance().getActiveScene();
105         ArrayList<LightBase> allLights = parentScene.getLights();
106         if (index >= allLights.size()) {
107             return false;
108         }
109         mLight = allLights.get(index);
110         if (mLight == null) {
111             return false;
112         }
113         return true;
114     }
115 
getTypeFromName()116     int getTypeFromName() {
117         int paramType = ScriptC_export.const_ShaderParam_FLOAT4_DATA;
118         if (mParamName.equalsIgnoreCase(cameraPos)) {
119             paramType = ScriptC_export.const_ShaderParam_FLOAT4_CAMERA_POS;
120         } else if(mParamName.equalsIgnoreCase(cameraDir)) {
121             paramType = ScriptC_export.const_ShaderParam_FLOAT4_CAMERA_DIR;
122         } else if(mParamName.startsWith(lightColor) && findLight(lightColor)) {
123             paramType = ScriptC_export.const_ShaderParam_FLOAT4_LIGHT_COLOR;
124         } else if(mParamName.startsWith(lightPos) && findLight(lightPos)) {
125             paramType = ScriptC_export.const_ShaderParam_FLOAT4_LIGHT_POS;
126         } else if(mParamName.startsWith(lightDir) && findLight(lightDir)) {
127             paramType = ScriptC_export.const_ShaderParam_FLOAT4_LIGHT_DIR;
128         }
129         return paramType;
130     }
131 
initLocalData()132     void initLocalData() {
133         mData.type = getTypeFromName();
134         if (mCamera != null) {
135             mData.camera = mCamera.getRSData().getAllocation();
136         }
137         if (mLight != null) {
138             mData.light = mLight.getRSData().getAllocation();
139         }
140     }
141 }
142 
143 
144 
145 
146 
147