1

AndroidのOpenGLESで基本を学んでいます。SurfaceView での 2D オブジェクトのレンダリングに問題があります。私は3つのクラスを使用しています:

  1. Graphic3D (名前の 3D は無視します。このクラスを 3D キューブの回転に使用します)
  2. Graphic3DRenderer (Renderer を実装)
  3. Graphic2DTriangle

デバイス (Samsung 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();
}

}

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

}

4

1 に答える 1

0

問題の解決策を見つけましたが、違いが見つかりません。手がかりはありますか?

グラフィック3D

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 に答える