GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Thursday, March 10, 2011

OpenGL - Moving Quads

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

#include <iostream>

using namespace std;

void init();
void display();

float a[4] = {10.0, 10.0, 100.0, 100.0};
float b[4] = {10.0, 100.0, 100.0, 10.0};

void setTree(float *x, float *y){
    glBegin(GL_QUADS);
        glColor3f(1.0, 1.0, 0.0);
        glVertex2f(x[0], y[0]);
        glColor3f(0.0, 1.0, 0.0);
        glVertex2f(x[1], y[1]);
        glColor3f(1.0, 0.5, 0.0);
        glVertex2f(x[2], y[2]);
        glColor3f(1.0, 1.0, 1.0);
        glVertex2f(x[3], y[3]);
    glEnd();
}

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

void display(){
    glClear( GL_COLOR_BUFFER_BIT);
    glPointSize(10.0);


    setTree(a,b);

    glFlush();
}

void mouse(int mouse, int state, int x, int y){
    switch(mouse){
        case GLUT_LEFT_BUTTON:
            if(state == GLUT_DOWN){
                cout << x << " -> " << y << endl;

                y = 300.0 - y;

                a[0] = x;
                a[1] = x;
                a[2] = x + 100.0;
                a[3] = x + 100.0;

                b[0] = y;
                b[1] = y + 100.0;
                b[2] = y + 100.0;
                b[3] = y;

                glutPostRedisplay();
            }
        break;

    }
}

void drag(int x, int y){
    y = 300.0 - y;

    a[0] = x;
    a[1] = x;
    a[2] = x + 100.0;
    a[3] = x + 100.0;

    b[0] = y;
    b[1] = y + 100.0;
    b[2] = y + 100.0;
    b[3] = y;

    glutPostRedisplay();
}

int main(int argc, char* argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("OpenGL Program");

    init();
    glutDisplayFunc(display);
    glutMotionFunc(drag);
    glutMouseFunc(mouse);

    glutMainLoop();

    return 0;

}

Share/Bookmark

No comments:

Post a Comment