2013-11-22 74 views
1

我是新來的android SDK和編程使用OGLES2.0。我的問題是,大多數程序沒有運行在我的電腦上。 我正在使用帶有512 MB RAM,VM堆64,內部存儲512和Android 4.3以及API 18(無SD卡)的Android虛擬設備連結4。Android OGLES2.0:所有應用程序出錯

這我試圖運行示例代碼

package com.example.mynewsample; 
// 
// Book:  OpenGL(R) ES 2.0 Programming Guide 
// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner 
// ISBN-10: 0321502795 
// ISBN-13: 9780321502797 
// Publisher: Addison-Wesley Professional 
// URLs:  http://safari.informit.com/9780321563835 
//   http://www.opengles-book.com 
// 

// Hello_Triangle 
// 
// This is a simple example that draws a single triangle with 
// a minimal vertex/fragment shader. The purpose of this 
// example is to demonstrate the basic concepts of 
// OpenGL ES 2.0 rendering. 

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.content.Context; 
import android.opengl.GLES20; 
import android.opengl.GLSurfaceView; 
import android.util.Log; 

public class myTriangleRenderer implements GLSurfaceView.Renderer 
{ 
    /// 
    // Constructor 
    // 
    public myTriangleRenderer(Context context) 
    { 
     mVertices = ByteBuffer.allocateDirect(mVerticesData.length * 4) 
      .order(ByteOrder.nativeOrder()).asFloatBuffer(); 
     mVertices.put(mVerticesData).position(0); 
    } 

    /// 
    // Create a shader object, load the shader source, and 
    // compile the shader. 
    // 
    private int LoadShader(int type, String shaderSrc) 
    { 
     int shader; 
     int[] compiled = new int[1]; 

     // Create the shader object 
     shader = GLES20.glCreateShader(type); 

     if (shader == 0) 
      return 0; 

     // Load the shader source 
     GLES20.glShaderSource(shader, shaderSrc); 

     // Compile the shader 
     GLES20.glCompileShader(shader); 

     // Check the compile status 
     GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); 

     if (compiled[0] == 0) 
     { 
      Log.e(TAG, GLES20.glGetShaderInfoLog(shader)); 
      GLES20.glDeleteShader(shader); 
      return 0; 
     } 
     return shader; 
    } 

    /// 
    // Initialize the shader and program object 
    // 
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
    { 
     String vShaderStr = 
       "attribute vec4 vPosition; \n" 
      + "void main()     \n" 
      + "{       \n" 
      + " gl_Position = vPosition; \n" 
      + "}       \n"; 

     String fShaderStr = 
       "precision mediump float;     \n" 
      + "void main()         \n" 
      + "{           \n" 
      + " gl_FragColor = vec4 (1.0, 0.0, 0.0, 1.0);\n" 
      + "}           \n"; 

     int vertexShader; 
     int fragmentShader; 
     int programObject; 
     int[] linked = new int[1]; 

     // Load the vertex/fragment shaders 
     vertexShader = LoadShader(GLES20.GL_VERTEX_SHADER, vShaderStr); 
     fragmentShader = LoadShader(GLES20.GL_FRAGMENT_SHADER, fShaderStr); 

     // Create the program object 
     programObject = GLES20.glCreateProgram(); 

     if (programObject == 0) 
      return; 

     GLES20.glAttachShader(programObject, vertexShader); 
     GLES20.glAttachShader(programObject, fragmentShader); 

     // Bind vPosition to attribute 0 
     GLES20.glBindAttribLocation(programObject, 0, "vPosition"); 

     // Link the program 
     GLES20.glLinkProgram(programObject); 

     // Check the link status 
     GLES20.glGetProgramiv(programObject, GLES20.GL_LINK_STATUS, linked, 0); 

     if (linked[0] == 0) 
     { 
      Log.e(TAG, "Error linking program:"); 
      Log.e(TAG, GLES20.glGetProgramInfoLog(programObject)); 
      GLES20.glDeleteProgram(programObject); 
      return; 
     } 

     // Store the program object 
     mProgramObject = programObject; 

     GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
    } 

    ///
    // Draw a triangle using the shader pair created in onSurfaceCreated() 
    // 
    public void onDrawFrame(GL10 glUnused) 
    { 
     // Set the viewport 
     GLES20.glViewport(0, 0, mWidth, mHeight); 

     // Clear the color buffer 
     GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); 

     // Use the program object 
     GLES20.glUseProgram(mProgramObject); 

     // Load the vertex data 
     GLES20.glVertexAttribPointer(0, 3, GLES20.GL_FLOAT, false, 0, mVertices); 
     GLES20.glEnableVertexAttribArray(0); 

     GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); 
    } 

    ///
    // Handle surface changes 
    // 
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
    { 
     mWidth = width; 
     mHeight = height; 
    } 

    // Member variables 
    private int mProgramObject; 
    private int mWidth; 
    private int mHeight; 
    private FloatBuffer mVertices; 
    private static String TAG = "HelloTriangleRenderer"; 

    private final float[] mVerticesData = 
    { 0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f }; 
} 

我曾嘗試不同的虛擬設備,但每次它說,遺憾的是時間停止運行。 我得到這個與所有OGLES2.0程序,不會使用CANVAS。 Canvas程序正在準確運行。

回答

1

到目前爲止,我的經驗一直是Android模擬器不完全支持OpenGL ES 2.0,只有ES 1.x.目前最簡單的方法是在物理設備上進行測試。

但是請結帳這個問題,這表明它現在可以做到:在自動真空澱積

Android OpenGL ES 2.0 emulator

1

OpenGL ES 2.0的模擬實際工作得很好,因爲現在的果凍豆版本。但是,關鍵因素是您安裝在主機開發系統上的底層OpenGL驅動程序。它確實必須是最近的Nvidia或AMD驅動程序。另外,安裝英特爾的HAXM使其運行速度更快。見這裏的第三篇文章:

http://montgomery1.com/opengl/