opengl创建柱面和鱼眼重展uv

发布于:2024-08-13 ⋅ 阅读:(79) ⋅ 点赞:(0)

专业软件

先看一下专业软件
可以拉取很多的uv点
在这里插入图片描述

以下是使用 OpenGL 创建不规则面片并指定 UV 的一般步骤:
1 顶点数据准备
2 定义面片的顶点坐标。这些顶点构成了面片的形状。
3 为每个顶点指定对应的纹理坐标(UV)。
4 创建顶点缓冲区对象(VBO)
5 将顶点数据和 UV 数据存储在 VBO 中。
6 顶点数组对象(VAO)设置
7 绑定 VAO 并配置顶点属性指针,指定顶点位置和 UV 的布局。
8 绘制面片
使用合适的绘制命令(如 glDrawArrays 或 glDrawElements )来绘制面片。

show me the code

下面是示例代码

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

// 顶点数据
float vertices[] = {
    // 位置              // UV
    -0.5f, -0.5f, 0.0f,  0.0f, 0.0f,
     0.5f, -0.5f, 0.0f,  1.0f, 0.0f,
     0.5f,  0.5f, 0.0f,  1.0f, 1.0f,
    -0.5f,  0.5f, 0.0f,  0.0f, 1.0f
};

// 索引数据(用于指定三角形的连接顺序)
unsigned int indices[] = {
    0, 1, 2,
    0, 2, 3
};

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

int main()
{
    // 初始化 GLFW
    if (!glfwInit())
    {
        std::cerr << "Failed to initialize GLFW" << std::endl;
        return -1;
    }

    // 创建窗口
    GLFWwindow* window = glfwCreateWindow(800, 600, "Irregular Patch", NULL, NULL);
    if (!window)
    {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    // 初始化 GLEW
    if (glewInit()!= GLEW_OK)
    {
        std::cerr << "Failed to initialize GLEW" << std::endl;
        return -1;
    }

    glfwSetKeyCallback(window, key_callback);

    // 创建顶点缓冲区对象(VBO)和顶点数组对象(VAO)
    unsigned int VBO, VAO, EBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    // 绑定 VAO
    glBindVertexArray(VAO);

    // 绑定 VBO 并上传顶点数据
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    // 绑定 EBO 并上传索引数据
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    // 设置顶点属性指针
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);

    // 解绑 VAO
    glBindVertexArray(0);

    // 渲染循环
    while (!glfwWindowShouldClose(window))
    {
        // 清除颜色缓冲区
        glClear(GL_COLOR_BUFFER_BIT);

        // 绑定 VAO 并绘制
        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;
}

网站公告

今日签到

点亮在社区的每一天
去签到