2017-02-27 53 views
1

我進入了開放式gl 2.0(在android上),我很困惑,因爲我在this video(在1:11:05)聽說所有3中顯示的座標系統範圍從-1到1如果我使用座標((-1 | -1 | 0),(0 | 1 | 0),(1 | 0 | 0)),但它們似乎範圍在-2到2之間,我得到一個三角形只有一半,因爲我希望它是一樣大:爲什麼我的OpenGL座標系統軸[-1; 1]?

triangle: ((-1|-1|0), (0|1|0), (1|0|0))

我asumption是,座標系統不正確時,莫名其妙地縮放,因爲如果我加倍的座標爲(( - 2 | -2 | 0),(0 | 2 | 0),(2 | 0 | 0)),我得到了我的前例pected擺在首位:

triangle: ((-2|-2|0), (0|2|0), (2|0|0))

爲什麼會出現這種情況,我怎麼可以使用第一組座標時解決它,讓我得到三角比例在整個屏幕(如第二個圖) ((-1 | -1 | 0),(0 | 1 | 0),(1 | 0 | 0))?

代碼:

活動XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="simonlammer.htlleonding.ac.at.openglcameraoverlay.MainActivity"> 

     <simonlammer.htlleonding.ac.at.openglcameraoverlay.GlOverlay 
      android:id="@+id/overlay" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

</RelativeLayout> 

MainActivity:

public class MainActivity extends AppCompatActivity { 
    private GlOverlay glOverlay; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     glOverlay = (GlOverlay) findViewById(R.id.overlay); 
    } 

    @Override 
    protected void onPause() { 
     glOverlay.onPause(); 
     super.onPause(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     glOverlay.onResume(); 
    } 
} 

GlOverlay:

public class GlOverlay extends GLSurfaceView { 
    private GlEs20Renderer renderer; 

    public GlOverlay(Context context) { 
     super(context); 
     init(); 
    } 

    public GlOverlay(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    private void init() { 
     this.renderer = new GlEs20Renderer(); 
     this.setEGLContextClientVersion(2); 
     this.setRenderer(renderer); 
     this.setRenderMode(RENDERMODE_CONTINUOUSLY); 
    } 
} 

GlEs20渲染器:

public class GlEs20Renderer implements GLSurfaceView.Renderer { 
    private static final int COORDS_PER_VERTEX = 4; 
    private final float[] triangleCoords = { 
      -1.0f, -1.0f, 0.0f, 1.0f, 
      0.0f, 1.0f, 0.0f, 1.0f, 
      1.0f, 0.0f, 0.0f, 1.0f 
    }; 
    private final int vertexCount = triangleCoords.length/COORDS_PER_VERTEX; 
    private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per float 
    private FloatBuffer vertexBuffer; 

    private boolean setup = false; 
    private int program = -1; 

    private void setup() { 
     vertexBuffer = ByteBuffer.allocateDirect(triangleCoords.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); 
     vertexBuffer.put(triangleCoords); 
     vertexBuffer.position(0); 

     String vertexShaderSource = 
       "attribute vec4 vPosition;" + 
       "uniform vec4 vTranslate;" + 
       "void main() {" + 
       " gl_Position = vPosition + vTranslate;" + 
       "}"; 

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


     int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, 
       vertexShaderSource); 
     int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, 
       fragmentShaderSource); 

     // create empty OpenGL ES Program 
     program = GLES20.glCreateProgram(); 

     // add the vertex shader to program 
     GLES20.glAttachShader(program, vertexShader); 

     // add the fragment shader to program 
     GLES20.glAttachShader(program, fragmentShader); 

     // creates OpenGL ES program executables 
     GLES20.glLinkProgram(program); 
    } 

    private static int loadShader(int type, String shaderCode){ 

     // create a vertex shader type (GLES20.GL_VERTEX_SHADER) 
     // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER) 
     int shader = GLES20.glCreateShader(type); 

     // add the source code to the shader and compile it 
     GLES20.glShaderSource(shader, shaderCode); 
     GLES20.glCompileShader(shader); 

     return shader; 
    } 

    @Override 
    public void onSurfaceCreated(GL10 gl, EGLConfig config) { 

    } 

    @Override 
    public void onSurfaceChanged(GL10 gl, int width, int height) { 

    } 

    @Override 
    public void onDrawFrame(GL10 gl) { 
     if (!setup) { 
      setup(); 
      setup = true; 
     } 

     GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
     GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); 

     // Add program to OpenGL ES environment 
     GLES20.glUseProgram(program); 

     // get handle to vertex shader's vPosition member 
     int mPositionHandle = GLES20.glGetAttribLocation(program, "vPosition"); 

     // Enable a handle to the triangle vertices 
     GLES20.glEnableVertexAttribArray(mPositionHandle); 

     // Prepare the triangle coordinate data 
     GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, 
       GLES20.GL_FLOAT, false, 
       vertexStride, vertexBuffer); 

     // get handle to fragment shader's vColor member 
     int mColorHandle = GLES20.glGetUniformLocation(program, "vColor"); 

     // Set color for drawing the triangle 
     GLES20.glUniform4fv(mColorHandle, 1, new float[] {0.5f, 0.0f, 0.0f, 1.0f}, 0); 

     // move 
     GLES20.glUniform4fv(GLES20.glGetUniformLocation(program, "vTranslate"), 1, new float[] {0f, 0f, 0f, 1f}, 0); 

     // Draw the triangle 
     GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); 

     // Disable vertex array 
     GLES20.glDisableVertexAttribArray(mPositionHandle); 
    } 
} 
+0

該代碼在github上可用:https://github.com/SimonLammer/Android-GlEs20CameraOverlay – EvilDevil

+0

我開始了一個新的分支,其中我刪除了與此問題無關的所有代碼:https://github.com/SimonLammer/Android的GlEs20CameraOverlay /樹/不帶攝像頭預覽 – EvilDevil

回答

1

問題來自翻譯向量以及齊次座標如何工作。簡而言之,裁剪後你的職位被他們的w-組件分割。當你現在有一個位置A = [x,y,z,1]並添加一個翻譯[0,0,0,1],那麼結果是[x,y,z,2],並且在角度劃分[x/2,y/2,z/2,1]後。

要解決您的問題,請將轉換矢量的第四個分量設置爲0,或者只使用三維平移向量。