c++ - Opengl 中飘扬的旗帜效果 (C++)

标签 c++ opengl freeglut

<分区>

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

/image/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;
}

结果是这样的: /image/MVSp0.png没有任何动画。

最佳答案

user2927848 提到的时间需要更新。我只想建议你的挥手效果。如果你想在你的vertex shader中传递时间,你需要超过四个你自己生成的顶点,因为你的vertex shader 只被pipleline调用4次 , 每个顶点之一。所以你不会得到你期望的美波效果。

总而言之,有两种方法可以让你的旗帜顺利飘扬。

  1. 将更多顶点传递给顶点着色器
  2. 将时间变量传递给片段着色器

在第一个建议中,您可能需要生成 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;
}

关于c++ - Opengl 中飘扬的旗帜效果 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40534628/

相关文章:

c++ - OpenGL - 调用不同颜色的方形函数

c++ - 我如何在 Linux Ubuntu 14.04 LTS 上设置 freeglut 库

c++ - C++ 中的内联函数 - 条件

c++ - OpenSSL CMS 在 C++ 和 Objective-c 中加密

c++ - 使定义/包含生成的结构适应 boost::fusion

c++ - OpenGL 截掉部分屏幕

c++ - 将 unique_ptr 与 gsl_vector 一起使用

c++ - 在 OpenGL 中自由类型文本后面的渲染正方形被裁剪

c++ - 以下代码中 glTransalatef 和 glScalef 的输入是什么?怎么运行的?

c++ - glutBitmapString/glutStrokeString 似乎需要 const unsigned char* - 字符串不起作用