er

Berbagai Tutorial dan Info Menarik

Belajar, Berjuang, dan Bertaqwa

Monday 24 November 2014

Bentuk Sederhana 3D dengan Opengl


Source Code :
#include <Windows.h>
#include <iostream>
#include <gl\GL.h>
#include <gl\GLU.h>
#include <gl\glut.h>
#include <math.h>

void cube()
{
//menggambar kubus dan transformasi tarnslasi ke titik 0.5 0.5 0.5 dan skala 1 1 1
glPushMatrix();
glTranslated(0.7,0.7,0.7);//cube
glScaled(0.5,0.5,0.5);
glutSolidCube(1.0);
glPopMatrix();

glPushMatrix();
glTranslated(0.7,0.7,0.7);//cube
glScaled(0.7,0.2,0.7);
glutSolidCube(1.0);
glPopMatrix();

glPushMatrix();
glTranslated(0.7,0.9,0.7);//cube
glScaled(0.5,1.0,0.5);
glutSolidCube(1.0);
glPopMatrix();



// mobil
    glPushMatrix();
    glPushMatrix();                   // body
    glScalef(2,.5,1);
    glutSolidCube(.5);
    glPopMatrix();
    glTranslatef(0,0,.25);
    glPushMatrix();
    glTranslatef(-.4,-.2,0);
    glutSolidTorus(.05,.1,8,8);       // wheel
    glTranslatef(.8,0,0);
    glutSolidTorus(.05,.1,8,8);       // wheel
    glPopMatrix();
    glTranslatef(0,0,-.5);
    glPushMatrix();
    glTranslatef(-.4,-.2,0);
    glutSolidTorus(.05,.1,8,8);       // wheel
    glTranslatef(.8,0,0);
    glutSolidTorus(.05,.1,8,8);       // wheel
    glPopMatrix();
    glPopMatrix();

    glPopMatrix();

}


void setMaterial()
{
//set properties of surfaces material
GLfloat mat_ambient[] = {0.7f,0.7f,0.7f,1.0f}; // ada 4 jenis material yang dipakai, dengan kombinasi warna tertentu
GLfloat mat_diffuse[] = {0.6f,0.6f,0.6f,1.0f};
GLfloat mat_specular[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[] = {50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
}


void setLighting()
{
//set light sources
GLfloat lightIntensity[] = {0.7f,0.7f,0.7f,1.0f};//mensetting pencahayaan
GLfloat light_position[] = {2.0f,6.0f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
}


void setViewport()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double winHt = 1.0;//half height of the window
glOrtho(-winHt*64/48,winHt*64/48,-winHt,winHt,0.1,100.0);
}


void setCamera()
{
//set the camera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(9.3,2.3,5.0,0,0.25,0,0.0,3.0,0.0);
}


void displayObject()
{
setMaterial();
setLighting();
setViewport();
setCamera();
//startDrawing
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
cube();//memanggil fungsi menggambar kubus
glFlush();//mengirim smua objek untuk dirender
}


int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("3d grafkom");
glutDisplayFunc(displayObject);//fungsi dari display object yang menggabungkan kubus lighting material dan kamera
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glClearColor(1.0f,1.0f,1.0f,0.0f);
glViewport(0,0,640,480);
glutMainLoop();

}


Screenshot :

Gambar Kubus Sederhana 3D dengan Opengl


Source Code :
#include <Windows.h>
#include <iostream>
#include <gl\GL.h>
#include <gl\GLU.h>
#include <gl\glut.h>
#include <math.h>

void cube()
{
//menggambar kubus dan transformasi tarnslasi ke titik 0.5 0.5 0.5 dan skala 1 1 1
glPushMatrix();
glTranslated(0.3,0.3,0.3);//cube
glScaled(0.4,0.4,0.4);
glutSolidCube(1.0);
glPopMatrix();
}
void setMaterial()
{
//set properties of surfaces material
GLfloat mat_ambient[] = {0.7f,0.7f,0.7f,1.0f}; // ada 4 jenis material yang dipakai, dengan kombinasi warna tertentu
GLfloat mat_diffuse[] = {0.6f,0.6f,0.6f,1.0f};
GLfloat mat_specular[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[] = {50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
}
void setLighting()
{
//set light sources
GLfloat lightIntensity[] = {0.7f,0.7f,0.7f,1.0f};//mensetting pencahayaan
GLfloat light_position[] = {2.0f,6.0f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
}
void setViewport()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double winHt = 1.0;//half height of the window
glOrtho(-winHt*64/48,winHt*64/48,-winHt,winHt,0.1,100.0);
}
void setCamera()
{
//set the camera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(9.3,2.3,5.0,0,0.25,0,0.0,3.0,0.0);
}

void displayObject()
{
setMaterial();
setLighting();
setViewport();
setCamera();
//startDrawing
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
cube();//memanggil fungsi menggambar kubus
glFlush();//mengirim smua objek untuk dirender
}


int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("3d grafkom");
glutDisplayFunc(displayObject);//fungsi dari display object yang menggabungkan kubus lighting material dan kamera
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glClearColor(1.0f,1.0f,1.0f,0.0f);
glViewport(0,0,640,480);
glutMainLoop();

}


Screenshot :

Tuesday 11 November 2014

Gambar Laba-Laba Sederhana dengan OpenGL


source code :

#include <iostream>
#include <GL/glut.h>
#include <math.h>

//LINGKARAN


const double PI = 3.141592653589793;

void drawCircle(float x_tengah,float y_tengah,float radius){
    glBegin(GL_POLYGON);

    int jumlah_titik = 50;

    for (int i=0;i<=360;i++)
    {
        float sudut = i*(2*PI/jumlah_titik);
        float x = x_tengah + radius*cos(sudut);
        float y = y_tengah + radius*sin(sudut);
        glVertex2f(x,y);
    }

    glEnd();

    //sikil kanan tengah
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (0.70, 0.00, 0.0);
glVertex3f (0.25, 0.00, 0.0);

glVertex3f (0.25, 0.00, 0.0);
glVertex3f (0.25, 0.02, 0.0);

glVertex3f (0.25, 0.02, 0.0);
glVertex3f (0.70, 0.02, 0.0);

glVertex3f (0.70, 0.02, 0.0);
glVertex3f (0.70, 0.00, 0.0);

glEnd ();

//sikil kanan atas
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (0.60, 0.40, 0.0);
glVertex3f (0.05, 0.20, 0.0);

glVertex3f (0.05, 0.20, 0.0);
glVertex3f (0.05, 0.18, 0.0);

glVertex3f (0.05, 0.18, 0.0);
glVertex3f (0.60, 0.38, 0.0);

glVertex3f (0.60, 0.18, 0.0);
glVertex3f (0.60, 0.40, 0.0);

glEnd ();

//sikil kanan bawah
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (0.60, -0.45, 0.0);
glVertex3f (0.05, -0.20, 0.0);

glVertex3f (0.05, -0.20, 0.0);
glVertex3f (0.05, -0.18, 0.0);

glVertex3f (0.05, -0.18, 0.0);
glVertex3f (0.60, -0.43, 0.0);

glVertex3f (0.60, -0.18, 0.0);
glVertex3f (0.60, -0.45, 0.0);

glEnd ();


//sikil kiri tengah
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (-0.70, 0.00, 0.0);
glVertex3f (-0.25, 0.00, 0.0);

glVertex3f (-0.25, 0.00, 0.0);
glVertex3f (-0.25, 0.02, 0.0);

glVertex3f (-0.25, 0.02, 0.0);
glVertex3f (-0.70, 0.02, 0.0);

glVertex3f (-0.70, 0.02, 0.0);
glVertex3f (-0.70, 0.00, 0.0);

glEnd ();

//sikil kiri atas
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (-0.60, 0.40, 0.0);
glVertex3f (-0.05, 0.20, 0.0);

glVertex3f (-0.05, 0.20, 0.0);
glVertex3f (-0.05, 0.18, 0.0);

glVertex3f (-0.05, 0.18, 0.0);
glVertex3f (-0.60, 0.38, 0.0);

glVertex3f (-0.60, 0.18, 0.0);
glVertex3f (-0.60, 0.40, 0.0);

glEnd ();

//sikil kiri bawah
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (-0.60, -0.45, 0.0);
glVertex3f (-0.05, -0.20, 0.0);

glVertex3f (-0.05, -0.20, 0.0);
glVertex3f (-0.05, -0.18, 0.0);

glVertex3f (-0.05, -0.18, 0.0);
glVertex3f (-0.60, -0.43, 0.0);

glVertex3f (-0.60, -0.18, 0.0);
glVertex3f (-0.60, -0.45, 0.0);

glEnd ();

//jari kanan bawah
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (0.60, -0.42, 0.0);
glVertex3f (0.58, -0.42, 0.0);

glVertex3f (0.58, -0.42, 0.0);
glVertex3f (0.58, -0.49, 0.0);

glVertex3f (0.58, -0.49, 0.0);
glVertex3f (0.60, -0.49, 0.0);

glVertex3f (0.60, -0.49, 0.0);
glVertex3f (0.60, -0.42, 0.0);

glEnd ();

//jari kiri bawah
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (-0.60, -0.42, 0.0);
glVertex3f (-0.58, -0.42, 0.0);

glVertex3f (-0.58, -0.42, 0.0);
glVertex3f (-0.58, -0.49, 0.0);

glVertex3f (-0.58, -0.49, 0.0);
glVertex3f (-0.60, -0.49, 0.0);

glVertex3f (-0.60, -0.49, 0.0);
glVertex3f (-0.60, -0.42, 0.0);

glEnd ();

//jari kiri atas
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (-0.60, 0.30, 0.0);
glVertex3f (-0.58, 0.30, 0.0);

glVertex3f (-0.58, 0.30, 0.0);
glVertex3f (-0.58, 0.39, 0.0);

glVertex3f (-0.58, 0.39, 0.0);
glVertex3f (-0.60, 0.39, 0.0);

glVertex3f (-0.60, 0.39, 0.0);
glVertex3f (-0.60, 0.30, 0.0);

glEnd ();
//jari kanan atas
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (0.60, 0.30, 0.0);
glVertex3f (0.58, 0.30, 0.0);

glVertex3f (0.58, 0.30, 0.0);
glVertex3f (0.58, 0.39, 0.0);

glVertex3f (0.58, 0.39, 0.0);
glVertex3f (0.60, 0.39, 0.0);

glVertex3f (0.60, 0.39, 0.0);
glVertex3f (0.60, 0.30, 0.0);

glEnd ();

//jari kanan tengah
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (0.70, 0.00, 0.0);
glVertex3f (0.68, 0.00, 0.0);

glVertex3f (0.68, 0.00, 0.0);
glVertex3f (0.68, -0.07, 0.0);

glVertex3f (0.68, -0.07, 0.0);
glVertex3f (0.70, -0.07, 0.0);

glVertex3f (0.70, -0.07, 0.0);
glVertex3f (0.70, 0.00, 0.0);

glEnd ();

//jari kiri tengah
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);

glVertex3f (-0.70, -0.00, 0.0);
glVertex3f (-0.68, -0.00, 0.0);

glVertex3f (-0.68, -0.00, 0.0);
glVertex3f (-0.68, -0.07, 0.0);

glVertex3f (-0.68, -0.07, 0.0);
glVertex3f (-0.70, -0.07, 0.0);

glVertex3f (-0.70, -0.07, 0.0);
glVertex3f (-0.70, -0.00, 0.0);

glEnd ();

//mata kanan
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (0.4, 0.4, 0.4);

glVertex3f (0.10, 0.43, 0.0);
glVertex3f (0.05, 0.43, 0.0);

glVertex3f (0.05, 0.43, 0.0);
glVertex3f (0.05, 0.45, 0.0);

glVertex3f (0.05, 0.45, 0.0);
glVertex3f (0.10, 0.45, 0.0);

glVertex3f (0.10, 0.45, 0.0);
glVertex3f (0.10, 0.43, 0.0);

glEnd ();

//mata kiri
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (0.4, 0.4, 0.4);

glVertex3f (-0.10, 0.43, 0.0);
glVertex3f (-0.05, 0.43, 0.0);

glVertex3f (-0.05, 0.43, 0.0);
glVertex3f (-0.05, 0.45, 0.0);

glVertex3f (-0.05, 0.45, 0.0);
glVertex3f (-0.10, 0.45, 0.0);

glVertex3f (-0.10, 0.45, 0.0);
glVertex3f (-0.10, 0.43, 0.0);

glEnd ();

//mulut
glPushMatrix ();
glLineWidth (1.9f);
glBegin (GL_LINES);
glBegin (GL_POLYGON);
glColor3f (0.4, 0.4, 0.4);

glVertex3f (-0.02, 0.35, 0.0);
glVertex3f (0.02, 0.35, 0.0);

glVertex3f (0.02, 0.35, 0.0);
glVertex3f (0.02, 0.37, 0.0);

glVertex3f (0.02, 0.37, 0.0);
glVertex3f (-0.02, 0.37, 0.0);

glVertex3f (-0.02, 0.37, 0.0);
glVertex3f (-0.02, 0.35, 0.0);

glEnd ();
    glFlush();
}



void display() {
drawCircle(0,0,0.3);
glColor3f (255/255, 255/255, 255/255);
drawCircle(0,0.40,0.15);
}


int main(int argc, char **argv){
    glutInit(&argc,argv);
    glutInitWindowSize(800,800);
    glutInitWindowPosition(100,100);//jarak windows dari tepi monitor

    glutCreateWindow("laba-laba");
    glutDisplayFunc(display);
    glutMainLoop();//mulai render
    return 0;
}