gpt4 book ai didi

c - 添加转换后对象呈现奇怪

转载 作者:行者123 更新时间:2023-12-05 02:28:38 25 4
gpt4 key购买 nike

我正在向我的 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 Messed up image

但是,该程序的预期输出是顶部不透明度为 20% 的企鹅和企鹅下方不透明度为 100% 的框。 Penguin Box

最佳答案

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

#version 330 core

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

但是,当您指定顶点时,位置 1 用于颜色属性,位置 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);

移除颜色属性并使用位置 1 作为纹理坐标。例如:

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

关于c - 添加转换后对象呈现奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72588254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com