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;
}

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;
}

OpenGL - Between Green And Blue

/**
 * This function must be called by glutMotionFunc(motion)
 */

void motion(int x, int y){
    float xx  = (x/400.00);
    float yy = (y/300.00);
    glClearColor(0.0, xx, yy, 1.0);
    glutPostRedisplay();
}

OpenGL - Between Red and Blue

void motion(int x, int y){
    float xx  = (x/400.00);        // must be float divider
    float yy = (y/300.00);         // must be float divider
    glClearColor(xx, 0.0, yy, 1.0);
    glutPostRedisplay();
}

OpenGL - Between Red and Green

void motion(int x, int y){
    float xx  = (x/400.00);      // must be float divider
    float yy = (y/300.00);       // must be float divider
    glClearColor(xx, yy, 0.0, 1.0);
    glutPostRedisplay();
}
/**
 * This snippet of code will change your background color from
 * pure of black to pure of white when your drag has been reached
 * 1000 event. This event will invoked when you drag your mouse button
 */

void motion(int x, int y){
    static float i = 0;
    glClearColor(i/1000, i/1000, i/1000, 1.0);
    glutPostRedisplay();
    i++;
}

Tuesday, March 1, 2011

OpenGL - Backgrouning Using Keyboard

/**
 * This function will backgrouning the background based on
 * the key you type.
 * for example, if you type y, you will get the yellow background
 */

void keyboard(unsigned char key, int x, int y){

    switch(key){
        case 'r':
            glClearColor(1.0, 0.0, 0.0, 1.0);
            glutPostRedisplay();              // refresh window
            break;
        case 'b':
            glClearColor(0.0, 0.0, 0.0, 1.0);
            glutPostRedisplay();
            break;
        case 'g':
            glClearColor(0.0, 1.0, 0.0, 1.0);
            glutPostRedisplay();
            break;
        case 'y':
            glClearColor(1.0, 1.0, 0.0, 1.0);
            glutPostRedisplay();
            break;
        case 'm':
            glClearColor(1.0, 0.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'a':
            glClearColor(0.0, 1.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'u':
            glClearColor(0.0, 0.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'w':
            glClearColor(1.0, 1.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'h':
            cout << "Enter a key on keyboard to backgrouning!" << endl;
            cout << "a -> aqua" << endl;
            cout << "r -> red" << endl;
            cout << "b -> blue" << endl;
            cout << "g -> green" << endl;
            cout << "y -> yellow" << endl;
            cout << "m -> magenta" << endl;
            cout << "b -> black" << endl;
            cout << "w -> white" << endl;
            break;
    }
}