Opengl (c++)中的挥舞旗帜效果

Waving Flag Effect in Opengl (C++)

本文关键字:旗帜 c++ Opengl      更新时间:2023-10-16

作为项目的一部分;我需要创建一个旗帜的挥舞效果,如下所示:

https://i.stack.imgur.com/db5zB.gif

我无法添加波浪效果,所以我删除了新月&现在想要自己挥舞国旗。

我相信当我通过时间时,它不会更新,所以动画不会发生。到目前为止,我所做的是:

    #include "Angel.h"
float PI = 3.14;
int verticeNumber = 0;
float time;
struct point {
    GLfloat x;
    GLfloat y;
};
point vertices[500];


// OpenGL initialization
void init()
{
    // Create a vertex array object
    vertices[0].x = -0.75;
    vertices[0].y = 0.5;
    vertices[1].x = 0.75;
    vertices[1].y = 0.5;
    vertices[2].x = 0.75;
    vertices[2].y = -0.5;
    vertices[3].x = -0.75;
    vertices[3].y = -0.5;
    vertices[4].x = -0.75;
    vertices[4].y = 0.5;
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    // Load shaders and use the resulting shader program
    time = glutGet(GLUT_ELAPSED_TIME);
    GLuint program = InitShader("vshader.glsl", "fshader.glsl");
    glUseProgram(program);
    // set up vertex arrays
    GLuint vPosition = glGetAttribLocation(program, "vPosition");
    glEnableVertexAttribArray(vPosition);
    glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
    // Paint the background
    glClearColor(0.36, 0.74, 0.82, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 5);

    glutSwapBuffers();
}
void display(void)
{
}

// Ends the program on ESC press.
void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
    case 033:
        exit(EXIT_SUCCESS);
        break;
    }
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(800, 800);
    // OpenGL Version Check
    glutInitContextVersion(3, 2);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    // Name the window
    glutCreateWindow("I'm Rick Harrison, and this is my pawn shop");
    glewExperimental = GL_TRUE;
    glewInit();
    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

我的着色器文件是:

#version 430
varying vec4 f_color;
void main(void) {
    gl_FragColor = vec4(1,0,0,1);
}

#version 430
in vec4 vPosition;
in float time;
void main()
{
    vec4 temp = vPosition;
    temp.y = cos(0.1*time)*temp.y;
    gl_Position = temp;
}

结果如下:https://i.stack.imgur.com/MVSp0.png没有任何动画

需要更新user2927848提到的时间我只是想建议一下你的摇摆效果。如果你想在你的顶点着色器中度过时间,你需要自己生成四个以上的顶点,因为你的顶点着色器只被流水线调用4次,每个顶点一个。所以你不会得到你所期望的美容波效果。

总之,有两种方法可以让你的旗帜顺利挥舞。

  1. 传递更多顶点到顶点着色器
  2. 将时间变量传递给Fragment Shader
在第一个建议中,您需要生成可能100 * 50个顶点以使其平滑,或者可能更多以使其看起来更好。

第二个建议也有一个小问题,如果你的图像完全适合你的平面,那么你需要以某种方式让图像有一些边界远离边界。解决这个问题的简单方法是使*.png图像在边框处有一些透明边距,并在uv值处做任何波浪函数。

我只是在shadertoy中实现了最简单的波浪效果。也把代码放在下面,因为它很短…

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = fragCoord.xy / iResolution.xy;
    uv.y = uv.y + 0.1 * sin(iGlobalTime +  10.0 * uv.x);
    vec4 textureColor = texture2D(iChannel0, uv);
    fragColor = textureColor;
}

我没有看到你每帧更新'time'。https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/uniform.php

在你的display函数里面是空的,所以你永远不会在init之后重新绘制图像。所以即使调整时间本身也不能解决这个问题。你应该在显示函数中完成所有实际的绘制/切换缓冲区。

同样,这样做将上下移动整个项目。你应该有cos(速度*(时间+偏移量)),其中偏移量是根据到标志底部的距离计算的。你可能需要更多的顶点使动画流畅,所以即使你得到这个工作,它看起来很奇怪,这就是为什么。

有很多问题,但这应该会让你朝着正确的方向前进。