Pages

Wednesday, March 2, 2011

OpneGL - Mouse Dragging

#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){
    /**
     * The value of y must be converted, because the difference 
     * of coordinate space system.
     */     
    y = 300 - y;          
    draw(x/1.0, y/1.0);
    glutPostRedisplay();

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

void draw(float x, float y){
    glColor3f(3.0, 0.9, 0.7);
    glRectf(10.0, 10.0, x,y);
    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