Version updates!

Started by EgonOlsen, March 28, 2010, 09:47:50 PM

Previous topic - Next topic

Thomas.

Is it correct?
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.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Logger;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.RGBColor;
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;

/**
* @author EgonOlsen
*
*/
public class HelloWorld extends Activity {

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 cube0 = null;
private Object3D cube1 = null;
private Object3D cube2 = null;
private Object3D cube3 = null;
private Object3D dummy = null;

private Texture renderTarget = null;

private int fps = 0;

private int cnt = 0;

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) {
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();
}

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();
private boolean stop = false;

public MyRenderer() {
Config.renderTargetsAsSubImages = false;
}

public void stop() {
stop = true;
}

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(255, 255, 255);

Texture texture = new Texture(BitmapHelper.rescale(
BitmapHelper.convert(getResources().getDrawable(R.drawable.icon)), 64, 64));
TextureManager.getInstance().addTexture("texture", texture);

dummy = Object3D.createDummyObj();

cube0 = Primitives.getCube(10);
cube0.rotateY(-(float) Math.PI / 4f);
cube0.rotateMesh();
cube0.clearRotation();
cube0.calcTextureWrapSpherical();
cube0.setTexture("texture");
cube0.strip();
cube0.build();

cube1 = cube0.cloneObject();
cube2 = cube0.cloneObject();
cube3 = cube0.cloneObject();

world.addObject(cube0);
world.addObject(cube1);
world.addObject(cube2);
world.addObject(cube3);

cube0.translate(-20, -20, 0);
cube1.translate(20, -20, 0);
cube2.translate(-20, 20, 0);
cube3.translate(20, 20, 0);

cube0.addParent(dummy);
cube1.addParent(dummy);
cube2.addParent(dummy);
cube3.addParent(dummy);

Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 100);

renderTarget = new Texture(256, 256, RGBColor.RED);

MemoryHelper.compact();

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

// Logger.setLogLevel(Logger.DEBUG);
}
}

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

public void onDrawFrame(GL10 gl) {

try {
if (!stop) {
if (touchTurn != 0) {
dummy.rotateY(touchTurn);
touchTurn = 0;
}

if (touchTurnUp != 0) {
dummy.rotateX(touchTurnUp);
touchTurnUp = 0;
}

cnt++;

Config.autoMaintainAspectRatio = false;
fb.setRenderTarget(renderTarget);
fb.clear();
/*world.renderScene(fb);
world.draw(fb);
fb.display();*/
fb.blit(TextureManager.getInstance().getTexture("texture"), 0, 0, 0, 0, 64, 64, false);
fb.removeRenderTarget();
TextureManager.getInstance().replaceTexture("texture", renderTarget);
Config.autoMaintainAspectRatio = true;

fb.clear(back);
world.renderScene(fb);
world.draw(fb);
fb.blit(renderTarget, 0, 0, 0, 0, 256, 256, false);
fb.display();

if (System.currentTimeMillis() - time >= 1000) {
Logger.log(fps + "fps");
fps = 0;
time = System.currentTimeMillis();
}
fps++;
} else {
if (fb != null) {
fb.dispose();
fb = null;
}
}
} catch (Exception e) {
Logger.log(e, Logger.MESSAGE);
}
}
}
}



EgonOlsen

The code seems to be correct, but the result should actually be a black texture, not a red one. Drivers ARE flaky with coyping data back from the frame buffer. If it doesn't work on your phone with neither setting, then it just doesn't work. There's no magic that i can apply to make it work. I can only repeat myself: Don't rely on it for your application. Make it an option for devices that can handle it, but don't base the app on it.
That said, what exactly do you want to use this feature for?

Thomas.

Last screen is from emulator... I have elevator in my game and I want control it by panel in game, so I need render floor names and position of elevator to texture

EgonOlsen

You don't need render to texture for that. You can either create a texture containing all these floor-names and stuff in different parts of it and just switch the u/v-coordinates...that might be the fastest solution. Or you can prerender those textures and just switch them on demand (that's what i would do). Or you can use an ITextureEffect to fiddle around with the texture on the pixel level to update it or you can render fonts into a Bitmap using standard Android methods and create a new texture from that each time. Using render to texture for this is the worst possible and most flaky solution IMHO...and i repeat myself again: Don't do it! It's flaky! No idea why the emulator is red in this screen shot (what emulator is that? Mine looks totally different but then again, i'm not using 2.3). Might be that your test case's code isn't correct and i haven't seen it. Fact is, that the code (actually it's one line with a gl call...you can't do much about it if it doesn't work) and my test cases work fine in the desktop version, on my phone, on other people's phones and in all the emulator versions that i've tried. Why it doesn't work for you...no idea. But i think that i've done all that i could to make it work as good as possible. For me, there's nothing left to do here.

Thomas.

Have you any example of texture coordinates and sets more layers of textures? I set this just in 3D max, I never saw, how it looking in code

EgonOlsen

Not layers. Just multiple images in one texture. I think i'm using this in Robombs to animate the explosion billboards. However, using multiple regenerated textures might be the simpler solution unless you your elevator has dozens of floors.

EgonOlsen


EgonOlsen

New version with more gc friendly behaviour of ellipsoid collision detection. A bug prevented a former optimization from kicking in, so that too many matrices got created: http://www.jpct.net/jpct-ae/download/alpha/jpct_ae.jar

EgonOlsen

New version with reduces object creations when calling calcMinDistance directly on Object3Ds. Get it here: http://www.jpct.net/jpct-ae/download/alpha/jpct_ae.jar

Kernle 32DLL

That just made my day :) Thanks!

EgonOlsen

As you all know...well, maybe not but anyway...jPCT-AE has support for libgdx (http://code.google.com/p/libgdx/) since a few releases. You can drop in the libs and the native parts as described on the libgdx page and jPCT-AE will auto-detect it and use it for some stuff. The main purpose is to increase buffer upload speeds for animated objects. My old Samsung Galaxy was too slow to show any benefit of this, so i tried it again with my new Nexus S. I ran An3DBenchXL which has a test that pretty much depends on buffer uploads: "Ninjas' garden"...the results, at least with Gingerbread, were disappointing: Framerate increased from 16.08fps using jPCT-AE's optimized buffer uploads to 17.37fps when using libgdx for this. While this is nice to have, i really expected more. I guess Google has improved the buffer uploads to a degree where it just doesn't matter anymore. It might be a different story with 2.1 or 2.2 though...

Thomas.

You have nexus S?? :) it is nice message, so we will look forward to OGL 2 support :)

EgonOlsen

I've already looked into it...it's a real pain to add support for it, because they scrapped so many things in 2.0. Plus i have to write a monster shader to mimic everything that the fixed function pipeline does...it'll take some time...

Thomas.

What about the new version, any new info? :)

EgonOlsen

No, i'm not working on that ATM...as said, it'll take some time.