Wednesday, July 6, 2011

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....

No comments:

Post a Comment

Question 1: Introduction to Variable

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