Keypad input event callbacks not working when I use OpenGL

Put your problem here if it does not fit any of the other categories.

Keypad input event callbacks not working when I use OpenGL

Postby TheChuckster » Sat Nov 17, 2007 4:23 pm

I tried to modify the OpenGL ES 3D Cube demo to rotate based on keypad
input using the directional arrow keys. I use the onKeyDown callback
override function in order to handle the key press events. However,
the callback function is never called when I use OpenGL ES.

I tried the keyboard input callback function with the Hello World demo
that just uses the Android GUI, and it works perfectly. It seems like
using OpenGL with Android is what's causing the callback function to
not show up with any keyboard events.

I may have overlooked something simple with the Android API, since
this is only my second time ever programming in Java (coming off of C+
+ and Nintendo DS handheld development). Hopefully, the problem is
simple to solve.

Current Source:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.hello;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.OpenGLContext;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.os.Message;
  10. import android.os.SystemClock;
  11. import android.view.KeyEvent;
  12. import android.view.View;
  13.  
  14. import java.nio.ByteBuffer;
  15. import java.nio.ByteOrder;
  16. import java.nio.IntBuffer;
  17.  
  18. import javax.microedition.khronos.opengles.GL10;
  19.  
  20. public class HelloAndroid extends Activity {
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle icicle)
  24.     {
  25.         super.onCreate(icicle);    
  26.         setContentView(new GLView( getApplication() ));
  27.     }
  28.  
  29.     @Override
  30.     protected void onResume()
  31.     {
  32.         super.onResume();
  33.     }
  34.  
  35.     @Override
  36.     protected void onStop()
  37.     {
  38.         super.onStop();
  39.     }
  40. }
  41.  
  42. class GLView extends View
  43. {
  44.     public GLView(Context context)
  45.     {
  46.         super(context);
  47.  
  48.         mGLContext = new OpenGLContext(0);
  49.         mCube = new Cube();
  50.         mAnimate = false;
  51.     }
  52.  
  53.     @Override
  54.     protected void onAttachedToWindow() {
  55.         mAnimate = true;
  56.         Message msg = mHandler.obtainMessage(INVALIDATE);
  57.         mNextTime = SystemClock.uptimeMillis();
  58.         mHandler.sendMessageAtTime(msg, mNextTime);
  59.         super.onAttachedToWindow();
  60.     }
  61.  
  62.     @Override
  63.     protected void onDetachedFromWindow() {
  64.         mAnimate = false;
  65.         super.onDetachedFromWindow();
  66.     }
  67.  
  68.     @Override
  69.     protected void onDraw(Canvas canvas) {
  70.         if (true) {
  71.  
  72.             GL10 gl = (GL10)(mGLContext.getGL());
  73.             mGLContext.waitNative(canvas, this);
  74.  
  75.             int w = getWidth();
  76.             int h = getHeight();
  77.  
  78.             gl.glViewport(0, 0, w, h);
  79.  
  80.             float ratio = (float)w / h;
  81.             gl.glMatrixMode(gl.GL_PROJECTION);
  82.             gl.glLoadIdentity();
  83.             gl.glFrustumf(-ratio, ratio, -1, 1, 2, 12);
  84.  
  85.             gl.glDisable(gl.GL_DITHER);
  86.             gl.glClearColor(0,0,0,1);
  87.             gl.glEnable(gl.GL_SCISSOR_TEST);
  88.             gl.glScissor(0, 0, w, h);
  89.             gl.glClear(gl.GL_COLOR_BUFFER_BIT);
  90.  
  91.             gl.glMatrixMode(gl.GL_MODELVIEW);
  92.             gl.glLoadIdentity();
  93.             gl.glTranslatef(0, 0, -3.0f);
  94.             gl.glScalef(0.5f, 0.5f, 0.5f);
  95.  
  96.             gl.glRotatef(mAngleX,        1, 0, 0);
  97.             gl.glRotatef(mAngleY,        0, 1, 0);
  98.  
  99.             gl.glColor4f(0.7f, 0.7f, 0.7f, 1.0f);
  100.             gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
  101.             gl.glEnableClientState(gl.GL_COLOR_ARRAY);
  102.             gl.glEnable(gl.GL_CULL_FACE);
  103.  
  104.             mCube.draw(gl);
  105.  
  106.         mGLContext.waitGL();
  107.         }
  108.     }
  109.  
  110.     /*
  111.      * handles keypad input and rotates cube
  112.      *
  113.      * (non-Javadoc)
  114.      *
  115.      * @see android.view.View#onKeyDown(int, android.os.KeyEvent)
  116.      */
  117.     @Override
  118.     public boolean onKeyDown(int keyCode, KeyEvent msg) {
  119.         if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
  120.             mAngleY+=0.5f;
  121.             return true;
  122.         }
  123.  
  124.         if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
  125.             mAngleY-=0.5;
  126.             return true;
  127.         }
  128.  
  129.         if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
  130.             mAngleX-=0.5;
  131.             return true;
  132.         }
  133.  
  134.         if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
  135.             mAngleX+=0.5;
  136.             return true;
  137.         }
  138.  
  139.         return false;
  140.     }
  141.  
  142.     // ------------------------------------------------------------------------
  143.  
  144.     private static final int INVALIDATE = 1;
  145.  
  146.     private final Handler mHandler = new Handler() {
  147.         @Override
  148.         public void handleMessage(Message msg) {
  149.             if (mAnimate && msg.what == INVALIDATE) {
  150.                 invalidate();
  151.                 msg = obtainMessage(INVALIDATE);
  152.                 long current = SystemClock.uptimeMillis();
  153.                 if (mNextTime < current) {
  154.                     mNextTime = current + 20;
  155.                 }
  156.                 sendMessageAtTime(msg, mNextTime);
  157.                 mNextTime += 20;
  158.             }
  159.         }
  160.     };
  161.  
  162.     private OpenGLContext   mGLContext;
  163.     private Cube            mCube;
  164.     private float           mAngleX = 0, mAngleY = 0;
  165.     private long            mNextTime;
  166.     private boolean         mAnimate;
  167. }
  168.  
  169. class Cube
  170. {
  171.     public Cube()
  172.     {
  173.         int one = 0x10000;
  174.         int vertices[] = {
  175.                -one, -one, -one,
  176.                 one, -one, -one,
  177.                 one,  one, -one,
  178.                -one,  one, -one,
  179.                -one, -one,  one,
  180.                 one, -one,  one,
  181.                 one,  one,  one,
  182.                -one,  one,  one,
  183.             };
  184.  
  185.         int colors[] = {
  186.                   0,    0,    0,  one,
  187.                 one,    0,    0,  one,
  188.                 one,  one,    0,  one,
  189.                   0,  one,    0,  one,
  190.                   0,    0,  one,  one,
  191.                 one,    0,  one,  one,
  192.                 one,  one,  one,  one,
  193.                   0,  one,  one,  one,
  194.             };
  195.  
  196.         byte indices[] = {
  197.                 0, 4, 5,    0, 5, 1,
  198.                 1, 5, 6,    1, 6, 2,
  199.                 2, 6, 7,    2, 7, 3,
  200.                 3, 7, 4,    3, 4, 0,
  201.                 4, 7, 6,    4, 6, 5,
  202.                 3, 0, 1,    3, 1, 2
  203.         };
  204.  
  205.     ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
  206.     vbb.order(ByteOrder.nativeOrder());
  207.     mVertexBuffer = vbb.asIntBuffer();
  208.     mVertexBuffer.put(vertices);
  209.     mVertexBuffer.position(0);
  210.  
  211.     ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
  212.     cbb.order(ByteOrder.nativeOrder());
  213.     mColorBuffer = cbb.asIntBuffer();
  214.     mColorBuffer.put(colors);
  215.     mColorBuffer.position(0);
  216.  
  217.     mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
  218.     mIndexBuffer.put(indices);
  219.     mIndexBuffer.position(0);
  220.     }
  221.  
  222.     public void draw(GL10 gl)
  223.     {
  224.         gl.glFrontFace(gl.GL_CW);
  225.         gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);
  226.         gl.glColorPointer(4, gl.GL_FIXED, 0, mColorBuffer);
  227.         gl.glDrawElements(gl.GL_TRIANGLES, 36, gl.GL_UNSIGNED_BYTE, mIndexBuffer);
  228.     }
  229.  
  230.     private IntBuffer   mVertexBuffer;
  231.     private IntBuffer   mColorBuffer;
  232.     private ByteBuffer  mIndexBuffer;
  233. }
Parsed in 0.054 seconds, using GeSHi 1.0.8.4
http://thechuckster.homelinux.com/
Programming: The art of turning caffeine into error messages.
TheChuckster
Freshman
Freshman
 
Posts: 2
Joined: Sat Nov 17, 2007 4:22 pm
Location: Pittsburgh

Top

Postby plusminus » Sat Nov 17, 2007 4:35 pm

I really appreciate that you posted your code here :!:

I've put this to the "Google Android --> OpenGL ES" until we made it fully working.

Edit: I fixed it and turned into a tutorial i'll post in some minutes...

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby TheChuckster » Sat Nov 17, 2007 5:00 pm

That's good that you made it into a tutorial... now others can learn from it too.
http://thechuckster.homelinux.com/
Programming: The art of turning caffeine into error messages.
TheChuckster
Freshman
Freshman
 
Posts: 2
Joined: Sat Nov 17, 2007 4:22 pm
Location: Pittsburgh

Postby plusminus » Sat Nov 17, 2007 5:53 pm

I'm done. :)
Check it out :arrow: here

The Fix was, reacting to KeyEvents not on View-Level but on Activity-Level!

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 18 guests