1

我想在opengl-ES的触摸屏上连续绘制点,这样看起来就好像我在opengl-ES屏幕上绘制了一个对象。我曾经onTouchEvent(MotionEvent e)实现GLSurfaceView类的触摸事件。下面是我的 GLSurfaceView 和 Renderer 类。

public class MySurfaceView extends GLSurfaceView
{
private MyRenderer mRenderer;
private Utility sPoint;
private float xTouchPoint=0.0f;
private float yTouchPoint=0.0f;
public static boolean touchEvent=false;
boolean b=true;
PointF surfacePoints=new PointF();




public MySurfaceView(Context context) {
    super(context);

    //surfacePoints=new PointF();
    setEGLContextClientVersion(1);
    // Set the Renderer for drawing on the GLSurfaceView
    mRenderer = new MyRenderer();
    super.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    setZOrderMediaOverlay(true);
    setRenderer(mRenderer);
    setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);


}
public boolean onTouchEvent(MotionEvent e) {

    sPoint =  new Utility();
    touchEvent=true;
    xTouchPoint = e.getX();
    yTouchPoint = e.getY();

    sPoint.setCurrentTouchPoint(xTouchPoint,yTouchPoint);
    Log.i("h","Touched (x , y) "+xTouchPoint+" , "+yTouchPoint);
    return true;
}




class MyRenderer implements GLSurfaceView.Renderer {
    private Triangle triangle;
@Override
    public void onDrawFrame(GL10 gl) {

        float viewWidth = (float)getWidth();
        float viewHeigth = (float)getHeight();

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
        gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);

        gl.glLoadIdentity();        //Reset The Current Modelview Matrix
        if(MySurfaceView.touchEvent){
            triangle = new Triangle(sPoint.getCurrentTouchPoint(),viewHeigth,viewWidth);
            Log.i("Hello","Log");
            //triangle=new Triangle(arrayListX,arrayListY, viewHeigth, viewWidth);
            triangle.draw(gl);  
        }

    }
@Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // TODO Auto-generated method stub
        if(height == 0) {                       //Prevent A Divide By Zero By
            height = 1;                         //Making Height Equal One
        }

        gl.glViewport(0, 0, width, height);     //Reset The Current Viewport
        gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix
        gl.glLoadIdentity();                    //Reset The Projection Matrix

        //Calculate The Aspect Ratio Of The Window
        GLU.gluOrtho2D(gl,0,width,0,height);
        gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
        gl.glLoadIdentity();                    //Reset The Modelview Matrix
    }
@Override
    public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
        // TODO Auto-generated method stub

            gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    //Black Background
            gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
            gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
            gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do

            //Really Nice Perspective Calculations
            gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 



    }

}


public class Triangle {

/** The buffer holding the vertices */
private FloatBuffer vertexBuffer;
private Utility gPoint;
/** The initial vertex definition */


private float vertices[] = { 
             500.0f, 0.0f, 0.0f,    //Top
            /* 500.0f, 250.0f, 0.0f, //Bottom Left
             1000.0f, 0.0f, 0.0f    //Bottom Right*/                                  };


int i=0;

public Triangle(PointF gPoint, float viewHeigth, float viewWidth) {
    vertices[0]=gPoint.x;
    vertices[1]=viewHeigth-gPoint.y;
    Log.i("Utility", gPoint.x+ " , " +gPoint.y);
    ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    vertexBuffer = byteBuf.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);
}

Iterator<Float> itX;
 Iterator<Float> itY;
 float v1[];
public Triangle(ArrayList<Float> arlX,ArrayList<Float> arlY,float viewHeigth, float viewWidth)
{


     int j=0;
     PointF pf=new PointF();

     Log.i("Points","Second   "+arlX.size());
      v1=new float[arlX.size()*3];
     i=arlX.size()*3;


     for (int a = 0; a < arlX.size(); a++)
     {
         v1[j++]=arlX.get(a);
         Log.i("Points","Second12345"+v1[j-1]);
         v1[j++]=arlY.get(a);
         Log.i("Points","Second12345"+v1[j-1]);
         v1[j++]=0.0f;
        Log.i("Points","Second1234567   "+pf.x+","+pf.y);
     }


        ByteBuffer byteBuf = ByteBuffer.allocateDirect(v1.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        vertexBuffer = byteBuf.asFloatBuffer();
        vertexBuffer.put(v1);
        vertexBuffer.position(0);
}

public void draw(GL10 gl) {
    //gl.glScalef(50.0f, 50.0f, 1.0f);
    //Set the face rotation
    gl.glFrontFace(GL10.GL_CCW);

    //Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    //Enable vertex buffer
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    //Draw the vertices as triangle strip

    //gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
    gl.glDrawArrays(GL10.GL_POINTS, 0, i / 3);

    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
4

0 回答 0