GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Thursday, March 10, 2011

OpenGL - Centering The Drag Point

#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 drag(int x, int y){
    y = 300.0 - y;
  
  
        a[0] = x - 50.0;
        a[1] = x - 50.0;
        a[2] = x + 50.0;
        a[3] = x + 50.0;
  
  
        b[0] = y - 50.0;
        b[1] = y + 50.0;
        b[2] = y + 50.0;
        b[3] = y - 50.0;
  
    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);
   
    glutMainLoop();

    return 0;

}

Share/Bookmark

No comments:

Post a Comment