opengl + orientation sensor

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

opengl + orientation sensor

Postby Lord Otori » Mon Mar 22, 2010 4:26 pm

Guys,
Let me first say that I've been trying to find some information about this for the last 3 days and still cant seam to find where the problem is.
I've done this tutorial:
http://www.droidnova.com/android-3d-gam ... i,312.html

But I've got an idea, what if I use the orientation sensor instead of using the touch event, because the touch event gives me only 2 values and the orientation sensor 3 which is perfect for the RGB part.

Here lies the problem. I need to find out how to mix the sensor data with the opengl tutorial.

Let's see some code first.

Vortex.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.droidnova.android.games.vortex;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9.  
  10.  
  11.  
  12.  
  13. public class Vortex extends Activity {
  14.  
  15.     @SuppressWarnings("unused")
  16.  
  17.         private static final String LOG_TAG = Vortex.class.getSimpleName();
  18.  
  19.     private VortexView _vortexView;
  20.  
  21.    
  22.  
  23.     @Override
  24.  
  25.     protected void onCreate(Bundle savedInstanceState) {
  26.  
  27.         super.onCreate(savedInstanceState);
  28.  
  29.         _vortexView = new VortexView(this);
  30.  
  31.         setContentView(_vortexView);
  32.  
  33.     }
  34.  
  35. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



VortexRenderer.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.droidnova.android.games.vortex;
  2.  
  3.  
  4.  
  5. import javax.microedition.khronos.egl.EGLConfig;
  6.  
  7. import javax.microedition.khronos.opengles.GL10;
  8.  
  9.  
  10.  
  11. import android.opengl.GLSurfaceView;
  12.  
  13. import android.util.Log;
  14.  
  15.  
  16.  
  17. public class VortexRenderer implements GLSurfaceView.Renderer {
  18.  
  19.     @SuppressWarnings("unused")
  20.  
  21.         private static final String LOG_TAG = VortexRenderer.class.getSimpleName();
  22.  
  23.    
  24.  
  25.     private float _red = 0.9f;
  26.  
  27.     private float _green = 0.2f;
  28.  
  29.     private float _blue = 0.5f;
  30.  
  31.    
  32.  
  33.     @Override
  34.  
  35.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  36.  
  37.         // Do nothing special.
  38.  
  39.     }
  40.  
  41.  
  42.  
  43.     @Override
  44.  
  45.     public void onSurfaceChanged(GL10 gl, int w, int h) {
  46.  
  47.         gl.glViewport(0, 0, w, h);
  48.  
  49.     }
  50.  
  51.  
  52.  
  53.     @Override
  54.  
  55.     public void onDrawFrame(GL10 gl) {
  56.  
  57.         // define the color we want to be displayed as the "clipping wall"
  58.  
  59.         gl.glClearColor(_red, _green, _blue, 1.0f);
  60.  
  61.         // clear the color buffer to show the ClearColor we called above...
  62.  
  63.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  64.  
  65.     }
  66.  
  67.  
  68.  
  69.     public void setColor(float r, float g, float b) {
  70.  
  71.         _red = r;
  72.  
  73.         _green = g;
  74.  
  75.         _blue = b;
  76.  
  77.         Log.d("Color","R:"+ Math.round((255*_red)) +"||G:"+ Math.round((255*_green)) +"||B:255");
  78.  
  79.     }
  80.  
  81. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4




VortexView.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.droidnova.android.games.vortex;
  3.  
  4.  
  5.  
  6. import android.content.Context;
  7.  
  8. import android.opengl.GLSurfaceView;
  9.  
  10. import android.view.MotionEvent;
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. public class VortexView extends GLSurfaceView {
  23.  
  24.     private VortexRenderer _renderer;
  25.  
  26.  
  27.  
  28.    
  29.  
  30.    
  31.  
  32.    
  33.  
  34.    
  35.  
  36.          public VortexView(Context context) {
  37.  
  38.         super(context);
  39.  
  40.         _renderer = new VortexRenderer();
  41.  
  42.         setRenderer(_renderer);
  43.  
  44.     }
  45.  
  46.    
  47.  
  48.        
  49.  
  50.          
  51.  
  52.    
  53.  
  54.     public boolean onTouchEvent(final MotionEvent event) {
  55.  
  56.  
  57.  
  58.         queueEvent(new Runnable() {
  59.  
  60.             public void run() {
  61.  
  62.                
  63.  
  64.                 _renderer.setColor(event.getX() / getWidth(), event.getY() / getHeight(), 1.0f);      
  65.  
  66.                
  67.  
  68.             }
  69.  
  70.         });
  71.  
  72.                 return true;
  73.  
  74.  
  75.  
  76.    
  77.  
  78.    
  79.  
  80.    
  81.  
  82.    
  83.  
  84.    
  85.  
  86.    
  87.  
  88. }
  89.  
  90.  
  91.  
  92.         }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4



I also have

orient.java which is the one with the orientation stuff...
I've tried to mix 'em both together as you can see:

orient.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.droidnova.android.games.vortex;
  2.  
  3.  
  4.  
  5.  
  6.  
  7. import static android.hardware.SensorManager.*;
  8.  
  9. import android.app.Activity;
  10.  
  11. import android.hardware.SensorListener;
  12.  
  13. import android.hardware.SensorManager;
  14.  
  15. import android.os.Bundle;
  16.  
  17. import android.util.Log;
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. /**
  28.  
  29. * Displays values from the orientation sensor.
  30.  
  31. *
  32.  
  33. */
  34.  
  35. @SuppressWarnings("deprecation")
  36.  
  37. public class orient extends Activity implements SensorListener {
  38.  
  39.  
  40.  
  41. private SensorManager sensorMgr;
  42.  
  43. private float x, y, z;
  44.  
  45. private VortexRenderer _renderer;
  46.  
  47.  
  48.  
  49. // deltas for calibration
  50.  
  51. @SuppressWarnings("unused")
  52.  
  53. private float cx, cy, cz;
  54.  
  55.  
  56.  
  57. private long lastUpdate = -1;
  58.  
  59.  
  60.  
  61.     @Override
  62.  
  63.     public void onCreate(Bundle savedInstanceState) {
  64.  
  65.         super.onCreate(savedInstanceState);
  66.  
  67.         setContentView(R.layout.main);
  68.  
  69.            
  70.  
  71.     }
  72.  
  73.  
  74.  
  75. @Override
  76.  
  77. protected void onPause() {
  78.  
  79. super.onPause();
  80.  
  81.  
  82.  
  83. sensorMgr.unregisterListener(this, SENSOR_ORIENTATION);
  84.  
  85. sensorMgr = null;
  86.  
  87.  
  88.  
  89. cx = 0;
  90.  
  91. cy = 0;
  92.  
  93. cz = 0;
  94.  
  95. }
  96.  
  97.  
  98.  
  99. @Override
  100.  
  101. protected void onResume() {
  102.  
  103. super.onResume();
  104.  
  105.  
  106.  
  107. sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
  108.  
  109. @SuppressWarnings("unused")
  110.  
  111. boolean accelSupported = sensorMgr.registerListener(this,
  112.  
  113. SENSOR_ORIENTATION,
  114.  
  115. SENSOR_DELAY_UI);
  116.  
  117.  
  118.  
  119.  
  120.  
  121. }
  122.  
  123.  
  124.  
  125. // from the android.hardware.SensorListener interface
  126.  
  127. public void onAccuracyChanged(int sensor, int accuracy) {
  128.  
  129.  
  130.  
  131. }
  132.  
  133.  
  134.  
  135. // from the android.hardware.SensorListener interface
  136.  
  137. public void onSensorChanged(int sensor, float[] values) {
  138.  
  139. if (sensor == SENSOR_ORIENTATION) {
  140.  
  141. long curTime = System.currentTimeMillis();
  142.  
  143.  
  144.  
  145. // only allow one update every 100ms, otherwise updates
  146.  
  147. // come way too fast and the phone gets bogged down
  148.  
  149. // with garbage collection
  150.  
  151. if (lastUpdate == -1 || (curTime - lastUpdate) > 100) {
  152.  
  153. lastUpdate = curTime;
  154.  
  155.  
  156.  
  157. x = values[0];
  158.  
  159. y = (values[1]+180);
  160.  
  161. z = (values[2]+90);
  162.  
  163.  
  164.  
  165. Log.d("Valor x", ""+x);
  166.  
  167. Log.d("Valor y", ""+y);
  168.  
  169. Log.d("Valor z", ""+z);
  170.  
  171.  
  172.  
  173.        
  174.  
  175.         _renderer.setColor(x / 360, y / 360, z / 180);      
  176.  
  177.        
  178.  
  179.     }
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. }
  190.  
  191. }
  192.  
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4



Please point me in the right direction :)
---
This is Lord Otori signing off from Buenos Aires, Argentina
Lord Otori
Freshman
Freshman
 
Posts: 2
Joined: Tue Mar 16, 2010 3:53 am

Top

Re: opengl + orientation sensor

Postby ahahn » Fri Apr 15, 2011 3:47 am

Did you ever find a solution to this problem? I just started digging into this and am at the same place.
Thanks!
ahahn
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Apr 15, 2011 3:45 am

Re: opengl + orientation sensor

Postby Lord Otori » Fri Apr 15, 2011 4:45 am

ahahn wrote:Did you ever find a solution to this problem? I just started digging into this and am at the same place.
Thanks!


To be completely honest, I've been away from Android Development for over a year now. Just yesterday I re-installed eclipse, the SDK and the ADT... That project is still stuck in the same place...


OFFTOPIC: This time I'll start from the basics...
---
This is Lord Otori signing off from Buenos Aires, Argentina
Lord Otori
Freshman
Freshman
 
Posts: 2
Joined: Tue Mar 16, 2010 3:53 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests