OpenGL textures纹理的实例
发布日期:2021-05-04 09:57:23 浏览次数:16 分类:技术文章

本文共 5762 字,大约阅读时间需要 19 分钟。

OpenGL textures纹理

先上图,再解答。

在这里插入图片描述

完整主要的源代码

#include 
#include
#include
#include
#include
#include
void framebuffer_size_callback(GLFWwindow* window, int width, int height);void processInput(GLFWwindow *window);const unsigned int SCR_WIDTH = 800;const unsigned int SCR_HEIGHT = 600;int main(){ 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);#endif GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "it_xiangqiang", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return -1; } Shader ourShader("texture.vs", "texture.fs"); float vertices[] = { // positions // colors // texture coords 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left }; unsigned int indices[] = { 0, 1, 3, // first triangle 1, 2, 3 // second triangle }; unsigned int VBO, VAO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); glEnableVertexAttribArray(2); unsigned int texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); int width, height, nrChannels; unsigned char *data = stbi_load(FileSystem::getPath("resources/textures/container.jpg").c_str(), &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; } stbi_image_free(data); while (!glfwWindowShouldClose(window)) { processInput(window); glClearColor(1.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, texture); ourShader.use(); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glfwSwapBuffers(window); glfwPollEvents(); } glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &EBO); glfwTerminate(); return 0;}void processInput(GLFWwindow *window){ if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true);}void framebuffer_size_callback(GLFWwindow* window, int width, int height){ glViewport(0, 0, width, height);}

源代码剖析

void framebuffer_size_callback(GLFWwindow* window, int width, int height)//每当窗口大小更改(通过操作系统或用户调整大小)时,此回调函数就会执行

void processInput(GLFWwindow *window)//查询GLFW是否在此帧中按下/释放了相关的按键并做出相应的反应

glfwInit();//初始化和配置

GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, “it_xiangqiang”, NULL, NULL);//glfw窗口创建

gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)//加载所有OpenGL函数指针

Shader ourShader(“4.1.texture.vs”, “4.1.texture.fs”); //您可以根据自己的喜好命名着色器文件

// 设置顶点数据(和缓冲区)并配置顶点属性 float vertices[] = {           // positions          // colors           // texture coords         0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right         0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right        -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left        -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // top left     };    unsigned int indices[] = {             0, 1, 3, // first triangle        1, 2, 3  // second triangle    };
// 位置属性    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);    glEnableVertexAttribArray(0);    // 颜色属性    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));    glEnableVertexAttribArray(1);    // 纹理坐标属性    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));    glEnableVertexAttribArray(2);
//加载并创建纹理 unsigned int texture;    glGenTextures(1, &texture);    glBindTexture(GL_TEXTURE_2D, texture);
//FileSystem :: getPath(...)是GitHub存储库的一部分,因此我们可以在任何IDE /平台上找到文件; 用您自己的图像路径替换它。unsigned char *data = stbi_load(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, &nrChannels, 0);

while (!glfwWindowShouldClose(window)){}//渲染循环

glClearColor(1.0f, 0.0f, 0.0f, 1.0f); //渲染RBG红色

glBindTexture(GL_TEXTURE_2D, texture);// 绑定纹理

ourShader.use();// 渲染 container

glfwSwapBuffers(window);//交换缓冲区和轮询IO事件(按下/释放键,移动鼠标等)

//一旦资源用完了,就取消所有资源的分配:    glDeleteVertexArrays(1, &VAO);    glDeleteBuffers(1, &VBO);    glDeleteBuffers(1, &EBO);

glfwTerminate();//终止,清除所有先前分配的GLFW资源

glViewport(0, 0, width, height);//确保视口与新窗口尺寸匹配; 请注意,宽度和高度将大大大于视网膜显示器上指定的宽度和高度。

上一篇:OpenGL textures combined组合纹理的实例
下一篇:OpenGL shader class自定义着色器的实例

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月06日 04时45分47秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章