Thursday, July 7, 2011

Mapping Texture (Raw image) into Object

//fstream declaration
//glut header declaration

GLuint texture; //as global variable

//function only works for RAW format image
GLuint loadtex(const char *filename, int w, int h)  //unasigned integer
{
    GLuint tex;
    FILE *file;
    unsigned char *data;
   
    file=fopen(filename,"rb");
    if(file==NULL) return 0;
   
    //preparation
    data=(unsigned char *)malloc(w*h*3);
    fread(data,w*h*3,1,file);
    fclose(file);

    //generate texture name
    glGenTextures(1,&tex);
    glBindTexture(GL_TEXTURE_2D,tex);

    //texture environment
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

    //set texture environment parameters
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

    //connect data to GL_TEXTURE_2D
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,w,h,0,GL_RGB,GL_UNSIGNED_BYTE,data);
    free(data);

    return tex;
}

void renderscene()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(0,0,-3,0,0,0,0,1,0);

    //enable
    glEnable(GL_TEXTURE_2D); //activate the texture
    glBindTexture(GL_TEXTURE_2D,texture);  //bind the texture   

    //do something
    glBegin(GL_QUADS);
        glTexCoord2f(0,0);
        glVertex2f(0,0);
        glTexCoord2f(1,0);
        glVertex2f(1,0);
        glTexCoord2f(1,1);
        glVertex2f(1,1);
        glTexCoord2f(0,1);
        glVertex2f(0,1);
    glEnd();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,(GLfloat)w/(GLfloat)h,1.0,100.0);
    glMatrixMode(GL_MODELVIEW);   
    glLoadIdentity();
}

void keypress(unsigned char key, int x, int y)
{
    if(key==27) exit(0);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(200,200);
    glutInitWindowSize(500,500);
    glutCreateWindow("Texture Example");
   
    glutDisplayFunc(renderscene);
    glutReshapeFunc(reshape);   
    glutKeyboardFunc(keypress);   

    texture=loadtex("texture.raw",256,256);
    glutMainLoop();
    glDeleteTextures(1,&texture); //reclean the memory and under glutMainLoop()
    return(0);
}

Wednesday, July 6, 2011

Create Text using Function

Just replace the ASCII code character codes with below function =) Simple and more effective!

void placetext(float x, float y, float z, void *font, char *string)
{
    char *c;
    glRasterPos3f(x,y,z);
    for(c=string; *c!='\0'; c++)
    {
        glutBitmapCharacter(font,*c);
    }
}

and replace your codes at display( ) function with;

placetext(0,0,0,GLUT_BITMAP_HELVETICA_18,"Hai Fiza!!");

My first ASCII Text using OpenGL

//OpenGL include goes here

GLfloat year=0;
GLfloat day=0;

void reshape(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,1,0.5,400);
    glMatrixMode(GL_MODELVIEW);   
    glLoadIdentity();
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glLoadIdentity();

    glRasterPos2f(0,0);
    //glRasterPos3f(0,1,0);
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15,72);   
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15,65);   
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15,70);
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15,73);
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15,90);
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15,65);
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15,72);
   
    gluLookAt(0,0,-2,0,0,0,0,1,0);
    glutSwapBuffers();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Text Example");
   
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);   

    glutMainLoop();
    return(0);
}

Create List and Spot Light

//OpenGL header file include goes here........................

GLuint list=1;

void reshape(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(60,1,0.5,400);

    glMatrixMode(GL_MODELVIEW);   
    glLoadIdentity();
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    gluLookAt(0,3,-20,0,0,0,0,1,0);

     //prepare lighting
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_COLOR_MATERIAL);
        GLfloat  lightPos[] = {0.0f, 50.0f, 0.0f, 0.0f };
        glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
        glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,lightPos);
   
    // Draw lantai
    glColor3f(0, 1, 0);
    glBegin(GL_QUADS);
    glVertex3f(-6, -0.8, -10);
    glVertex3f(-10, -0.8,  10);
    glVertex3f( 10, -0.8,  10);
    glVertex3f( 6, -0.8, -10);
    glEnd();

    glNewList(list,GL_COMPILE);   
        // Draw Head
        glColor3f(1,1,0);
        glutSolidSphere(1,12,12);
        glTranslatef(0,1,0);
        glColor3f(1,0,0);
        glutSolidCone(0.3,3,8,5);       
        //the eyes
        glPushMatrix();
        glColor3f(0,0,0);
        glTranslatef(0.5, -0.8, 1);
        glutSolidSphere(0.1,10,10);
        glTranslatef(-1, 0, 0);
        glutSolidSphere(0.1,10,10);
        glPopMatrix();
    glEndList();

    //duplicate call of list
    for(int x=-4; x<=4; x+=4)
    for(int z=-5; z<=10; z+=5)
    {
        glPushMatrix();
        glTranslatef(x,0,z);
        glCallList(list);
        glPopMatrix();
    }
    glLoadIdentity();
    glutSwapBuffers();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(500,500);
    glutCreateWindow("List Example");
   
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();
    return(0);
}

Then the result... alamak ayam sy terbalik....

Turn On and Off the Light

//code for include glut header  goes here

void createcube(float,float,float); //function declaration

void display()
{
    //clearing
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //set type of display and depth color
    glEnable(GL_DEPTH_TEST);

    gluLookAt(-3,2,-10,0,0,0,0,2,0);

    createcube(-2,0,0);
    createcube(2,0,0);
    createcube(2,0,0);
    createcube(0,0,2);
    createcube(-2,0,0);
    createcube(-2,0,0);
       
    glutSwapBuffers();
    glLoadIdentity();
}

void createcube(float x, float y, float z)
{
    glTranslatef(x,y,z);
    glutSolidCube(1);   
}

void reshape(int w, int h)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0,0,w,h);
    gluPerspective(45,1,0.1,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void keypress(unsigned char key, int x, int y)
{
    //int mod;

    //mod=glutGetModifiers();
    if (key == 27) {exit(0); }
    if (key == 'a')
    {
        //prepare lighting
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);

        //light position
        GLfloat lightpos[]={-2,3,-3,1};
        glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

        //light color
        GLfloat lightcolor[]={1,1,0};
        glLightfv(GL_LIGHT0, GL_DIFFUSE, lightcolor);
       
        //light ambient
        GLfloat ambcolor[]={1.5,0.8,0.4};
        glLightfv(GL_LIGHT0, GL_AMBIENT, ambcolor);
    }
    glutPostRedisplay();
    else if (key == 's')
    {
        glDisable(GL_LIGHT0);       
    }
    glutPostRedisplay();
    else if (key == 'd')
    {
        //change color   
        //prepare lighting
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);

        //light position
        GLfloat lightpos[]={-2,3,-3,1};
        glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

        //light color
        GLfloat lightcolor[]={0,0,1};
        glLightfv(GL_LIGHT0, GL_DIFFUSE, lightcolor);
       
        //light ambient
        GLfloat ambcolor[]={0,0.8,0.4};
        glLightfv(GL_LIGHT0, GL_AMBIENT, ambcolor);
    }
    glutPostRedisplay();
   
}


int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutInitWindowPosition(100,100);
    glutInitWindowSize(500,500);
    glutCreateWindow("Exercise 1 Lighting");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keypress);
   
    glutMainLoop();
    return 0;
}

Tuesday, July 5, 2011

Creating Movement Using Keyboard Function

// glut.h include codes goes here

float angle=0.0, ratio;
float eyeY=1.5f;
float r=1,g=1,b=1;
bool movetoggle=false, buttondown=false;
int w, h;

//define function declaration
void display();
void viewobject();
void reshape(int,int);
void keypress(unsigned char,int,int);

void display()
{
    glClearColor(0,0,0,1); //set output to alpha color
    glClear(GL_COLOR_BUFFER_BIT); //set type of display color
    viewobject(); // call object gluLookAt thru viewobject() function
    glTranslatef(0,0,-2);
    glColor3f(r,g,b);   
    glutWireCube(2);

    glutSwapBuffers();
    glLoadIdentity();

}

void viewobject()
{
    gluLookAt(0,eyeY,5,0,0,0,0,1,0);
}

void reshape(int w1, int h1)
{
    if(h1 == 0) h1=1;

    w=w1; h=h1;
    ratio=1.0f * w/h ;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0,0,w,h);
    gluPerspective(45,ratio,0.1,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void keypress(unsigned char key, int x, int y)
{
    int mod;

    mod=glutGetModifiers();
    if (key == 27) {exit(0); }
    if (key == 13)
    {
        if((mod==GLUT_ACTIVE_CTRL) && !movetoggle)
            movetoggle=true;
        else
            movetoggle=false;
    }
    glutPostRedisplay();
}

void keyspecial(int key, int x, int y)
{
    if(key==GLUT_KEY_UP)
    {
        if(movetoggle)
        {
            eyeY+=0.1;
        }
    }
    if(key==GLUT_KEY_DOWN)
    {
        if(movetoggle)
        {
            eyeY-=0.1;
        }
    }
    if(key==GLUT_KEY_F1)
    {
        r=1.0f,g=0.0f,b=0.0f;
    }
    if(key==GLUT_KEY_F2)
    {
        r=01.0f,g=1.0f,b=0.0f;
    }
    if(key==GLUT_KEY_F3)
    {
        r=0.0f,g=0.0f,b=1.0f;
    }
    glutPostRedisplay();
    /* switch(key){
    case GLUT_KEY_F1 : red = 1.0; green = 0.0; blue = 0.0; break;
    case GLUT_KEY_F2 : red = 0.0; green = 1.0; blue = 0.0; break;
    case GLUT_KEY_F3 : red = 0.0; green = 0.0; blue = 1.0; break;
    }*/
}

void mouseclick(int button, int state, int x, int y)
{
    if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN && movetoggle)
        buttondown=true;
    else
        buttondown=false;
}

void mousemove(int x, int y)
{
    int mid=h/2;
    if(buttondown)
    {
        if(y>mid){
            eyeY-=0.1f;}
        else{
            eyeY+=0.1f;
        }
    }
    glutPostRedisplay();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Exercise 2");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keypress);
    glutSpecialFunc(keyspecial);
    glutMouseFunc(mouseclick);
    glutMotionFunc(mousemove);

    glutMainLoop();
    return 0;
}

Question 1: Introduction to Variable

Based on code below, create a dynamic program that solve problem below: Source Code: #include <iostream> using na...