Kamis, 28 Mei 2015

prak 9 .. grafika komputer

#include <windows.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <stdarg.h> 
#include <glut.h>
//#include <glu.h>



void initRendering() {
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);
    glEnable(GL_NORMALIZE);
    glShadeModel(GL_SMOOTH);
}

void handleResize(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

float _angle = 60.0f;
void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -8.0f);

    GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

    GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f};
    GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f};
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//radius
    GLfloat lightColor1[] = {1.0f, 1.0f, 1.0f, 1.0f};

    GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
    glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
    glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);

        glRotatef(_angle, 1.0f, 0.0f, 0.0f); //putar

        glColor3f(1.0f, 1.0f, 0.0f); //warna
        glBegin(GL_QUADS);
   
        //depan
        glNormal3f(1.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, -2.0f, 1.0f);
        glVertex3f(-1.0f, -2.0f, 1.0f);
        glVertex3f(1.5f, 1.0f, 1.5f);
        glVertex3f(-1.5f, 1.0f, 1.5f);

        //kanan
        glColor3f(0.0f, 0.0f, 1.0f); //warna biru
        glNormal3f(0.0f, 0.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(1.5f, 1.0f, -1.5f);
        glVertex3f(1.5f, 1.0f, 1.5f);
        glVertex3f(1.0f, -1.0f, 1.0f);

        //belakang
        glColor3f(0.0f, 1.0f, 0.0f); //warna hijau
        glNormal3f(0.0f, 0.0f, 1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(-1.5f, 1.0f, -1.5f);
        glVertex3f(1.5f, 1.0f, -1.5f);
        glVertex3f(1.0f, -1.0f, -1.0f);

        //kiri
        glColor3f(1.0f, 0.0f, 0.0f); //warna merah
        glNormal3f(0.0f, 0.0f, 1.0f);
        glVertex3f(-1.5f, -1.0f, -1.5f);
        glVertex3f(-1.5f, -1.0f, 1.5f);
        glVertex3f(-1.5f, 1.0f, 1.5f);
        glVertex3f(-1.5f, 1.0f, -1.5f);

        //bawah
        glColor3f(1.0f, 1.0f, 1.0f); //warna
        glNormal3f(0.0f, 0.0f, 1.0f);
        glVertex3f(-1.5f, 1.0f, 1.5f);
        glVertex3f(1.5f, 1.0f, 1.5f);
        glVertex3f(1.5f, 1.0f, -1.5f);
        glVertex3f(-1.5f, 1.0f, -1.5f);


        glEnd();
    glutSwapBuffers();
}

void update(int value) {
    _angle += 1.5f;
    if (_angle > 360) {
        _angle -= 360;
    }
   
    glutPostRedisplay();
    glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Lighting - videotutorialsrock.com");
    initRendering();
    glutDisplayFunc(drawScene);
    glutReshapeFunc(handleResize);   
    glutTimerFunc(25, update, 0);
   
    glutMainLoop();
    return 0;
}

0 komentar:

Posting Komentar

Posting Lama Beranda

 

Followers

 

Blogroll

Templates by ridha ammi | CSS3 by David Walsh | Powered by {N}Code & Blogger