简体   繁体   English

来自选定像素的波(openGL C++)

[英]waves from selected pixel (openGL C++)

I've created a program that draws a waving flag and I want to add a functionality that will create new wave on selected pixel, but I can't make it start where I want it to start an that even make the flag stop waving (prob. because of synced sin).我创建了一个绘制挥动旗帜的程序,我想添加一个功能,在选定的像素上创建新的波浪,但我无法让它从我想要它开始的地方开始,甚至让旗帜停止挥动(可能是因为同步罪)。

Here's my display func.这是我的显示功能。

const int W = 800;
const int H = 600;
// simulates Frame Buffer
unsigned char pixels[H][W][3] = { 0 }; // 3 is for RGB
    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT); // clean frame buffer
    
        createFlag();
        int i, j;
        double dist;
        offset += 0.25;
        for (i = 0; i < H; i++)
            for (j = 0; j < W; j++)
            {
                dist = sqrt(pow(i + H / 2.0, 2) + pow(j + W / 2.0, 2));
                pixels[i][j][0] += 135 + 55 * (1 + 1 * sin(dist / 25 - offset)) / 2; // red
                pixels[i][j][1] += 135 + 85 * (1 + 1 * sin(dist / 25 - offset)) / 2; // green
                pixels[i][j][2] += 135 + 105 * (1 + 1 * sin(dist / 25 - offset)) / 2; // blue
    
            }
        // draws the matrix pixels
        glDrawPixels(W, H, GL_RGB, GL_UNSIGNED_BYTE, pixels);
    
        glutSwapBuffers(); // show all
    }

And here is my mouse func.这是我的鼠标功能。

void mouse(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        double dist;
        offset += 0.1;
        for (y = 0; y < H; y++)
            for (x = 0; x < W; x++)
            {
                dist = sqrt(pow(H/2.0 -(H - y), 2) + pow(W/2.0 -x, 2)); //problem is prob. here
                pixels[y][x][0] += 135+ 55 * (1 + 1 * sin(dist / 50.0 - offset)) / 2; // red
                pixels[y][x][1] += 135+ 85 * (1 + 1 * sin(dist / 50.0 - offset)) / 2; // green
                pixels[y][x][2] += 135+105 * (1 + 1 * sin(dist / 50.0 - offset)) / 2; // blue
                if (offset < 0.3)
                    offset += 0.05;
            }
    }
}

Here are some points that I see:以下是我看到的几点:

  • in your mouse function you don't use your arguments x and y which are the position of your mouse click, instead you create local variables x and y .在鼠标函数中,您不使用作为鼠标单击位置的参数xy ,而是创建局部变量xy If you want to start a wave from the pixel you clicked on, you have to center your dist to this point, so an idea of code could be (inspired by GLUT mouse button down ):如果你想从你点击的像素开始一个波浪,你必须将你的dist居中到这一点,所以代码的想法可能是(灵感来自GLUT mouse button down ):

     void mouse(int button, int state, int x, int y) { // save the left button state if (button == GLUT_LEFT_BUTTON) { leftMouseButtonDown = (state == GLUT_DOWN); } // save the mouse position mouseXPos = x; mouseYPos = y; } void display() { glClear(GL_COLOR_BUFFER_BIT); // clean frame buffer createFlag(); double dist; offset += 0.25; for (i = 0; i < H; i++) for (j = 0; j < W; j++) { dist = sqrt(pow(i + H / 2.0, 2) + pow(j + W / 2.0, 2)); pixels[i][j][0] += 135 + 55 * (1 + 1 * sin(dist / 25 - offset)) / 2; // red pixels[i][j][1] += 135 + 85 * (1 + 1 * sin(dist / 25 - offset)) / 2; // green pixels[i][j][2] += 135 + 105 * (1 + 1 * sin(dist / 25 - offset)) / 2; // blue } if (leftMouseButtonDown) // the way you can get leftMouseButtonDown depends on your // configuration of your different files // (main.cpp, scene.cpp, ...) new_wave() ; //see below // draws the matrix pixels glDrawPixels(W, H, GL_RGB, GL_UNSIGNED_BYTE, pixels); glutSwapBuffers(); // show all } void new_wave() { x = mouseXPos ; // same remark as leftMouseButtonDown y = mouseYPos ; offset = 0.1; for (i = 0; i < H; i++) // i and not y for (j = 0; j < W; j++) { dist = sqrt(pow(i - y, 2) + pow(j - x, 2)); // change here pixels[i][j][0] += 135+ 55 * (1 + 1 * sin(dist / 50.0 - offset)) / 2; // red pixels[i][j][1] += 135+ 85 * (1 + 1 * sin(dist / 50.0 - offset)) / 2; // green pixels[i][j][2] += 135+105 * (1 + 1 * sin(dist / 50.0 - offset)) / 2; // blue if (offset < 0.3) offset += 0.05; // I don't really get what you do here with the offset, but another parameter to // play with could be the amplitude of this wave that starts from the point // you clicked, like when a drop falls on water } }
  • if you want a real-time waving flag you have to take time as a parameter of your pixels color value如果您想要一个实时挥动的旗帜,您必须将时间作为像素颜色值的参数

  • I haven't tested it but I'm not sure on how openGL reacts to RGB coloring with values over 255, which happens in your case, maybe keep that in mind if you still have bugs我尚未对其进行测试,但我不确定openGL如何对值超过255的RGB着色做出反应,这发生在您的情况下,如果您仍然有错误,请记住这一点

  • dist = sqrt(pow(i + H / 2.0, 2) + pow(j + W / 2.0, 2)); this is centered in (-W/2.0, -H/2.0) , is that what you want?这以(-W/2.0, -H/2.0)为中心,这是你想要的吗? (maybe yes just want to make sure, if you want to simulate some wind you could set wind's origin where you want, which is what you do here) (也许是的,只是想确保,如果你想模拟一些风,你可以在你想要的地方设置风的起源,这就是你在这里所做的)

  • int i, j; in your display isn't useful (just to clear some code)在您的显示中没有用(只是为了清除一些代码)

So these are some remarks I would have made myself, this piece of code probably won't be your final one.所以这些是我自己会做的一些评论,这段代码可能不会是你的最后一个。 Let me know if I misunderstood what you are aiming to do, or if something I wrote is unclear.如果我误解了您的目标,或者我写的内容不清楚,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM