opengl 添加转换后对象未呈现

v6ylcynt  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(125)

我在我的C OpenGL程序中添加变换。我使用CGLM作为我的数学库。程序没有警告或错误。然而,当我编译和运行程序时,我只是得到了一个用我的透明色着色的窗口。下面是我的程序的主循环

// Initialize variables for framerate counting
double lastTime = glfwGetTime();
int frameCount = 0;

// Program loop
while (!glfwWindowShouldClose(window)) {
    // Calculate framerate
    double thisTime = glfwGetTime();
    frameCount++;

    // If a second has passed.
    if (thisTime - lastTime >= 1.0) {
        printf("%i FPS\n", frameCount);

        frameCount = 0;
        lastTime = thisTime;
    }

    processInput(window);

    // Clear the window
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    // Bind textures on texture units
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, texture2);

    // Create transformations
    mat4 transform = {{1.0f}};
    glm_translate(transform, (vec3){0.5f, -0.5f, 0.0f});
    glm_rotate(transform, (float)glfwGetTime(), (vec3){0.0f, 0.0f, 1.0f});

    printf("%i\n", transform);

    // Get matrix's uniform location and set matrix
    shaderUse(myShaderPtr);
    GLint transformLoc = glGetUniformLocation(myShaderPtr->shaderID, "transform");
    printf("%i\n", transformLoc);
    glUniformMatrix4fv(transformLoc, 1, GL_FALSE, *transform);

    glBindVertexArray(VAO);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

    glfwSwapBuffers(window); // Swap the front and back buffers
    glfwPollEvents(); // Check for events (mouse movement, mouse click, keyboard press, keyboard release etc.)
}

如果你想查看完整的代码,这个程序在github here上。
此程序的输出为

但是,预期的输出是一个旋转框,上面有我的个人资料图片。

dgiusagp

dgiusagp1#

mat4 transform = {{1.0f}};不会执行您所期望的操作。C没有像C那样的构造函数。C版本的构造函数使用Identity matrix初始化矩阵。您必须使用glm_mat4_identity使用单位矩阵进行初始化:
第一个
此外,还需要指定并添加一个orthographic projection矩阵,以补偿视口的纵横比:
第一个

相关问题