Sure. I am pasting the code here.What it does is when you move mouse left or righ , the shapes on the screen rotates.The red color box is the candidate here.This one is not getting stretched on x-axis when I press 'A' key on keyboard.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.me.home;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.GenericVertexController;
import com.threed.jpct.IVertexController;
import com.threed.jpct.Light;
import com.threed.jpct.Logger;
import com.threed.jpct.Mesh;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.RGBColor;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.World;
import com.threed.jpct.util.MemoryHelper;
import java.lang.reflect.Field;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;
/**
*
* @author Unni Vemanchery Mana
*/
public class MainActivity extends Activity {
// Used to handle pause and resume...
private static MainActivity master = null;
private GLSurfaceView mGLView;
private MyRenderer renderer = null;
private FrameBuffer fb = 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 cube = null;
private Object3D box = null;
private Object3D newbox = null;
private Object3D line3d = null;
private int fps = 0;
private Light sun = null;
private float xDelta = 1.5f;
private float yDelta = 0;
private float zDelta = 0;
private int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
Logger.log("onCreate");
if (master != null) {
copy(master);
}
super.onCreate(savedInstanceState);
mGLView = new GLSurfaceView(getApplication());
mGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
// Ensure that we get a 16bit framebuffer. Otherwise, we'll fall
// back to Pixelflinger on some device (read: Samsung I7500)
int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };
EGLConfig[] configs = new EGLConfig[1];
int[] result = new int[1];
egl.eglChooseConfig(display, attributes, configs, 1, result);
return configs[0];
}
});
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();
}
private void copy(Object src) {
try {
Logger.log("Copying data from master Activity!");
Field[] fs = src.getClass().getDeclaredFields();
for (Field f : fs) {
f.setAccessible(true);
f.set(this, f.get(src));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
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);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
if( keyCode == KeyEvent.KEYCODE_A ) {
System.out.println(" total angle swept " + box.getTransformedCenter().calcAngle(box.getXAxis()) );
Mesh mesh = box.getMesh();
VertexController vc = new VertexController(mesh);
mesh.setVertexController(vc,true);
mesh.applyVertexController();
xDelta+=1.5f;
return true;
}
return true;
}
private Object3D createLine (SimpleVector pointA, SimpleVector pointB, float width, String textureName)
{
Object3D line = new Object3D( 8 );
float offset = width / 2.0f;
// Quad A:
line.addTriangle( new SimpleVector( pointA.x, pointA.y - offset, pointA.z ), 0, 0,
new SimpleVector( pointA.x, pointA.y + offset, pointA.z ), 0, 1,
new SimpleVector( pointB.x, pointB.y + offset, pointB.z ), 1, 1
);
line.addTriangle( new SimpleVector( pointB.x, pointB.y + offset, pointB.z ), 0, 0,
new SimpleVector( pointB.x, pointB.y - offset, pointB.z ), 0, 1,
new SimpleVector( pointA.x, pointA.y - offset, pointA.z ), 1, 1
);
// Quad A, back-face:
line.addTriangle( new SimpleVector( pointB.x, pointB.y - offset, pointB.z ), 0, 0,
new SimpleVector( pointB.x, pointB.y + offset, pointB.z ), 0, 1,
new SimpleVector( pointA.x, pointA.y + offset, pointA.z ), 1, 1
);
line.addTriangle( new SimpleVector( pointA.x, pointA.y + offset, pointA.z ), 0, 0,
new SimpleVector( pointA.x, pointA.y - offset, pointA.z ), 0, 1,
new SimpleVector( pointB.x, pointB.y - offset, pointB.z ), 1, 1
);
// Quad B:
line.addTriangle( new SimpleVector( pointA.x, pointA.y, pointA.z + offset ), 0, 0,
new SimpleVector( pointA.x, pointA.y, pointA.z - offset ), 0, 1,
new SimpleVector( pointB.x, pointB.y, pointB.z - offset ), 1, 1
);
line.addTriangle( new SimpleVector( pointB.x, pointB.y, pointB.z - offset ), 0, 0,
new SimpleVector( pointB.x, pointB.y, pointB.z + offset ), 0, 1,
new SimpleVector( pointA.x, pointA.y, pointA.z + offset ), 1, 1
);
// Quad B, back-face:
line.addTriangle( new SimpleVector( pointB.x, pointB.y, pointB.z + offset ), 0, 0,
new SimpleVector( pointB.x, pointB.y, pointB.z - offset ), 0, 1,
new SimpleVector( pointA.x, pointA.y, pointA.z - offset ), 1, 1
);
line.addTriangle( new SimpleVector( pointA.x, pointA.y, pointA.z - offset ), 0, 0,
new SimpleVector( pointA.x, pointA.y, pointA.z + offset ), 0, 1,
new SimpleVector( pointB.x, pointB.y, pointB.z + offset ), 1, 1
);
// If you don't want the line to react to lighting:
line.setLighting( Object3D.LIGHTING_NO_LIGHTS );
line.setAdditionalColor( RGBColor.WHITE );
// done
return line;
}
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) {
if (fb != null) {
fb.dispose();
}
fb = new FrameBuffer(gl, w, h);
if (master == null) {
world = new World();
world.setAmbientLight(20, 20, 20);
sun = new Light(world);
sun.setIntensity(250, 250, 250);
// Create a texture out of the icon...:-)
//Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(getResources().getDrawable(R.drawable.icon)), 64, 64));
//TextureManager.getInstance().addTexture("texture", texture);
cube = Primitives.getCube(1);
//cube.calcTextureWrapSpherical();
//cube.setTexture("texture");
//cube.strip();
//cube.build();
box = Primitives.getBox(1.5f, 0.75f);
box.setAdditionalColor(RGBColor.RED);
// box.setTransparency(0);
box.translate(-10, 10, 0);
box.translateMesh();
// box.strip();
System.out.println(" first box center "+ box.getOrigin());
// adding one more cube
newbox = Primitives.getBox(1.5f, 0.70f);
newbox.setAdditionalColor(RGBColor.RED);
// newbox.setTransparency(0);
newbox.translate((-10.0f + 1.0f), 10.0f, -1.0f);
newbox.translateMesh();
newbox.strip();
world.addObject(cube);
world.addObject(box);
world.addObject(newbox);
line3d = createLine(new SimpleVector(5,0,0), new SimpleVector(5,10,0), 2, "");
line3d.translate(5, 0, 0);
line3d.translateMesh();
world.addObject(line3d);
Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
cam.lookAt(cube.getTransformedCenter());
SimpleVector sv = new SimpleVector();
sv.set(cube.getTransformedCenter());
sv.y -= 100;
sv.z -= 100;
sun.setPosition(sv);
MemoryHelper.compact();
if (master == null) {
Logger.log("Saving master Activity!");
master = MainActivity.this;
}
}
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
public void onDrawFrame(GL10 gl) {
if (touchTurn != 0) {
cube.rotateY(touchTurn);
box.rotateY(touchTurn) ;
line3d.rotateY(touchTurn);
touchTurn = 0;
yDelta +=0.02f;
}
if (touchTurnUp != 0) {
cube.rotateX( touchTurnUp );
touchTurnUp = 0;
}
fb.clear(back);
world.renderScene(fb);
world.draw(fb);
fb.display();
if (System.currentTimeMillis() - time >= 1000) {
//Logger.log(fps + "fps");
fps = 0;
time = System.currentTimeMillis();
}
fps++;
}
}
class VertexController extends GenericVertexController {
public VertexController( Mesh toCheck ) {
// super.init(toCheck, true);
}
public void apply() {
SimpleVector[] vertices = getSourceMesh();
SimpleVector[] destination = getDestinationMesh();
for (int i = 0; i < vertices.length; i++){
System.out.println("before vertices[i].x "+vertices[i].x);
//vertices[i].x *= vertices[i].x * 5;
System.out.println("after vertices[i].x "+vertices[i].x);
destination[i].x+=destination[i].x + xDelta;
}
this.updateMesh();
}
}
}