Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从OpenGLES1.0到OpenGLES2.0,我不能画正方形_Android_Opengl Es - Fatal编程技术网

Android 从OpenGLES1.0到OpenGLES2.0,我不能画正方形

Android 从OpenGLES1.0到OpenGLES2.0,我不能画正方形,android,opengl-es,Android,Opengl Es,我正在学习OpenGL ES 2.0,我刚刚为1.0制作了一个简单的游戏 我正在学习教程,但我不能理解这件事 在OpenGL ES 1.0中,如果这样定义正方形: private float[] vertices = { // Vertices for a face 0.0f, 0.0f, 0.2f, // 0. left-bottom-front 1.0f, 0.0f, 0.2f, // 1. right-bottom-front 0.0f, 1.0f, 0.2f, // 2. left-to

我正在学习OpenGL ES 2.0,我刚刚为1.0制作了一个简单的游戏

我正在学习教程,但我不能理解这件事

在OpenGL ES 1.0中,如果这样定义正方形:

private float[] vertices = { // Vertices for a face
0.0f, 0.0f, 0.2f, // 0. left-bottom-front
1.0f, 0.0f, 0.2f, // 1. right-bottom-front
0.0f, 1.0f, 0.2f, // 2. left-top-front
1.0f, 1.0f, 0.2f // 3. right-top-front
};
这样画就行了

public void draw(GL10 gl)
{

    gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise
                                    // orientation
    gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
    gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display)

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Enable
                                                            // texture-coords-array
                                                            // (NEW)
    gl.glTexCoordPointer(3, GL10.GL_FLOAT, 0, texBuffer); // Define
                                                            // texture-coords
                                                            // buffer (NEW)

    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

    // front
    gl.glBindTexture(GL10.GL_TEXTURE_2D,TextureLoader.TeclaBlancatextureIDs[0]);


    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Disable
                                                            // texture-coords-array
                                                            // (NEW)
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);
    gl.glDisable(GL10.GL_BLEND);
}
但在OpenGL ES 2.0中,如果我这样做(使用相同的顶点),它只会写一个三角形:

private void drawSuelo(Suelo suelo)
{       
    // Pass in the position information
    suelo.mPositions.position(0);       
    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
            0, suelo.mPositions);        

    GLES20.glEnableVertexAttribArray(mPositionHandle);        

    // Pass in the color information
    suelo.mColors.position(0);
    GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false,
            0, suelo.mColors);        

    GLES20.glEnableVertexAttribArray(mColorHandle);

    // Pass in the normal information
    cubo.mCubeNormals.position(0);
    GLES20.glVertexAttribPointer(mNormalHandle, mNormalDataSize, GLES20.GL_FLOAT, false, 
            0, cubo.mCubeNormals);

    GLES20.glEnableVertexAttribArray(mNormalHandle);

    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
    // (which currently contains model * view).
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);   

    // Pass in the modelview matrix.
    GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);                

    // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
    // (which now contains model * view * projection).
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

    // Pass in the combined matrix.
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);

    // Pass in the light position in eye space.        
    GLES20.glUniform3f(mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]);

    // Draw the cube.
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);                               
}   
为了画一个正方形,它似乎需要所有这些顶点

final float[] PositionData = {

// Top face
-1.0f, 1.0f, -1.0f, 
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f, 
-1.0f, 1.0f, 1.0f, 
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f };

为什么会这样?我为什么失踪?在OpenGL ES 2.0中,是否有任何方法可以编写具有4个顶点的正方形?

您使用的是不同的基本体类型。在正在运行的ES 1.0代码中,您有:

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
在ES 2.0代码中,当您为三角形指定4个顶点时,该代码仅渲染三角形:

GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
因此,在ES 1.0代码中,您指定要绘制具有4个顶点的三角形条带(基本类型
GL\u triangle\u strip
)。这对应于2个三角形。在ES 2.0代码中,指定要绘制两个单独的三角形(基本体类型
GL_triangles
),这确实需要6个顶点

如果要为ES 2.0代码使用原始4个顶点,只需对ES 1.0版本中使用的相同基本体类型使用等效的绘制调用:

GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

看和。我看到他们使用了顺序,但是我找不到画正方形的代码(只有三角形)你看过演示吗?非常感谢,有问题,我似乎有不同的类型:GL_点,GL_线,GL_线,GL_环,GL_线、GL_三角带、GL_三角扇和GL_三角哪个更好?或者每个都有什么用途?