GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Monday, February 28, 2011

OpenGL - Creating Strips

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

#include <iostream>
using namespace std;

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

// 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(){
    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();
    }
}

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(){
    glBegin(GL_LINES);
        // horizontal lines
        glVertex2f(-LIMIT, 0.0);
        glVertex2f(LIMIT, 0.0);

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    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

OpenGL - Histogram Function

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

#include <iostream>
using namespace std;

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


// 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*);

void createHistogram(int len, float *data){
    float x, y = 1.0, width = 2.0;
    int i;
    for(i=0; i<len; i++){
        x = (i * width) + i + 1;
        createBox(x, y, width, data[i]);
    }
}



Share/Bookmark

OpenGL - Histogram With Iteration

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

#include <iostream>
using namespace std;

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


// 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 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);

    draw();
    createCoordinate();

    glFlush();
}


void draw(){

    float value[] = {10.0, 15.0, 13.0, 9.0, 7.0, 10.};
    float x, y = 1.0, width = 2.0;
    int i;
    for(i=0; i<=5; i++){
        x = (i * width) + i + 1;
        createBox(x, y, width, value[i]);       
    }

}

void createHistogram(float data[][2]){

}

void createBox(float x, float y, float width, float height){
    glBegin(GL_POLYGON);
        glVertex2f(x, y);
        glVertex2f(x, y+height);
        glVertex2f(x+width, y+height);
        glVertex2f(x+width, y);
    glEnd();
}

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

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    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

OpenGL - Simple Drawing Box

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

#include <iostream>
using namespace std;

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


// prototype
void setup();   // initialization of the program
void display(); // drawing method
void createCoordinate();  // certasian coordinate
void draw();    // draw the object

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);

    draw();
    createCoordinate();

    glFlush();
}


void draw(){
    // drawing a box
    float x1 = 1, x2=2, y1=1, y2=2;
    glBegin(GL_POLYGON);
        glVertex2f(x1, y1);
        glVertex2f(x1, y2);
        glVertex2f(x2, y2);
        glVertex2f(x2, y1);
    glEnd();

}

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

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    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

OpenGL - Many Square Box

/**
 * This program renders 2 dimensional boxes that iterate 
 * over x and y
 */

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

#include <iostream>
using namespace std;

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


// prototype
void setup();   // initialization of the program
void display(); // drawing method
void createCoordinate();  // certasian coordinate
void draw();    // draw the object

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);

    draw();
    createCoordinate();

    glFlush();
}


void draw(){

    int i = 1, k = 1;

    // vertical multiplier
    for(k=1;k<=12;k++){
        float y = k * 2;
        // horizontal multiplier
        for(i=1; i<= 15; i++){
            float x = i * 2;
            float width = 1, height = 1;
            glBegin(GL_POLYGON);
                glVertex2f(x-1, y);         // bottom left
                glVertex2f(x-1, y+height);  // top left
                glVertex2f(x, y+height);    // top right
                glVertex2f(x, y);           // bottom right
            glEnd();
        }
    }

}

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

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    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

OpenGL - Iterate Square Box

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

#include <iostream>
using namespace std;

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


// prototype
void setup();   // initialization of the program
void display(); // drawing method
void createCoordinate();  // certasian coordinate
void draw();    // draw the object

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);

    draw();
    createCoordinate();

    glFlush();
}


void draw(){

    int i = 1;
   
    for(i=1; i<= 15; i++){
        float x = i * 2, y = 2;
        float width = 1, height = 1;
        glBegin(GL_POLYGON);
            glVertex2f(x-1, y);         // bottom left
            glVertex2f(x-1, y+height);  // top left
            glVertex2f(x, y+height);    // top right
            glVertex2f(x, y);           // bottom right
        glEnd();
    }

}

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

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    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

OpenGL - Drawing Square Box

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

#include <iostream>
using namespace std;

#define LIMIT 10.0      // limit

// drawing metric
#define X  2.0
#define Y  2.0
#define WIDTH  2.0
#define HEIGHT  2.0

// prototype
void setup();   // initialization of the program
void display(); // drawing method
void createCoordinate();  // certasian coordinate
void draw();    // draw the object

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

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

    draw();
    createCoordinate();

    glFlush();
}

// drawing box square
void draw(){

   
    glBegin(GL_POLYGON);
        glVertex2f(X, Y);
        glVertex2f(X, Y+HEIGHT);
        glVertex2f(X+WIDTH, Y+HEIGHT);
        glVertex2f(X+WIDTH, Y);
    glEnd();
   

}

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

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    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

OpenGL - Drawing Triangle

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

#include <iostream>
using namespace std;

#define LIMIT 10.0      // limit

// prototype
void setup();   // initialization of the program
void display(); // drawing method
void createCoordinate();  // certasian coordinate
void draw();    // draw the object

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

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

    draw();
    createCoordinate();

    glFlush();
}

void draw(){

    // vector defining
    GLfloat one[] = {0.5, 0.5};
    GLfloat two[] = {0.5, 5.0};
    GLfloat three[] = {5.0, 5.0};

    glBegin(GL_POLYGON);
        glVertex2fv(one);
        glVertex2fv(two);
        glVertex2fv(three);
    glEnd();

}

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

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    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

Callback Function With Arguments

/**
 * Callback function is a function that passed as argument 
 * parameter. It's called as argument parameter.
 */

#include <iostream>

using namespace std;

void hello(int a){
    cout << a << endl;
}

// callback function
// function as argument parameter
void greeting(void (*func) (int)){
    int b = 45;   // initialization
    func(b);
}

int main(int argc, char *argv[]){
    // callback function
    greeting(hello);

    return 0;
}

Share/Bookmark

Callback Function

/**
 * Callback function is a function that passed as argument 
 * parameter. It's called as argument parameter.
 */

#include <iostream>

using namespace std;

void hello(){
    cout << "Hello For You All Here" << endl;
}

// callback function
// function as argument parameter
void greeting(void (*func) (void)){
    func();
}

int main(int argc, char *argv[]){
    // callback function
    greeting(hello);

    return 0;
}

Share/Bookmark

Sunday, February 27, 2011

OpenGL - Vector Data

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



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

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

    // vector defining
    GLfloat one[] = {0.5, 0.5};
    GLfloat two[] = {0.5, 5.0};
    GLfloat three[] = {5.0, 5.0};

    glBegin(GL_POLYGON);
        glVertex2fv(one);
        glVertex2fv(two);
        glVertex2fv(three);
    glEnd();


    glBegin(GL_LINES);
        // horizontal lines
        glVertex2f(-10.0, 0.0);
        glVertex2f(10.0, 0.0);

        // vertical lines
        glVertex2f(0.0, -10.00);
        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

OpenGL - Polygon

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



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

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

    glBegin(GL_POLYGON);
        glVertex2f(0.5, 0.5);
        glVertex2f(0.5, 5.0);
        glVertex2f(5.0, 5.0);
    glEnd();

    glBegin(GL_LINES);
        // horizontal lines
        glVertex2f(-10.0, 0.0);
        glVertex2f(10.0, 0.0);

        // vertical lines
        glVertex2f(0.0, -10.00);
        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

OpenGL - Boxes

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

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

void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    // boxes
    glColor3f(1.0, 0.0, 1.1);
    glRectf(1.0, 1.0, 5.0, 5.0);    // x1, y1, x2, y2

    glColor3f(0.0, 1.0, 0.0);
    glRectf(6.0, 1.0, 8.0, 5.0);

    glColor3f(0.0, 0.0, 1.0);
    glRectf(1.0, 6.0, 5.0, 8.0);

    glColor3f(0.0, 1.0, 1.0);
    glRectf(6.0, 6.0, 8.0, 8.0);

    // box lines
    glColor3f(0.4, 0.4, 1.0);
    glBegin(GL_LINES);
        // left
        glVertex2f(0.5, 0.5);    // x, y
        glVertex2f(0.5, 8.5);

        // top
        glVertex2f(0.5, 8.5);
        glVertex2f(8.5, 8.5);

        // right
        glVertex2f(8.5, 8.5);
        glVertex2f(8.5, 0.5);

        // bottom
        glVertex2f(8.5, 0.5);
        glVertex2f(0.5, 0.5);
    glEnd();

    // coordinate system
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
        // horizontal
        glVertex2f(-10.0, 0.0);
        glVertex2f(10.0, 0.0);

        // vertical
        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

OpenGl - Rectangle

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

/**
 * glRectf( x1, y1, x2, y2 )
 */

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

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 1.1);
    glPointSize(2.0);
    // x1, x2, y1, y2
    glRectf(1.0, 1.0, 5.0, 5.0);
 
    glBegin(GL_LINES);
        // glVertex2f( x, y )
        // horizontal line
        glVertex2f(-10.0, 0.0);
        glVertex2f(10.0, 0.0);

        //vertical line
        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

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

OpenGL - Linear Equation

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

/**
 * The equation is
 * y = x + 2
 */

void setup(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(-10.0, 10.0, -10.0, 10.0);  // -x, x, -y, y
}

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

    // y = x + 2
    float x, y;
    glBegin(GL_POINTS);
        for(x=0.0; x< 35.0; x++){
            y = x + 2.0;
            glVertex2f(x/5, y/5);
        }
    glEnd();

    // create coordinate line
    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

OpenGL - Lines

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

void setup(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
}
void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 1.1);
    glPointSize(5.0);

    // create lines
    glBegin(GL_LINES);
        glVertex2f(-10.0, 0.0);  // left - x negative
        glVertex2f(10.0, 0.0);   // right - x positive
        glVertex2f(0.0, 10.0);   // top - y positive
        glVertex2f(0.0, -10.0);  // bottom - y negative
    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

OpenGL - Points

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

void setup(){
    // backgrouning the screen
    glClearColor(0.0, 0.0, 0.0, 1.0);
    // make the coord width and height 20 in scale
    gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
}

void display(){
    // clearing the buffer screen in memory
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0, 1.0, 0.1);
    glPointSize(5.0);        // make the point 5 in width

    glBegin(GL_POINTS);
        glVertex2f(1.0, 1.0);
        glVertex2f(2.0, 2.0);
        glVertex2f(3.0, 3.0);
        glVertex2f(4.0, 4.0);
        glVertex2f(5.0, 5.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

Saturday, February 26, 2011

OpenGL - Draw Rectangle

Here's a code on how to make backgrouning color:

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

void setup(){
   glClearColor(1.0f, 0.0f, 0.0f, 0.0f);    // backgrouning
}

void display(){

   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(0.0f, 0.0f, 0.0f);    // coloring rect
   glRectf(-0.75f, 0.75f, 0.75f, -0.75f);  // draw rect
   glFlush();
   glutSwapBuffers();
}

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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400, 300);    // horizontal, vertical
    glutInitWindowPosition(200, 100);    // horizontal, vertical
    glutCreateWindow("Hello World");    // title of the window

    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}



Share/Bookmark

OpenGL - Backgrouning

Here's a code on how to make backgrouning color:

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

void setup(){
   glClearColor(1.0f, 0.0f, 0.0f, 0.0f);    // backgrouning
}

void display(){

   glClear(GL_COLOR_BUFFER_BIT);
   glutWireTeapot(0.5);
   glFlush();
}

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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400, 300);    // horizontal, vertical
    glutInitWindowPosition(200, 100);    // horizontal, vertical
    glutCreateWindow("Hello World");    // title of the window

    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

You can get learning from this link: Wikibooks or cs.uccs
Share/Bookmark

Starting OpenGL

Here's a code on how to start your hello world in openGL:

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

void display(){

   glClear(GL_COLOR_BUFFER_BIT);
   glutWireTeapot(0.5);
   glFlush();
}

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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400, 300);    // horizontal, vertical
    glutInitWindowPosition(200, 100);    // horizontal, vertical
    glutCreateWindow("Hello World");    // title of the window

    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

You can get learning from this link: Wikibooks or cs.uccs
Share/Bookmark

Elegan Box Style

<div id="zero">
    <div id="satu">
        <p>Holla for the one of the other</p>
    </div>
</div>

<style>
#zero { width: 500px; padding: 20 20 60px; border: 1px solid #ccc;
        -moz-box-shadow: 0 0 4px #ccc; -moz-border-radius: 4px;}
#satu { border: 1px solid #ccc; -moz-box-shadow: 0 0 4px #ccc; 
        -moz-border-radius: 4px;}
#satu p { margin: 1px; padding: 15px; -moz-border-radius: 4px;
        background: #eee; border: 1px solid #eee; font: 12px sans-serif;
        color: #444; }
</style>

Share/Bookmark

Friday, February 25, 2011

Handling Zip File Using Python

To start import the needed module
>>> import zipfile
Then get your file handle
>>> file = zipfile.ZipFile("d:/luna/me.zip", "r")
>>> for name in file.namelist():
            print name


If you want more informations, please do like this one
>>> for info in infolist():
           print info.filename, info.date_time, info.file_size

Share/Bookmark

MD5 In Python

To use md5 service, import it firstly
>>> import md5
Then make new instance
>>> m = md5.new()
Then supply the resources
>>> m.update("From Barack Obama")
>>> m.update("To Barack Obama")
Then process it
>>> m.digest()

You want to more condese? Do like this one
>>> md5.new("From Barack Obama To Barack Obama").digest()

It's the same
>>> m.update("Me")
>>> m.update("You")
with
>>> m.update("MeYou")
Share/Bookmark

Ajax Auth For Login

This is a code on how to use ajax as handler for login authentication like google login page. There are you see that when you done wrong with your username or password, Google won't reload the page till your username and password are correct.

HTML file:
<form action="some.php" method="post" onsubmit="return isAuth()">
<input type="text" name="username" id="username"/>
<input type="password" name="password" id="password"/>
<p id="msg"></p>
<input type="submit" value="Log In"/>


Javascript file:
<script>
function isAuth(){
   var username = document.getElementById('username').value;
   var password = document.getElementById('password').value;
   var msg = document.getElementById('msg');

   var ajax = new XMLHttpRequest();
   ajax.open("POST", "auth/"+username+"/"+password, false);
   ajax.send();
   var response = eval(ajax.responseText);

   if(response == 1){
      return true;
   }else if(response == 0){
      msg.innerHTML = 'Access Denied."
                 +"Please recheck your username and password";
      return false;
    }  
}
</script>

PHP file:
<?php
/**
 * check if the username and password match over what 
 * if YES
 * echo 1
 * if NO
 * echo 0
 */


Share/Bookmark

Thursday, February 24, 2011

Drupal - Database Handling

SELECT * FROM data
drupal>>db_select('data','d')
         ->fields('d')
         ->execute()
         ->fetchAll()

SELECT * FROM data WHERE name = 'Lady Gaga'
drupal>> db_select('data','d')
         ->fields('d')
         ->condition('name','Lady Gaga')
         ->execute()
         ->fetchAll()

SELECT * FROM data WHERE age >= 30
drupal>> $query = db_select('data','d')->fields('d')->condition('age',30,'>=')->execute()->fetchAll()

SELECT * FROM data WHERE age >=30 OR name = 'Lady Gaga'
drupal>> db_select('data','d')
         ->fields('d')
         ->condition(db_or()
                     ->condition('age',30,'>=')
                     ->condition('name','Lady Gaga'))
         ->execute()
         ->fetchAll();

SELECT * FROM data WHERE age>=30 AND id>5
drupal>> db_select('data','d')->fields('d')->condition(db_and()->
          
Share/Bookmark

Drupal - Show Result Of Tables

<?php

function ipod_menu(){
    $items = array();
   
    $items['ipod'] = array(
        'title' => 'iPod',
        'page callback' => 'show_data',
        'access callback' => TRUE,
    );
   
    return $items;
}

function show_data(){
    $query = db_select('data','d')->fields('d')->execute()->fetchAll();
    $output = '<table><th>No</th><th>Name</th><th>Age</th><th>City</th></tr>';
    $i = 1;
    foreach($query as $key){
        $output .= '<tr>';
        $output .= '<td>'.$i++.'</td>';
        $output .= '<td>'.$key->name.'</td>';
        $output .= '<td>'.$key->age.'</td>';
        $output .= '<td>'.$key->city.'</td>';
        $output .= '</tr>';
    }
    $output .= '</table>';
   
    return $output;
}

Share/Bookmark

SQL - Complicated Queries

The table structure is like this below:
1. teacher table:
   a. id
   b. name
2. subject table:
  a. id
  b. major
3. handler table: who teacher handle the major
  a. id
  b. tid   // teacher id
  c. sid   // subject id

The problem is how to get a display of the third table with naming by using just one select statement. The answer is below:
SELECT t.name, s.major FROM teacher t, subject s, handler
WHERE handler.tid = t.id AND handler.sid = s.id
Share/Bookmark

Wednesday, February 23, 2011

Drupal - Theming The Table

To render a table in Drupal, use
function get_table(){
    $header = array(t('Name'),t('City'),t('Country'));
    $rows = array(
        array(t('Bill Gates'), t('Redmond'), t('Americas')),
        array(t('Luna Maya'), t('Denpasar'), t('Indonesia')),
        array(t('Aura Kasih'), t('Jakarta'), t('Indonesia')),
    );
    $output = theme('table',array('header' => $header,
                                  'rows'   => $rows));
    return $output;
}
Share/Bookmark

Tuesday, February 22, 2011

Drupal - Theming The List

Below is the code on how to use theme for list

<?php

/**
 * Implements hook_menu()
 */

function luna_menu(){

    $items = array();
   
    $items['luna/main'] = array(
        'title' => 'Main Menu',
        'page callback' => 'intro',
        'access callback' => TRUE,
        'expanded' => TRUE,
    );
   
    $items['luna/main/list'] = array(
        'title' => 'List',
        'page callback' => 'get_list',
        'access callback' => TRUE,
    );
   
    return $items;

}

function intro(){
    return t('This menu demos to you on how to handle with the theme hook menu');
}

function get_list(){

    $list = array();

    $list[] = t('Passing The University');
    $list[] = t('Getting The Job');
    $list[] = t('Moving To Americas');
    $list[] = t('Building The Business');
   
    $render_array['get_list'] = array(
        '#theme' => 'item_list',       // calling item list
        '#title' => t('Title Demos'),
        '#items' => $list,
        '#type' => 'ol',               // or "ul"
    );
   
    return $render_array;
}


Share/Bookmark

Drupal Page Callback With Arguments

If you want to get the value of  the arguments, please look like this code

 <?php

/**
 * Implements hook_menu()
 */
 
function luna_menu(){
    $items = array();
   
    $items['luna/main'] = array(
        'title' => 'Main Menu',
        'page callback' => 'description',
        'access callback' => TRUE,
    );
   
    $items['luna/po/%'] = array(
        'title' => 'Hello For',
        'page callback' => 'four',
        'page arguments' => array(2),
        'type' => MENU_CALLBACK,
        'access arguments' => array('access arguments page'),
    );
   
    return $items;

}

function description(){
    return array('#markup' => t('The link for this one: '
         .'<a href="@link">About Us</a>',
          array('@link'=>url('luna/po/5'),
          array('absolute'=>TRUE))       ));
}

function four($first){
    return t('You get the info with this number: @number', 
           array('@number'=>$first+10));
}
Share/Bookmark