// VR snow and rain
import android.os.Bundle;
import android.view.WindowManager;
import android.view.MotionEvent;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorEvent;
mport android.hardware.SensorEventListener;
import ketai.camera.*;
Context context;
SensorManager manager;
Sensor sensor;
AccelerometerListener listener;
float ax, ay, az;
float roll,azimuth,pitch;
float psi,theta,phi;
float[] values = new float[4];
float[] m = new float[16];
float xp,yp,zp;
float ux,uy,uz;
float vx,vy,vz;
float px,py,pz;
float ratio;
float k;
float x,y,r;
float tx,ty;
float dr=5;
float aspect=320/240;
KetaiCamera cam;
float time;
int Nmax=300;
float xs[],ys[],zs[];
float L=500;
boolean state= true;
void onCreate(Bundle icicle) {
super.onCreate(icicle);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//requestPermission(this,new String[] {Manifest.permission.CAMERA},PERMISSION_CAMERA);
requestPermission("android.permission.CAMERA");
}
void setup(){
background(0,160,0);
textMode(CENTER);
textSize(50);
stroke(255);
text(" Please Wait...",200,300);
textSize(30);
text("Programmed by Delta Fares",100,100);
text("Mail:deltafares@gmail.com",100,150);
cam = new KetaiCamera(this, 320, 240, 15);
cam.start();
cam.setCameraID((cam.getCameraID() + 0 ) % cam.getNumberOfCameras());
fullScreen(P3D);
orientation(LANDSCAPE);
stroke(0, 255, 0);
strokeWeight(2);
noFill();
rectMode(CENTER);
imageMode(CENTER);
context = getActivity();
manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
sensor = manager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
listener = new AccelerometerListener();
manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
ratio = width/height;
x=width/2;y=height/2;
xp=0;yp=0;zp=10;
r=0;
vx=vy=0;
k=0.5;
init_snow(L);
}
void draw(){
background(0);
pushMatrix();
ortho(-width/2,width/2,-height/2,height/2,-1000,1000);
translate(width/2.0, height/2.00,-1);
image(cam, 0, 0,width ,height );
if (sensor==null) then text("sparthphone doesn't support Rotation Vector...",10,100);
popMatrix();
lights();
pushMatrix();
monoVR();
if (state) draw_snow(L); else draw_rain(L);
popMatrix();
textSize(40);
stroke(255);
drawUI();
time+=.03;
}
void mousePressed() {
if (mouseX < width/3 && mouseY < 100) state=!state;
if (mouseX>width/2) {xp=xp+dr*px;yp=yp+dr*py;}
if (mouseX<width/2) {xp=xp-dr*px;yp=yp-dr*py;}
if (mouseX>2*width/3 && mouseY<400) {
saveFrame("/sdcard/picture-###.jpg");
textSize(40);
text("saved",100,100);}
}
void onCameraPreviewEvent() {
cam.read();
}
void monoVR() {
// code hidden
}
void init_snow(float l) {
int i;
xs= new float[Nmax];ys= new float[Nmax];zs= new float[Nmax];
for (i=0;i<Nmax;i++) {
xs[i]=random(l)-l/2;ys[i]=random(l)-l/2;zs[i]=random(l)+10; }
}
void draw_snow(float l) {
int i;
noLights();
stroke(255,255,255);
fill(255,255,255);
strokeWeight(5);
for (i=0;i<Nmax;i++) {
zs[i]-=5;
if (zs[i]<=-100) {
xs[i]=random(l)-l/2;ys[i]=random(l)-l/2;zs[i]=random(l);}
pushMatrix();
rotateX(0.5);
translate(xs[i],ys[i],zs[i]);
// line(xs[i],ys[i],zs[i], xs[i],ys[i],zs[i] );
scale(1.0);
box(1)
popMatrix();
}
}
void draw_rain(float l) {
int i;
noLights();
stroke(255,255,255,100);
fill(255,255,255,10);
strokeWeight(5);
for (i=0;i<Nmax;i++) {
zs[i]-=20;
if (zs[i]<=-100) {
xs[i]=random(l)-l/2;ys[i]=random(l)-l/2;zs[i]=random(l);}
pushMatrix();
translate(xs[i],ys[i],zs[i]);
scale(0.03,0.03,10.0);
box(1);
popMatrix();
}
}
void drawUI() {
pushMatrix();
ortho(-width/2,-width/2,-height/2,height/2,-1000,1000);
textAlign(LEFT);
fill(0,0,0,100);
stroke(255,255,255,100);
rect(0, 0, width/3 , 100);
rect(width/3, 0, width/3, 100);
rect((width/3)*2, 0, width/3, 100);
fill(255);
if (state)
text("snow", 0, 10);
else
text("rain", 0, 10);
text("take picture", width/3*2, 10);
popMatrix();
}
class AccelerometerListener implements SensorEventListener {
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType()==Sensor.TYPE_ROTATION_VECTOR) {
SensorManager.getRotationMatrixFromVector(m,event.values);
SensorManager.getOrientation(m,values);
azimuth = values[0];
pitch= values[1];
roll = values[2]; }
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
if (mouseX<width/2) {xp=xp-dr*px;yp=yp-dr*py;}
if (mouseX>2*width/3 && mouseY<400) {
saveFrame("/sdcard/picture-###.jpg");
textSize(40);
text("saved",100,100);}
}
void onCameraPreviewEvent() {
cam.read();
}
void monoVR() {
// code hidden
}
void init_snow(float l) {
int i;
xs= new float[Nmax];ys= new float[Nmax];zs= new float[Nmax];
for (i=0;i<Nmax;i++) {
xs[i]=random(l)-l/2;ys[i]=random(l)-l/2;zs[i]=random(l)+10; }
}
void draw_snow(float l) {
int i;
noLights();
stroke(255,255,255);
fill(255,255,255);
strokeWeight(5);
for (i=0;i<Nmax;i++) {
zs[i]-=5;
if (zs[i]<=-100) {
xs[i]=random(l)-l/2;ys[i]=random(l)-l/2;zs[i]=random(l);}
pushMatrix();
rotateX(0.5);
translate(xs[i],ys[i],zs[i]);
// line(xs[i],ys[i],zs[i], xs[i],ys[i],zs[i] );
scale(1.0);
box(1)
popMatrix();
}
}
void draw_rain(float l) {
int i;
noLights();
stroke(255,255,255,100);
fill(255,255,255,10);
strokeWeight(5);
for (i=0;i<Nmax;i++) {
zs[i]-=20;
if (zs[i]<=-100) {
xs[i]=random(l)-l/2;ys[i]=random(l)-l/2;zs[i]=random(l);}
pushMatrix();
translate(xs[i],ys[i],zs[i]);
scale(0.03,0.03,10.0);
box(1);
popMatrix();
}
}
void drawUI() {
pushMatrix();
ortho(-width/2,-width/2,-height/2,height/2,-1000,1000);
textAlign(LEFT);
fill(0,0,0,100);
stroke(255,255,255,100);
rect(0, 0, width/3 , 100);
rect(width/3, 0, width/3, 100);
rect((width/3)*2, 0, width/3, 100);
fill(255);
if (state)
text("snow", 0, 10);
else
text("rain", 0, 10);
text("take picture", width/3*2, 10);
popMatrix();
}
class AccelerometerListener implements SensorEventListener {
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType()==Sensor.TYPE_ROTATION_VECTOR) {
SensorManager.getRotationMatrixFromVector(m,event.values);
SensorManager.getOrientation(m,values);
azimuth = values[0];
pitch= values[1];
roll = values[2]; }
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
Aucun commentaire:
Enregistrer un commentaire
Remarque : Seul un membre de ce blog est autorisé à enregistrer un commentaire.