1

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

  1. Graphic3D(名称中忽略 3D,我将使用此类进行 3D 立方体旋转)
  2. Graphic3DRenderer(实现渲染器)
  3. 图形二维三角形

当我在设备(三星 i5700)上运行应用程序时,我只能看到 Graphic3DRenderer.onDrawFrame 中指定的背景颜色。没有三角形。相机位置/方向还可以(我认为..)。在调试模式下,Graphic2DTriangle 中的 draw() 方法被调用,但屏幕上什么也没有发生。

类代码:

图形3D

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();
}

}

图形3DR渲染器

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);

}

}

图形二维三角形

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);
}

}

4

1 回答 1

0

I have found a solution for my problem, but can't find the difference. Any clues?

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);
   }
}
于 2012-09-01T11:04:38.357 回答