我无法正确加载以下图片 enter image description here
这是我的加载结果。 enter image description here
这是我的代码。我使用stb_load()和glTexImage2d()来读取图片并使用GL_RGB格式,但是结果是像图片显示一样发生了脱位。
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Shader.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include <stb-master/stb_image.h>
#include <iostream>
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 800;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
float imgVertices[] = {
// positions // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat)
0.5f, -0.5f, 1.0, 1.0,
-0.5f, -0.5f, 0.0, 1.0,
-0.5f, 0.5f, 0.0, 0.0,
-0.5, 0.5, 0.0, 0.0,
0.5, 0.5, 1.0, 0.0,
0.5, -0.5, 1.0, 1.0
};
// Create plane VAO, VBO
// ---------------
unsigned int imgVAO, imgVBO;
glGenVertexArrays(1, &imgVAO);
glBindVertexArray(imgVAO);
glGenBuffers(1, &imgVBO);
glBindBuffer(GL_ARRAY_BUFFER, imgVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(imgVertices), imgVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)(sizeof(float) * 2));
glEnableVertexAttribArray(1);
// load textures
// -------------
unsigned int imageTexture;
glGenTextures(1, &imageTexture);
glBindTexture(GL_TEXTURE_2D, imageTexture);
int width, height, urChannel;
unsigned char *data = stbi_load("img/image1.jpg", &width, &height, &urChannel, 0);
if(data){
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
stbi_image_free(data);
}
else{
cout << "Read iamge failed\n";
stbi_image_free(data);
}
// configuration
// --------------------
Shader imgShader("shader/image.vs", "shader/image.fs");
// configure the shaders
// ---------------------
imgShader.use();
imgShader.setInt("texture1", 0);
// render loop
// -----------
while(!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
imgShader.use();
glBindVertexArray(imgVAO);
glBindTexture(GL_TEXTURE_2D, imageTexture);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &imgVAO);
glDeleteBuffers(1, &imgVBO);
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
我不知道哪里出了问题,很高兴您能帮助我。
答案 0 :(得分:0)
替换此行:
unsigned char *data = stbi_load("img/image1.jpg", &width, &height, &urChannel, 0);
使用
unsigned char *data = stbi_load("img/image1.jpg", &width, &height, &urChannel, 3);
那是因为您提到格式是RGB。它将组件数强制为3,而不是文件可能包含的任何数量。
答案 1 :(得分:0)
OpenGL假定图像的行是4字节长的倍数,但事实并非如此(它们是3 x 515字节长)。要解决此问题,请使用
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
在您进行glTexImage2D
通话之前。