18 June 2012

OpenGL untuk membuat tanda plus.



Berikut ini adalah program OpenGL untuk membuat tanda plus.

Praktikum grafika 8.


#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);
      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,7.0, 100.0);
      glMatrixMode (GL_MODELVIEW);
}

void drawScene() {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      glTranslatef(0.0f, 0.0f, -8.0f);
      GLfloat ambientColor[] = {1.0f, 0.0f, 0.0f, 1.0f};
      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
      GLfloat lightColor0[] = {2.0,0.5,3.0,1.0};
      GLfloat lightPos0[] = {3.0f, 10.0f, 2.0f, 4.0f};
      glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
      glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
      gluLookAt (3.0, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   
      glutSolidCube(1.0);
      glTranslatef(1.0, 1.0, 0.0);
   
      glutSolidCube(1.0);
      GLfloat lightColor1[] = {1.0,3.0,4.0,1.0};
      glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
      glTranslatef(-2.0, 0.0, 0.0);
    
      glutSolidCube(1.0);
      GLfloat lightColor2[] = {2.0,3.0,0.0,4.0};
      glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor1);
      glTranslatef(1.0, 0.0, 0.0);
   
      glutSolidCube(1.0);
      glTranslatef(0.0, 1.0, 0.0);
   
      glutSolidCube(1.0);


      glFlush ();
      glEnd();
      glutSwapBuffers();
}

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);           
      glutMainLoop();
      return 0;

}


0 komentar:

Post a Comment