GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Tuesday, March 1, 2011

OpenGL - Color Defining

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

#define LIMIT 20.0      // limit
#define CENTER (LIMIT - LIMIT - 1)

// color defining
#define RED 1.0, 0.0, 0.0
#define GREEN 0.0, 1.0, 0.0
#define BLUE 0.0, 0.0, 1.0
#define WHITE 1.0, 1.0, 1.0
#define BLACK 0.0, 0.0, 0.0
#define YELLOW 1.0, 1.0, 0.0
#define MAGENTA 1.0, 0.0, 1.0
#define AQUA 0.0, 1.0, 1.0

// prototype
void setup();   // initialization of the program
void display(); // drawing method
void createCoordinate();  // certasian coordinate
void draw();    // draw the object
void createBox(float, float, float, float);
void createHistogram(int, float*);


// draw strips
void draw(){

    glColor3f(GREEN);
    glBegin(GL_POLYGON);
        glVertex2f(1.0, 1.0);
        glVertex2f(1.0, 4.0);
        glVertex2f(5.0, 4.0);
        glVertex2f(5.0, 1.0);
    glEnd();

    glColor3f(BLUE);
    glBegin(GL_POLYGON);
        glVertex2f(6.0, 1.0);
        glVertex2f(6.0, 4.0);
        glVertex2f(10.0, 4.0);
        glVertex2f(10.0, 1.0);
    glEnd();

    glColor3f(RED);
    glBegin(GL_POLYGON);
        glVertex2f(11.0, 1.0);
        glVertex2f(11.0, 4.0);
        glVertex2f(15.0, 4.0);
        glVertex2f(15.0, 1.0);
    glEnd();
}

void setup(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(CENTER, LIMIT, CENTER, LIMIT); // -x1, x2, -y1, y2
}

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


    createCoordinate();
    draw();


    glFlush();
}

void createCoordinate(){
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
        // horizontal lines
        glVertex2f(-LIMIT, 0.0);
        glVertex2f(LIMIT, 0.0);

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    glEnd();

    // creating strips
    int i=0;
    for(i=0;i<20;i++){
    glColor3f(0.1, 0.0, 1.0);
    glBegin(GL_LINES);
        // horizontal strips
        glVertex2f(i, 0.10);
        glVertex2f(i, -0.10);

        // vertical strips
        glVertex2f(0.10, i);
        glVertex2f(-0.10, i);
    glEnd();
    }
}

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