【问题标题】:OpenGL random size rectangle code changes drawn rectanglesOpenGL随机大小矩形代码更改绘制的矩形
【发布时间】:2018-09-16 03:21:43
【问题描述】:

我想在鼠标点击的位置绘制随机大小的方块。但是我的代码改变了已经绘制的矩形的大小。我想问一下如何更改我的代码以不更改之前绘制的矩形的大小。 这是我的代码。

GLfloat myVertices[10][2];      
GLint count = 0;

std::default_random_engine(dre);
std::uniform_int_distribution<> uid(10, 100);

void Mouse(int button, int state, GLint x, GLint y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        myVertices[count][0] = x;
        myVertices[count][1] = (600 - y);

        count++;         
    }
}

GLvoid drawScene()                                  
{
    GLint index;
    if (count > 0)
    {
        for (index = 0; index < count; index++)
        {
            glRectf(myVertices[index][0], myVertices[index][1], myVertices[index][0] + uid(dre), myVertices[index][1] + uid(dre));
        }
    }

    glFlush();                              
}

【问题讨论】:

    标签: c++ opengl glut


    【解决方案1】:

    每次绘制场景时,您的代码都会生成新大小的矩形。您也必须存储它们。

    我会说,类似的。

    struct Rect {
        GLfloat x1,y1;
        GLfloat x2,y2;
    };
    
    std::vector <Rect> myVertices;
    
    void Mouse(int button, int state, GLint x, GLint y)
    {
        if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
        {
            myVertices.emplace_back(x,(600 - y),x + uid(dre), (600 - y) + uid(dre) );   
        }
    }
    
    GLvoid drawScene()                                  
    {
        GLint index;
        if (count > 0)
        {
            for(auto const& rect: myVertices) 
            {
                glRectf(rect.x1,rect.y1,rect.x2,rect.y2);
            }
        }
    
        glFlush();                              
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多