GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Showing posts with label OpenGL. Show all posts
Showing posts with label OpenGL. Show all posts

Monday, March 14, 2011

OpenGL - Vertex Array

Beside vertex arrays, there's also:
GL_COLOR_ARRAY, 
GL_INDEX_ARRAY, 
GL_NORMAL_ARRAY, 
GL_TEXTURE_COORD_ARRAY, and 
GL_EDGE_FLAG_ARRAY

The first is that you call this function for enable the array:
glEnableClientState(GL_VERTEX_ARRAY);

Then you set the data:
static GLint data[] = {
    1, 1,
    3, 3,
    5, 1
};

After finishing it, try
glVertexPointer(2, GLint, 0, data);

Then the last is:
glBegin(GL_TRIANGLES);
    glArrayElement(2);
glEnd();

See more on: http://glprogramming.com/red/chapter02.html
Share/Bookmark

OpenGL - Polygon

By default the polygon fill is solid. But you can change what the fill looks like by
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_LINE);
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
Share/Bookmark

OpenGL - Lines

The default line width is 1 pixel. You can set it as you want by
glLineWidth(2.5);

To make the line smoother, use
glEnable(GL_LINE_SMOOTH);
This command makes your line bolder.

To create a dashed line, try
glEnable(GL_LINE_STIPPLE);
Then make the line
glLineStipple(1, 0x33ff);
The above function is glLineStipple(factor, pattern)


See more on: http://glprogramming.com/red/
Share/Bookmark

Thursday, March 10, 2011

OpenGL - Centering The Particles

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

#include <iostream>

using namespace std;

void init();
void display();

#define N 1000

float xx[N];
float yy[N];

void setRand(){
    srand((unsigned) time(NULL));
    int i = 0;
    for(i=0; i< N; i++){
        xx[i] = rand() / 100.00;
        yy[i] = rand() / 100.00;
    }
}

void setPoints(){
    glBegin(GL_POINTS);
        glColor3f(1.0,0.0,0.0);
        int i = 0;
        for(i=0;i<N;i++){
            glVertex2f(xx[i], yy[i]);
        }
    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(2.0);

    setPoints();


    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 - y;
                int a = 0;
                for(a=0;a<N;a++){
                    if(xx[a] < x){
                        xx[a] += 5;
                    }
                    if(xx[a] > x){
                        xx[a] -= 5;
                    }
                    if(yy[a] < y){
                        yy[a] += 5;
                    }
                    if(yy[a] > y){
                        yy[a] -= 5;
                    }
                }

                glutPostRedisplay();
            }
        break;

    }
}

void drag(int x, int y){
    y = 300.0 - 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();
    setRand();

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



    glutMainLoop();

    return 0;

}

Share/Bookmark

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

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

OpenGL - Moving Triangle With Mouse

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

#include <iostream>

using namespace std;

void init();
void display();

float a[3] = {10.0, 50.0, 100.0};  // x
float b[3] = {10.0, 100.0, 10.0};  // y

void setTree(float *x, float *y){
    glBegin(GL_TRIANGLES);
        glColor3f(1.0, 0.0, 0.0);
        glVertex2f(x[0], y[0]);
        glColor3f(0.0, 1.0, 0.0);
        glVertex2f(x[1], y[1]);
        glColor3f(1.0, 1.0, 0.0);
        glVertex2f(x[2], y[2]);
    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 + 50.0;
                a[2] = x + 100.0;

                b[0] = y;
                b[1] = y + 100.0;
                b[2] = y;
                glutPostRedisplay();
            }
        break;

    }
}

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

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

    b[0] = y;
    b[1] = y + 100.0;
    b[2] = 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

OpenGL - Full Dragging Objects

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

#include <iostream>

using namespace std;

void init();
void display();

float a = 200.0, b= 200.0;

void setPoint(float x, float y){
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_POINTS);
        glVertex2f(x,y);
    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);

    setPoint(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;
                a = x/1.0;
                b = 300.0 - (y/1.0);

                glutPostRedisplay();
            }
        break;

    }
}

void drag(int x, int y){
    a = x/1.0;
    b = 300.0 - (y/1.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);
    glutMouseFunc(mouse);

    glutMainLoop();

    return 0;

}

Share/Bookmark

OpenGL - Dragging Object

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

#include <iostream>

using namespace std;

void init();
void display();

float a = 200.0, b= 200.0;

void setPoint(float x, float y){
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_POINTS);
        glVertex2f(x,y);
    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);

    setPoint(a,b);

    glFlush();
}

void drag(int x, int y){
    a = x/1.0;
    b = 300.0 - (y/1.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

OpenGL - Moving Object By Mouse

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

#include <iostream>

using namespace std;

void init();
void display();

float a = 200.0, b= 200.0;

void setPoint(float x, float y){
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_POINTS);
        glVertex2f(x,y);
    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);

    setPoint(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;
                a = x/1.0;
                b = 300.0 - (y/1.0);

                glutPostRedisplay();
            }
        break;

    }

}

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);
    glutMouseFunc(mouse);

    glutMainLoop();

    return 0;

}

Share/Bookmark

Tuesday, March 8, 2011

OpenGL - Timer Function

/**
 * This program show on how to use timer function in OpenGL
 * 
 * For more tutorial, see: 
 * http://60hz.csse.uwa.edu.au/workshop/workshop0/workshop2.html
 */


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

#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;

int angle = 1;

static void Timer(int value){
    angle += 0.1;
    glutPostRedisplay();
    // 100 milliseconds
    glutTimerFunc(100, Timer, 0);
}


void init(){
    glClearColor(1.0,1.0,1.0,1.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    glRotatef(angle, 0.0, 1.0, 0.0);
    glRotatef(angle, 0.0, 0.0, 01.0);

    glColor3f(0.9,0.9,0.1);
    glutSolidTeapot(0.5);

    glFlush();

}


int main(int argc, char *argv[]){


    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("Simulation App");

    init();
    glutDisplayFunc(display);

    Timer(0);

    glutMainLoop();



    return 0;
}

Share/Bookmark

OpenGL - Moving Closer Particles

/**
 * This program will do the particle generation. Then directs the
 * particles move as the mouse moves.
 * The hope of the program is that the particle will move to the
 * right direction if the particle position
 * lies in left side than the mouse cursor. And reciprocally.
 *
 * @author   : irfanudin ridho
 * @email    : irfan.ub@gmail.com
 * @version  : 3.0
 * @date     : March 7, 2011
 */


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

#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;


#define N 2000 // particle size
void grid();
void particle();
void init();

float aa[N];
float bb[N];
float ccc = 0.0;
float bbb = 0.0;

void mouse(int mouse, int state, int x, int y){

    float a = x/1000.00;
    float b = y/1000.00;
    bbb = a;
    ccc = b;
    switch(mouse){
        case GLUT_LEFT_BUTTON:
            if(state == GLUT_DOWN){

                glutPostRedisplay();
                cout << a << " " << b << endl;
            }
            break;
        case GLUT_RIGHT_BUTTON:
            if(state == GLUT_UP){

                particle();
                glutPostRedisplay();
            }
            break;

    }
}

void grid(){
    int i = 0, j = 0;
    glColor3f(0.89,0.99,0.79);
    glBegin(GL_LINES);
        //hoz line
        for(i=0; i<= 10; i++){
            glVertex2f(-0.1, i/10.0);
            glVertex2f(1.0, i/10.0);
        }
        //verical line
        for(j=0; j<=10; j++){
            glVertex2f(j/10.0, -0.1);
            glVertex2f(j/10.0, 1.0);
        }

        // coordinate line
        glColor3f(0.0,1.0,0.0);
        //hoz line
        glVertex2f(-0.1, 0.0);
        glVertex2f(1.0, 0.0);
        // vertical line
        glVertex2f(0.0, -0.1);
        glVertex2f(0.0, 1.0);


    glEnd();
}




void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(-0.1,1,-0.1,1.0);

    // creating position of the particles
    // starting position of the particle
        srand((unsigned) time(NULL));
        int i=0;
        for(i;i<N;i++){
            aa[i] = rand()*4.0/100000.00;
            bb[i] = rand()*3.0/100000.00;
        }

}

void particle(){

    glPointSize(2.0);
    glColor3f(0.0, 0.0, 1.0);

    float *x = aa;
    float *y = bb;

    glBegin(GL_POINTS);
        int i=0;
        for(i;i<N;i++){
            x[i] ;
            y[i] ;

            // if mouse position is greater than
            // particle position, move the particle right away.
            // hoz direction
            if(bbb<x[i]){
                x[i] -= 0.01;
            }else if(bbb>x[i]){
                x[i] += 0.01;
            }

            // vertical direction
            if(ccc<y[i]){
                y[i] -= 0.01;
            }else if(ccc>y[i]){
                y[i] += 0.01;
            }

            glVertex2f(x[i], y[i]);
        }

    glEnd();


}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    // particle rendering
    particle();
    grid();

    glFlush();

}


int main(int argc, char *argv[]){


    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("Simulation App");

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

    glutMainLoop();



    return 0;
}

Share/Bookmark

Friday, March 4, 2011

OpenGL - Strandardizing

/**
 * OpenGL tutorial:
 * http://www.swiftless.com/opengltuts.html
 */


#include <GL/glew.h> // Include the GLEW header file
#include <GL/glut.h> // Include the GLUT header file

void display (void) {
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red
glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations
glFlush(); // Flush the OpenGL buffers to the window
}

void reshape (int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}

int main (int argc, char **argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
glutInitWindowSize (500, 500); // Set the width and height of the window
glutInitWindowPosition (100, 100); // Set the position of the window
glutCreateWindow ("You’re first OpenGL Window"); // Set the title for the window
glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering
glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for rendering
glutMainLoop(); // Enter GLUT's main loop
}

Share/Bookmark

Thursday, March 3, 2011

OpenGL - Gradient Background


void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POLYGON);
        glColor3f(0.50, 1.0, 0.0);
        glVertex2f(-1.0,-1.0);

        glVertex2f(-1.0,10.0);

        glColor3f(01.01,0.50,1.0);
        glVertex2f(10.0,10.0);

        glVertex2f(10.0, -1.0);

    glEnd();

    glBegin(GL_LINES);
    glColor3f(0.0,0.0,1.0);
        glVertex2f(1.0,1.0);

        glVertex2f(7.0,7.0);
    glEnd();
   
    glFlush();
}

Share/Bookmark

OpenGL - Coloring

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


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

void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POLYGON);
        glColor3f(1.0, 0.0, 0.0);
        glVertex2f(1.0,1.0);

        glColor3f(1.0,1.0,0.0);
        glVertex2f(4.0,8.0);

        glColor3f(1.0,0.0,1.0);
        glVertex2f(8.0,1.0);

    glEnd();

    glFlush();
}


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

    glutDisplayFunc(display);
    init();

    glutMainLoop();

    return 0;
}

Share/Bookmark

Tuesday, March 1, 2011

OpenGL - 3D

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

void dispay(void);
void setup();

void draw(){
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
        glVertex3f(1.0, 1.0, 1.0);
        glVertex3f(1.0, 5.0, 1.0);
        glVertex3f(5.0, 5.0, 1.0);
        glVertex3f(5.0, 1.0, 1.0);

        glVertex3f(1.0, 1.0, 5.0);
        glVertex3f(1.0, 5.0, 5.0);
        glVertex3f(5.0, 5.0, 5.0);
        glVertex3f(5.0, 1.0, 5.0);
    glEnd();
}

void coord(){
    glColor3f(0.0,1.0,0.0);
    glBegin(GL_LINES);
        // hoz line
        glVertex3f(-1.0, 0.0, 0.0);
        glVertex3f(10.0, 0.0, 0.0);
        // vertical line
        glVertex3f(0.0, -1.0, 0.0);
        glVertex3f(0.0, 10.0, 0.0);
        // z lines
        glVertex3f(0.0, 0.0, -1.0);
        glVertex3f(0.0, 0.0, 10.0);
    glEnd();
}

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

}

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

    coord();
    draw();
    glFlush();
}


int main(int argc, char *argv[]){

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400, 300);
    glutInitWindowPosition(200, 100);
    glutCreateWindow("Hacking openGL");
    glutDisplayFunc(display);

    setup();
    glutMainLoop();

    return 0;
}

Share/Bookmark

OpenGL - Mouse Function

/**
 * This function is called by glutMouseFunc(mouse)
 * The parameter here is
 * button : type of the button
 * state: down and up
 * x: horizontal position, start from left
 * y: vertical position, start from top
 */

void mouse(int button, int state, int x, int y){
    // called twice
    cout << "State: " << state << endl;
    cout << "Position: x->" << x << " y->" << y << endl;
    switch(button){
        case GLUT_LEFT_BUTTON:
            cout << "Left Button" << endl;   // called twice
            if(state == GLUT_DOWN){
                cout << "Left Button Down" << endl;
            }
            if(state == GLUT_UP){
                cout << "Left Button Up" << endl;
            }
            break;
        case GLUT_MIDDLE_BUTTON:
            cout << "Middle Button" << endl;
            if(state == GLUT_DOWN){
                cout << "Middle Button Down" << endl;
            }
            if(state == GLUT_UP){
                cout << "Middle Button Up" << endl;
            }
            break;
        case GLUT_RIGHT_BUTTON:
            cout << "Right Button" << endl;
            if(state == GLUT_DOWN){
                cout << "Right Button Down" << endl;
            }else if(state == GLUT_UP){
                cout << "Right Button Up " << endl;
            }
    }
}

Share/Bookmark

OpenGL - Keyboard Function

/**
 * Call this function in the main entry point with
 * glutKeyboardFunc(keyboard);
 */

/**
 * x - mouse pointer position - zero to corner of window
 * y - mouse pointer position - zero to corner of window
 */
void keyboard(unsigned char key, int x, int y){
    switch(key){
        case 'a':
            cout << "You type a" << endl;
            break;
        case 'b':
            cout << "You type b" << endl;
            break;
    }
}

Share/Bookmark

OpenGL - 3D

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

void dispay(void);
void setup();

void draw(){
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
        glVertex3f(1.0, 1.0, 1.0);
        glVertex3f(1.0, 5.0, 1.0);
        glVertex3f(5.0, 5.0, 1.0);
        glVertex3f(5.0, 1.0, 1.0);

        glVertex3f(1.0, 1.0, 5.0);
        glVertex3f(1.0, 5.0, 5.0);
        glVertex3f(5.0, 5.0, 5.0);
        glVertex3f(5.0, 1.0, 5.0);
    glEnd();
}

void coord(){
    glColor3f(0.0,1.0,0.0);
    glBegin(GL_LINES);
        // hoz line
        glVertex3f(-1.0, 0.0, 0.0);
        glVertex3f(10.0, 0.0, 0.0);
        // vertical line
        glVertex3f(0.0, -1.0, 0.0);
        glVertex3f(0.0, 10.0, 0.0);
        // z lines
        glVertex3f(0.0, 0.0, -1.0);
        glVertex3f(0.0, 0.0, 10.0);
    glEnd();
}

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

}

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

    coord();
    draw();
    glFlush();
}


int main(int argc, char *argv[]){

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400, 300);
    glutInitWindowPosition(200, 100);
    glutCreateWindow("Hacking openGL");
    glutDisplayFunc(display);

    setup();
    glutMainLoop();

    return 0;
}

Share/Bookmark

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