Pages

Wednesday, March 2, 2011

OpenGL - Dragging Object

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

#include <iostream>

using namespace std;
void draw(float, float);

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){
    y = 300 - y;
    draw(x/1.0, y/1.0);
    glutPostRedisplay();

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

void draw(float x, float y){
    glColor3f(1.0, 0.5, 0.1);
    glRectf(x, y, x-100.0, y-50.0);  // x1, y1, x2, y2
    glFinish();
}

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