I am trying to get the object to follow the mouse after it has been clicked and appeared on openGL C++. Here is my code so far:
#include <iostream>
#include <GL/glut.h>// include GLUT library
#include <string>
#include <stdlib.h>
#include <math.h>
//***********************************************************************************
float x, y, xDistance, yDistance;
int check = 0;
double speed = 10;
static GLubyte smiley[] = /* Man */
{
0x01, 0x80, 0, 0, ///1
0x03, 0xE0, 0, 0,
0x01, 0xFF, 0x80, 0,
0x00, 0x3f, 0x80, 0,
0x70, 0x00, 0x80, 0, ///2
0xFC, 0x00, 0x80, 0,
0x1f, 0x80, 0x80, 0,
0x03, 0xC0, 0xC0, 0,
0x00, 0xC1, 0x80, 0, //3
0x00, 0xC1, 0x80, 0,
0x00, 0xc1, 0x80, 0,
0x00, 0xc3, 0, 0,
0x00, 0xC3, 0, 0, //4
0x00, 0x86, 0, 0,
0x00, 0x86, 0, 0,
0x00, 0x8c, 0, 0 ,
0x00, 0x8c, 0, 0,//5
0x01, 0x8c, 0, 0,
0x01, 0x9c, 0, 0,
0x01, 0x9c, 0, 0,
0x01, 0x9c, 0, 0,//6
0x01, 0xDC, 0, 0,
0x01, 0xDC, 0, 0,
0x01, 0xFC, 0, 0,
0x01, 0xFC, 0x10, 0,//7
0x01, 0xFE, 0x70, 0,
0x01, 0xFE, 0xC0, 0,
0x03, 0xFF, 0x80, 0,
0x03, 0xFF, 0, 0,//8
0x03, 0xFF, 0, 0,
0x03, 0xFD, 0xFC, 0,
0x03, 0xF8, 0x3, 0,//////////
0x03, 0xF8, 0x3, 0,//9
0x03, 0xF8, 0x6, 0,
0x03, 0xF8, 0xC, 0,
0x03, 0xF8, 0x18, 0,
0x03, 0xFc, 0x70, 0,//10/////
0x03, 0xFC, 0xE0, 0,
0x01, 0xFD, 0xC0, 0,
0x01, 0xFF, 0xC0, 0,/////
0x01, 0xFF, 0x80, 0,//11
0x03, 0xFF, 0, 0,
0x07, 0xBE, 0, 0,
0x0F, 0x98, 0, 0,
0x0F, 0x30, 0, 0,//12
0x1E, 0x3c, 0, 0,
0x3c, 0x7f, 0, 0,
0x38, 0x7f, 0x80, 0,
0x70, 0x7f, 0xc0, 0,//13
0x63, 0xFF, 0xE0, 0,
0x60, 0x7f, 0xF0, 0,
0xE0, 0x3f, 0xF0, 0,/////
0xE0, 0x1f, 0xF0, 0,//14
0xE0, 0x1f, 0xF0, 0,
0xF0, 0x0F, 0xF0, 0,
0x3f, 0xF7, 0xF0, 0,
0x01, 0xF3, 0xF0, 0,//5///
0x00, 0x10, 0x1c, 0,
0x00, 0x18, 0x06, 0,
0x00, 0x0F, 0x06, 0,
0x00, 0x06, 0x01, 0,//5////
0x00, 0x00, 0x01, 0xc0,
0x00, 0x00, 0, 0x20,
0x00, 0x00, 0, 0x20,
0x00, 0x00, 0, 0xE0,//5
0x00, 0x00, 0, 0,
0x00, 0x00, 0, 0,
0x00, 0x00, 0, 0,
};
std::string input;
void drawPoints()
{
float x, y;
//axis labels
glColor3f(0, 0, 0);
glRasterPos2i(155, -5);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, 'X');
glRasterPos2i(-5, 155);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, 'Y');
glPointSize(5);
glBegin(GL_POINTS); // use points to form X-/Y-axes
// change drawing color to black
for (int xAxis = -150; xAxis <= 150; xAxis++) // draw X-axis
glVertex2i(xAxis, 0);
for (int yAxis = -150; yAxis <= 150; yAxis++) // draw Y-axis
glVertex2i(0, yAxis);
glEnd();
}
void mouse(int button, int state, int mousex, int mousey)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
check = 1;
x = mousex - 400/2;
y = -mousey + 400/2;
}
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)//undo(clear)the drawing
{
check = 2;
x = mousex - 400 / 2;
y = -mousey + 400 / 2;
}
else
{
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT);
}
glutPostRedisplay();
}
void move() {
}
//***********************************************************************************
void myInit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClearColor(1, 1, 1, 0); // specify a background clor: white
gluOrtho2D(-200, 200, -200, 200); // specify a viewing area
glFlush();
}
//***********************************************************************************
void myDisplayCallback()
{
glClear(GL_COLOR_BUFFER_BIT); // draw the background
drawPoints();
if (check == 1)
{
glColor3f(1.0, 0.0, 0.0);
glRasterPos2i(x, y);
glBitmap(32, 72, 8.0, 8.0, 0.0, 0.0, smiley);
glEnd();
}
else if (check == 2)
{
glColor3f(0.0, 1.0, 0.0);
glRasterPos2i(x, y);
glBitmap(32, 72, 8.0, 8.0, 0.0, 0.0, smiley);
glEnd();
}
glFlush(); // flush out the buffer contents
}
//***********************************************************************************
int main(int argc, char** argv)
{
glutInit(& argc, argv); // optional in our environment
glutInitWindowSize(400, 400); // specify a window size
glutInitWindowPosition(100, 0); // specify a window position
glutCreateWindow("Simple Point Drawing"); // create a titled window
myInit(); // setting up
glutDisplayFunc(myDisplayCallback); // register a callback
glutMouseFunc(mouse);
glutMainLoop(); // get into an infinite loop
return 0;
}