GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Sunday, February 27, 2011

OpenGL - Quadratic Equation

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

/**
 * Quadratic equation:
 * y = x * x / 50 + 5
 */

void setup(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(-01.0, 10.0, -01.0, 10.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 1.1);
    glPointSize(2.0);

    float x, y;
    float limit = 300;   // big limit
    // y = x * x / 50 + 2
    glBegin(GL_POINTS);
        for(x=0.0; x< limit; x++){
            y = x*x/50 + 2.0;
            glVertex2f(x/25, y/25);  // more, more smooth
        }
    glEnd();

    // create coordinates
    glBegin(GL_LINES);
        glVertex2f(-10.0, 0.0);
        glVertex2f(10.0, 0.0);
        glVertex2f(0.0, 10.0);
        glVertex2f(0.0, -10.0);
    glEnd();

    glFlush();
}

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

    setup();
    glutMainLoop();

    return 0;

}


Share/Bookmark

No comments:

Post a Comment