计算机动画实验(一)OpenGL实现关键帧动画

您所在的位置:网站首页 逐帧动画例子大全 计算机动画实验(一)OpenGL实现关键帧动画

计算机动画实验(一)OpenGL实现关键帧动画

2023-11-04 15:19| 来源: 网络整理| 查看: 265

实验题目来自2021年春季学期山东大学软件学院计算机动画基础课程 本人比较菜,代码有很多bug以及莫名其妙的地方,发在这记录一下写代码的艰辛😭,仅供参考思路哦! 现在代码已经找不到了,请不要找我要文件啦!(当然,欢迎指正)

使用glfw,glad库,C++编写,参考LearnOpenGL 实验题目

通过关键帧插值动画技术展现出椭圆形嘴巴变化成长方形口罩的过程,同时体现出颜色的变化

思路

分别定义一个椭圆和一个长方形的顶点数组,用于形状插值。分别加载嘴巴(接近椭圆形)和口罩(接近长方形)的纹理图片,其位置坐标即分别为椭圆和长方形的位置坐标,纹理坐标用其位置坐标稍加计算修改即可。

实现效果

变变变

步骤

定义顶点数组分别绘制椭圆和长方形。按照对称的一上一下顶点绘制,这种顺序可以直接用于绘制三角形面片,在定义ebo时稍加改动即可。

void drawEllipse() { float s = 3 * 2 * 2 * a / points; //distance float x, y; for (int t = 0,t1 = 0; t float n = points / (2 * 3); //number of points of one long edge float s = 2 * le / n; //distance float x; for (int t = 0, t2 = 0; t indices[j * 3] = j; indices[j * 3 + 1] = j + 1; indices[j * 3 + 2] = j + 2; }

建立vao、vbo,椭圆和长方形的顶点数组分别绑定到不同的vbo(用不同的position值区分),并将两者绑定到同一个vao上,同时绑定ebo。

unsigned int VBOs[2], VAO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(2, VBOs); glGenBuffers(1, &EBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(elip), elip, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glEnableVertexAttribArray(0);

加载嘴和口罩的纹理图片,设置环绕方式,并将其与着色器绑定。

unsigned int texture1, texture2; // texture 1 glGenTextures(1, &texture1); glBindTexture(GL_TEXTURE_2D, texture1); // set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // set texture wrapping to GL_REPEAT (default wrapping method) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load image, create texture and generate mipmaps int width, height, nrChannels; stbi_set_flip_vertically_on_load(true); unsigned char* data = stbi_load("mouth5.png", &width, &height, &nrChannels, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else { std::cout


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3