Kamis, 13 Desember 2012

Segitiga sierpinski

Kali ini saya akan memberikan source code untuk openGL segitiga sierpinski


 #include <GL/glut.h>

/* initial triangle */

GLfloat v[3][2]={{-1.0, -0.58}, 
     {1.0, -0.58}, {0.0, 1.15}};

int n; /* number of recursive steps */



 void triangle( GLfloat *a, GLfloat *b, GLfloat *c)

/* display one triangle */
     {
       glVertex2fv(a); 
       glVertex2fv(b); 
       glVertex2fv(c);
     }


 void divide_triangle(GLfloat *a, GLfloat *b, GLfloat *c, int m)
{
     /* triangle subdivision using vertex numbers */
     GLfloat v0[2], v1[2], v2[2];
     int j;
     if(m>0)
     {
       for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;
       for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;
       for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;
       divide_triangle(a, v0, v1, m-1);
       divide_triangle(c, v1, v2, m-1);
       divide_triangle(b, v2, v0, m-1);
     }
     else
       triangle(a,b,c);
     /* draw triangle at end of recursion */
}



 void display()
{
     glClear(GL_COLOR_BUFFER_BIT);
     glBegin(GL_TRIANGLES);
     divide_triangle(v[0], v[1], v[2], n);
     glEnd();
     glFlush();
}

void myinit()
{
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
     glMatrixMode(GL_MODELVIEW);
     glClearColor (1.0, 1.0, 1.0,1.0);
     glColor3f(0.0,0.0,0.0);
}



 int main(int argc, char **argv)
{
     n=4;
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
     glutInitWindowSize(500, 500);
     glutCreateWindow("2D Gasket");
     glutDisplayFunc(display);
     myinit();
     glutMainLoop();
}


Jangn lupa dicompile.
Salam PETOK!!!


Camera 26 sudut openGL

Hari ini saya akan memposting cara buat sudut camera dengan menggunakan glut OpenGL.

Sebelumnya saya berterima kasih dengan Kotomo hakase  (kotomo.org) yang telah menshare ilmunya.

ini Source codenya :





 /***************************************************************************************
  *                               Deklarasi pemanggilan                                 *
  ***************************************************************************************/

#include<stdlib.h>
#include<ctype.h>
#include<GL/glut.h>
#include<GL/gl.h>
#include<math.h>


static GLfloat rot_y, rot_x;
static GLfloat bgn_y, bgn_x;
static int mouse_x, mouse_y;
static GLuint cubelist;







/***************************************************************************************
 *                               Pemanggilan layar sisplay                             *
 ***************************************************************************************/

void display_func(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0,1.0,1.0,1.0);                       //warna background window
glPushMatrix();
glTranslatef(0.0, 0.0, -15.0);
glRotatef(rot_x, 1.0, 0.0, 0.0);
glRotatef(rot_y, 0.0, 1.0, 0.0);
glCallList(cubelist);
glPopMatrix();
glutSwapBuffers();
}







/***************************************************************************************
 *                           Titik Koordinat pada kubus                                *
 ***************************************************************************************/

GLuint make_cube(void)
{
GLint list;

static GLfloat vert[][4]=
{

{ 3.0,  3.0,  3.0},
{ 0.0,  3.0,  3.0},
{-3.0,  3.0,  3.0},
{ 3.0,  0.0,  3.0},
{ 0.0,  0.0,  3.0},
{-3.0,  0.0,  3.0},
{ 3.0, -3.0,  3.0},
{ 0.0, -3.0,  3.0},
{-3.0, -3.0,  3.0},

{ 3.0,  3.0, -3.0},
{ 0.0,  3.0, -3.0},
{-3.0,  3.0, -3.0},
{ 3.0,  0.0, -3.0},
{ 0.0,  0.0, -3.0},
{-3.0,  0.0, -3.0},
{ 3.0, -3.0, -3.0},
{ 0.0, -3.0, -3.0},
{-3.0, -3.0, -3.0},

{ 3.0,  3.0,  0.0},
{ 0.0,  3.0,  0.0},
{-3.0,  3.0,  0.0},
{ 3.0,  0.0,  0.0},
{ 0.0,  0.0,  0.0},
{-3.0,  0.0,  0.0},
{ 3.0, -3.0,  0.0},
{ 0.0, -3.0,  0.0},
{-3.0, -3.0,  0.0},


};


static GLfloat color[][4]=
{
{1.0, 0.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 1.0, 1.0, 0.0},
{1.0, 0.0, 1.0, 0.0},
{1.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0},
};


list=glGenLists(1);
glNewList(list, GL_COMPILE);


glutSolidTeapot(1);            //membuat gambar teapot


glBegin(GL_LINES);
glColor3fv(color[6]);

glVertex3fv(vert[0]);
glVertex3fv(vert[2]);
glVertex3fv(vert[3]);
glVertex3fv(vert[5]);
glVertex3fv(vert[6]);
glVertex3fv(vert[8]);

glVertex3fv(vert[0]);
glVertex3fv(vert[6]);
glVertex3fv(vert[2]);
glVertex3fv(vert[8]);
glVertex3fv(vert[1]);
glVertex3fv(vert[7]);



glVertex3fv(vert[9]);
glVertex3fv(vert[11]);
glVertex3fv(vert[12]);
glVertex3fv(vert[14]);
glVertex3fv(vert[15]);
glVertex3fv(vert[17]);

glVertex3fv(vert[9]);
glVertex3fv(vert[15]);
glVertex3fv(vert[11]);
glVertex3fv(vert[17]);
glVertex3fv(vert[10]);
glVertex3fv(vert[16]);


glVertex3fv(vert[18]);
glVertex3fv(vert[20]);
glVertex3fv(vert[21]);
glVertex3fv(vert[23]);
glVertex3fv(vert[24]);
glVertex3fv(vert[26]);

glVertex3fv(vert[18]);
glVertex3fv(vert[24]);
glVertex3fv(vert[20]);
glVertex3fv(vert[26]);
glVertex3fv(vert[19]);
glVertex3fv(vert[25]);


glVertex3fv(vert[0]);
glVertex3fv(vert[9]);
glVertex3fv(vert[1]);
glVertex3fv(vert[10]);
glVertex3fv(vert[2]);
glVertex3fv(vert[11]);
glVertex3fv(vert[3]);
glVertex3fv(vert[12]);
glVertex3fv(vert[4]);
glVertex3fv(vert[13]);
glVertex3fv(vert[5]);
glVertex3fv(vert[14]);
glVertex3fv(vert[6]);
glVertex3fv(vert[15]);
glVertex3fv(vert[7]);
glVertex3fv(vert[16]);
glVertex3fv(vert[8]);
glVertex3fv(vert[17]);

glEnd();

glBegin(GL_POINTS);

glColor3fv(color[0]);

glVertex3fv(vert[0]);
glVertex3fv(vert[1]);
glVertex3fv(vert[2]);
glVertex3fv(vert[3]);
glVertex3fv(vert[4]);
glVertex3fv(vert[5]);
glVertex3fv(vert[6]);
glVertex3fv(vert[7]);
glVertex3fv(vert[8]);


glColor3fv(color[2]);
glVertex3fv(vert[9]);
glVertex3fv(vert[10]);
glVertex3fv(vert[11]);
glVertex3fv(vert[12]);
glVertex3fv(vert[13]);
glVertex3fv(vert[14]);
glVertex3fv(vert[15]);
glVertex3fv(vert[16]);
glVertex3fv(vert[17]);


glColor3fv(color[1]);
glVertex3fv(vert[18]);
glVertex3fv(vert[19]);
glVertex3fv(vert[20]);
glVertex3fv(vert[21]);
glVertex3fv(vert[22]);
glVertex3fv(vert[23]);
glVertex3fv(vert[24]);
glVertex3fv(vert[25]);
glVertex3fv(vert[26]);

glEnd();
glPointSize(10); //Size untuk tiap titik
glEndList();

return list;
}





/***************************************************************************************
 *                               fungsi buat bangun ruang                              *
 ***************************************************************************************/

void reshape_func(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 3.0, 10000.0);
glMatrixMode(GL_MODELVIEW);
}






/***************************************************************************************
 *                               Fungsi gerak pada keyboard                            *
 ***************************************************************************************/
void skey_func(int key, int x, int y)
{
switch(key){
case GLUT_KEY_UP:
if(rot_x<90){
rot_x+=5;
}
break;
case GLUT_KEY_DOWN:
if(-90<rot_x){
rot_x-=5;
}
break;
case GLUT_KEY_LEFT:
rot_y+=5;
break;
case GLUT_KEY_RIGHT:
rot_y-=5;
break;
}

glutPostRedisplay();
}







/***************************************************************************************
 *                               Fungsi renderscene teapot                             *
 ***************************************************************************************/
void renderScene(void)
{

 glutWireTeapot(4);
 glutSwapBuffers();
}







/***************************************************************************************
 *                                   Main Function                                     *
 ***************************************************************************************/
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);    //Ukuran Layar Window
glutCreateWindow("CAMERA TEAMARC X KOTOMO");  //Nama Window yang ingin ditampilkan
glutDisplayFunc(display_func); //fungsi untuk memunculkan window
glutReshapeFunc(reshape_func);        //Fungsi Membuat garis kubus
glutSpecialFunc(skey_func); //Fungsi untuk Keyboard Arah
cubelist=make_cube();
glEnable(GL_DEPTH_TEST);
glutMainLoop();
glLoadIdentity();


return 0;
}


Terus dicompile, dan jangan lupa simpan dengan ekstensi cpp.


Salam PETOK!!!.





Program C #1

Halo Semua!!.
Ini adalah postingan pertama saya. kali ini saya akan membahas programing dengan bahasa C.
Sebelumnya saya berterima kasih dengan Bos Bintang di linknya : http://blajar-komputer.blogspot.com/2011/06/contoh-penggabungan-perulangan-array.html.
 Tugas ini merupakan tugas kelompok, dengan dengan beberapa perubahn di codingan.


Nama kelompok :
- Aditya Dwi Saputra (1110511003)
- Syamsul Amin (1110511048)

MENGHITUNG LUAS BALOK
Study kasus
Ini adalah postingan pertama saya. kali ini saya akan membahas programing dengan bahasa C.
Program kali ini adalah menghitung luas balok dengan menggunakan array, function, Abstract data type, dan struct. dengan beberapa perubahan.


Pseduocode:


mulai
mendeklarasikan
typedef int jumlah
struct{
jumlah hasiluas;
}persegi;
menginisialisasi fungsi luas
void luas (jumlah p, jumlah l){
persegi.hasiluas=p*l;
}
melakukan perulangan sebanyak data array
input data persegi ke array
input panjang
input lebar
pemanggilan fungsi luas
tutup perulangan
cetak data yang tersimpan didalam array
finish



Source Code:

//preprocessor
#include <stdio.h>
typedef int jumlah;

struct
{
    jumlah hasiluas;
}
balok;



//Fungsi penghitung luas.

void luas (jumlah p, jumlah l, jumlah t)
{
    balok.hasiluas = p * l * t;
}



//Fungsi utama.
main()
{

    //Pendeklarasian variabel yang dibutuhkan.
    jumlah p[10], l[10], t[10], ulangan;

    //inputan proses perulangan
    printf("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    printf("xxxxxxxxxxx                                                          xxxxxxxxxxx");
    printf("xxxxxxxxxxx   Program Menghitung Luas Balok Dengan rumus p x l x t   xxxxxxxxxxx");
    printf("xxxxxxxxxxx                                                          xxxxxxxxxxx");
    printf("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\n\n\n");
    printf("Masukan perhitungan balok yang diinginkan (max 10) = ");
    scanf("%d", &ulangan);

    //Proses penginputan dengan perulangan sesuai nilai yang ditulis.
    jumlah input;
    for (input = 0; input < ulangan; input++)
    {

        printf("Perhitungan untuk balok  ke - %d\n", input + 1);
        printf("Masukan panjang balok\t= ");
        scanf("%d", &p[input]);
        printf("Masukan lebar balok\t= ");
        scanf("%d", &l[input]);
        printf("Masukan Tinggi balok\t= ");
        scanf("%d", &t[input]);

    }
    printf("\n\n\n");

    //Proses menampilkan hasil dari perhitungan
    for ( input = 0; input < ulangan; input++)
    {
        luas(p[input], l[input], t[input]);
        printf("hasil hitungan balok ke - %d luasnya adalah = %d\n", input + 1, balok.hasiluas);
    }

}
 
 
Sekarang coba dicompile dan ingat ekstensinya C ya.
Salam PETOK!!!