下面是一个使用GLFW缩放纹理并显示的简单示例代码:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
// 回调函数:窗口大小变化时触发,用于调整视口
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main()
{
// 初始化GLFW
if (!glfwInit())
{
std::cout << "Failed to initialize GLFW" << std::endl;
return -1;
}
// 创建窗口和OpenGL上下文
GLFWwindow* window = glfwCreateWindow(800, 600, "Texture Scaling Example", nullptr, nullptr);
if (window == nullptr)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// 设置当前窗口为当前线程的主上下文
glfwMakeContextCurrent(window);
// 设置视口大小回调函数
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// 初始化GLEW
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
glfwTerminate();
return -1;
}
// 纹理坐标数组
float textureCoordinates[] = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
// 顶点数组
float vertices[] = {
// 位置 // 纹理坐标
-0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 1.0f
};
// 索引数组
unsigned int indices[] = {
0, 1, 2,
2, 3 ,0
};
// 创建并绑定顶点数组对象(VAO)
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// 创建并绑定顶点缓冲对象(VBO)
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices), vertices,GL_STATIC_DRAW);
// 设置顶点属性指针
glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,sizeof(float)*4,(void*)nullptr);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,sizeof(float)*4,(void*)(sizeof(float)*2));
glEnableVertexAttribArray(1);
// 创建并绑定索引缓冲对象(EBO)
unsigned int EBO;
glGenBuffers(1,&EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
// 解绑VAO、VBO和EBO
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// 加载纹理
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// 设置纹理参数
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// 加载图片并生成纹理
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true); // 翻转加载图片的Y轴,因为OpenGL坐标系的Y轴方向与大部分图片格式相反
unsigned char* data = stbi_load("texture.jpg", &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
{
std::cout << "Failed to load texture" << std::endl;
glfwTerminate();
return -1;
}
stbi_image_free(data);
while(!glfwWindowShouldClose(window))
{
// 清除颜色缓冲区
glClear(GL_COLOR_BUFFER_BIT);
// 绑定纹理
glBindTexture(GL_TEXTURE_2D, texture);
// 使用着色器程序、VAO和EBO绘制矩形
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// 交换前后缓冲区
glfwSwapBuffers(window);
// 处理事件
glfwPollEvents();
}
// 清理资源
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1,&VBO);
glDeleteBuffers(1,&EBO);
glDeleteTextures(1,&texture);
// 终止GLFW
glfwTerminate();
return 0;
}
这个示例使用了glfw库来创建窗口和OpenGL上下文,glew库来初始化GLEW,stb_image库来加载纹理图像。你需要在代码中替换掉"texture.jpg"为你自己的纹理图像路径。
以上是一个简单的纹理缩放并显示的例子,但是没有包括完整的着色器程序等内容,你可以根据实际需求进行扩展和修改。
内容由零声教学AI助手提供,问题来源于学员提问




