纹理不填充图形 - OpenGL
Texture does not fill the figure - OpenGL
我正在尝试显示整个图像 window。
为此,我只画了一个矩形,它占据了整个 window 并将其应用为纹理。
问题是图像根本没有填满整个屏幕:
Output image
图像不是太小,因为它是 5600 x 3000。另外,我不希望图像重复。我不知道我错过了什么,为什么它没有填满屏幕,为什么它只在左上角。我已经尝试过这个解决方案,但没有用:
以下是我创建和绑定着色器的方法:
Shader::Shader(const char* vertSrcFile, const char* fragSrcFile, const char* texSrcFile) {
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
GLint vlen;
GLint flen;
char* source = loadFile(vertSrcFile, vlen);
char* fragment = loadFile(fragSrcFile, flen);
glShaderSource(vertShader, 1, &source, &vlen);
glShaderSource(fragShader, 1, &fragment, &flen);
GLint compiled = 0;
glCompileShader(vertShader);
glGetShaderiv(vertShader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint maxLength = 0;
glGetShaderiv(vertShader, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(vertShader, maxLength, &maxLength, &infoLog[0]);
// We don't need the shader anymore.
glDeleteShader(vertShader);
Logger::GetSystemLogger()->error("Vertex shader compilation error");
Logger::GetSystemLogger()->error("{}", infoLog.data());
return;
}
glCompileShader(fragShader);
glGetShaderiv(fragShader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint maxLength = 0;
glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(fragShader, maxLength, &maxLength, &infoLog[0]);
// We don't need the shader anymore.
glDeleteShader(fragShader);
Logger::GetSystemLogger()->error("Fragment shader compilation error");
Logger::GetSystemLogger()->error("{}", infoLog.data());
return;
}
prog = glCreateProgram();
glAttachShader(prog, vertShader);
glAttachShader(prog, fragShader);
glLinkProgram(prog);
GLint isLinked = 0;
glGetProgramiv(prog, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(prog, maxLength, &maxLength, &infoLog[0]);
// We don't need the program anymore.
glDeleteProgram(prog);
// Don't leak shaders either.
glDeleteShader(vertShader);
glDeleteShader(fragShader);
Logger::GetSystemLogger()->error("Program link error");
Logger::GetSystemLogger()->error("{}", infoLog.data());
return;
}
glDetachShader(prog, vertShader);
glDetachShader(prog, fragShader);
glGenTextures(1, &texID); // generate texture ID
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
LoadTexture(texSrcFile);
}
以下是我如何将图像加载为纹理:
void Shader::LoadTexture(const char* texSrcFile) const {
int width, height, nrChannels;
unsigned char* data = stbi_load(texSrcFile, &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
Logger::GetSystemLogger()->error("Failed to load textures");
}
stbi_image_free(data);
}
这是我的着色器:
片段:
#version 330
precision highp float; // needed only for version 1.30
layout(location = 0) out vec4 out_Color;
in vec2 ex_Tex;
uniform sampler2D textureUnit0;
void main(void){
out_Color = texture(textureUnit0, ex_Tex);
}
顶点:
#version 330
layout (location = 0) in vec3 in_Position;
layout (location = 1) in vec2 in_Tex;
out vec2 ex_Tex;
void main(void){
gl_Position = vec4(in_Position, 1.0);
ex_Tex = in_Tex;
}
主要代码如下:
glGenVertexArrays(1, &m_VAO);
glBindVertexArray(m_VAO);
glGenBuffers(1, &m_VBO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
GLfloat vertices[4 * 5]{
// Positions // Textures
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Position
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
// Texture coord
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
glGenBuffers(1, &m_IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IBO);
unsigned int indices[6] = { 0, 1, 2, 0, 1, 3 };
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
m_Shader = std::make_shared<Engine::Shader>("src/Shaders/Background.vert", "src/Shaders/Background.frag", "res/background.jpg");
这是主循环
while (m_Running) {
// clear the screen and set the background color to grey
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
m_Shader->Bind();
glBindTexture(GL_TEXTURE_2D, m_Shader->GetTexID());
glBindVertexArray(m_VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
m_Window->OnUpdate();
}
虽然视口坐标从负一变为一 [-1, 1)
,但纹理坐标从零变为一 [0,1)
。因此,根据 GL_CLAMP_TO_BORDER
政策,四分之三的视口都被黑色填充。
您需要按如下方式修正您的 VAO 坐标:
GLfloat vertices[4 * 5]{
// Positions // Textures
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f
};
编辑: 此外,您的纹理坐标偏移量不正确。应该是:
// Texture coord
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
如果没有适当的偏移,它将使用位置的 X 和 Y 作为纹理坐标。
我正在尝试显示整个图像 window。 为此,我只画了一个矩形,它占据了整个 window 并将其应用为纹理。
问题是图像根本没有填满整个屏幕: Output image
图像不是太小,因为它是 5600 x 3000。另外,我不希望图像重复。我不知道我错过了什么,为什么它没有填满屏幕,为什么它只在左上角。我已经尝试过这个解决方案,但没有用:
以下是我创建和绑定着色器的方法:
Shader::Shader(const char* vertSrcFile, const char* fragSrcFile, const char* texSrcFile) {
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
GLint vlen;
GLint flen;
char* source = loadFile(vertSrcFile, vlen);
char* fragment = loadFile(fragSrcFile, flen);
glShaderSource(vertShader, 1, &source, &vlen);
glShaderSource(fragShader, 1, &fragment, &flen);
GLint compiled = 0;
glCompileShader(vertShader);
glGetShaderiv(vertShader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint maxLength = 0;
glGetShaderiv(vertShader, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(vertShader, maxLength, &maxLength, &infoLog[0]);
// We don't need the shader anymore.
glDeleteShader(vertShader);
Logger::GetSystemLogger()->error("Vertex shader compilation error");
Logger::GetSystemLogger()->error("{}", infoLog.data());
return;
}
glCompileShader(fragShader);
glGetShaderiv(fragShader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint maxLength = 0;
glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(fragShader, maxLength, &maxLength, &infoLog[0]);
// We don't need the shader anymore.
glDeleteShader(fragShader);
Logger::GetSystemLogger()->error("Fragment shader compilation error");
Logger::GetSystemLogger()->error("{}", infoLog.data());
return;
}
prog = glCreateProgram();
glAttachShader(prog, vertShader);
glAttachShader(prog, fragShader);
glLinkProgram(prog);
GLint isLinked = 0;
glGetProgramiv(prog, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(prog, maxLength, &maxLength, &infoLog[0]);
// We don't need the program anymore.
glDeleteProgram(prog);
// Don't leak shaders either.
glDeleteShader(vertShader);
glDeleteShader(fragShader);
Logger::GetSystemLogger()->error("Program link error");
Logger::GetSystemLogger()->error("{}", infoLog.data());
return;
}
glDetachShader(prog, vertShader);
glDetachShader(prog, fragShader);
glGenTextures(1, &texID); // generate texture ID
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
LoadTexture(texSrcFile);
}
以下是我如何将图像加载为纹理:
void Shader::LoadTexture(const char* texSrcFile) const {
int width, height, nrChannels;
unsigned char* data = stbi_load(texSrcFile, &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
Logger::GetSystemLogger()->error("Failed to load textures");
}
stbi_image_free(data);
}
这是我的着色器: 片段:
#version 330
precision highp float; // needed only for version 1.30
layout(location = 0) out vec4 out_Color;
in vec2 ex_Tex;
uniform sampler2D textureUnit0;
void main(void){
out_Color = texture(textureUnit0, ex_Tex);
}
顶点:
#version 330
layout (location = 0) in vec3 in_Position;
layout (location = 1) in vec2 in_Tex;
out vec2 ex_Tex;
void main(void){
gl_Position = vec4(in_Position, 1.0);
ex_Tex = in_Tex;
}
主要代码如下:
glGenVertexArrays(1, &m_VAO);
glBindVertexArray(m_VAO);
glGenBuffers(1, &m_VBO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
GLfloat vertices[4 * 5]{
// Positions // Textures
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Position
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
// Texture coord
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
glGenBuffers(1, &m_IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IBO);
unsigned int indices[6] = { 0, 1, 2, 0, 1, 3 };
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
m_Shader = std::make_shared<Engine::Shader>("src/Shaders/Background.vert", "src/Shaders/Background.frag", "res/background.jpg");
这是主循环
while (m_Running) {
// clear the screen and set the background color to grey
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
m_Shader->Bind();
glBindTexture(GL_TEXTURE_2D, m_Shader->GetTexID());
glBindVertexArray(m_VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
m_Window->OnUpdate();
}
虽然视口坐标从负一变为一 [-1, 1)
,但纹理坐标从零变为一 [0,1)
。因此,根据 GL_CLAMP_TO_BORDER
政策,四分之三的视口都被黑色填充。
您需要按如下方式修正您的 VAO 坐标:
GLfloat vertices[4 * 5]{
// Positions // Textures
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f
};
编辑: 此外,您的纹理坐标偏移量不正确。应该是:
// Texture coord
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
如果没有适当的偏移,它将使用位置的 X 和 Y 作为纹理坐标。