Membuat Animasi bintang dengan warna Gradasi
#include <GL/glut.h>
typedef struct point2D_t
{
float x;
float y;
};
typedef struct Color_t
{
float r;
float g;
float b;
};
void setColor(Color_t col)
{
glColor3f(col.r, col.g, col.b);
}
void drawPolyline(point2D_t pnt[], int n)
{
int i;
glBegin(GL_LINE_STRIP);
for (i=0;i<n;i++){
glVertex2f(pnt[i].x,pnt[i].y);
}
glEnd();
}
void drawPolygon(point2D_t p[], int n)
{
int i;
glBegin(GL_LINE_LOOP);
for (i=0;i<n;i++){
glVertex2f(p[i].x,p[i].y);
}
glEnd();
}
void fillPolygon(point2D_t p[], Color_t col, int n){
int i;
glBegin(GL_POLYGON);
setColor(col);
for (int i=0;i<n;i++) glVertex2f (p[i].x,p[i].y);
glEnd();
}
void gradatePolygon(point2D_t p[], Color_t col[],int n){
int i;
glBegin(GL_POLYGON);
for (int i=0;i<n;i++){
setColor(col[i]);
glVertex2f(p[i].x,p[i].y);
}
glEnd();
}
void GambarPolyline(int x1,int y1,int x2,int y2,int x3, int y3,int x4, int y4,int x5, int y5,int x6, int y6,int x7, int y7,int x8,int y8, int x9, int y9, int x10, int y10){
glPointSize(4);
glBegin(GL_LINE_STRIP);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glVertex2i(x3,y3);
glVertex2i(x4,y4);
glVertex2i(x5,y5);
glVertex2i(x6,y6);
glVertex2i(x7,y7);
glVertex2i(x8,y8);
glVertex2i(x9,y9);
glVertex2i(x10,y10);
glEnd();
}
//draw area
//void userdraw() {
//static int tick=0;
//set warna (R,G,B{{0,30},{10,10},{35,10},{15,0},{25,-20},{0,-7.5},{-25,-20},{-15,0},{-35,10},{-10,10}}
// glColor3f(0,0,1);
// GambarPolyline(140,0,300,70,460,0,410,150,580,250,370,250,300,380,230,250,30,250,190,140);
/* glColor3f(0,1,0);
GambarPolyline(160,20,300,85,440,20,395,155,540,240,360,240,300,360,240,240,65,240,205,145);
glColor3f(1,1,0);
GambarPolyline(180,40,300,100,420,40,380,160,500,230,350,230,300,340,250,230,100,230,220,150);
glColor3f(1,0,0);
GambarPolyline(200,60,300,115,400,60,365,160,460,220,340,220,300,320,260,220,135,220,235,155);
*/
//}
void latar(){
Color_t merah={1,0,0};
Color_t ijo={0,1,0};
Color_t biru={0,0,1};
Color_t putih={1,1,1};
Color_t Kuning = {1,1,0};
Color_t ungu={0.7,0,1};
Color_t birumuda={0,1,1};
Color_t gradasi1[4]={{0,1,1},{0,0,0},{0,1,1},{1,0,0}};
point2D_t kotak1[4]={{0,190},{0,-20},{300,-20},{300,190}};
gradatePolygon(kotak1,gradasi1,4);
point2D_t kotak1[4]={{300,-20},{600,-20},{600,190},{300,190}};
gradatePolygon(kotak1,gradasi1,4);
point2D_t kotak1[4]={{600,190},{600,400},{300,400},{300,190}};
gradatePolygon(kotak1,gradasi1,4);
point2D_t kotak1[4]={{300,400},{0,400},{0,190},{300,190}};
gradatePolygon(kotak1,gradasi1,4);
}
void userdraw2(){
Color_t merah={1,0,0};
Color_t ijo={0,1,0};
Color_t biru={0,0,1};
Color_t putih={1,1,1};
Color_t Kuning = {1,1,0};
Color_t ungu={0.7,0,1};
Color_t birumuda={0,1,1};
point2D_t segi1[8]={{300,70},{460,0},{410,150},{580,250},{370,250},{300,380},{230,250},{30,250}};
fillPolygon(segi1,biru,8);
point2D_t segi2[8]={{300,85},{440,20},{395,155},{540,240},{360,240},{300,360},{240,240},{65,240}};
fillPolygon(segi2,ijo,8);
point2D_t segi3[8]={{300,100},{420,40},{380,160},{500,230},{350,230},{300,340},{250,230},{100,230}};
fillPolygon(segi3,Kuning,8);
}
void userdraw3(){
Color_t merah={1,0,0};
Color_t ijo={0,1,0};
Color_t biru={0,0,1};
Color_t putih={1,1,1};
Color_t Kuning = {1,1,0};
Color_t ungu={0.7,0,1};
Color_t birumuda={0,1,1};
point2D_t segitiga1[3]={{140,0},{300,70},{200,150}};
fillPolygon(segitiga1,biru,3);
point2D_t segitiga2[3]={{160,20},{300,85},{215,155}};
fillPolygon(segitiga2,ijo,3);
point2D_t segitiga3[3]={{180,40},{300,100},{230,160}};
fillPolygon(segitiga3,Kuning,3);
Color_t gradasi[4]={{0,1,1},{0,0,0},{0,1,1},{1,0,0}};
//(200,60,300,115,400,60,365,160,460,220,340,220,300,320,260,220,135,220,235,155)
point2D_t segitiga4[4]={{235,155},{200,60},{300,115},{300,190}};
gradatePolygon(segitiga4,gradasi,4);
point2D_t segitiga5[4]={{300,115},{400,60},{365,160},{300,190}};
gradatePolygon(segitiga5,gradasi,4);
point2D_t segitiga6[4]={{365,160},{460,220},{340,220},{300,190}};
gradatePolygon(segitiga6,gradasi,4);
point2D_t segitiga7[4]={{340,220},{300,320},{260,220},{300,190}};
gradatePolygon(segitiga7,gradasi,4);
point2D_t segitiga8[4]={{260,220},{135,220},{235,155},{300,190}};
gradatePolygon(segitiga8,gradasi,4);
}
void display(void){
//clear screen
glClear(GL_COLOR_BUFFER_BIT);
latar();
userdraw2();
userdraw3(); //jalanan userdraw()
glutSwapBuffers();
}
int main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowPosition(100,100); //posisi windownya di(100,100)
glutInitWindowSize(640,480); //set ukuran window jadi 640x480
glutCreateWindow("Bintang"); //set judul app
glClearColor(0,0,0,0); //set warna backgroun(r,g,b,a)(range 0-1)
gluOrtho2D(0.,600.,-20.,400.); //set area menggambar
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
terimakasih sudah melihat....
0 komentar: