0

我正在尝试扩展一个简单的 Hello Triangle 应用程序以使三角形围绕 Y 轴旋转。我尝试使用 Android Hello Triangle 作为参考,只是将代码调整为 C++ 并使用 PowerVR SDK。到目前为止,我的运气并不好。三角形就在那里,静止不动。不确定是什么问题。这是我的初始化函数:

int Init(ESContext* esContext)
{
    UserData* userData = (UserData *)esContext->userData;
    const char *vShaderStr =
        "attribute vec4 vPosition;  \n"
        "uniform mat4 MVPMatrix;"
        "void main()                \n"
        "{                          \n"
        "   gl_Position = MVPMatrix * vPosition;\n"
        "}                          \n";

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

    GLuint vertexShader;
    GLuint fragmentShader;
    GLuint programObject;
    GLint linked;
    GLfloat ratio = 320.0f/240.0f;

    vertexShader = LoadShader(GL_VERTEX_SHADER, vShaderStr);
    fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr);

    programObject = glCreateProgram();

    if (programObject == 0)
        return 0;

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

    glBindAttribLocation(programObject, 0, "vPosition");
    glLinkProgram(programObject);
    glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &linked);

    if (!linked)
    {
        GLint infoLen = 0;
        glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);

        if (infoLen > 1)
        {
            char* infoLog = (char *)malloc(sizeof(char) * infoLen);
            glGetProgramInfoLog(programObject, infoLen, NULL, infoLog);

            free(infoLog);
        }

        glDeleteProgram(programObject);
        return FALSE;
    }

    userData->programObject = programObject;

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    glViewport(0, 0, esContext->width, esContext->height);
    glClear(GL_COLOR_BUFFER_BIT);


    glUseProgram(userData->programObject);

    userData->angle = 0.0f;
    userData->start = time(NULL);
    userData->ProjMatrix = PVRTMat4::Perspective(ratio*2.0f, 2.0f, 3.0f, 7.0f, PVRTMat4::eClipspace::OGL, false, false);
    userData->ViewMatrix = PVRTMat4::LookAtLH(PVRTVec3(0.0f, 0.0f, -3.0f), PVRTVec3(0.0f, 0.0f, 0.0f), PVRTVec3(0.0f, 1.0f, 0.0f));
    return TRUE;
}

这是绘制函数:

void Draw(ESContext *esContext)
{
    GLfloat vVertices[] = {0.0f, 0.5f, 0.0f,
                          -0.5f, -0.5f, 0.0f,
                           0.5f, -0.5f, 0.0f};

    GLint MVPHandle;

    PVRTMat4 MVPMatrix = PVRTMat4::Identity();
    UserData* userData = (UserData *)esContext->userData;

    if((GLint)(difftime(time(NULL), userData->start)) % 2 == 0)
    {
        if (userData->angle > 360.0f)
        {
            userData->angle = 0.0f;
        }
        else
        {
            userData->angle += 0.1f;
        }
    }

    userData->ModelMatrix = PVRTMat4::RotationY(userData->angle);

    MVPMatrix = userData->ViewMatrix * userData->ModelMatrix;
    MVPMatrix = userData->ProjMatrix * MVPMatrix;

    MVPHandle = glGetUniformLocation(userData->programObject, "MVPMatrix");
    glUniformMatrix4fv(MVPHandle, 1, FALSE, MVPMatrix.ptr());

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
    glEnableVertexAttribArray(0);

    glDrawArrays(GL_TRIANGLES, 0, 3);
    eglSwapBuffers(esContext->eglDisplay, esContext->eglSurface);
}

所以基本上我在我的初始化中设置了视图和投影矩阵。然后,在 Draw 函数中,我使用旋转矩阵和新角度定期重新计算模型矩阵。当角度达到 360 度时,我将其重置为 0,冲洗,重复。

我检查了矩阵值,它们随着角度的变化而变化。所以我猜它在将值传递给着色器时会在某个地方发生故障。

4

1 回答 1

1

你能在绘制循环结束时调用 glGetError 并检查它是否始终为 0?这通常是最好的起点。你的统一相关代码对我来说看起来不错(虽然我不知道 PVRTMat4 的细节,但我现在假设它正在做它应该做的事情)。

我确实看到了一个小错误,但我不确定它是否相关。

这:glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &linked);

应该:glGetProgramiv(programObject, GL_LINK_STATUS, &linked);

您不想将信息日志长度作为通过/失败条件进行检查,因为在成功链接的情况下不能保证为 0。

===编辑===

啊,我看到了问题。您只清除 中的颜色缓冲区Init,您应该在Draw每一帧中清除它。您的三角形可能正在旋转,但您看不到它,因为它被永远不会清除的原始三角形所遮蔽。

于 2012-04-11T16:57:46.690 回答