在SurfaceView上渲染2D对象

时间:2012-08-31 21:14:14

标签: android opengl-es

我正在使用Android上的OpenGLES学习基础知识。我在SurfaceView上渲染2D对象时遇到问题。我正在使用3个班级:

  1. Graphic3D(在名称中忽略3D,我将使用此类进行3D立方体旋转)
  2. Graphic3DRenderer(实现渲染器)
  3. Graphic2DTriangle
  4. 当我在设备上运行应用程序(三星i5700)时,我只能看到的是Graphic3DRenderer.onDrawFrame中指定的背景颜色。没有三角形。相机位置/方向正常(我认为..)。在调试模式下,调用Graphic2DTriangle的方法draw(),但屏幕上没有任何事情发生。

    课程代码:

    Graphic3D

    public class Graphic3D extends Activity{
    
    GLSurfaceView mySurface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mySurface = new GLSurfaceView(this);
        //mySurface.setRenderer(new Graphics3DRenderer());
        mySurface.setRenderer(new Graphics3DRenderer());
    
    
        setContentView(mySurface);
    }
    @Override
    protected void onPause() {
        super.onPause();
        mySurface.onPause();
    }
    @Override
    protected void onResume() {
        super.onResume();
        mySurface.onResume();
    }
    

    }

    Graphic3DRenderer

    public class Graphics3DRenderer implements Renderer{
    
    
    private Graphic2DTriangle tri;
    public Graphics3DRenderer()
    {
        tri = new Graphic2DTriangle();      
    }
    
    public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
    
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        gl.glClearColor(.8f, 0f, .2f, 1f);
        gl.glClearDepthf(1f);
    
    
    }
    
    public void onDrawFrame(GL10 gl) {
        gl.glDisable(GL10.GL_DITHER);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2f, 0); // camera position | looking direction 
    
        tri.draw(gl);
    }
    
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        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, 25);
    
    }
    

    }

    Graphic2DTriangle

    public class Graphic2DTriangle {
    
    private float vertices[] = {
            0f, 1f, //p0
            1f, -1f, //p1
            -1f, -1f //p2
    };
    
    private FloatBuffer vertBuff;
    private short[] pIndex = {0, 1, 2};
    private ShortBuffer pBuff;
    
    public Graphic2DTriangle()
    {
        ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
        bBuff.order(ByteOrder.nativeOrder());
        vertBuff = bBuff.asFloatBuffer();
        vertBuff.put(vertices);
        vertBuff.position(0);
    
        ByteBuffer pbBuff = ByteBuffer.allocate(pIndex.length * 2);
        pbBuff.order(ByteOrder.nativeOrder());
        pBuff = pbBuff.asShortBuffer();
        pBuff.put(pIndex);
        pBuff.position(0);
    }
    
    public void draw(GL10 gl)
    {
        gl.glFrontFace(GL10.GL_CW);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);
        gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
    
    
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }
    

    }

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法,但找不到区别。有线索吗?

<强> Graphic3D

public class Graphic3D extends Activity{

private GLSurfaceView glView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    glView = new GLSurfaceView(this);           // Allocate a GLSurfaceView
      glView.setRenderer(new Graphics3DRenderer(this)); // Use a custom renderer
      this.setContentView(glView);                // This activity sets to GLSurfaceView
   }

   // Call back when the activity is going into the background
   @Override
   protected void onPause() {
      super.onPause();
      glView.onPause();
   }

   // Call back after onPause()
   @Override
   protected void onResume() {
      super.onResume();
      glView.onResume();
   }
}

<强> Graphic3DRenderer

public class Graphics3DRenderer implements Renderer {
   Context context;   // Application's context
   Graphic2DTriangle triangle;

   // Constructor with global application context
   public Graphics3DRenderer(Context context) {
      this.context = context;
      triangle = new Graphic2DTriangle();
   }

   // Call back when the surface is first created or re-created
   public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);  // Set color's clear-value to black
      gl.glClearDepthf(1.0f);            // Set depth's clear-value to farthest
      gl.glEnable(GL10.GL_DEPTH_TEST);   // Enables depth-buffer for hidden surface removal
      gl.glDepthFunc(GL10.GL_LEQUAL);    // The type of depth testing to do
      gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);  // nice perspective view
      gl.glShadeModel(GL10.GL_SMOOTH);   // Enable smooth shading of color
      gl.glDisable(GL10.GL_DITHER);      // Disable dithering for better performance

      // You OpenGL|ES initialization code here
      // ......
   }

   // Call back after onSurfaceCreated() or whenever the window's size changes
   public void onSurfaceChanged(GL10 gl, int width, int height) {
      if (height == 0) height = 1;   // To prevent divide by zero
      float aspect = (float)width / height;

      // Set the viewport (display area) to cover the entire window
      gl.glViewport(0, 0, width, height);

      // Setup perspective projection, with aspect ratio matches viewport
      gl.glMatrixMode(GL10.GL_PROJECTION); // Select projection matrix
      gl.glLoadIdentity();                 // Reset projection matrix
      // Use perspective projection
      GLU.gluPerspective(gl, 45, aspect, 0.5f, 100.f);

      gl.glMatrixMode(GL10.GL_MODELVIEW);  // Select model-view matrix
      gl.glLoadIdentity();               // Reset

      // You OpenGL|ES display re-sizing code here
      // ......
   }

   // Call back to draw the current frame.
   public void onDrawFrame(GL10 gl) {
      // Clear color and depth buffers using clear-value set earlier
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

      gl.glLoadIdentity();                 // Reset model-view matrix ( NEW )
      GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2f, 0);
      // gl.glTranslatef(-1.5f, 0.0f, -6.0f); // Translate left and into the screen ( NEW )
      triangle.draw(gl);                   // Draw triangle ( NEW )
   }
}

<强> Graphic2DTriangle

public class Graphic2DTriangle {
   private FloatBuffer vertexBuffer;  // Buffer for vertex-array
   private ByteBuffer indexBuffer;    // Buffer for index-array

   private float[] vertices = {  // Vertices of the triangle
       0.0f,  1.0f, 0.0f, // 0. top
      -1.0f, -1.0f, 0.0f, // 1. left-bottom
       1.0f, -1.0f, 0.0f  // 2. right-bottom
   };
   private byte[] indices = { 0, 1, 2 }; // Indices to above vertices (in CCW)

   // Constructor - Setup the data-array buffers
   public Graphic2DTriangle() {
      // Setup vertex-array buffer. Vertices in float. A float has 4 bytes.
      ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
      vbb.order(ByteOrder.nativeOrder()); // Use native byte order
      vertexBuffer = vbb.asFloatBuffer(); // Convert byte buffer to float
      vertexBuffer.put(vertices);         // Copy data into buffer
      vertexBuffer.position(0);           // Rewind

      // Setup index-array buffer. Indices in byte.
      indexBuffer = ByteBuffer.allocateDirect(indices.length);
      indexBuffer.put(indices);
      indexBuffer.position(0);
   }

   // Render this shape
   public void draw(GL10 gl) {
      // Enable vertex-array and define the buffers
      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

      // Draw the primitives via index-array
      gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
      gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
   }
}