Monday, March 5, 2012



Dibawah ini akan saya tunjukkan coding-coding nya untuk membangun sebuah gambar dua dimensi . Di sini saya membuat gambar monyet dua dimensi codingnya sebagai berikut :

#include
#include

#define PHI 3.1415926535897932


// Membangun struktur titik
typedef struct {
float x;
float y;
} point2D_t ;

//membangun struktur warna
typedef struct {
float r;
float g;
float b;
}color_t;

//membangun fungsi warna
void setWarna(color_t col)
{
glColor3f(col.r,col.g,col.b);
}

//membangun fungsi obyek warna
void LaburGambar(point2D_t p[],int n,color_t col)
{
setWarna(col);
glBegin(GL_POLYGON);
for(int i=0;i glVertex2d(p[i].x,p[i].y);
glEnd();
}


// Membangun Fungsi untuk menggambar titik
void drawGaris(point2D_t p[], int n) {
glBegin(GL_LINE_STRIP);
for(int i=0; i glVertex2f(p[i].x, p[i].y);
glEnd();
}


void Gambar_Mata2D(void)
{
glBegin(GL_LINES);
glEnd();
}

void Gambar_kepala2D(void)
{
glBegin(GL_LINES);
glEnd();
}

// Membangun Fungsi Obyek 2D
void drawObyek2D(point2D_t p[], int n)
{
glBegin(GL_LINE_STRIP);
for(int i=0; i glVertex2f(p[i].x, p[i].y);
glEnd();
}

void koordinatSumbu()
{
glColor3f(1,0,0);
glBegin(GL_LINES);
//sumbu X
glVertex2i(-320,0);
glVertex2i(320,0);
//sumbu Y
glVertex2i(0,-240);
glVertex2i(0,240);
}


void mataKiri()
{
glColor3f(0,0,0);
point2D_t mata1[361];
point2D_t mata2[361];
point2D_t mataDlm1[361];
point2D_t mataDlm2[361];
double srad, r1,r2,r3,r4;

Gambar_Mata2D();

r1 = 7;
r2 = 10;
r3 = 4;
r4 = 6;


for(int i = 0; i <=360; i++)
{
srad = (i*PHI)/180;
mata1[i].x = -15 + r1 *cos(srad);
mata1[i].y = 90 + r2 *sin(srad);
mata2[i].x = 25 + r1 *cos(srad);
mata2[i].y = 105 + r2 *sin(srad);
mataDlm1[i].x = -15 + r3 *cos(srad);
mataDlm1[i].y = 90 + r4 *sin(srad);
mataDlm2[i].x = 25 + r3 *cos(srad);
mataDlm2[i].y = 105 + r4 *sin(srad);
}

drawObyek2D(mata1, 361);
drawObyek2D(mata2, 361);
drawObyek2D(mataDlm1, 361);
drawObyek2D(mataDlm2, 361);
color_t putih = {1,1,1};
color_t hitam = {0,0,0};

LaburGambar(mata1, 361, putih);
LaburGambar(mata2, 361, putih);
LaburGambar(mataDlm1, 361, hitam);
LaburGambar(mataDlm2, 361, hitam);
}


void kepala ()
{
point2D_t telingaKiri[8]= {
{-40,46},{-50,50},{-55,60},{-50,70},{-40,67},
{-37,60},
{-33,50},{-40,46}
};

point2D_t telingaKanan[6]= {
{40,98},{38,100},{40,105},{45,104},{46,96},
{40,98}
};
point2D_t kplatas1[45]= {
{30,125}, {30,138}, {22,132},
{20,144}, {18,135}, {10,148}, {10,148}, {10,140},
{0,130}, {-10,130}, {-20,128}, {-30,125}, {-40,120},
{-50,110}, {-55,100}, {-60,90}, {-58,80}, {-55,70},
{-54,68}, {-50,70}, {-40,68}, {-37,60}, {-35,50},
{-40,45},{-30,40}, {-20,35},{-10,40},
{-15,45}, {-20,50}, {-30,60}, {-30,70}, {-20,73},
{-5,80}, {-30,90}, {-32,100}, {-30,110}, {-25,115},
{-20,118}, {-10,117}, {0,116}, {10,120}, {15,126},
{20,130}, {25,128}, {30,125}
};


point2D_t kplBawah[31]= {
{5,38}, {10,49},{20,50}, {30,45}, {40,55},
{50,75}, {52,80},{50,90}, {40,98}, {38,100},
{35,105}, {32,110},{30,122}, {26,128}, {20,130},
{15,125}, {5,115},{0,113}, {-10,120}, {-20,122},
{-30,110},{-30,100},{-25,80},{-21,75},{-30,70},
{-30,60},{-20,50},{-15,45},{-10,40},{0,39},{5,38}
};
point2D_t mulut[14]= {
{0,50},{10,55},{20,52}, {30,50}, {38,60}, {40,70},{30,65},
{20,60},{10,58},{20,59}, {-10,61}, {-15,64}, {-5,52},{0,50}
};
point2D_t lidah[7]= {
{0,50},{10,53},{20,51},{25,50},
{20,47},
{10,48}, {0,50}
};





Gambar_kepala2D();
color_t kuning = {1,1,0};
color_t coklat = {0.9,0.4,0.3};
color_t merah = {1,0,0};
color_t hitam = {0,0,0};


LaburGambar(telingaKiri, 8, kuning);
LaburGambar(telingaKanan, 6, kuning);
LaburGambar(kplatas1, 45,coklat);
LaburGambar(kplBawah, 31, kuning);
LaburGambar(mulut, 14, hitam);
LaburGambar(lidah, 7, merah);



}


void badan ()
{
point2D_t tanganKiri[16]= {
{-135,-20},{-130,-12},{-140,-15},{-150,-20},{-160,-25},{-165,-30},{-167,-40},
{-160,-45},{-150,-42},{-135,-35},{-140,-55},{-130,-40},{-120,-32},{-125,-30},
{-120,-18},{-135,-20}
};


point2D_t tanganKanan[16]= {
{150,90},{160,95},{165,92},{170,100},{172,110},{179,120},{172,130},
{170,135},{160,130},{152,135},{140,120},{138,110},{140,102},{135,105},
{140,95},{150,90}
};

point2D_t kakiKiri[13]= {
{-95,-95},{-100,-100},{-115,-110},{-125,-110},{-130,-105},{-130,-90},{-128,-80},
{-120,-68},{-110,-60},{-115,-70},{-117,-80},{-110,-90},{-95,-95}
};

point2D_t kakiKanan[14]= {
{-55,-125},{-65,-123},{-73,-115},{-71,-105},{-80,-117},{-90,-118},{-80,-120},
{-93,-130},{-92,-140},{-85,-145},{-75,-146},{-65,-140},{-60,-130},{-55,-125}
};

point2D_t badanKeliling[56]= {
{-20,35}, {-30,35}, {-40,33}, {-50,30},
{-60,28},{-70,25}, {-80,20}, {-90,15}, {-100,10}, {-110,0},
{-120,-5},{-130,-10}, {-135,-20}, {-125,-15}, {-128,-30}, {-120,-33},
{-110,-35},{-100,-20}, {-90,-18}, {-80,-15}, {-70,-10}, {-50,-8},
{-60,-18},{-70,-25}, {-80,-34}, {-90,-50}, {-100,-60}, {-110,-60},
{-115,-70},{-117,-80}, {-110,-90}, {-105,-95}, {-95,-93}, {-80,-90},
{-60,-80},{-55,-82}, {-60,-90}, {-70,-100}, {-75,-105}, {-72,-115},
{-65,-125},{-50,-120}, {-40,-112}, {-30,-110}, {-22,-100}, {-15,-90},
{-10,-80},{0,-70}, {5,-60}, {10,-50}, {15,-40}, {30,-20},
{40,0},{50,15}, {60,30},{-20,35}

};

point2D_t tangan[21]= {
{60,30},{80,42}, {120,70}, {150,90},
{135,105}, {142,102}, {138,110}, {140,120}, {130,115},
{110,105}, {80,90}, {50,68}, {40,55}, {30,45},
{20,40}, {10,39}, {5,38}, {0,39}, {-10,40},
{-20,35},{60,30}

};


Gambar_kepala2D();
color_t kuning = {1,1,0};
color_t coklat = {0.9,0.4,0.3};


LaburGambar(tanganKiri, 16, kuning);
LaburGambar(tanganKanan, 16, kuning);
LaburGambar(kakiKiri, 13, kuning);
LaburGambar(kakiKanan, 14, kuning);
LaburGambar(badanKeliling, 56, coklat);
LaburGambar(tangan, 21, coklat);
}



void outline()
{

point2D_t badanKeliling[74]= {
{-20,35}, {-30,35}, {-40,33}, {-50,30},
{-60,28},{-70,25}, {-80,20}, {-90,15}, {-100,10}, {-110,0},
{-120,-5},{-130,-10}, {-135,-20}, {-125,-15}, {-128,-30}, {-120,-33},
{-110,-35},{-100,-20}, {-90,-18}, {-80,-15}, {-70,-10}, {-50,-8},
{-60,-18},{-70,-25}, {-80,-34}, {-90,-50}, {-100,-60}, {-110,-60},
{-115,-70},{-117,-80}, {-110,-90}, {-105,-95}, {-95,-93}, {-80,-90},
{-60,-80},{-55,-82}, {-60,-90}, {-70,-100}, {-75,-105}, {-72,-115},
{-65,-125},{-50,-120}, {-40,-112}, {-30,-110}, {-22,-100}, {-15,-90},
{-10,-80},{0,-70}, {5,-60}, {10,-50}, {15,-40}, {30,-20},
{40,0},{50,15}, {60,30}, {80,42}, {120,70}, {150,90},
{135,105}, {142,102}, {138,110}, {140,120}, {130,115},
{110,105}, {80,90}, {50,68}, {40,55}, {30,45},
{20,40}, {10,39}, {5,38}, {0,39}, {-10,40},{-20,35}
};


point2D_t garisPerut[7]= {
{-75,-55},{-70,-70},{-60,-80},{-50,-82},{-40,-80},
{-30,-79},{-20,-75}
};


point2D_t garisHidung1[4]= {
{10,88},{15,90},{20,85},{24,90}
};


point2D_t garisHidung2[3]= {
{10,90},{15,95},{22,96}
};

point2D_t senyum1[3]= {
{-18,60},{-15,68},{-10,70}
};

point2D_t senyum2[3]= {
{40,70},{45,80},{42,90}
};

point2D_t mulut[14]= {
{0,50},{10,48},{20,49}, {30,50}, {38,60}, {40,70},{30,65},
{20,60},{10,58},{20,59}, {-10,61}, {-15,64}, {-5,52},{0,50}
};

point2D_t kplBawah[31]= {
{5,38}, {10,39},{20,40}, {30,45}, {40,55},
{50,75}, {52,80},{50,90}, {40,98}, {38,100},
{35,105}, {32,110},{30,122}, {26,128}, {20,130},
{15,125}, {5,115},{0,113}, {-10,120}, {-20,122},
{-30,110},{-30,100},{-25,80},{-21,75},{-30,70},
{-30,60},{-20,50},{-15,45},{-10,40},{0,39},{5,38}
};

point2D_t lidah[7]= {
{0,50},{10,48},{20,47},{25,50},
{20,52},
{10,53}, {0,50}
};

point2D_t telingaKiri[8]= {
{-40,46},{-50,50},{-55,60},{-50,70},{-40,67},
{-37,60},
{-33,50},{-40,46}
};
point2D_t kepalaAtas[26]= {
{30,126}, {30,138}, {22,132},
{20,144}, {18,135}, {10,148}, {10,148}, {10,140},
{0,130}, {-10,130}, {-20,128}, {-30,125}, {-40,120},
{-50,110}, {-55,100}, {-60,90}, {-58,80}, {-55,70},
{-54,68}, {-50,70}, {-40,68}, {-37,60}, {-35,50},
{-40,45},{-30,40}, {-20,35}
};
point2D_t tanganKiri[16]= {
{-135,-20},{-130,-12},{-140,-15},{-150,-20},{-160,-25},{-165,-30},{-167,-40},
{-160,-45},{-150,-42},{-135,-35},{-140,-55},{-130,-40},{-120,-32},{-125,-30},
{-120,-18},{-135,-20}
};
point2D_t tanganKanan[16]= {
{150,90},{160,95},{165,92},{170,100},{172,110},{179,120},{172,130},
{170,135},{160,130},{152,135},{140,120},{138,110},{140,102},{135,105},
{140,95},{150,90}
};
point2D_t kakiKiri[13]= {
{-95,-95},{-100,-100},{-115,-110},{-125,-110},{-130,-105},{-130,-90},{-128,-80},
{-120,-68},{-110,-60},{-115,-70},{-117,-80},{-110,-90},{-95,-95}
};
point2D_t kakiKanan[14]= {
{-55,-125},{-65,-123},{-73,-115},{-71,-105},{-80,-117},{-90,-118},{-80,-120},
{-93,-130},{-92,-140},{-85,-145},{-75,-146},{-65,-140},{-60,-130},{-55,-125}
};
point2D_t telingaKanan[6]= {
{40,98},{38,100},{40,105},{45,104},{46,96},
{40,98}
};
point2D_t jariTgnKanan1[5]= {
{160,130},{162,125},{160,120},{155,115},{145,105}
};
point2D_t jariTgnKanan2[3]= {
{178,118},{168,117},{160,118}
};
point2D_t jariTgnKanan3[3]= {
{170,108},{160,107},{155,112}
};
point2D_t jariTgnKanan4[3]= {
{170,100},{160,101},{152,103}
};
point2D_t jariTgnKiri1[2]= {
{-160,-45},{-150,-35}
};
point2D_t jariTgnKiri2[2]= {
{-165,-40},{-150,-30}
};
point2D_t jariTgnKiri3[2]= {
{-163,-30},{-148,-22}
};
point2D_t kakiKiri1[2]= {
{-105,-105},{-102,-98}
};
point2D_t kakiKiri2[2]= {
{-125,-110},{-115,-95}
};
point2D_t kakiKiri3[2]= {
{-130,-100},{-124,-90}
};
point2D_t kakiKanan1[2]= {
{-92,-140},{-80,-130}
};
point2D_t kakiKanan2[2]= {
{-85,-145},{-78,-132}
};
point2D_t kakiKanan3[2]= {
{-75,-146},{-72,-138}
};


glColor3f(0,0,0);
glPointSize(30); // titik diperbesar 3x
drawGaris(badanKeliling, 74);
drawGaris(garisPerut, 7);
drawGaris(garisHidung1, 4);
drawGaris(garisHidung2, 3);
drawGaris(senyum1, 3);
drawGaris(senyum2, 3);
drawGaris(mulut, 14);
drawGaris(kplBawah, 31);
drawGaris(lidah, 7);
drawGaris(telingaKiri, 8);
drawGaris(kepalaAtas, 26);
drawGaris(tanganKiri, 16);
drawGaris(tanganKanan, 16);
drawGaris(kakiKiri, 13);
drawGaris(kakiKanan, 14);
drawGaris(telingaKanan, 6);
drawGaris(jariTgnKanan1, 5);
drawGaris(jariTgnKanan2, 3);
drawGaris(jariTgnKanan3, 3);
drawGaris(jariTgnKanan4, 3);
drawGaris(jariTgnKiri1, 2);
drawGaris(jariTgnKiri2, 2);
drawGaris(jariTgnKiri3, 2);
drawGaris(kakiKiri1, 2);
drawGaris(kakiKiri2, 2);
drawGaris(kakiKiri3, 2);
drawGaris(kakiKanan1, 2);
drawGaris(kakiKanan2, 2);
drawGaris(kakiKanan3, 2);

}


void userdraw()
{
koordinatSumbu();
kepala();
badan();
mataKiri();
outline();
}



void display( void) {

glClear(GL_COLOR_BUFFER_BIT); //membersihkan semua pixel
userdraw();//memanggil fungsi gambargw()
glutSwapBuffers();

}
int main( int argc, char ** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE|GLUT_RGB);
glutInitWindowPosition( 300,100);
glutInitWindowSize(640,480);
glutCreateWindow ("monyet");
glClearColor( 1.0, 1.0, 1.0, 0.0);
gluOrtho2D(- 320., 320., -240.0,240.0);
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();

return 0;

}

0 comments:

Post a Comment