Drawing of controls.

Started by J2EEGuy, January 08, 2013, 03:24:42 PM

Previous topic - Next topic

J2EEGuy

I've managed to implement some basic controls by monitoring touch events for my FPS. As for drawing the joy sticks on screen, would you guys recommend drawing some basic shapes that are always in front of the camera? Or is there a better way to do it.


EgonOlsen

Or, if you want it to be a real 3d shape, consider to use a second World-instance with a static camera for it. That way, you don't have to worry about alignment with the current camera.

J2EEGuy

At first I tried to do actual math with the vectors, but it just wasn't happening. The second world view worked only because I could not get blit to run :/
When I'm thinking clearer I'll read up on framebuffers. This code is pretty hacked up.

For now, this is my working code with no blit. It uses three models, a gun, and plane for the ground, and an animated double cube setup. If someone could spontaneously inject a working blit method call that would be tits.
package com.threed.jpct.example;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
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.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.widget.Toast;

import com.threed.jpct.Animation;
import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Light;
import com.threed.jpct.Loader;
import com.threed.jpct.Logger;
import com.threed.jpct.Matrix;
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
*
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public class HelloWorld extends Activity {

// Used to handle pause and resume...
InputStream is;

private static float move = 0;
private int controlx;
private int controly;
private int ylimiter = 0;
private float ind = 0;
private static HelloWorld master = null;

private float objectscale =1f;
private GLSurfaceView mGLView;
private MyRenderer renderer = null;
private FrameBuffer fb = null;
private World world = null;
private World world2 = null;
private RGBColor back = new RGBColor(50, 50, 100);
private int derp =0;
private float touchTurn = 0;
private float touchTurnUp = 0;

private float xpos = -1;
private float ypos = -1;

private Object3D cube = null;
private Object3D thing = null;
private Object3D pistola = null;
private Object3D testplane = null;
private int fps = 0;
SimpleVector AB = null;

 
private Light sun = null;
private Light sun2 = null;
SimpleVector sv2 = new SimpleVector();
Camera cam;
Camera cam2;
//Camera cam = world.getCamera();
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);
}
}

public boolean onTouchEvent(MotionEvent me) {

if (me.getAction() == MotionEvent.ACTION_DOWN) {

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

int y =metrics.heightPixels;
int x =metrics.widthPixels;
Context context = getApplicationContext();
CharSequence text = "Test" + x + y + cam.getYAxis();;
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

controlx = (int)me.getX(); 
    controly = (int)me.getY();
   
   // if (x1 <= 20){
   
   // }
   // SimpleVector campos;
    //campos = cam.getPosition();
   
   
    //SimpleVector camlook;
   // camlook = cam.getDirection();
    //cube.translate(camlook);
//the difference between out position and where we are looking

   
    // move thing and set camera to it again
   
 
//cube.translate(movething);
 

xpos = me.getX();
ypos = me.getY();
doAnim();

return true;
}

if (me.getAction() == MotionEvent.ACTION_UP) {

controlx = 0;
controly = 0;
xpos = -1;
ypos = -1;
touchTurn = 0;
touchTurnUp = 0;
//dfoAnim();
//cam.rotateAxis(cam.getXAxis(), .1f);
return true;
}

if (me.getAction() == MotionEvent.ACTION_MOVE) {
float xd = me.getX() - xpos;
float yd = me.getY() - ypos;
int xx = (int)me.getX(); 
    int yy = (int)me.getY();
xpos = me.getX();
ypos = me.getY();

touchTurn = xd / -100f;
touchTurnUp = yd / -100f;
if (xx > controlx){
cam.rotateAxis(cam.getYAxis(), +.025f);
}
if (xx < controlx){
cam.rotateAxis(cam.getYAxis(), -.025f);
}
//if ((cam.getXAxis().y > 0 && 170 < Math.PI / 4.2) || (cam.getXAxis().y < 0 && 170 > -Math.PI / 4.2)) {

if (yy > controly && ylimiter > -25){
cam.rotateX(-.025f);
ylimiter --;
}
if (yy < controly && ylimiter < 25){
cam.rotateX(.025f);
ylimiter ++;
}

if (xx > controlx && yy > controly){
cam.rotateAxis(cam.getYAxis(), +.025f);
if (ylimiter > -25){
cam.rotateX(-.025f);
ylimiter --;}

}
if (xx < controlx && yy < controly){
cam.rotateAxis(cam.getYAxis(), -.025f);
if (ylimiter <= 25){
cam.rotateX(+.025f);
ylimiter ++;
}

}
//}




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();
}
fb = new FrameBuffer(gl, w, h);

if (master == null) {

world = new World();
world2 = new World();
Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(getResources().getDrawable(R.drawable.gun)), 128, 128));
TextureManager.getInstance().addTexture("texture", texture);

//fb.blit(texture, 0, 0, 0, 0, 64, 64, false);

world.setAmbientLight(20, 20, 20);
world2.setAmbientLight(20, 20, 20);
sun = new Light(world);
sun2 = new Light(world2);
sun.setIntensity(250, 250, 250);
sun2.setIntensity(250, 250, 250);
// Create a texture out of the icon...:-)


//fb.blit(texture, 10, 10, 10,10, 100, 100, false);
//fb.display();
//cube = Primitives.getCube(.2f);
//cube.calcTextureWrapSpherical();
//cube.setTexture("texture");

//cube.strip();
//cube.build();
try {
thing = loadModel(1, objectscale);
pistola = loadModel(4, 2f);
testplane = loadModel(5, 10f);


//pistola.build();
Animation anim = new Animation(3);
anim.createSubSequence("walk");

anim.addKeyFrame(loadModel(2, objectscale).getMesh());
derp =1;
anim.addKeyFrame(loadModel(3, objectscale).getMesh());


//anim.addKeyFrame(loadModel(2, 1f).getMesh());
thing.setAnimationSequence(anim);
thing.calcTextureWrapSpherical();
thing.setTexture("texture");

pistola.calcTextureWrapSpherical();
pistola.setTexture("texture");
testplane.calcTextureWrapSpherical();
testplane.setTexture("texture");
    SimpleVector movething = new SimpleVector();
movething.x = 2f;
pistola.translate(movething);
SimpleVector moveplane = new SimpleVector();
moveplane.y = 4f;
testplane.translate(moveplane);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}





     


world.addObject(thing);
//world2.addObject(cube);
world2.addObject(pistola);
world.addObject(testplane);
cam = world.getCamera();
cam2 = world2.getCamera();
//Camera cam = world.getCamera();

cam.moveCamera(Camera.CAMERA_MOVEOUT, 10);
cam2.moveCamera(Camera.CAMERA_MOVEOUT, 5);
cam.moveCamera(Camera.CAMERA_MOVEUP, 3);
//cube.align(cam);
//cam.lookAt(thing.getTransformedCenter());


cam.lookAt(thing.getTransformedCenter());
//cam2.lookAt(pistola.getTransformedCenter());
SimpleVector sv = new SimpleVector();
sv.set(thing.getTransformedCenter());
sv.y -= 100;
sv.z -= 100;
sun.setPosition(sv);
sun2.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) {
thing.rotateY(touchTurn);
touchTurn = 0;

}

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

fb.clear(back);
world.renderScene(fb);
world2.renderScene(fb);
world2.draw(fb);
world.draw(fb);


fb.display();

if (System.currentTimeMillis() - time >= 1000) {
Logger.log(fps + "fps");
fps = 0;
time = System.currentTimeMillis();
}
fps++;
}
}
private Object3D loadModel(int fileid, float scale) throws FileNotFoundException {
    //InputStream is = getResources().openRawResource(R.raw.animated); // copy this and external ref
    InputStream is = getKeyFrameResource(fileid);

        Object3D[] model = Loader.load3DS(is,scale);
        Object3D o3d = new Object3D(0);
        Object3D temp = null;
        for (int i = 0; i < model.length; i++) {
            temp = model[i];
            temp.setCenter(SimpleVector.ORIGIN);
            temp.rotateX((float)( -.5*Math.PI));
            temp.rotateMesh();
            temp.setRotationMatrix(new Matrix());
            o3d = Object3D.mergeObjects(o3d, temp);
           
            o3d.calcBoundingBox();
            o3d.calcNormals();
            if (derp == 1){
            o3d.build();
            }
           
        }
        return o3d;
    }



public void doAnim() {
        ind += 0.1f;
        if (ind >= 1f) {
                ind = 0;
        }
       
        Logger.log("frame: "+ind);
        thing.animate(ind, thing.getAnimationSequence().getSequence("walk"));
       
}
public InputStream getKeyFrameResource(int fileid){
InputStream dl = null;

switch (fileid) {
            case 1: 
            dl = getResources().openRawResource(R.raw.animationnew1);
                     
            break;
            case 2: 
            dl = getResources().openRawResource(R.raw.animationnew);
            break;
            case 3: 
            dl = getResources().openRawResource(R.raw.animationnew1);
            break;
            case 4: 
            dl = getResources().openRawResource(R.raw.pistol2);
            break;
            case 5: 
            dl = getResources().openRawResource(R.raw.plane);
            break;
               
       
       
    }
return dl;
}
}

EgonOlsen

What is the actual problem with using blit()?

J2EEGuy

The picture simply does not show up on the screen when i call the blit method from anywhere.

EgonOlsen

Makes no sense...can you post a code snippet that shows how and where you have been using it?

J2EEGuy

I posted this line EVERYWHERE in my code.

fb.blit(texture, 10, 10, 10,10, 100, 100, false);

nothing happens.

J2EEGuy

Thanks again for the support, i was able to get blit to work by reading this guys post.
http://www.jpct.net/forum2/index.php/topic,2940.msg21620.html#msg21620
It works superbly. When I finish my game i will provide the source code and animations here for everyone to attack.