Pages

Wednesday, March 2, 2011

OpenGL - Dragging Object Within It

/**
 * this program moving the geometric object with the mouse dragging
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : alpha
 */


#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <iostream>

using namespace std;
float* draw(float x=100.0, float y=100.0);

void init(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(-1.0, 400.0, -1.0, 300.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
}

void motion(int x, int y){

    float* bb;

    bb = draw();

    if(bb[0] < x && x < bb[0]+100 && bb[1] < y && y < bb[1]+100){
        y = 300 - y;
        float xx = (float) x/1.00;
        float yy = (float) y/1.00;
        draw(xx,yy);

        glutPostRedisplay();

        cout << x << " " << y << endl;
    }
}

float* draw(float x, float y){
    glColor3f(1.0, 0.5, 0.1);
    glRectf(x, y, x+100.0, y+100.0);
    glFinish();
    float b[] = {x,y};
    return b;
}

int main(int argc, char *argv[]){

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400, 300);
    glutInitWindowPosition(200, 100);
    glutCreateWindow("iPod OpenGL");
    init();
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();

    return 0;
}

No comments:

Post a Comment