在3D中绘制简单三角形。只有一个出现在模拟器上,在真实设备上 - 无三角形

时间:2013-12-01 17:35:50

标签: opengl-es android-emulator android-ndk opengl-es-2.0

    // Front face
    fPyramid[0] = 0.0f; fPyramid[1] = 5.0f; fPyramid[2] = 0.0f;
    fPyramid[3] = -3.0f; fPyramid[4] = 0.0f; fPyramid[5] = 3.0f;
    fPyramid[6] = 3.0f; fPyramid[7] = 0.0f; fPyramid[8] = 3.0f;

    // Left face
    fPyramid[18] = 0.0f; fPyramid[19] = 5.0f; fPyramid[20] = 0.0f;
    fPyramid[21] = -3.0f; fPyramid[22] = 0.0f; fPyramid[23] = -3.0f;
    fPyramid[24] = -3.0f; fPyramid[25] = 0.0f; fPyramid[26] = 3.0f;

    // Back face
    fPyramid[9] = 0.0f; fPyramid[10] = 5.0f; fPyramid[11] = 0.0f;
    fPyramid[12] = 3.0f; fPyramid[13] = 0.0f; fPyramid[14] = -3.0f;
    fPyramid[15] = -3.0f; fPyramid[16] = 0.0f; fPyramid[17] = -3.0f;

    // Right face
    fPyramid[27] = 0.0f; fPyramid[28] = 5.0f; fPyramid[29] = 0.0f;
    fPyramid[30] = 3.0f; fPyramid[31] = 0.0f; fPyramid[32] = 3.0f;
    fPyramid[33] = 3.0f; fPyramid[34] = 0.0f; fPyramid[35] = -3.0f;

    for(int i = 0; i < 4 ; i++)
    {
        fPyramidColor[i*9] = 1.0f; fPyramidColor[i*9+1] = 0.0f; fPyramidColor[i*9+2] = 0.0f;
        if(i < 2)
        {
            fPyramidColor[i*9+1] = 0.0f; fPyramidColor[i*9+4] = 1.0f; fPyramidColor[i*9+5] = 0.0f;
            fPyramidColor[i*9+2] = 0.0f; fPyramidColor[i*9+7] = 0.0f; fPyramidColor[i*9+8] = 1.0f;
        }
        else
        {
            fPyramidColor[i*9+2] = 0.0f; fPyramidColor[i*9+7] = 1.0f; fPyramidColor[i*9+8] = 0.0f;
            fPyramidColor[i*9+1] = 0.0f; fPyramidColor[i*9+4] = 0.0f; fPyramidColor[i*9+5] = 1.0f;
        }
    }
    ...

    glBindBuffer(GL_ARRAY_BUFFER, uiVBO[0]);
    glBufferData(GL_ARRAY_BUFFER, 36*sizeof(float), fPyramid, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, uiVBO[1]);
    glBufferData(GL_ARRAY_BUFFER, 36*sizeof(float), fPyramidColor, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    ****************************************************************
    //Shaders
    ****************************************************************
        char vShaderStr[] =
        "uniform mat4 projectionMatrix;                                                         \n"
        "uniform mat4 modelViewMatrix;                                                          \n"
        "attribute vec3 vPosition;                                                              \n"
        "attribute vec3 inColor;                                                                \n"
        "varying vec3 theColor;                                                                 \n"
        "void main()                                                                            \n"
        "{                                                                                      \n"
        "   gl_Position = projectionMatrix*modelViewMatrix*vec4(vPosition, 1.0);                \n"
        "   theColor = inColor;                                                                 \n"
        "}                                                                                      \n";

char fShaderStr[] =
        "varying vec3 theColor;                                                                 \n"
        "void main()                                                                            \n"
        "{                                                                                      \n"
        "   gl_FragColor = vec4(theColor, 1.0);                                                 \n"
        "}                                                                                      \n";


    ****************************************************************
    //in drawframe() function
    ****************************************************************
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindBuffer(GL_ARRAY_BUFFER, uiVBO[0]);
    glEnableVertexAttribArray(0);//0 is or position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, uiVBO[1]);
    glEnableVertexAttribArray(2);//2 is for colors
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    int iModelViewLoc = glGetUniformLocation(globalProgramObject, "modelViewMatrix");
    int iProjectionLoc = glGetUniformLocation(globalProgramObject, "projectionMatrix");
    glUniformMatrix4fv(iProjectionLoc, 1, GL_FALSE, getPerspectiveMatrixInFloat());

    ESMatrix mModelView = ogmLookAt(0, 15, 40,     0.0f, 0.0f, 0.0f,     0.0f, 1.0f, 0.0f);
    ESMatrix mCurrent = ogmRotate(&mModelView, fRotationAngle, 0.0f, 1.0f, 0.0f);
    ogmTranslate(&mCurrent, 0, 13, 35);
    glUniformMatrix4fv(iModelViewLoc, 1, GL_FALSE, getCurrentMatrixInFloat(mCurrent));

    //and finally draw arrays
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 12);

fRotationAngle正在改变0.01。

这是代码,输出是

enter image description here 这是模拟器上的输出。在真实的设备上,我什么都看不到。 Just WHITE屏幕(从黑色变为白色是一种精神上的平安!)

我没有尝试。尝试将几何图形改为只有2个三角形,但我只看到一个。这有什么问题?

1 个答案:

答案 0 :(得分:3)

您的着色器不应在ES2上编译。根据{{​​3}}(与ES2.0相关),没有layout限定符,并且没有in/out(就像桌面GL上的GLSL&lt; = 1.20一样) ,但您必须使用attribute进行顶点着色器输入,并使用varying进行顶点和片段着色器之间的通信。

在链接程序之前必须使用GLES Shading Language 1.0.17 spec将属性索引分配给着色器输入(或者如果不指定,则查询GL分配的那些)。

作为旁注:当前,您尝试为着色器中的颜色指定索引1,但在客户端代码中使用2。