2012-10-05 70 views
0

我是新來打開GL的,我只是想在屏幕上隨機移動一個圓圈,當用戶觸摸圓圈時,我需要知道命中和錯過。 這是我能夠通過在線課程實現的。open GL ES1.0路徑

render class: 


    public class HelloOpenGLES10Renderer implements Renderer { 
    private int points = 250; 
    private float vertices[] = { 0.0f, 0.0f, 0.0f }; 
    private FloatBuffer vertBuff; 
    public float x = 0.0f, y = 0.0f; 
    public boolean color = false; 
    boolean first = true; 

    @Override 
    public void onDrawFrame(GL10 gl) { 
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
     gl.glMatrixMode(GL10.GL_MODELVIEW); 
     gl.glLoadIdentity(); 
     gl.glTranslatef(x, y, 0); 
     gl.glColor4f(1.0f, 1.0f, 1.0f, 0f); 
     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff); 
     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points); 

    } 

    @Override 
    public void onSurfaceChanged(GL10 gl, int width, int height) { 
     Log.i("TAG", "change" + width + ":" + height); 
     gl.glViewport(0, 0, width, height); 
     float ratio = (float) width/height; 
     gl.glMatrixMode(GL10.GL_PROJECTION); 
     gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7); 
     GLU.gluLookAt(gl, 0f, 0f, -5f, 0.0f, 0.0f, 0f, 0.0f, 1.0f, 0.0f); 
    } 

    @Override 
    public void onSurfaceCreated(GL10 gl, EGLConfig config) { 

     Log.i("TAG", "CREATE"); 
     gl.glClearColor(0.1f, 0.1f, 0.0f, 1.0f); 
     initShapes(); 

    } 

    private void initShapes() { 

     vertices = new float[(points + 1) * 3]; 
     for (int i = 3; i < (points + 1) * 3; i += 3) { 
      double rad = (i * 360/points * 3) * (3.14/180); 
      vertices[i] = (float) Math.cos(rad) * 0.10f; 
      vertices[i + 1] = (float) Math.sin(rad) * 0.10f; 
      vertices[i + 2] = 0; 
     } 
     ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4); 
     bBuff.order(ByteOrder.nativeOrder()); 
     vertBuff = bBuff.asFloatBuffer(); 
     vertBuff.put(vertices); 
     vertBuff.position(0); 

    } 

} 

Activity: 
    package com.example.opengltrail; 

    import java.util.Random; 

    import android.app.Activity; 
    import android.opengl.GLSurfaceView; 
    import android.os.Bundle; 
    import android.util.FloatMath; 
    import android.util.Log; 
    import android.view.MotionEvent; 
    import android.view.View; 
    import android.view.View.OnTouchListener; 

    public class MainActivity extends Activity implements OnTouchListener { 

    private GLSurfaceView mGLView; 
    float oldx = 0, oldy = 0; 
    private HelloOpenGLES10Renderer m; 
    ProgressDialogThread p; 
    ProgressDialogThread123 t; 
    boolean stop = true; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mGLView = new GLSurfaceView(this); 

     m = new HelloOpenGLES10Renderer(); 

     mGLView.setRenderer(m); 
     mGLView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 
     setContentView(mGLView); 

     mGLView.setOnTouchListener(this); 
     p = new ProgressDialogThread(); 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     mGLView.onPause(); 
     p.stop(); 
     stop = false; 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     mGLView.onResume(); 
     t = new ProgressDialogThread123(); 

     t.start(); 
     p.start(); 

    } 

    /** 
    * gets u the random number btw 0.0 to 1.0 
    */ 
    public float getmearandom() { 
     Random rng = new Random(); 
     float next = rng.nextFloat(); 
     Integer i = rng.nextInt(); 
     if (i % 2 == 0) 
      next = next * (-1); 
     return next; 
    } 

    class ProgressDialogThread123 extends Thread { 
     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      super.run(); 

      for (; stop;) { 
       p.run(); 
      } 

     } 
    } 

    class ProgressDialogThread extends Thread { 
     @Override 
     public void run() { 
      super.run(); 
      if (stop) { 

       try { 
        Thread.sleep(2000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

       m.x = getmearandom(); 
       m.y = getmearandom(); 
       Log.i("m.x=" + m.x + ":", "m.y=" + m.y); 
       mGLView.requestRender(); 
      } 

     } 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     float x = event.getX(); 
     float y = event.getY(); 
     float x1 = ((x * 2)/480); 
     float y1 = ((y * 2)/724); 
     x1 = -1 + x1; 
     y1 = -1 + y1; 
     if (y < 362) { 
      if (y1 > 0) 
       y1 = -y1; 
     } else if (y > 362) { 
      if (y1 < 0) 
       y1 = -y1; 
     } else { 
      y1 = 0; 
     } 

     if (x > 240) { 
      if (x1 < 0) 
       x1 = -x1; 

     } else if (x < 240) { 
      if (x1 > 0) 
       x1 = -x1; 
     } else { 
      x1 = 0; 
     } 

     Log.i("x1=" + x1, "y1=" + y1); 
     float d = (((m.x - x1) * (m.x - x1)) + ((m.y - y1) * (m.y - y1))); 

     float dd = FloatMath.sqrt(d); 
     if (dd <= 0.10f) { 
      m.color = true; 
      // mGLView.requestRender(); 
      Log.i("Tag", "Circle"); 
     } 
     // m.x += 0.10f; 

     return true; 
    } 
} 

Please anyone help me !! thanks in advance 
+1

請原諒我提出一個不相干的問題,但似乎是什麼問題? –

+0

我只是想知道當用戶點擊屏幕時,它是不是圓圈!我無法做到這一點。 – paibhavesh

+0

一個更多這是視圖不覆蓋屏幕的全部高度..即當我打印屏幕的價值,它是來724,而不是800.(正常800X480) – paibhavesh

回答

1

如果你只在二維圖紙,我建議你鬆「視錐」和「的lookAt」與「鄰」座標替換它們:ortho(viewOrigin.x, viewOrigin.x + viewSize.width, viewOrigin.y + viewSize.height, viewOrigin.y, -1.0f, 1.0f)。這將使您的GL座標系與您的視圖座標相同。至於圓形頂點,而是以1.0f的半徑創建它們。現在繪製圓,在繪製調用之前,您只需「推」矩陣,轉換爲屏幕座標(X,Y,.0f),按像素(R,R,1.0f)縮放到半徑R,通話結束後的矩陣。現在來檢查聯繫,如果圓圈被打:

`bool didHit = ((touch.x-cicrcleCenter.x)*(touch.x-cicrcleCenter.x) + 
       (touch.y-cicrcleCenter.y)*(touch.y-cicrcleCenter.y)) 
       < R*R`; 

注意,「viewOrigin」中的「鄰」取決於你正在追趕觸摸:如果您在同一趕上他們查看它可能會在(0,0)無論視圖在哪裏。這些座標應該與您在視圖的左上角和右下角按下時在觸摸事件中收到的座標相對應。

如果你真的需要用3d繪製,你將不得不使用平截頭體,你需要得到你的投影矩陣的逆矩陣,從你的觸摸中創建3d射線,然後檢查命中是否與子彈相同你喜歡。

+0

謝謝謝謝謝謝非常感謝:) :) :) :)它工作得很好:) – paibhavesh