PRAKTIKUM 1

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

void mydisplay(){
glClear (GL_COLOR_BUFFER_BIT);
glBegin (GL_POLYGON);
glVertex2f(0, 0);
glVertex2f(0.5, 0.5);
glVertex2f(0, 1);
glVertex2f(-0.5, 0.5);
glVertex2f(0, 0);

glVertex2f(0.5, -0.5);
glVertex2f(0, -1);
glVertex2f(-0.5, -0.5);
glVertex2f(0, 0);

glEnd();
glFlush();
}
int main (int argc, char** argv)
{
printf ("contoh sederhana kotak");
glutCreateWindow("Praktikum 1");
glutDisplayFunc (mydisplay);
glutMainLoop();}







PRAKTIKUM 2

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

#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES);
glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd();

using namespace std;

void init(void)
{
       glClearColor(0.0, 0.0, 0.0, 0.0);
       glShadeModel(GL_FLAT);
}

void display(void)
{
       int i;
       glClear (GL_COLOR_BUFFER_BIT);
       glColor3f (1.0, 1.0, 1.0);
       glEnable (GL_LINE_STIPPLE);
       glLineStipple (1, 0x0101);
       drawOneLine (50.0, 125.0, 150.0, 125.0);
       glLineStipple (1, 0x00FF);
       drawOneLine (150.0, 125.0, 250.0, 125.0);
       glLineStipple (1, 0x1C47);
       drawOneLine (250.0, 125.0, 350.0, 125.0);

       glLineWidth(5.0);
       glLineStipple(1, 0x0101);
       drawOneLine(50.0, 100.0, 150.0, 100.0);
       glLineStipple(1, 0x00FF);
       drawOneLine(150.0, 100.0, 250.0, 100.0);
       glLineStipple(1, 0x1C47);
       drawOneLine(250.0, 100.0, 350.0, 100.0);
       glLineWidth(1.0);
/*
       glLineWidth(1.0);
       glLineStipple (1, 0x0101);
       drawOneLine (48.0, 48.0, 50.0, 125.0);
       glLineWidth(1.0);
       glLineStipple (1, 0x0101);
       drawOneLine (248.0, 248.0, 50.0, 50.0);
*/
       glLineStipple(1, 0x0101);
       glBegin(GL_LINE_STRIP);
       for(i=0;i<7;i++)
       glVertex2f(50.0 + ((GLfloat) i * 50.0), 75.0);
       glEnd();

       for (i=0;i<6;i++) {
              drawOneLine(50.0 + ((GLfloat) i * 50.0),

50.0, 50.0 + ((GLfloat) (i+1) * 50.0), 50.0);
       }

       glLineStipple(5, 0x1C47);
       drawOneLine(50.0, 25.0, 350.0, 25.0);
       glDisable(GL_LINE_STIPPLE);
       glFlush();
}

void reshape (int w, int h)
{
       glViewport(0, 0, (GLsizei) w, (GLsizei) h);
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

int main(intargc, char** argv)
{
       glutInit(&argc,argv);
       glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
       glutInitWindowSize(400,150);
       glutInitWindowPosition(100,100);
       glutCreateWindow(argv[0]);
       init();
       glutDisplayFunc(display);
       glutReshapeFunc(reshape);
       glutMainLoop();
    system("PAUSE");
    return EXIT_SUCCESS;
}






PRAKTIKUM 3

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

void display(void)
{
GLubyte fly[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,
0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,
0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22,
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC,
0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08};
GLubyte halftone[] = {
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};

GLubyte S[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
0x00, 0x03, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
0xC0, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00,
0xC0, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,};

glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
/* draw one solid, unstippled rectangle, */
/* then two stippled rectangles */
glRectf (25.0, 25.0, 125.0, 125.0);
glEnable (GL_POLYGON_STIPPLE);
/*
glPolygonStipple (fly);
glRectf (125.0, 25.0, 225.0, 125.0);
glPolygonStipple (halftone);
glRectf (225.0, 25.0, 325.0, 125.0);
*/
glPolygonStipple (S);
glRectf (125.0, 25.0, 225.0, 125.0);

glDisable (GL_POLYGON_STIPPLE);
glFlush ();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (350, 150);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();

return 0;
}












PRAKTIKUM 4

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

void display (void)
{
       glClearColor(1.0,1.0,1.0,0.0);
       glMatrixMode (GL_PROJECTION);
       gluOrtho2D(0.0,300.0,0.0,300.0);
}
void setPixel (GLint xCoordinate, GLint yCoordinate)
{
       glBegin (GL_POINTS);
       glVertex2i (xCoordinate, yCoordinate);
       glEnd();
       glFlush();
}
void lineBres (GLint x0, GLint y0, GLint xEnd, GLint yEnd)
{
       GLint dx = (float) fabs ((float) xEnd - x0);
       GLint dy = (float) fabs ((float) yEnd - y0);
       GLint p = 2 * dy - dx;
       GLint twoDy = 2 * dy;
       GLint twoDyMinusDx = 2 * (dy-dx);
       GLint x,y;
       if (x0 > xEnd){
              x = xEnd;
              y = yEnd;
              xEnd = x;
       }else{
              x = x0;
              y = y0;
       }
       setPixel (x,y);
       while (x<xEnd){
              x++;
              if(p<0)
                     p += twoDy;
              else{
                     y++;
                     p+= twoDyMinusDx;
              }
              setPixel (x, y);
       }
}
void drawMyLine(void)
{
       glClear(GL_COLOR_BUFFER_BIT);
       glColor3f (1.0,0.0,0.0);
       glPointSize(4.0);
       GLint x0 = 50;
       GLint y0 = 50;
       GLint xEnd = 125;
       GLint yEnd = 100;
       lineBres (x0, y0, xEnd, yEnd);

       glColor3f (0.0,1.0,0.0);
       glPointSize(4.0);
       GLint x1 = 75;
       GLint y1 = 50;
       GLint x1End = 125;
       GLint y1End = 150;
       lineBres (x1, y1, x1End, y1End);

       glColor3f (0.0,0.0,1.0);
       glPointSize(4.0);
       GLint x2 = 50;
       GLint y2 = 50;
       GLint x2End = 75;
       GLint y2End = 50;
       lineBres (x2, y2, x2End, y2End);

       }
int main (int argc, char** argv)
{
       glutInit(&argc, argv);
       glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
       glutInitWindowSize (400,400);
       glutInitWindowPosition (0,0);
       glutCreateWindow ("Digital Differential Analyzer Algorithm");
       display();
       glutDisplayFunc (drawMyLine);
       glutMainLoop();
       return 0;
}





PRAKTIKUM 5

#include <iostream>
#include <windows.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdarg.h> 
#include <glut.h> 
#include<math.h>
using namespace std;
typedef unsigned char uchar;
// number of line segments
static int num_lines = 20;
static int num_liness = 3;
// callback prototypes
void disp(void);
void keyb(uchar k, int x, int y);
void reshape(int x, int y);

// main

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

  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  glutInitWindowSize(400,400);
  glutInitWindowPosition(100,100);
  glutCreateWindow("circle.cpp");
  glClearColor(0.0,0.0,0.0,0.0);
  glutDisplayFunc(disp);
  glutKeyboardFunc(keyb);
  glutReshapeFunc(reshape);
  glutMainLoop();
  return 0; }
// disp

void disp(void){
  double angle;
  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_LINE_LOOP);
  glColor3f(1.0,1.0,0.0);
    for(int i =0;i<num_lines;i++)
            {
            angle = i*2*3.14/num_lines;
        glVertex2f(cos(angle),sin(angle));
    }
  glEnd();

  //  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_LINE_LOOP);
  glColor3f(0.0,0.0,1.0);
    for(int i =0;i<3;i++)
            {
         angle = i*2*3.14/3;
       glVertex2f(cos(angle),sin(angle));
    }
  glEnd();

glBegin(GL_LINE_LOOP);
    for(int i =0;i<num_lines;i++){
            glColor3f(0.0,1.0,0.0);
            angle = i*2*3.14/num_lines;
        glVertex2f(cos(angle)/2,sin(angle)/2);
    }
  glEnd();
glBegin(GL_LINE_LOOP);
    for(int i =0;i<4;i++){
            glColor3f(1.0,0.0,0.0);
            angle = i*2*3.14/4;
        glVertex2f(cos(angle)/4,sin(angle)/4);
    }
  glEnd();

glBegin(GL_LINE_LOOP);
    for(int i =0;i<num_lines;i++){
            glColor3f(1.0,0.1,0.0);
            angle = i*2*3.14/num_lines;
        glVertex2f(cos(angle)/4,sin(angle)/4);
    }
  glEnd();
  glutSwapBuffers();
}

// keyb
void keyb(uchar k, int x, int y){
  switch (k){
  case 'q':
    exit(0);
    break;
  case '+':
    if(num_lines < 99){
      num_lines++;
      cout << "Circle consists of " << num_lines << " lines " << endl;
      glutPostRedisplay();
    }
    break;
  case '-':
    if(num_lines >3){
      num_lines--;
      cout << "Circle consists of " << num_lines << " lines " << endl;
      glutPostRedisplay();
    }
    break;   } }

// reshape
void reshape(int x,int y){
  if(x<y)
    glViewport(0,(y-x)/2,x,x);
  else
    glViewport((x-y)/2,0,y,y); }




PRAKTIKUM 6

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

float _angle = 45.0f;  //sudut saat melakukan rotasi
//Draws the 3D scene

void mydisplay()  //bentuk objek berupa kotak
{ 
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glMatrixMode(GL_MODELVIEW);//Switch to setting
       glLoadIdentity();//Reset the camera


       glPushMatrix();//Save the transformations
          //glTranslatef(0.0f, 0.0f, 0.0f);      //mentraslasikan kotak geser sesuai koor
          //glScalef(0.7f, 0.7f, 0.7f);          //merubah ukuran dari objek kotak
          glRotatef(_angle, 0.0f, 0.0f, 1.0f); //diputar sebesar angle
       //bentuk kotak
glBegin(GL_POLYGON); 
       glVertex3f(-0.1f, -0.1f,0.0f); 
       glVertex3f(-0.1f, 0.1f,0.0f); 
       glVertex3f(0.1f, 0.1f,0.0f); 
       glVertex3f(0.1f, -0.1f,0.0f); 
       glEnd();
       //glPopMatrix();//Undo the move to the center of

      

glPushMatrix();//Save the transformations
       glTranslatef(0.0f, 0.4f, 0.0f);      //mentraslasikan kotak geser sesuai koor
          glScalef(0.6f, 0.6f, 0.6f);          //merubah ukuran dari objek kotak
          glRotatef(_angle, 0.0f, 0.0f, -1.0f); //diputar sebesar angle
       //bentuk kotak
       glBegin(GL_POLYGON); 
       glVertex3f(-0.1f, -0.1f,0.0f); 
       glVertex3f(-0.1f, 0.1f,0.0f); 
       glVertex3f(0.1f, 0.1f,0.0f); 
       glVertex3f(0.1f, -0.1f,0.0f); 
       glEnd();
       //glPopMatrix();//Undo the move to the center of
        

       glTranslatef(0.0f, 0.6f, 0.0f);      //mentraslasikan kotak geser sesuai koor
          glScalef(0.5f, 0.5f, 0.5f);          //merubah ukuran dari objek kotak
          glRotatef(_angle, 0.0f, 0.0f, -1.0f); //diputar sebesar angle
          glPushMatrix();//Save the transformations
       //bentuk kotak
       glBegin(GL_POLYGON); 
       glVertex3f(-0.1f, -0.1f,0.0f); 
       glVertex3f(-0.1f, 0.1f,0.0f); 
       glVertex3f(0.1f, 0.1f,0.0f); 
       glVertex3f(0.1f, -0.1f,0.0f); 
       glEnd();



       glPopMatrix();//Undo the move to the center of
       glutSwapBuffers();//Send the 3D scene to the
       glFlush(); 
}

void update(int value) { //bantuan dari glrotate
       _angle += 9.0f;
       if (_angle > 360) {
              _angle -= 360;
       }
      
       glutPostRedisplay();
       glutTimerFunc(35, update, 0);//25:ukuran milisecond
}
    
int main(int argc, char** argv) 
{ 
glutInitWindowSize(400, 400);
printf("Contoh Sederhana Kotak "); 
glutCreateWindow("Praktikum06");
glutDisplayFunc(mydisplay);
glutTimerFunc(35, update, 0);
glutMainLoop(); 
return 0;
}







                                                                      

PRAKTIKUM 7

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

void init(void)
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_FLAT);
}
void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT);
   glColor3f (1.0, 1.0, 1.0);
   glLoadIdentity ();/* clear the matrix */
/* viewing transformation  */
gluLookAt (0.5, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   glScalef (1.0, 1.0, 1.0);      /* modeling transformation 1*/
   glutWireCube(0.5);


   glTranslatef(-1.0,0.0,0.0);      /* modeling transformation 2*/
   glutWireCube(0.5);

   glTranslatef(2.0,0.0,0.0);      /* modeling transformation 3*/
   glutWireCube(0.5);
                                                     /* modeling transformation */
   glTranslatef(0.0,0.0,1.0);
   glutWireCube(0.5);

   glTranslatef(-2.0,0.0,0.0);      /* modeling transformation */
   glutWireCube(0.5);

    glTranslatef(1.0,0.0,0.0);      /* modeling transformation */
   glutWireCube(0.5);

   glColor3f(0.0,0.0,1.0);
   glTranslatef(0.5,0.5,-0.5);      /* modeling transformation */
   glutWireCube(0.5);

      glColor3f(1.0,0.0,0.0);
   glTranslatef(-1.0,0.0,0.0);      /* modeling transformation */
   glutWireCube(0.5);

    glColor3f(1.0,1.0,1.0);
   glTranslatef(0.5,0.5,0.0);      /* modeling transformation */
   glutWireCube(0.5);

   glFlush ();
}
void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   glFrustum (-1.0, 1.0, -1.0, 1.0, 2.0, 20.0);
   glMatrixMode (GL_MODELVIEW);
}
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (500, 500);
   //glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutMainLoop();
   return 0;
}






PRAKTIKUM 8

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


float _angle = 45.0f;

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); //sdt pandang
       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);//pindah posisi xyz

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

       GLfloat lightColor0[] = {0.0f, 1.0f, 1.0f, 2.0f};
       GLfloat lightPos0[] = {3.0f, 10.0f, 2.0f, 4.0f}; //

       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
       glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

       gluLookAt (2.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
       //glScalef (2.0, 2.0, 2.0);  
       glTranslatef(0.0, 0.0,0.0);
       glutSolidCube(1.0);
//
       glTranslatef(1.0f, 0.0f, 0.0f);//pindah posisi xyz
       GLfloat lightColor1[] = {0.0f, 1.0f, 1.0f, 2.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor1);
       glutSolidCube(1.0);

       glTranslatef(1.0f, 0.0f, 0.0f);//pindah posisi xyz
       GLfloat lightColor2[] = {0.0f, 1.0f, 1.0f, 2.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor2);
       glutSolidCube(1.0);
//
       glTranslatef(0.0f, -1.0f, 0.0f);//pindah posisi xyz
       GLfloat lightColor3[] = {1.0f, 1.0f, 0.0f, 2.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor3);
       glutSolidCube(1.0);

       glTranslatef(0.0f, -1.0f, 0.0f);//pindah posisi xyz
       GLfloat lightColor4[] = {1.0f, 1.0f, 0.0f, 2.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor4);
       glutSolidCube(1.0);

      
       glTranslatef(0.0f, -1.0f, 0.0f);//pindah posisi xyz
       GLfloat lightColor5[] = {1.0f, 1.0f, 0.0f, 2.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor5);
       glutSolidCube(1.0);

       glTranslatef(-2.0f, 0.0f, 0.0f);//pindah posisi xyz
       GLfloat lightColor6[] = {0.0f, 0.0f, 2.0f, 2.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor6);
       glutSolidCube(1.0);

       glTranslatef(-1.0f, 0.0f, 0.0f);//pindah posisi xyz
       GLfloat lightColor7[] = {0.0f, 0.0f, 2.0f, 2.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor7);
       glutSolidCube(1.0);

       glTranslatef(-1.0f, 0.0f, 0.0f);//pindah posisi xyz
       GLfloat lightColor8[] = {0.0f, 0.0f, 2.0f, 2.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor8);
       glutSolidCube(1.0);

       glTranslatef(0.0f, 1.0f, 0.0f);//pindah posisi xyz
       GLfloat lightColor9[] = {0.0f, 1.0f, 0.0f, 0.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor9);
       glutSolidCube(1.0);

       glTranslatef(0.0f, 1.0f, 0.0f);//pindah posisi xyz
       GLfloat lightColor10[] = {0.0f, 1.0f, 0.0f, 0.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor10);
       glutSolidCube(1.0);

       glTranslatef(0.0f, 1.0f, 0.0f);//pindah posisi xyz
       GLfloat lightColor11[] = {0.0f, 1.0f, 0.0f, 0.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor11);
       glutSolidCube(1.0);

       glTranslatef(0.0f, 2.0f, 0.0f);//pindah posisi xyz
      
glRotatef(_angle, 0.0f, 1.0f, 0.0f);
       GLfloat lightColor12[] = {1.0f, 0.0f, 0.0f, 2.0f};
       glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor12);
       glutSolidCube(1.0);


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

void update(int value) {
       _angle += 9.0f;
       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("praktikum_8");
       initRendering();
       glutDisplayFunc(drawScene);
       glutReshapeFunc(handleResize);   
glutTimerFunc(25, update, 0);    
       glutMainLoop();
       return 0;
}












PRAKTIKUM 9

#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[] = {0.5f, 0.2f, 0.2f, 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.5f, 1.0f, 2.0f);

glBegin(GL_QUADS);

glColor3f(0.0f, 1.0f, 0.0f); //green
    glVertex3f(0, 3, 0);
glVertex3f(1, 0, -1);
glVertex3f(1, 0, 1);
glVertex3f(0, 3, 0);

glColor3f(0.0f, 0.0f, 1.0f); //blue
    glVertex3f(0, 3, 0);
glVertex3f(1, 0, 1);
glVertex3f(-1, 0, 1);
glVertex3f(0, 3, 0);

glColor3f(1.0f, 0.0f, 0.0f); //red
    glVertex3f(0, 3, 0);
glVertex3f(1, 0, -1);
glVertex3f(-1, 0, -1);
glVertex3f(0, 3, 0);

glColor3f(1.0f, 1.0f, 0.0f); //yellow
    glVertex3f(0, 3, 0);
glVertex3f(-1, 0, -1);
glVertex3f(-1, 0, 1);
glVertex3f(0, 3, 0);

glColor3f(1.0f, 1.0f, 1.0f); //alas
    glVertex3f(-1, 0, -1);
glVertex3f(-1, 0, 1);
glVertex3f(1, 0, 1);
glVertex3f(1, 0, -1);

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









PRAKTIKUM 10
main

#include <windows.h> 
#include <iostream>
#include <stdlib.h>
#include <stdio.h> 
#include <string.h>
#include <stdarg.h> 
#include <glut.h>
#include <glu.h>
#include "imageloader.h"
using namespace std;

Image::Image(char* ps, int w, int h) : pixels(ps), width(w), height(h) {
      
}

Image::~Image() {
       delete[] pixels;
}

namespace {
       //Converts a four-character array to an integer, using little-endian form
       int toInt(const char* bytes) {
              return (int)(((unsigned char)bytes[3] << 24) |
                                   ((unsigned char)bytes[2] << 16) |
                                   ((unsigned char)bytes[1] << 8) |
                                   (unsigned char)bytes[0]);
       }
      
       //Converts a two-character array to a short, using little-endian form
       short toShort(const char* bytes) {
              return (short)(((unsigned char)bytes[1] << 8) |
                                     (unsigned char)bytes[0]);
       }
      
       //Reads the next four bytes as an integer, using little-endian form
       int readInt(ifstream &input) {
              char buffer[4];
              input.read(buffer, 4);
              return toInt(buffer);
       }
      
       //Reads the next two bytes as a short, using little-endian form
       short readShort(ifstream &input) {
              char buffer[2];
              input.read(buffer, 2);
              return toShort(buffer);
       }
      
       //Just like auto_ptr, but for arrays
       template<class T>
       class auto_array {
              private:
                     T* array;
                     mutable bool isReleased;
              public:
                     explicit auto_array(T* array_ = NULL) :
                           array(array_), isReleased(false) {
                     }
                    
                     auto_array(const auto_array<T> &aarray) {
                           array = aarray.array;
                           isReleased = aarray.isReleased;
                           aarray.isReleased = true;
                     }
                    
                     ~auto_array() {
                           if (!isReleased && array != NULL) {
                                  delete[] array;
                           }
                     }
                    
                     T* get() const {
                           return array;
                     }
                    
                     T &operator*() const {
                           return *array;
                     }
                    
                     void operator=(const auto_array<T> &aarray) {
                           if (!isReleased && array != NULL) {
                                  delete[] array;
                           }
                           array = aarray.array;
                           isReleased = aarray.isReleased;
                           aarray.isReleased = true;
                     }
                    
                     T* operator->() const {
                           return array;
                     }
                    
                     T* release() {
                           isReleased = true;
                           return array;
                     }
                    
                     void reset(T* array_ = NULL) {
                           if (!isReleased && array != NULL) {
                                  delete[] array;
                           }
                           array = array_;
                     }
                    
                     T* operator+(int i) {
                           return array + i;
                     }
                    
                     T &operator[](int i) {
                           return array[i];
                     }
       };
}

Image* loadBMP(const char* filename) {
       ifstream input;
       input.open(filename, ifstream::binary);
       assert(!input.fail() || !"Could not find file");
       char buffer[2];
       input.read(buffer, 2);
       assert(buffer[0] == 'B' && buffer[1] == 'M' || !"Not a bitmap file");
       input.ignore(8);
       int dataOffset = readInt(input);
      
       //Read the header
       int headerSize = readInt(input);
       int width;
       int height;
       switch(headerSize) {
              case 40:
                     //V3
                     width = readInt(input);
                     height = readInt(input);
                     input.ignore(2);
                     assert(readShort(input) == 24 || !"Image is not 24 bits per pixel");
                     assert(readShort(input) == 0 || !"Image is compressed");
                     break;
              case 12:
                     //OS/2 V1
                     width = readShort(input);
                     height = readShort(input);
                     input.ignore(2);
                     assert(readShort(input) == 24 || !"Image is not 24 bits per pixel");
                     break;
              case 64:
                     //OS/2 V2
                     assert(!"Can't load OS/2 V2 bitmaps");
                     break;
              case 108:
                     //Windows V4
                     assert(!"Can't load Windows V4 bitmaps");
                     break;
              case 124:
                     //Windows V5
                     assert(!"Can't load Windows V5 bitmaps");
                     break;
              default:
                     assert(!"Unknown bitmap format");
       }
      
       //Read the data
       int bytesPerRow = ((width * 3 + 3) / 4) * 4 - (width * 3 % 4);
       int size = bytesPerRow * height;
       auto_array<char> pixels(new char[size]);
       input.seekg(dataOffset, ios_base::beg);
       input.read(pixels.get(), size);
      
       //Get the data into the right format
       auto_array<char> pixels2(new char[width * height * 3]);
       for(int y = 0; y < height; y++) {
              for(int x = 0; x < width; x++) {
                     for(int c = 0; c < 3; c++) {
                           pixels2[3 * (width * y + x) + c] =
                                  pixels[bytesPerRow * y + 3 * x + (2 - c)];
                     }
              }
       }
      
       input.close();
       return new Image(pixels2.release(), width, height);
}


GLuint loadTexture(Image* image) {
       GLuint textureId;
       glGenTextures(1, &textureId); //Make room for our texture
       glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
       //Map the image to the texture
       glTexImage2D(GL_TEXTURE_2D,                //Always GL_TEXTURE_2D
                            0,                            //0 for now
                            GL_RGB,                       //Format OpenGL uses for image
                            image->width, image->height,  //Width and height
                            0,                            //The border of the image
                            GL_RGB, //GL_RGB, because pixels are stored in RGB format
                            GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
                                              //as unsigned numbers
                            image->pixels);               //The actual pixel data
       return textureId; //Returns the id of the texture
}

GLuint _textureId; //The id of the texture


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

       Image* image = loadBMP("mu.bmp");
       _textureId = loadTexture(image);
       delete image;
}

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[] = {0.5f, 0.2f, 0.2f, 1.0f};

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

glEnable(GL_TEXTURE_2D);
       glBindTexture(GL_TEXTURE_2D, _textureId);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

       glRotatef(_angle, 0.0f, 1.0f, 0.0f);
       glColor3f(1.0f, 1.0f, 0.0f);
       //glScalef(1.0f, 1.5f, 1.0f);
       glBegin(GL_QUADS);
      
       //Front

       glNormal3f(0.1f, 0.0f, 1.0f);
       //glNormal3f(-1.0f, 0.0f, 1.0f);
       glTexCoord2f(0.0f, 0.0f);
       glVertex3f(-1.5f, -1.0f, 1.5f);
       glNormal3f(1.0f, 0.0f, 1.0f);
       glTexCoord2f(1.0f, 0.0f);
       glVertex3f(1.5f, -1.0f, 1.5f);
       glNormal3f(1.0f, 0.0f, 1.0f);
       glTexCoord2f(1.0f, 1.0f);
       glVertex3f(1.5f, 1.0f, 1.5f);
       glNormal3f(-1.0f, 0.0f, 1.0f);
       glTexCoord2f(0.0f, 1.0f);
       glVertex3f(-1.5f, 1.0f, 1.5f);
      
       //Right
       glColor3f(0.0f, 1.0f, 2.0f);
       glNormal3f(1.0f, 0.0f, 0.0f);
       //glNormal3f(1.0f, 0.0f, -1.0f);
       glTexCoord2f(0.0f, 0.0f);
       glVertex3f(1.5f, -1.0f, -1.5f);
       glNormal3f(1.0f, 0.0f, -1.0f);
       //glTexCoord2f(1.0f, 0.0f);
       glVertex3f(1.5f, 1.0f, -1.5f);
       glNormal3f(1.0f, 0.0f, 1.0f);
       glVertex3f(1.5f, 1.0f, 1.5f);
       //glTexCoord2f(1.0f, 1.0f);
       glNormal3f(1.0f, 0.0f, 1.0f);
       //glTexCoord2f(0.0f, 1.0f);
       glVertex3f(1.5f, -1.0f, 1.5f);
      
       //Back
       glColor3f(0.0f, 1.0f, 1.0f);
       glNormal3f(0.0f, 0.0f, -1.0f);
       //glNormal3f(-1.0f, 0.0f, -1.0f);
       glVertex3f(-1.5f, -1.0f, -1.5f);
       glNormal3f(-1.0f, 0.0f, -1.0f);
       glVertex3f(-1.5f, 1.0f, -1.5f);
       glNormal3f(1.0f, 0.0f, -1.0f);
       glVertex3f(1.5f, 1.0f, -1.5f);
       glNormal3f(1.0f, 0.0f, -1.0f);
       glVertex3f(1.5f, -1.0f, -1.5f);
      
       //Left
       glColor3f(1.0f, 1.0f, 1.0f);
       glNormal3f(-1.0f, 0.0f, 0.0f);
       //glNormal3f(-1.0f, 0.0f, -1.0f);
       glVertex3f(-1.5f, -1.0f, -1.5f);
       glNormal3f(-1.0f, 0.0f, 1.0f);
       glVertex3f(-1.5f, -1.0f, 1.5f);
       glNormal3f(-1.0f, 0.0f, 1.0f);
       glVertex3f(-1.5f, 1.0f, 1.5f);
       glNormal3f(-1.0f, 0.0f, -1.0f);
       glVertex3f(-1.5f, 1.0f, -1.5f);
       glEnd();
       glDisable(GL_TEXTURE_2D);
      
       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");
       initRendering();
       glutDisplayFunc(drawScene);
       glutReshapeFunc(handleResize);   
       glutTimerFunc(25, update, 0);
      
       glutMainLoop();
       return 0;
}


header


#ifndef IMAGE_LOADER_H_INCLUDED
#define IMAGE_LOADER_H_INCLUDED

//Represents an image
class Image {
      public:
            Image(char* ps, int w, int h);
            ~Image();
           
            /* An array of the form (R1, G1, B1, R2, G2, B2, ...) indicating the
             * color of each pixel in image.  Color components range from 0 to 255.
             * The array starts the bottom-left pixel, then moves right to the end
             * of the row, then moves up to the next column, and so on.  This is the
             * format in which OpenGL likes images.
             */
            char* pixels;
            int width;
            int height;
};

//Reads a bitmap image from file.
Image* loadBMP(const char* filename);

#endif