//Partial Lab 4 solution //Click and move portion only //Alan Jamieson #include GLfloat xyz[3] = {0.0, 0.0, 0.0}; GLfloat theta[3] = {0.0, 0.0, 0.0}; GLint mousepos[2] = {0,0}; GLfloat size = 100; int inflag = 0; void myinit(){ glColor3f(0.0, 1.0, 0.0); glClearColor(0.0,0.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 500.0, 500.0, 0.0, 0, 500.0); glMatrixMode(GL_MODELVIEW); } void display(){ glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); glTranslatef(xyz[0], xyz[1], xyz[2]); glutWireCube(size); glFlush(); } //dead simple cursor inbound detection int inbounds(){ if(mousepos[0] < (xyz[0] + size/2) && mousepos[0] > (xyz[0] - size/2)) if(mousepos[1] < (xyz[1] + size/2) && mousepos[1] > (xyz[1] - size/2)) return(1); return(0); } void mymouse(int btn, int state, int x, int y){ mousepos[0] = x; mousepos[1] = y; //check for inbounds if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN && inbounds() == 1){ inflag = 1; } if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN && inbounds() == 1){ inflag = 1; } //upon release, move the cube to the cursor if(btn == GLUT_LEFT_BUTTON && state == GLUT_UP && inflag == 1){ inflag = 0; xyz[0] = x; xyz[1] = y; } if(btn == GLUT_RIGHT_BUTTON && state == GLUT_UP && inflag == 1){ inflag = 0; } //update the display glutPostRedisplay(); } int main(int argc, char **argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow("Lab 4 Solution"); glutDisplayFunc(display); glutMouseFunc(mymouse); myinit(); glutMainLoop(); return(0); }