Coder123
Coder123

Reputation: 854

Waving flag in openGL (C)

I am trying to make a waving New Zealand flag using openGL and c (or c++), i made the flag using a matrix but i dont know how to make the wave effect, here is my whole code, i tried changing the parameters of the matrix in the idle function but i cant make the effect (it changes my whole flag to other shades which I am changing there:

#include "GLUT.H"
#include <math.h>

#define HEIGHT 600
#define WIDTH 600

unsigned char matrix[HEIGHT][WIDTH][3]; // r,g,b
double offset=0;

void init()
{
    int i,j;

    for(i=0;i<200;i++)
        for(j=0;j<WIDTH;j++)
        {
            matrix[i][j][0] = 0; // red
            matrix[i][j][1] = 255; // green
            matrix[i][j][2] = 0; // blue
        }

    for(i=200;i<400;i++)
        for(j=0;j<WIDTH;j++)
        {
            matrix[i][j][0] = 255; // red
            matrix[i][j][1] = 255; // green
            matrix[i][j][2] = 255; // blue
        }

    for(i=400;i<600;i++)
        for(j=0;j<WIDTH;j++)
        {
            matrix[i][j][0] = 0; // red
            matrix[i][j][1] = 0; // green
            matrix[i][j][2] = 255; // blue
        }

    glClearColor(0,0,0.4,0);
    glOrtho(-1,1,-1,1,-1,1);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawPixels(WIDTH,HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,matrix);
    glutSwapBuffers();
}

void idle()
{
    int i,j;
    double dist;
    offset-=0.14;

    for(i=0;i<HEIGHT;i++)
        for(j=0;j<WIDTH;j++)
        {
            dist = sqrt((double)(i-HEIGHT/2)*(i-HEIGHT/2)+(j-WIDTH/2)*(j-WIDTH/2));
            //matrix[i][j][0] = 0;//int(255*(1+sin((2*j)*0.03+offset))/2); // red
            //matrix[i][j][1] = dist;//int(255*(1+sin((2*dist)*0.03+offset))/2); // green
            //matrix[i][j][2] = 0;//int(255*(1+sin((j)*0.03+offset))/2); // blue
        }


    glutPostRedisplay();
}


void main( int argc, char* argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
    glutInitWindowSize(WIDTH,HEIGHT);
    glutInitWindowPosition(100,100);
    glutCreateWindow("ok");
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    init();
    glutMainLoop();
}

Any ideas\suggestions\solution(lol) how i can make it wave?

p.s: I will need to add some star to it in middle also (also clueless how to draw it so it will wave)

Upvotes: 0

Views: 4947

Answers (1)

JMA
JMA

Reputation: 494

You can play with colors, making some of them darker, something like that (sorry beucause it's a spagetti code, you will have to adjust and optimize it):

void idle()
{
    static float count = 0.0;

    int i,j;
    count += 0.05;
    offset-=0.14;

    for(i=0;i<200;i++)
        for(j=0;j<WIDTH;j++)
        {
            matrix[i][j][0] = 0; // red
            matrix[i][j][1] = 200 + 55 * sin(count + j * 0.02); // green
            matrix[i][j][2] = 0; // blue
        }

    for(i=200;i<400;i++)
        for(j=0;j<WIDTH;j++)
        {
            matrix[i][j][0] = 200 + 55 * sin(count + j * 0.02); // red
            matrix[i][j][1] = 200 + 55 * sin(count + j * 0.02); // green
            matrix[i][j][2] = 200 + 55 * sin(count + j * 0.02); // blue
        }

    for(i=400;i<600;i++)
        for(j=0;j<WIDTH;j++)
        {
            matrix[i][j][0] = 0; // red
            matrix[i][j][1] = 0; // green
            matrix[i][j][2] = 200 + 55 * sin(count + j * 0.02); // blue
        }

    glutPostRedisplay();
}

Upvotes: 1

Related Questions