Author Topic: Help in HelloWorld example  (Read 930 times)

Offline Ktag

  • byte
  • *
  • Posts: 3
    • View Profile
Help in HelloWorld example
« on: January 15, 2015, 06:25:43 am »
Hello everyone !
I am using jpct and learning it through wiki and examples,
I am facing a problem in this code.
I want the camera to follow the box which is on a plane named "cube".
But in return it gave me a "force close prompt"
My Device is samsung gt-s5360
Android 2.3.6

The code------
Code: [Select]
package com.threed.jpct.example;

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;

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.FrameBuffer;
import com.threed.jpct.Light;
import com.threed.jpct.Logger;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
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.BitmapHelper;
import com.threed.jpct.util.MemoryHelper;

/**
 * A simple demo. This shows more how to use jPCT-AE than it shows how to write
 * a proper application for Android. It includes basic activity management to
 * handle pause and resume...
 *
 * @author EgonOlsen
 *
 */
public class HelloWorld extends Activity {

// Used to handle pause and resume...
private static HelloWorld 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 int fps = 0;
private boolean gl2 = true;

private Light sun = null;

protected void onCreate(Bundle savedInstanceState) {

Logger.log("onCreate");

if (master != null) {
copy(master);
}

super.onCreate(savedInstanceState);
mGLView = new GLSurfaceView(getApplication());

if (gl2) {
mGLView.setEGLContextClientVersion(2);
} else {
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). Current devices usually don't need this, but it
// doesn't hurt either.
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);
}
}

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) {
if (fb != null) {
fb.dispose();
}

if (gl2) {
fb = new FrameBuffer(w, h); // OpenGL ES 2.0 constructor
} else {
fb = new FrameBuffer(gl, w, h); // OpenGL ES 1.x constructor
}

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);

SimpleVector boxpos = new SimpleVector();
boxpos.set(cube.getTransformedCenter());
boxpos.y -= 50 ;
boxpos.z += 5 ;

box = Primitives.getCube(5);
box.calcTextureWrapSpherical();
box.setTexture("texture");
box.setOrigin(boxpos);
box.strip();
box.build();

cube = Primitives.getPlane(10,10);
cube.calcTextureWrapSpherical();
cube.setTexture("texture");
cube.strip();
cube.build();

world.addObject(cube);
world.addObject(box);

Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
cam.lookAt(box.getTransformedCenter());

SimpleVector sv = new SimpleVector();
sv.set(box.getTransformedCenter());
sv.y -= 100;
sv.z -= 100;
sun.setPosition(sv);
MemoryHelper.compact();

if (master == null) {
Logger.log("Saving master Activity!");
master = HelloWorld.this;
}
}
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}

public void onDrawFrame(GL10 gl) {
if (touchTurn != 0) {
box.rotateZ(touchTurn);
touchTurn = 0;
}

if (touchTurnUp != 0) {
box.translate(0, touchTurnUp, 0);
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++;
}
}
}
« Last Edit: January 15, 2015, 08:32:15 am by EgonOlsen »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11749
    • View Profile
    • http://www.jpct.net
Re: Help in HelloWorld example
« Reply #1 on: January 15, 2015, 08:33:12 am »
Just have a look at the logcat output from your device and post the actual exception.

Offline Ktag

  • byte
  • *
  • Posts: 3
    • View Profile
Re: Help in HelloWorld example
« Reply #2 on: January 15, 2015, 12:13:26 pm »
It shows nothing,
Every time I open the logcat it says "run the app" but even after running the app
It says "run the app"

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11749
    • View Profile
    • http://www.jpct.net
Re: Help in HelloWorld example
« Reply #3 on: January 15, 2015, 12:26:16 pm »
Then it's not the correct log output you are looking at...try to switch to DDMS view in your IDE. You should be able to see the complete logging output of the device.

Offline Ktag

  • byte
  • *
  • Posts: 3
    • View Profile
Re: Help in HelloWorld example
« Reply #4 on: January 15, 2015, 03:59:02 pm »
How ?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11749
    • View Profile
    • http://www.jpct.net
Re: Help in HelloWorld example
« Reply #5 on: January 15, 2015, 11:13:19 pm »
Which IDE are you using?