2010-10-08 68 views
1

我再次嘗試進入openGL,但像往常一樣,當我繞過vertices/vertixes /時,會窒息/無論什麼和每一個小細節都可能導致災難(格式錯誤,初始化不行正確設置,保存內存等)。爲什麼我的android openGL ES測試渲染器崩潰

我的主要目標是使用OpenGL的2D圖形來加快性能相比,普通的CPU繪圖。

不管怎麼說,我的OpenGL渲染是這樣的:

package com.derp.testopengl; 


import java.nio.Buffer; 
import java.nio.ByteBuffer; 
import java.nio.ByteOrder; 
import java.nio.FloatBuffer; 

import javax.microedition.khronos.egl.EGLConfig; 
import javax.microedition.khronos.opengles.GL10; 

import android.opengl.GLU; 
import android.opengl.GLSurfaceView.Renderer; 

public class OpenGLRenderer implements Renderer { 

    public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
     // Set the background color to black (rgba). 
     gl.glClearColor(1.0f, 0.0f, 0.0f, 0.5f); // OpenGL docs. 
     // Enable Smooth Shading, default not really needed. 
     gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs. 
     // Depth buffer setup. 
     gl.glClearDepthf(1.0f);// OpenGL docs. 
     // Enables depth testing. 
     gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs. 
     // The type of depth testing to do. 
     gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs. 
     // Really nice perspective calculations. 
     gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs. 
          GL10.GL_NICEST); 
    } 


    public void onDrawFrame(GL10 gl) { 
     // Clears the screen and depth buffer. 
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // OpenGL docs. 
          GL10.GL_DEPTH_BUFFER_BIT); 

     // Define the points of my triangle 
     float floatbuff[] = { 
       1.0f,0.0f,0.0f, 
       0.0f,1.0f,0.0f, 
       -1.0f,0.0f,0.0f 
       }; 
     // Create memory on the heap 
     ByteBuffer vbb = ByteBuffer.allocateDirect(3 * 3 * 4); 
     vbb.order(ByteOrder.nativeOrder()); 
     FloatBuffer vertices = vbb.asFloatBuffer(); 

     // Insert points into floatbuffer 
     vertices.put(floatbuff); 
     // Reset position 
     vertices.position(0); 

     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 

     // Change color to green 
     gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f); 

     // Pass vertices to openGL 
     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices); 

     // Draw 'em 
     gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); 

     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    } 


    public void onSurfaceChanged(GL10 gl, int width, int height) { 
     // Sets the current view port to the new size. 
     gl.glViewport(0, 0, width, height);// OpenGL docs. 
     // Select the projection matrix 
     gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs. 
     // Reset the projection matrix 
     gl.glLoadIdentity();// OpenGL docs. 

     // Should give a 2D coordinate system that responds to the screen 
     gl.glOrthof(0.0f, width, 0.0f, height, 0, 200.0f); 

     gl.glMatrixMode(GL10.GL_MODELVIEW); 
     gl.glLoadIdentity();// OpenGL docs. 
    } 
} 

目前,它崩潰就行了:

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); 

隨着IndexOutOfBoundsException異常,但我敢肯定有更多的問題與代碼。

感謝您的幫助!

回答

0

嘗試改變這一點:

// Create memory on the heap 
ByteBuffer vbb = ByteBuffer.allocateDirect(3 * 3 * 4); 
vbb.order(ByteOrder.nativeOrder()); 
FloatBuffer vertices = vbb.asFloatBuffer(); 

// Insert points into floatbuffer 
vertices.put(floatbuff); 
// Reset position 
vertices.position(0); 

這樣:

FloatBuffer vertices = FloatBuffer.wrap(floatbuff); 
1

你可能已經解決了這個問題,但我想我把答案爲別人。當你叫這條線時:

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); 

你說的是在頂點緩衝區中有3個三角形。但實際上有一個三角形。您應該替換3:

vertices.length/3 

這樣,如果你把更多的多邊形任何更多的點,它會正確地渲染。希望這可以幫助。

-Brian

PS:我目前正在學習如何在android中使用opengl。所以我也在搞清楚所有這些小問題。祝你好運:)

0

它可能不會回答,但建議... 不要把下面的代碼放在OnDrawFrame中,因爲你的三角座標不會改變...在onSurfaceCreated中做這個東西。 使FloatBuffer類

// Define the points of my triangle 
    float floatbuff[] = { 
      1.0f,0.0f,0.0f, 
      0.0f,1.0f,0.0f, 
      -1.0f,0.0f,0.0f 
      }; 
    // Create memory on the heap 
    ByteBuffer vbb = ByteBuffer.allocateDirect(3 * 3 * 4); 
    vbb.order(ByteOrder.nativeOrder()); 
    FloatBuffer vertices = vbb.asFloatBuffer(); 

    // Insert points into floatbuffer 
    vertices.put(floatbuff); 

的成員變量來看看這個類TriangleRenderer.java 它會給你一個基本