Jane
Jane

Reputation: 25

OpenGL Mouse Tracking (Freeform Line)

At the moment I am trying to make a program that tracks the mouse coordinates so that it draws a freeform line as you are moving it. So far the program is tracking the mouse and drawing the lines but the lines are all going back to a coordinate that almost acts as a origin. My professor mentioned that the freeform line is achieved through the variable previous x and previous y and how each time the mouse moves the old one is stored in previous x and previous y and then it will connect to the new x and y coordinate.

How would I go about changing the previousx and previousy variable to achieve the freeform line?

// To compile in Linux: g++ -o mouse_exam mouse_exam.cpp -lGL -lGLU -lglut -lm

#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GlUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif

#include <iostream>
#include <cmath>
using namespace std;

#define WIDTH 600
#define HEIGHT 600

#define WLEFT 0
#define WRIGHT WIDTH
#define WBOTTOM 0
#define WTOP HEIGHT 

#define RGBBLACK 0,0,0
#define RGBGREY .8,.8,.8

static int tracking = 0;
float previousx;
float previousy;

int inwindow(int x, int y)
{
    return(x>WLEFT && x<WRIGHT && y>WBOTTOM && y<WTOP);
}

void m_motion(int x, int y)
{
    y = WTOP-y;



    if(tracking && inwindow(x,y))
    {
        glBegin(GL_LINES);
            glVertex2f(previousx,previousy);
            glVertex2f(x,y);
        glEnd();

        glFlush();
    }
}

void handleButton(int button, int state, int x, int y)
{
    y=WTOP-y; 

    if(button != GLUT_LEFT_BUTTON)
    {
        return;
    }

    if(state == GLUT_DOWN)
    {
        if(inwindow(x,y))
        {
            tracking = 1;  
            previousx =x;
            previousy =y;

            cout << previousx << " " << previousy << endl;
        }
    }

    else
    {
        tracking = 0;
    }
}

void drawMouse(void)
{
    int i;

    glClearColor(RGBGREY,1);
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(RGBBLACK);

    glFlush();

}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(WIDTH,HEIGHT);
    glutCreateWindow("Mouse Exam");
    glutDisplayFunc(drawMouse);
    glutMouseFunc(handleButton);
    glutMotionFunc(m_motion);
    gluOrtho2D(0,WIDTH,0,HEIGHT);

    glutMainLoop();

    return 0;
}

This outputs this on the window when I compile and run it

enter image description here

Upvotes: 0

Views: 995

Answers (1)

Gato
Gato

Reputation: 671

Replace this part:

if(tracking && inwindow(x,y))
{
    glBegin(GL_LINES);
        glVertex2f(previousx,previousy);
        glVertex2f(x,y);
    glEnd();

    glFlush();
}

With this:

if(tracking && inwindow(x,y))
{
    glBegin(GL_LINES);
        glVertex2f(previousx,previousy);
        glVertex2f(x,y);
    glEnd();

    glFlush();

    previousx = x;  /* ADDED */
    previousy = y;  /* ADDED */
}

Notice the two lines I added. The idea is to always keep the last point of the line you have already drawn in previousx and previousy, so the next time you draw another segment, you start from there.

Upvotes: 1

Related Questions