2015-06-03 27 views
2

我一直试图在开发人员网站中使用android opengl 2.0教程创建基本三角形。但无法创建三角形。只有GLSurface正在创建,但Triangle没有渲染。我正在使用Android Studio 1.2.1.1与目标SDK版本22并在Android 4.4.4上运行。有人可以指出我的错误吗?在OpenGL中无法在android中创建三角形

import android.content.Context; 
import android.opengl.GLES20; 
import android.opengl.GLSurfaceView; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 

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

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


public class MainActivity extends AppCompatActivity { 

    private GLSurfaceView mGLview; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mGLview = new MyGLSurfaceView(this); 
     setContentView(mGLview); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public class MyGLSurfaceView extends GLSurfaceView { 

     private final MyGLRenderer mRenderer; 
     public MyGLSurfaceView(Context context) { 
      super(context); 
      setEGLContextClientVersion(2); 
      mRenderer = new MyGLRenderer(); 
      setRenderer(mRenderer); 
      setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 
     } 
    } 

    public static class MyGLRenderer implements GLSurfaceView.Renderer { 

     private Triangle mTriangle; 

     @Override 
     public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
      mTriangle = new Triangle(); 
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
     } 

     @Override 
     public void onSurfaceChanged(GL10 gl, int width, int height) { 
      GLES20.glViewport(0, 0, width, height); 
     } 

     @Override 
     public void onDrawFrame(GL10 gl) { 
      GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
      mTriangle.draw(); 
      //mSquare.draw(); 
     } 

     public static int loadShader(int type, String shaderCode) { 
      int shader = GLES20.glCreateShader(type); 
      GLES20.glShaderSource(shader, shaderCode); 
      GLES20.glCompileShader(shader); 
      return shader; 
     } 
    } 

    public static class Triangle { 
     private FloatBuffer vertexBuffer; 
     static final int COORDS_PER_VERTEX = 3; 
     float[] triangleCoords = 

       { 
         0.0f, 0.622008459f, 0.0f, 
         -0.5f, -0.311004243f, 0.0f, 
         0.5f, -0.311004243f, 0.0f 
       }; 

     float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f }; 

     private final String vertexShaderCode = 
       "attribute vec4 vPosition;" + 
         "void main() {" + 
         " gl_Position = vPosition;" + 
         "}"; 

     private final String fragmentShaderCode = 
       "precision mediump float;" + 
         "uniform vec4 vColor;" + 
         "void main() {" + 
         " gl_FragColor = vColor;" + 
         "}"; 

     private final int mProgram; 
     private int mPositionHandle; 
     private int mColorHandle; 
     private final int vertexCount = triangleCoords.length/COORDS_PER_VERTEX; 
     private final int vertexStride = COORDS_PER_VERTEX*4; 
     public Triangle() { 
      ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4); 
      bb.order(ByteOrder.nativeOrder()); 
      vertexBuffer = bb.asFloatBuffer(); 
      vertexBuffer.put(triangleCoords); 
      vertexBuffer.position(); 
      int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,vertexShaderCode); 
      int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentShaderCode); 
      mProgram = GLES20.glCreateProgram(); 
      GLES20.glAttachShader(mProgram,vertexShader); 
      GLES20.glAttachShader(mProgram,fragmentShader); 
      GLES20.glLinkProgram(mProgram); 
     } 

     public void draw() { 
      GLES20.glUseProgram(mProgram); 
      mPositionHandle = GLES20.glGetAttribLocation(mProgram,"vPosition"); 
      GLES20.glEnableVertexAttribArray(mPositionHandle); 
      GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); 
      mColorHandle=GLES20.glGetUniformLocation(mProgram, "vColor"); 
      GLES20.glUniform4fv(mColorHandle, 1, color, 0); 
      GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); 
      GLES20.glEnableVertexAttribArray(mPositionHandle); 
     } 
    } 
} 
+0

第一件事的建议,检查时调试GLES问题是*否*什么是发生。将'glClearColor'更改为红色。如果您看到红色,则渲染正在发生,并且您的三角形存在问题。如果它保持黑色,那么你的问题是更早。 – fadden

+0

您需要在填充数据后倒带缓冲区。尝试将'vertexBuffer.position()'更改为'vertexBuffer.position(0)'。 –

+0

正如我已经指出,表面正在创建,因为我可以看到黑色屏幕,而不是默认的白色屏幕。 Reto给出的解决方案工作正常。我把它变成了vertexBuffer.position(0)。 –

回答

0

的解决问题的办法是更换vertexBuffer.position()来vertexBuffer.position(0)由Reto Koradi