使用OpenGL ES,我正在渲染一个简单的多维数据集类,将其绘制在屏幕中央。但是,我希望能够在屏幕上的任意位置绘制多个这样的多维数据集,但不知道该怎么做。这是我的自定义表面视图,该视图将多维数据集呈现为私有类。我没有包括我的主要ActivityManager,因为它不是问题。

 public class TouchSurfaceView extends GLSurfaceView  {

private final float TRACKBALL_SCALE_FACTOR=52.0f;
private final float TOUCH_SCALE_FACTOR=100.0f/320;
private MyGLRenderer mRenderer;
private float mPreviousX;
private float mPreviousY;


public TouchSurfaceView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub

        mRenderer=new MyGLRenderer();
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

}

    private class MyGLRenderer implements GLSurfaceView.Renderer{

    private MyGLCube mCube;
    public float mAngleX;
    public float mAngleY;


    public MyGLRenderer(){
        mCube=new MyGLCube();
    }



    @Override
    public void onDrawFrame(GL10 gl) {
        // TODO Auto-generated method stub


        gl.glDisable(GL10.GL_DITHER);
         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

         gl.glMatrixMode(GL10.GL_MODELVIEW);
         gl.glLoadIdentity();
         gl.glClientActiveTexture(DRAWING_CACHE_QUALITY_HIGH);
         // gl.glTranslatef(0, 0, -3.0f);
         gl.glTranslatef(0, 0, -3.0f);
         gl.glRotatef(mAngleX, 0, 1, 0);
         gl.glRotatef(mAngleY, 1, 0, 0);
         mCube.draw(gl);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // TODO Auto-generated method stub
        gl.glViewport(0, 0, width, height);
        float ratio=(float) width/height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // TODO Auto-generated method stub
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        gl.glClearColor(0.5f, 2, 0.63f, 0.2f);

    }


}
 }


这是MYGLCube,其中包含用于构造多维数据集的顶点,索引和其他内容

    public class MyGLCube {


private float vertices[]={
        0.3f,0.3f,-0.3f, // topFront Right
        0.3f,-0.3f,-0.3f, //BottomFront Right
       -0.3f,-0.3f,-0.3f, //BottomFront Left
       -0.3f,0.3f,-0.3f,  //topFront Left
       0.3f,0.3f,0.3f, // topBack Right
       0.3f,-0.3f,0.3f, //BottomBack Right
       -0.3f,-0.3f,0.3f, //BottomBack Left
       -0.3f,0.3f,0.3f,  //topBack Left
 };

private float rgbaVals[]={
        1,1,0,.5f,   // topFront Right color
        0.25f,0, 0.8f,1, //BottomFront Right color
        0,1,1,1, //BottomFront Left color
        0.35f,0.26f,1,0.5f,  //topFront Left color
        0.23f,0.62f,3,0.2f,  // topBack Right color
        0.3f,0.43f,1,0.2f,  //BottomBack Right color
        0.2f, 0.3f, 0.73f,0.6f,  //BottomBack Left color
        0.6f, 0.9f, 0.65f, 0.2f //topBack Left color
};

private short pIndex[]={
          3,4,0,  0,4,1,   3,0,1,
          3,7,4,   7,6,4,  7,3,6,
          3,1,2,   1,6,2,  6,3,2,
          1,4,5,   5,6,1,  6,5,4
};
private FloatBuffer vertBuff, cBuff;
private ShortBuffer pBuff;


public MyGLCube(){
  ByteBuffer bBuff=ByteBuffer.allocateDirect(vertices.length*4);
  bBuff.order(ByteOrder.nativeOrder());
  vertBuff=bBuff.asFloatBuffer();
  vertBuff.put(vertices);
  vertBuff.position(0);

  ByteBuffer pbBuff=ByteBuffer.allocateDirect(pIndex.length*2);
  pbBuff.order(ByteOrder.nativeOrder());
  pBuff=pbBuff.asShortBuffer();
  pBuff.put(pIndex);
  pBuff.position(0);

  ByteBuffer colorBuff=ByteBuffer.allocateDirect(rgbaVals.length*4);
  colorBuff.order(ByteOrder.nativeOrder());
  cBuff=colorBuff.asFloatBuffer();
  cBuff.put(rgbaVals);
  cBuff.position(0);


}

public void draw(GL10 gl){
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glEnable(GL10.GL_SMOOTH);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glCullFace(GL10.GL_BACK);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, cBuff);
    gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);
 }



}

最佳答案

翻译,旋转后,必须使用glPushMatrix和glPopMatrix返回到先前的位置。

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub


    gl.glDisable(GL10.GL_DITHER);
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

     gl.glMatrixMode(GL10.GL_MODELVIEW);
     gl.glLoadIdentity();
     gl.glClientActiveTexture(DRAWING_CACHE_QUALITY_HIGH);

     //Draw cube 1
     gl.glPushMatrix();
     gl.glTranslatef(-0.5f, 0, -3.0f);
     gl.glRotatef(mAngleX, 0, 1, 0);
     gl.glRotatef(mAngleY, 1, 0, 0);
     mCube.draw(gl);
     gl.glPopMatrix();

     //Draw cube 2
     gl.glPushMatrix();
     gl.glTranslatef(0, 0, -3.0f);
     gl.glRotatef(mAngleX, 0, 1, 0);
     gl.glRotatef(mAngleY, 1, 0, 0);
     mCube.draw(gl);
     gl.glPopMatrix();

     //Draw cube 3
     gl.glPushMatrix();
     gl.glTranslatef(0.5f, 0, -3.0f);
     gl.glRotatef(mAngleX, 0, 1, 0);
     gl.glRotatef(mAngleY, 1, 0, 0);
     mCube.draw(gl);
     gl.glPopMatrix();

}

关于java - 在OpenGL ES Android中的随机位置绘制多个多维数据集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9879201/

10-11 20:01