Waving Flag Effect in Opengl (C++) -


as part of project; need create flag waving effect shown below:

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

i couldn't manage add wave effect removed crescent & star , trying wave flag itself.

i believe when pass time, doesn't update animation doesn't happen. did far is:

    #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 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 , use resulting shader program     time = glutget(glut_elapsed_time);     gluint program = initshader("vshader.glsl", "fshader.glsl");     gluseprogram(program);     // set vertex arrays     gluint vposition = glgetattriblocation(program, "vposition");     glenablevertexattribarray(vposition);     glvertexattribpointer(vposition, 2, gl_float, gl_false, 0, 0);     // paint 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 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 window     glutcreatewindow("i'm rick harrison, , pawn shop");     glewexperimental = gl_true;     glewinit();      init();      glutdisplayfunc(display);     glutkeyboardfunc(keyboard);      glutmainloop();     return 0; } 

my shader files are:

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

and

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

it results in this: https://i.stack.imgur.com/mvsp0.png without animations.

time needed updated mentioned user2927848. want advise waving effect. if want pass time in vertex shader, need more 4 vertices generated yourself, because of vertex shader only called 4 times pipleline, 1 of each vertex. not beauty wave effect expected.

in conclusion, there 2 ways flag waving smoothly.

  1. pass more vertex vertex shader
  2. pass time variable fragment shader

in first suggestion, need generate maybe 100 * 50 vertices make smoothly, or maybe more better like.

the second suggestion has little issue, if image entirely fit in plane, need somehow let image have margin away border. easy way slove make *.png image have transparency margin @ border , whatever waving function @ uv value.

i implement simplest waving effect in shadertoy. put code below, because short...

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

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -