我正在向我的 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.)
}
如果您想查看完整的代碼,
但是,該程式的預期輸出是頂部不透明度為 20% 的企鵝和企鵝下方不透明度為 100% 的框。
預先感謝您的幫助。
uj5u.com熱心網友回復:
在頂點著色器中,紋理坐標的位置為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);
uj5u.com熱心網友回復:
查看您的源代碼,您傳入了三個屬性(位置、顏色和紋理坐標),但您的頂點著色器只需要兩個。
洗掉顏色屬性,而是將紋理坐標作為屬性 #1 而不是 #2 傳遞應該使它看起來像預期的那樣。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/490101.html