Finishing light linking.
Change-Id: I5d76115410bf0b9a7ea882e57242a0b0bba6ddcc
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
varying vec3 varWorldPos;
|
||||
varying vec3 varWorldNormal;
|
||||
varying vec2 varTex0;
|
||||
|
||||
void main() {
|
||||
|
||||
vec3 V = normalize(UNI_cameraPos.xyz - varWorldPos.xyz);
|
||||
vec3 worldNorm = normalize(varWorldNormal);
|
||||
|
||||
vec3 light0Vec = normalize(UNI_lightPos_0.xyz - varWorldPos.xyz);
|
||||
vec3 light0R = reflect(light0Vec, worldNorm);
|
||||
float light0_Diffuse = clamp(dot(worldNorm, light0Vec), 0.0, 1.0);
|
||||
float light0Spec = clamp(dot(-light0R, V), 0.001, 1.0);
|
||||
float light0_Specular = pow(light0Spec, 10.0) * 0.7;
|
||||
|
||||
vec3 light1Vec = normalize(UNI_lightPos_1.xyz - varWorldPos.xyz);
|
||||
vec3 light1R = reflect(light1Vec, worldNorm);
|
||||
float light1_Diffuse = clamp(dot(worldNorm, light1Vec), 0.0, 1.0);
|
||||
float light1Spec = clamp(dot(-light1R, V), 0.001, 1.0);
|
||||
float light1_Specular = pow(light1Spec, 10.0) * 0.7;
|
||||
|
||||
vec2 t0 = varTex0.xy;
|
||||
lowp vec4 col = UNI_diffuse;
|
||||
col.xyz = col.xyz * (light0_Diffuse * UNI_lightColor_0.xyz +
|
||||
light1_Diffuse * UNI_lightColor_1.xyz);
|
||||
col.xyz += (light0_Specular + light1_Specular);
|
||||
gl_FragColor = col;
|
||||
}
|
||||
|
||||
@@ -19,19 +19,23 @@ package com.android.scenegraph;
|
||||
import java.lang.Math;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.renderscript.RenderScriptGL;
|
||||
import com.android.scenegraph.Scene;
|
||||
import com.android.scenegraph.SceneManager;
|
||||
|
||||
import android.renderscript.Element;
|
||||
import android.renderscript.Float4;
|
||||
import android.renderscript.Matrix4f;
|
||||
import android.renderscript.ProgramFragment;
|
||||
import android.renderscript.ProgramStore;
|
||||
import android.renderscript.ProgramVertex;
|
||||
import android.renderscript.Element;
|
||||
import android.renderscript.RenderScriptGL;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* @hide
|
||||
*/
|
||||
public class Float4Param extends ShaderParam {
|
||||
private static String TAG = "Float4Param";
|
||||
|
||||
Float4 mValue;
|
||||
Camera mCamera;
|
||||
@@ -97,17 +101,39 @@ public class Float4Param extends ShaderParam {
|
||||
mLight = l;
|
||||
}
|
||||
|
||||
boolean findLight(String property) {
|
||||
String indexStr = mParamName.substring(property.length() + 1);
|
||||
if (indexStr == null) {
|
||||
Log.e(TAG, "Invalid light index.");
|
||||
return false;
|
||||
}
|
||||
int index = Integer.parseInt(indexStr);
|
||||
if (index == -1) {
|
||||
return false;
|
||||
}
|
||||
Scene parentScene = SceneManager.getInstance().getActiveScene();
|
||||
ArrayList<LightBase> allLights = parentScene.getLights();
|
||||
if (index >= allLights.size()) {
|
||||
return false;
|
||||
}
|
||||
mLight = allLights.get(index);
|
||||
if (mLight == null) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int getTypeFromName() {
|
||||
int paramType = FLOAT4_DATA;
|
||||
if (mParamName.equalsIgnoreCase(cameraPos)) {
|
||||
paramType = FLOAT4_CAMERA_POS;
|
||||
} else if(mParamName.equalsIgnoreCase(cameraDir)) {
|
||||
paramType = FLOAT4_CAMERA_DIR;
|
||||
} else if(mParamName.startsWith(lightColor)) {
|
||||
} else if(mParamName.startsWith(lightColor) && findLight(lightColor)) {
|
||||
paramType = FLOAT4_LIGHT_COLOR;
|
||||
} else if(mParamName.startsWith(lightPos)) {
|
||||
} else if(mParamName.startsWith(lightPos) && findLight(lightPos)) {
|
||||
paramType = FLOAT4_LIGHT_POS;
|
||||
} else if(mParamName.startsWith(lightDir)) {
|
||||
} else if(mParamName.startsWith(lightDir) && findLight(lightDir)) {
|
||||
paramType = FLOAT4_LIGHT_DIR;
|
||||
}
|
||||
return paramType;
|
||||
|
||||
@@ -167,13 +167,11 @@ public class Renderable extends RenderableBase {
|
||||
String inputName = constantElem.getSubElementName(i);
|
||||
int offset = constantElem.getSubElementOffsetBytes(i);
|
||||
ShaderParam matchingParam = findParamByName(inputName);
|
||||
Element subElem = constantElem.getSubElement(i);
|
||||
// Make one if it's not there
|
||||
if (matchingParam == null) {
|
||||
Element subElem = constantElem.getSubElement(i);
|
||||
if (subElem.getDataType() == Element.DataType.FLOAT_32) {
|
||||
Float4Param fParam = new Float4Param(inputName);
|
||||
fParam.setVecSize(subElem.getVectorSize());
|
||||
matchingParam = fParam;
|
||||
matchingParam = new Float4Param(inputName);
|
||||
} else if (subElem.getDataType() == Element.DataType.MATRIX_4X4) {
|
||||
TransformParam trParam = new TransformParam(inputName);
|
||||
trParam.setTransform(mTransform);
|
||||
@@ -181,6 +179,10 @@ public class Renderable extends RenderableBase {
|
||||
}
|
||||
}
|
||||
matchingParam.setOffset(offset);
|
||||
if (subElem.getDataType() == Element.DataType.FLOAT_32) {
|
||||
Float4Param fParam = (Float4Param)matchingParam;
|
||||
fParam.setVecSize(subElem.getVectorSize());
|
||||
}
|
||||
paramList.add(matchingParam);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +116,10 @@ public class Scene extends SceneGraphBase {
|
||||
return mCameras;
|
||||
}
|
||||
|
||||
public ArrayList<LightBase> getLights() {
|
||||
return mLights;
|
||||
}
|
||||
|
||||
public void appendRenderable(RenderableBase d) {
|
||||
mRenderables.add(d);
|
||||
mRenderableMap.put(d.getName(), d);
|
||||
|
||||
@@ -16,30 +16,30 @@
|
||||
|
||||
package com.android.scenegraph;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.Writer;
|
||||
|
||||
import java.lang.Math;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.android.scenegraph.Scene;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
import android.renderscript.RenderScriptGL;
|
||||
import android.renderscript.Mesh;
|
||||
import android.os.AsyncTask;
|
||||
import android.renderscript.*;
|
||||
import android.renderscript.Allocation.MipmapControl;
|
||||
import android.content.res.Resources;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.renderscript.Mesh;
|
||||
import android.renderscript.RenderScriptGL;
|
||||
import android.util.Log;
|
||||
import android.os.AsyncTask;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
/**
|
||||
* @hide
|
||||
@@ -58,6 +58,9 @@ public class SceneManager extends SceneGraphBase {
|
||||
int mWidth;
|
||||
int mHeight;
|
||||
|
||||
Scene mActiveScene;
|
||||
private static SceneManager sSceneManager;
|
||||
|
||||
public static boolean isSDCardPath(String path) {
|
||||
int sdCardIndex = path.indexOf("sdcard/");
|
||||
// We are looking for /sdcard/ or sdcard/
|
||||
@@ -140,7 +143,22 @@ public class SceneManager extends SceneGraphBase {
|
||||
}
|
||||
}
|
||||
|
||||
public SceneManager() {
|
||||
public Scene getActiveScene() {
|
||||
return mActiveScene;
|
||||
}
|
||||
|
||||
public void setActiveScene(Scene s) {
|
||||
mActiveScene = s;
|
||||
}
|
||||
|
||||
public static SceneManager getInstance() {
|
||||
if (sSceneManager == null) {
|
||||
sSceneManager = new SceneManager();
|
||||
}
|
||||
return sSceneManager;
|
||||
}
|
||||
|
||||
protected SceneManager() {
|
||||
}
|
||||
|
||||
public void loadModel(String name, SceneLoadedCallback cb) {
|
||||
|
||||
@@ -63,11 +63,13 @@ public class TestAppRS {
|
||||
private RenderScriptGL mRS;
|
||||
|
||||
private ProgramFragment mPF_Paint;
|
||||
private ProgramFragment mPF_Lights;
|
||||
private ProgramFragment mPF_Aluminum;
|
||||
private ProgramFragment mPF_Plastic;
|
||||
private ProgramFragment mPF_Diffuse;
|
||||
private ProgramFragment mPF_Texture;
|
||||
ScriptField_FShaderParams_s mFsConst;
|
||||
ScriptField_FShaderLightParams_s mFsConst2;
|
||||
private ProgramVertex mPV_Paint;
|
||||
ScriptField_VShaderParams_s mVsConst;
|
||||
|
||||
@@ -107,7 +109,7 @@ public class TestAppRS {
|
||||
|
||||
mTouchHandler = new TouchHandler();
|
||||
|
||||
mSceneManager = new SceneManager();
|
||||
mSceneManager = SceneManager.getInstance();
|
||||
// Initializes all the RS specific scenegraph elements
|
||||
mSceneManager.initRS(mRS, mRes, mWidth, mHeight);
|
||||
|
||||
@@ -199,6 +201,7 @@ public class TestAppRS {
|
||||
mPV_Paint = vb.create();
|
||||
|
||||
mFsConst = new ScriptField_FShaderParams_s(mRS, 1);
|
||||
mFsConst2 = new ScriptField_FShaderLightParams_s(mRS, 1);
|
||||
|
||||
mPF_Paint = createFromResource(R.raw.paintf, true);
|
||||
mPF_Aluminum = createFromResource(R.raw.metal, true);
|
||||
@@ -207,6 +210,11 @@ public class TestAppRS {
|
||||
mPF_Diffuse = createFromResource(R.raw.diffuse, false);
|
||||
mPF_Texture = createFromResource(R.raw.texture, false);
|
||||
|
||||
ProgramFragment.Builder fb = new ProgramFragment.Builder(mRS);
|
||||
fb.addConstant(mFsConst2.getAllocation().getType());
|
||||
fb.setShader(mRes, R.raw.plastic_lights);
|
||||
mPF_Lights = fb.create();
|
||||
|
||||
FullscreenBlur.initShaders(mRes, mRS, mVsConst, mFsConst);
|
||||
}
|
||||
|
||||
@@ -235,11 +243,13 @@ public class TestAppRS {
|
||||
}
|
||||
|
||||
public void prepareToRender(Scene s) {
|
||||
mSceneManager.setActiveScene(s);
|
||||
mActiveScene = s;
|
||||
RenderState plastic = new RenderState(mPV_Paint, mPF_Plastic, null, null);
|
||||
RenderState diffuse = new RenderState(mPV_Paint, mPF_Diffuse, null, null);
|
||||
RenderState paint = new RenderState(mPV_Paint, mPF_Paint, null, null);
|
||||
RenderState aluminum = new RenderState(mPV_Paint, mPF_Aluminum, null, null);
|
||||
RenderState lights = new RenderState(mPV_Paint, mPF_Lights, null, null);
|
||||
RenderState glassTransp = new RenderState(mPV_Paint,
|
||||
mPF_Paint,
|
||||
ProgramStore.BLEND_ALPHA_DEPTH_TEST(mRS),
|
||||
@@ -261,6 +271,8 @@ public class TestAppRS {
|
||||
|
||||
mActiveScene.assignRenderStateToMaterial(glassTransp, "^#GlassLight");
|
||||
|
||||
mActiveScene.assignRenderStateToMaterial(lights, "^#LightBlinn");
|
||||
|
||||
Renderable plane = (Renderable)mActiveScene.getRenderableByName("pPlaneShape1");
|
||||
if (plane != null) {
|
||||
RenderState texState = new RenderState(mPV_Paint, mPF_Texture, null, null);
|
||||
|
||||
@@ -31,4 +31,4 @@ VertexShaderInputs *iExport;
|
||||
|
||||
VShaderParams *vConst;
|
||||
FShaderParams *fConst;
|
||||
|
||||
FShaderLightParams *fConts2;
|
||||
|
||||
@@ -18,7 +18,14 @@
|
||||
|
||||
#include "transform_def.rsh"
|
||||
|
||||
//#define DEBUG_PARAMS
|
||||
|
||||
static void writeFloatData(float *ptr, const float4 *input, uint32_t vecSize) {
|
||||
#ifdef DEBUG_PARAMS
|
||||
rsDebug("Writing value ", *input);
|
||||
rsDebug("Writing vec size ", vecSize);
|
||||
#endif // DEBUG_PARAMS
|
||||
|
||||
switch (vecSize) {
|
||||
case 1:
|
||||
*ptr = input->x;
|
||||
@@ -39,10 +46,28 @@ static void writeFloatData(float *ptr, const float4 *input, uint32_t vecSize) {
|
||||
}
|
||||
|
||||
static void processParam(SgShaderParam *p, uint8_t *constantBuffer, const SgCamera *currentCam) {
|
||||
#ifdef DEBUG_PARAMS
|
||||
rsDebug("____________ Param bufferOffset", p->bufferOffset);
|
||||
rsDebug("Param Type ", p->type);
|
||||
#endif // DEBUG_PARAMS
|
||||
|
||||
uint8_t *dataPtr = constantBuffer + p->bufferOffset;
|
||||
const SgTransform *pTransform = NULL;
|
||||
if (rsIsObject(p->transform)) {
|
||||
pTransform = (const SgTransform *)rsGetElementAt(p->transform, 0);
|
||||
|
||||
#ifdef DEBUG_PARAMS
|
||||
rsDebug("Param transform", pTransform);
|
||||
printName(pTransform->name);
|
||||
#endif // DEBUG_PARAMS
|
||||
}
|
||||
|
||||
const SgLight *pLight = NULL;
|
||||
if (rsIsObject(p->light)) {
|
||||
pLight = (const SgLight *)rsGetElementAt(p->light, 0);
|
||||
#ifdef DEBUG_PARAMS
|
||||
printLightInfo(pLight);
|
||||
#endif // DEBUG_PARAMS
|
||||
}
|
||||
|
||||
switch(p->type) {
|
||||
@@ -53,8 +78,12 @@ static void processParam(SgShaderParam *p, uint8_t *constantBuffer, const SgCame
|
||||
writeFloatData((float*)dataPtr, ¤tCam->position, p->float_vecSize);
|
||||
break;
|
||||
case SHADER_PARAM_FLOAT4_CAMERA_DIR: break;
|
||||
case SHADER_PARAM_FLOAT4_LIGHT_COLOR: break;
|
||||
case SHADER_PARAM_FLOAT4_LIGHT_POS: break;
|
||||
case SHADER_PARAM_FLOAT4_LIGHT_COLOR:
|
||||
writeFloatData((float*)dataPtr, &pLight->color, p->float_vecSize);
|
||||
break;
|
||||
case SHADER_PARAM_FLOAT4_LIGHT_POS:
|
||||
writeFloatData((float*)dataPtr, &pLight->position, p->float_vecSize);
|
||||
break;
|
||||
case SHADER_PARAM_FLOAT4_LIGHT_DIR: break;
|
||||
|
||||
case SHADER_PARAM_TRANSFORM_DATA:
|
||||
|
||||
@@ -154,6 +154,7 @@ void root(const void *v_in, void *v_out) {
|
||||
rsForEach(gTransformScript, gRootNode->children, nullAlloc, 0, 0);
|
||||
|
||||
prepareCameras();
|
||||
prepareLights();
|
||||
|
||||
rsgClearDepth(1.0f);
|
||||
|
||||
|
||||
@@ -161,6 +161,15 @@ typedef struct FShaderParams_s {
|
||||
float4 cameraPos;
|
||||
} FShaderParams;
|
||||
|
||||
typedef struct FShaderLightParams_s {
|
||||
float4 lightPos_0;
|
||||
float4 lightColor_0;
|
||||
float4 lightPos_1;
|
||||
float4 lightColor_1;
|
||||
float4 cameraPos;
|
||||
float4 diffuse;
|
||||
} FShaderLightParams;
|
||||
|
||||
typedef struct FBlurOffsets_s {
|
||||
float blurOffset0;
|
||||
float blurOffset1;
|
||||
@@ -174,7 +183,7 @@ typedef struct VertexShaderInputs_s {
|
||||
float2 texture0;
|
||||
} VertexShaderInputs;
|
||||
|
||||
static void printCameraInfo(SgCamera *cam) {
|
||||
static void printCameraInfo(const SgCamera *cam) {
|
||||
rsDebug("***** Camera information. ptr:", cam);
|
||||
printName(cam->name);
|
||||
const SgTransform *camTransform = (const SgTransform *)rsGetElementAt(cam->transformMatrix, 0);
|
||||
@@ -190,7 +199,7 @@ static void printCameraInfo(SgCamera *cam) {
|
||||
rsDebug("View: ", &cam->view);
|
||||
}
|
||||
|
||||
static void printLightInfo(SgLight *light) {
|
||||
static void printLightInfo(const SgLight *light) {
|
||||
rsDebug("***** Light information. ptr:", light);
|
||||
printName(light->name);
|
||||
const SgTransform *lTransform = (const SgTransform *)rsGetElementAt(light->transformMatrix, 0);
|
||||
|
||||
Reference in New Issue
Block a user