0

I want to draw random size squares in the position where the mouse clicks. But my code changes the size of the rectangle already painted. I want to ask how I can change my code for not Change the size of the rectangle drew before. here is my code little.

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();                              
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
cookiecho
  • 11
  • 1

1 Answers1

1

Your code generates new size of rectangles every time scene is drawn. You have to store those too.

I'd say , something like that.

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();                              
}
Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42