我正在向我的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_mat4_identity(transform);

    glm_translate(transform, (vec3){0.5f, -0.5f, 0.0f});
    glm_rotate(transform, (float)glfwGetTime(), (vec3){0.0f, 0.0f, 1.0f});

    // Get matrix's uniform location and set matrix
    shaderUse(myShaderPtr);
    GLint transformLoc = glGetUniformLocation(myShaderPtr->shaderID, "transform");
    // mat4 transform;
    glUniformMatrix4fv(transformLoc, 1, GL_FALSE, (float*)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上.

The Output of the program is this (The square also rotates): The Messed up image

However, the intended output of the program is the penguin at 20% opacity on top and the box at 100% opacity underneath the penguin. Penguin Box

提前感谢您的帮助.

推荐答案

在顶点着色器中,纹理坐标的位置为1:

#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;

但是,指定顶点时,位置1用于 colored颜色 属性,位置2用于文本坐标:

// Colour attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);

// Texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);

移除" colored颜色 "(color)属性,并使用位置1作为纹理坐标.例如.:

// Texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(1);

C++相关问答推荐

如何使fputs功能提示错误输入并要求用户重新输入.程序停止而不是请求新的输入

识别和处理c中整数溢出的最佳方法?

有没有可能我不能打印?(C,流程)

自定义变参数函数的C预处置宏和警告 suppress ?

Linux不想运行编译后的文件

C在声明带有值的数组时,声明大小有用吗?

如何使用C for Linux和Windows的标准输入与gdb/mi进行通信?

如何在C中打印包含扫描字符和整数的语句?

关于";*(++p)->;t";、&++p->;t";和&q;++*p->;t";的问题

C I/O:在Windows控制台上处理键盘输入

Boyer Moore算法的简单版本中的未定义行为

如何在C++中安全地进行浮点运算

按长度对argv中的单词进行排序

将数组插入数组

为什么realloc函数在此代码中修改变量?

pthread_create的用法

为什么我的旧式&q;函数在传递浮点数时会打印2?

DennisM.Ritchie的C编程语言一书中关于二进制搜索的代码出现错误?

C 语言中 CORDIC 对数的问题

多行表达式:C 编译器如何处理换行符?