Here is the full code:
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
import com.threed.jpct.Camera;
import com.threed.jpct.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Light;
import com.threed.jpct.Loader;
import com.threed.jpct.Logger;
import com.threed.jpct.Object3D;
import com.threed.jpct.Projector;
import com.threed.jpct.RGBColor;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.ExtendedPrimitives;
import com.threed.jpct.util.MemoryHelper;
import com.threed.jpct.util.ShadowHelper;
public class ShadowExample extends Activity {
private GLSurfaceView mGLView;
private MyRenderer renderer = null;
private FrameBuffer buffer = null;
private World world = null;
private RGBColor back = new RGBColor(50, 50, 100);
private float touchTurn = 0;
private float touchTurnUp = 0;
private float xpos = -1;
private float ypos = -1;
private Object3D myObject = null;
private Object3D plane = null;
private int fps = 0;
private Light sun = null;
private ShadowHelper sh;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLView = new GLSurfaceView(getApplication());
mGLView.setEGLContextClientVersion(2);
renderer = new MyRenderer();
mGLView.setRenderer(renderer);
setContentView(mGLView);
}
@Override
protected void onPause() {
super.onPause();
mGLView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mGLView.onResume();
}
@Override
protected void onStop() {
super.onStop();
System.exit(0);
}
public boolean onTouchEvent(MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
xpos = me.getX();
ypos = me.getY();
return true;
}
if (me.getAction() == MotionEvent.ACTION_UP) {
xpos = -1;
ypos = -1;
touchTurn = 0;
touchTurnUp = 0;
return true;
}
if (me.getAction() == MotionEvent.ACTION_MOVE) {
float xd = me.getX() - xpos;
float yd = me.getY() - ypos;
xpos = me.getX();
ypos = me.getY();
touchTurn = xd / -100f;
touchTurnUp = yd / -100f;
return true;
}
try {
Thread.sleep(15);
} catch (Exception e) {
// No need for this...
}
return super.onTouchEvent(me);
}
protected boolean isFullscreenOpaque() {
return true;
}
class MyRenderer implements GLSurfaceView.Renderer {
private long time = System.currentTimeMillis();
public MyRenderer() {
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
try {
Config.farPlane = 5000;
buffer = new FrameBuffer(w, h);
world = new World();
world.setAmbientLight(100, 100, 100);
TextureManager tm = TextureManager.getInstance();
tm.addTexture("floor", new Texture(ShadowExample.this.getBaseContext().getAssets().open("floor.jpg")));
myObject = Object3D.mergeAll(Loader.loadOBJ(
getApplicationContext().getAssets().open("teapot.obj"), null, 12));
myObject.build();
myObject.translate(0, 10, 0);
world.addObject(myObject);
plane = ExtendedPrimitives.createPlane(100, 2);
plane.setTexture("floor");
plane.setTransparency(20);
plane.build();
plane.translate(0, 40, 10);
world.addObject(plane);
Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
cam.lookAt(plane.getTransformedCenter());
SimpleVector sv = new SimpleVector(myObject.getTransformedCenter());
sv.y -= 100;
sv.z += 30;
sun = new Light(world);
sun.setIntensity(250, 250, 250);
sun.setPosition(sv);
Projector projector = new Projector();
projector.setClippingPlanes(0.001f, 100f);
projector.setFOVLimits(0, 999);
float fov = projector.convertDEGAngleIntoFOV(90);
projector.setFOV(fov);
projector.setYFOV(fov);
sh = new ShadowHelper(buffer, projector, 1024);
sh.setLightMode(true);
sh.setLightSource(projector);
sh.addCaster(myObject);
sh.addReceiver(plane);
MemoryHelper.compact();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
public void onDrawFrame(GL10 gl) {
if (touchTurn != 0) {
myObject.rotateY(touchTurn);
touchTurn = 0;
}
if (touchTurnUp != 0) {
myObject.rotateX(touchTurnUp);
touchTurnUp = 0;
}
sh.updateShadowMap(buffer, world);
buffer.clear(back);
world.renderScene(buffer);
world.draw(buffer);
// sh.blit(buffer);
buffer.display();
if (System.currentTimeMillis() - time >= 1000) {
Logger.log(fps + "fps");
fps = 0;
time = System.currentTimeMillis();
}
fps++;
}
}
}
Apart from the shadow color, why there's no shadow at all when I set the projector's position in the sun's position ( projector.setPosition(sun.getPosition) ) ?
Isn't projection's position supposed to control the position of the shadow on the receiver? Just like what sun does with the ShadowHelper in your zip example?