Sunday, 22 July 2018

membuat projek Hujan 3D dengan openGL C++ di codeblock

ini adalah sebuah projek openGL full sourcode. projek ini membuat sebuah animasi hujan turun dan ditampung didalam tempat penyimpanan. adapun hasil dari projek ini adalah seperti video dibawah ini:


langkah-langkagh untuk membuatnya adalah sebagai berikut.

  1. download terlebih dahulu openGL disini :  View openGL
  2. setting codeblock dengan openGL caranya dapat dilihat disini: view
  3. buka codeblock dan buat projek baru caranya dapat dilihat disini : view
  4. copy semua sourcode yang tersedia di blog ini.
adapun sourcode yang tersedia adalah sebagai berikut ini :


#include<stdio.h>
#include<math.h>
#include<gl/glut.h>
#include<string.h>

char str3[30]="Help:-";
char str4[50]="Press 'q' or 'Q' or 'ESC' to Exit";
char str5[50]="Press 's' or 'S' to Create House";
char str6[50]="press 'r' or 'R' to Rain";
char str7[50]="TURBINE";
char str8[50]="WATER STORAGE";
char str1[30]="CHARCOAL";
char str2[20]="CHLORINE CONTAINER";


#define radian 3.14159/180.0
static GLfloat spin=0.0;
float theta=0.0;
GLfloat x,y;
GLint i;

int l;

void flow2();
void flow3();
void flow4();
void flow5();
void flow6();
void flow7();
void flow8();
void flow9();
void flow10();
void flow11();
void flow12();
void disprectangle();
void disprectangle1();
void disprectangle2();
void drawhouse();
void drawconts();
void drawpump();
void drawtank();
void square();
void rain1();
void circle_draw(GLint,GLint,GLint);
typedef float points[2];
GLfloat count=0;
GLfloat count1=0;
GLfloat house1[10][2]={{-625,255},{-700,400},{-500,375}};
GLfloat house[3][15]={{-700,-700,-550,-550,-550,-400,-400,-675,-675,-625,-625,-700,-550,-400,-550},{-50,150,150,-50,300,300,100,-50,25,25,-50,175,325,325,175},{1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}};
GLfloat conts[3][17]={{-540,-520,-520,-425,-425,-375,-375,-250,-250,-5,-5,10,10,-390,-390,-535,-535},{185,185,-100,-100,-235,-235,-275,-275,-435,-435,-50,-50,-450,-450,-250,-250,165},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
GLfloat pump[3][15]={{-325,-275,-275,-325,-175,-125,-125,-175,-50,30,30,-25,-25,-50,0},{225,225,0,0,225,225,0,0,25,25,-50,-50,-25,-25,0},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
GLfloat tank[3][18]={{-400,-365,-365,-350,-350,-115,-115,-75,-75,-50,-50,-90,-90,-100,-100,-350,-350,-400},{200,200,240,240,360,360,375,375,10,10,-5,-5,360,360,225,225,185,185},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};


/* flow function points */
points waterpoint[]={{-535.0,175.0},{-527.0,175.0}};
points waterpoint2[]={{-527.0,180.0},{-527.0,170.0},{-527.0,160.0},{-527.0,150.0},{-527.0,140.0},{-527.0,130.0},{-527.0,120.0},{-527.0,110.0},{-527.0,100.0},{-527.0,90.0},{-527.0,80.0},{-527.0,70.0},{-527.0,60.0},{-527.0,50.0},{-527.0,40.0},{-527.0,30.0},{-527.0,20.0},{-527.0,10.0},{-527.0,0.0},{-527.0,-10.0},{-527.0,-20.0},{-527.0,-30.0},{-527.0,-40.0},{-527.0,-50.0},{-527.0,-60.0},{-527.0,-70.0},{-527.0,-80.0},{-527.0,-90.0},{-527.0,-100.0},{-527.0,-110.0},{-527.0,-120.0},{-527.0,-130.0},{-527.0,-140.0},{-527.0,-150.0},{-527.0,-160.0},{-527.0,-170.0}};
points waterpoint3[]={{-435.0,-243.0},{-425.0,-243.0},{-415.0,-243.0},{-405.0,-243.0},{-395.0,-243.0},{-385.0,-243.0}};
points waterpoint4[]={{-383.0,-440.0},{-383.0,-430.0},{-383.0,-420.0},{-383.0,-410.0},{-383.0,-400.0},{-383.0,-390.0},{-383.0,-380.0},{-383.0,-370.0},{-383.0,-360.0},{-383.0,-350.0},{-383.0,-340.0},{-383.0,-330.0},{-383.0,-320.0},{-383.0,-310.0},{-383.0,-300.0},{-383.0,-290.0},{-383.0,-280.0},{-383.0,-270.0},{-383.0,-260.0},{-383.0,-250.0}};
points waterpoint5[]={{-250.0,-443.0},{-240.0,-443.0},{-230.0,-443.0},{-220.0,-443.0},{-210.0,-443.0},{-200.0,-443.0},{-190.0,-443.0},{-180.0,-443.0},{-170.0,-443.0},{-160.0,-443.0},{-150.0,-443.0},{-140.0,-443.0},{-130.0,-443.0},{-120.0,-443.0},{-110.0,-443.0},{-100.0,-443.0},{-90.0,-443.0},{-80.0,-443.0},{-70.0,-443.0},{-60.0,-443.0},{-50.0,-443.0},{-40.0,-443.0},{-30.0,-443.0},{-20.0,-443.0},{-10.0,-443.0},{-0.0,-443.0}};
points waterpoint6[]={{3.0,-440.0},{3.0,-430.0},{3.0,-420.0},{3.0,-410.0},{3.0,-400.0},{3.0,-390.0},{3.0,-380.0},{3.0,-370.0},{3.0,-360.0},{3.0,-350.0},{3.0,-340.0},{3.0,-330.0},{3.0,-320.0},{3.0,-310.0},{3.0,-300.0},{3.0,-290.0},{3.0,-280.0},{3.0,-270.0},{3.0,-260.0},{3.0,-250.0},{3.0,-240.0},{3.0,-230.0},{3.0,-220.0},{3.0,-210.0},{3.0,-200.0},{3.0,-190.0},{3.0,-180.0},{3.0,-170.0},{3.0,-160.0},{3.0,-150.0},{3.0,-140.0},{3.0,-130.0},{3.0,-120.0},{3.0,-110.0},{3.0,-100.0},{3.0,-90.0},{3.0,-80.0},{3.0,-70.0},{3.0,-60.0},{3.0,-50.0}};
points waterpoint7[]={{-55.0,3.0},{-63.0,3.0},{-70.0,3.0},{-77.0,3.0},};
points waterpoint8[]={{-83.0,10.0},{-83.0,20.0},{-83.0,30.0},{-83.0,40.0},{-83.0,50.0},{-83.0,60.0},{-83.0,70.0},{-83.0,80.0},{-83.0,90.0},{-83.0,100.0},{-83.0,110.0},{-83.0,120.0},{-83.0,130.0},{-83.0,140.0},{-83.0,150.0},{-83.0,160.0},{-83.0,170.0},{-83.0,180.0},{-83.0,190.0},{-83.0,200.0},{-83.0,210.0},{-83.0,220.0},{-83.0,230.0},{-83.0,240.0},{-83.0,250.0},{-83.0,260.0},{-83.0,270.0},{-83.0,280.0},{-83.0,290.0},{-83.0,300.0},{-83.0,310.0},{-83.0,320.0},{-83.0,330.0},{-83.0,340.0},{-83.0,350.0},{-83.0,360.0},};
points waterpoint9[]={{-85.0,367.0},{-95.0,367.0},{-105.0,367.0}};
points waterpoint10[]={{-107.0,370.0},{-107.0,360.0},{-107.0,350.0},{-107.0,340.0},{-107.0,330.0},{-107.0,320.0},{-107.0,310.0},{-107.0,300.0},{-107.0,290.0},{-107.0,280.0},{-107.0,270.0},{-107.0,260.0},{-107.0,250.0},{-107.0,240.0},{-107.0,230.0}};
points waterpoint11[]={{-110.0,230.0},{-120.0,230.0},{-130.0,230.0},{-140.0,230.0},{-150.0,230.0},{-160.0,230.0},{-170.0,230.0},{-180.0,230.0},{-190.0,230.0},{-200.0,230.0},{-210.0,230.0},{-220.0,230.0},{-230.0,230.0},{-240.0,230.0},{-250.0,230.0},{-260.0,230.0},{-270.0,230.0},{-280.0,230.0},{-290.0,230.0},{-300.0,230.0},{-310.0,230.0},{-320.0,230.0},{-330.0,230.0},{-340.0,230.0}};
points waterpoint12[]={{-357.0,225.0},{-357.0,215.0},{-357.0,205.0},{-357.0,195.0}};
points rectangle[]={{-390.0,-450.0},{-250.0,-450.0},{-250.0,-440.0},{-390.0,-440.0}};
points rectangle1[]={{-350.0,225.0},{-100.0,225.0},{-100.0,235.0},{-350.0,235.0}};
points rectangle2[]={{-535.0,-165.0},{-425.0,-165.0},{-425.0,-170.0},{-535.0,-170.0}};

/*flow functions*/
 void flow1()
{
int k,l;
glPointSize(4.0);
for( k=1;k>=0;k--)
{
if(waterpoint[k][0]==-527.0)
{
waterpoint[k][0]=-535.0;
}
}
for(i=1;i>=0;i--)
{

waterpoint[i][0]=waterpoint[i][0]+0.5;
}
for( l=1;l>=0;l--)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint[l]);
glEnd();
}
}
void flow2()
{

int k,l;
glPointSize(4.0);
for( k=35;k>=0;k--)
{
if(waterpoint2[k][1]==-170.0)
{
waterpoint2[k][1]=175.0;
}
}
    for(i=35;i>=0;i--)
{

waterpoint2[i][1]=waterpoint2[i][1]-0.5;
}
for( l=0;l<=35;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint[l]);
glEnd();
}

}

void flow3()
{
int k,l;
glPointSize(4.0);
for( k=5;k>=0;k--)
{
if(waterpoint3[k][0]==-385.0)
{
waterpoint3[k][0]=-435.0;
}
}
for(i=5;i>=0;i--)
{

waterpoint3[i][0]=waterpoint3[i][0]+0.5;
}
for( l=5;l>=0;l--)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint3[l]);
glEnd();
}

}
void flow4()
{
int k,l;
glPointSize(4.0);
for( k=20;k>=0;k--)
{
if(waterpoint4[k][1]==-440.0)
{
waterpoint4[k][1]=-250.0;
}
}
for(i=20;i>=0;i--)
{

waterpoint4[i][1]=waterpoint4[i][1]-0.5;
}
for( l=0;l<=19;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint4[l]);
glEnd();
}

}



void flow5()
{
int k,l;
glPointSize(4.0);
for( k=26;k>=0;k--)
{
if(waterpoint5[k][0]==0.0)
{
waterpoint5[k][0]=-250.0;
}
}
for(i=0;i<=26;i++)
{

waterpoint5[i][0]=waterpoint5[i][0]+0.5;
}
for( l=1;l<=25;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint5[l]);
glEnd();
}

}
void flow6()
{
int k,l;
glPointSize(4.0);
for( k=40;k>=0;k--)
{
if(waterpoint6[k][1]==-50.0)
{
waterpoint6[k][1]=-440.0;
}
}

for( l=1;l<=39;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint6[l]);
glEnd();
}
for(i=39;i>=0;i--)
{

waterpoint6[i][1]=waterpoint6[i][1]+0.5;
}

}
void flow7()
{
int k,l;
glPointSize(4.0);
for( k=4;k>=0;k--)
{
if(waterpoint7[k][0]==-77.0)
{
waterpoint7[k][0]=-55.0;
}
}

for( l=3;l>=0;l--)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint7[l]);
glEnd();
}
for(i=4;i>=0;i--)
{

waterpoint7[i][0]=waterpoint7[i][0]-0.5;
}

}
void flow8()
{
int k,l;
glPointSize(4.0);
for( k=36;k>=0;k--)
{
if(waterpoint8[k][1]==360.0)
{
waterpoint8[k][1]=10.0;
}
}

for( l=1;l<=36;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint8[l]);
glEnd();
}
for(i=35;i>=0;i--)
{

waterpoint8[i][1]=waterpoint8[i][1]+0.5;
}

}
void flow9()
{
int k,l;
glPointSize(4.0);
for( l=3;l>=1;l--)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint9[l]);
glEnd();
}
for( k=3;k>=0;k--)
{
if(waterpoint9[k][0]==-105.0)
{
waterpoint9[k][0]=-85.0;
}
}
for(i=2;i>=0;i--)
{

waterpoint9[i][0]=waterpoint9[i][0]-0.5;
}

}
void flow10()
{
int k,l;
glPointSize(4.0);
for( k=15;k>=0;k--)
{
if(waterpoint10[k][1]==230.0)
{
waterpoint10[k][1]=370.0;
}
}

for( l=1;l<=15;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint10[l]);
glEnd();
}
for(i=14;i>=0;i--)
{

waterpoint10[i][1]=waterpoint10[i][1]-0.5;
}
if(count>1730)
flow11();
}
void flow11()
{
int k,l;
glPointSize(4.0);
for( l=24;l>=0;l--)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint11[l]);
glEnd();
}
for(k=24;k>=0;k--)
{
if(waterpoint11[k][0]==-340.0)
{
waterpoint11[k][0]=-110.0;
}
}
for(i=23;i>=0;i--)
{

waterpoint11[i][0]=waterpoint11[i][0]-0.5;
}
if(count>1760)
flow12();
}
void flow12()
{
int k,l;
glPointSize(4.0);
for( k=4;k>=0;k--)
{
if(waterpoint12[k][1]==190.0)
{
waterpoint12[k][1]=225.0;
}
}

for( l=1;l<=4;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.25,1.0);
glVertex2fv(waterpoint12[l]);
glEnd();
}
for(i=3;i>=0;i--)
{

waterpoint12[i][1]=waterpoint12[i][1]-0.5;
}

}

void disprectangle1()
{
int l;
glColor3f(0.0,0.25,1.0);
glBegin(GL_POLYGON);
    glVertex2f(-400.0,185.0);
glVertex2f(-350.0,185.0);
glVertex2f(-350.0,200.0);
glVertex2f(-400.0,200.0);
glEnd();
glBegin(GL_POLYGON);
    glVertex2f(-350.0,200.0);
glVertex2f(-350.0,240.0);
glVertex2f(-365.0,240.0);
glVertex2f(-365.0,200.0);
glEnd();

glBegin(GL_POLYGON);
for( l=0;l<=3;l++)
{
glVertex2fv(rectangle1[l]);
}
glEnd();
if(count>1730 && count<4500)
{
rectangle1[2][1]=rectangle1[2][1]+0.04;
rectangle1[3][1]=rectangle1[3][1]+0.04;
}
}

/*rainwater points*/
void rain1();

void rain3();

void rain5();
void rain7();
void rain9();
void rain11();
void rain13();
void rain15();
void rain17();
void rain19();
void rain21();
void rain23();
void rain25();
void rain27();
void rain29();
void rain31();
void rain33();
void rain35();


points rainpoint1[]={{-745.0,400.0},{-745.0,380.0},{-745.0,360.0},{-745.0,340.0},{-745.0,320.0},{-745.0,300.0},{-745.0,280.0},{-745.0,260.0},{-745.0,240.0},{-745.0,220.0},{-745.0,200.0},{-745.0,180.0},{-745.0,160.0},{-745.0,140.0},{-745.0,120.0},{-745.0,100.0},{-745.0,80.0},{-745.0,60.0},{-745.0,40.0},{-745.0,20.0},{-745.0,0.0},{-745.0,-20.0},{-745.0,-40.0},{-745.0,-60.0}};
points rainpoint3[]={{-705.0,400.0},{-705.0,380.0},{-705.0,360.0},{-705.0,340.0},{-705.0,320.0},{-705.0,300.0},{-705.0,280.0},{-705.0,260.0},{-705.0,240.0},{-705.0,220.0},{-705.0,200.0},{-705.0,180.0},{-705.0,160.0},{-705.0,140.0},{-705.0,120.0},{-705.0,100.0},{-705.0,80.0},{-705.0,60.0},{-705.0,40.0},{-705.0,20.0},{-705.0,0.0},{-705.0,-20.0},{-705.0,-40.0},{-705.0,-60.0}};
points rainpoint5[]={{-665.0,400.0},{-665.0,380.0},{-665.0,360.0},{-665.0,340.0},{-665.0,320.0},{-665.0,300.0},{-665.0,280.0},{-665.0,260.0},{-665.0,240.0},{-665.0,220.0},{-665.0,200.0},{-665.0,180.0}};
points rainpoint7[]={{-625.0,400.0},{-625.0,380.0},{-625.0,360.0},{-625.0,340.0},{-625.0,320.0},{-625.0,300.0},{-625.0,280.0},{-625.0,260.0},{-625.0,240.0},{-625.0,220.0},{-625.0,200.0},{-625.0,180.0}};
points rainpoint9[]={{-585.0,400.0},{-585.0,380.0},{-585.0,360.0},{-585.0,340.0},{-585.0,320.0},{-585.0,300.0},{-585.0,280.0},{-585.0,260.0},{-585.0,240.0},{-585.0,220.0},{-585.0,200.0},{-585.0,180.0}};
points rainpoint11[]={{-545.0,400.0},{-545.0,380.0},{-545.0,360.0},{-545.0,340.0},{-545.0,320.0},{-545.0,300.0},{-545.0,280.0},{-545.0,260.0},{-545.0,240.0},{-545.0,220.0},{-545.0,200.0},{-545.0,180.0}};
points rainpoint13[]={{-505.0,400.0},{-505.0,380.0},{-505.0,360.0},{-505.0,340.0},{-505.0,320.0},{-505.0,300.0},{-505.0,280.0},{-505.0,260.0},{-505.0,240.0},{-505.0,220.0},{-505.0,200.0},{-505.0,180.0},{-505.0,160.0},{-505.0,140.0},{-505.0,120.0},{-505.0,100.0},{-505.0,80.0},{-505.0,60.0},{-505.0,40.0},{-505.0,20.0},{-505.0,0.0},{-505.0,-20.0},{-505.0,-40.0},{-505.0,-60.0}};
points rainpoint15[]={{-465.0,400.0},{-465.0,380.0},{-465.0,360.0},{-465.0,340.0},{-465.0,320.0},{-465.0,300.0},{-465.0,280.0},{-465.0,260.0},{-465.0,240.0},{-465.0,220.0},{-465.0,200.0},{-465.0,180.0},{-465.0,160.0},{-465.0,140.0},{-465.0,120.0},{-465.0,100.0},{-465.0,80.0},{-465.0,60.0},{-465.0,40.0},{-465.0,20.0},{-465.0,0.0},{-465.0,-20.0},{-465.0,-40.0},{-465.0,-60.0}};
points rainpoint17[]={{-425.0,400.0},{-425.0,380.0},{-425.0,360.0},{-425.0,340.0},{-425.0,320.0},{-425.0,300.0},{-425.0,280.0},{-425.0,260.0},{-425.0,240.0},{-425.0,220.0},{-425.0,200.0},{-425.0,180.0},{-425.0,160.0},{-425.0,140.0},{-425.0,120.0},{-425.0,100.0},{-425.0,80.0},{-425.0,60.0},{-425.0,40.0},{-425.0,20.0},{-425.0,0.0},{-425.0,-20.0},{-425.0,-40.0},{-425.0,-60.0}};
points rainpoint19[]={{-385.0,400.0},{-385.0,380.0},{-385.0,360.0},{-385.0,340.0},{-385.0,320.0},{-385.0,300.0},{-385.0,280.0},{-385.0,260.0},{-385.0,240.0},{-385.0,220.0},{-385.0,200.0},{-385.0,180.0},{-385.0,160.0},{-385.0,140.0},{-385.0,120.0},{-385.0,100.0},{-385.0,80.0},{-385.0,60.0},{-385.0,40.0},{-385.0,20.0},{-385.0,0.0},{-385.0,-20.0},{-385.0,-40.0},{-385.0,-60.0}};
points rainpoint21[]={{-555.0,400.0},{-555.0,380.0},{-555.0,360.0},{-555.0,340.0},{-555.0,320.0},{-555.0,300.0},{-555.0,280.0}};
points rainpoint23[]={{-515.0,400.0},{-515.0,380.0},{-515.0,360.0},{-515.0,340.0},{-515.0,320.0},{-515.0,300.0},{-515.0,280.0}};
points rainpoint25[]={{-475.0,400.0},{-475.0,380.0},{-475.0,360.0},{-475.0,340.0},{-475.0,320.0},{-475.0,300.0},{-475.0,280.0}};
points rainpoint27[]={{-435.0,400.0},{-435.0,380.0},{-435.0,360.0},{-435.0,340.0},{-435.0,320.0},{-435.0,300.0},{-435.0,280.0}};
points rainpoint29[]={{-695.0,400.0},{-695.0,380.0},{-695.0,360.0},{-695.0,340.0},{-695.0,320.0},{-695.0,300.0},{-695.0,280.0},{-695.0,260.0},{-695.0,240.0},{-695.0,220.0},{-695.0,200.0},{-695.0,180.0}};
points rainpoint31[]={{-655.0,400.0},{-655.0,380.0},{-655.0,360.0},{-655.0,340.0},{-655.0,320.0},{-655.0,300.0},{-655.0,280.0},{-655.0,260.0},{-655.0,240.0},{-655.0,220.0},{-655.0,200.0}};
points rainpoint33[]={{-615.0,400.0},{-615.0,380.0},{-615.0,360.0},{-615.0,340.0},{-615.0,320.0},{-615.0,300.0},{-615.0,280.0},{-615.0,260.0},{-615.0,240.0}};
points rainpoint35[]={{-575.0,400.0},{-575.0,380.0},{-575.0,360.0},{-575.0,340.0},{-575.0,320.0},{-575.0,300.0},{-575.0,280.0},{-575.0,260.0}};




/*rain function*/

void rain1()
{
int k,l;
glPointSize(4.0);
for( k=24;k>=0;k--)
{
if(rainpoint1[k][1]==-60.0)
{
rainpoint1[k][1]=400.0;
}
}

for( l=1;l<=24;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint1[l]);
glEnd();
}
for(i=23;i>=0;i--)
{

rainpoint1[i][1]=rainpoint1[i][1]-2;
}
rain3();
}

void rain3()
{
int k,l;
glPointSize(4.0);
for( k=24;k>=0;k--)
{
if(rainpoint3[k][1]==-60.0)
{
rainpoint3[k][1]=400.0;
}
}

for( l=1;l<=24;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint3[l]);
glEnd();
}
for(i=23;i>=0;i--)
{

rainpoint3[i][1]=rainpoint3[i][1]-2;
}
rain5();
}
void rain5()
{
int k,l;
glPointSize(4.0);
for( k=12;k>=0;k--)
{
if(rainpoint5[k][1]==160.0)
{
rainpoint5[k][1]=400.0;
}
}

for( l=1;l<=12;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint5[l]);
glEnd();
}
for(i=11;i>=0;i--)
{

rainpoint5[i][1]=rainpoint5[i][1]-2;
}
rain7();
}

void rain7()
{
int k,l;
glPointSize(4.0);
for( k=12;k>=0;k--)
{
if(rainpoint7[k][1]==160.0)
{
rainpoint7[k][1]=400.0;
}
}

for( l=1;l<=12;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint7[l]);
glEnd();
}
for(i=11;i>=0;i--)
{

rainpoint7[i][1]=rainpoint7[i][1]-2;
}
rain9();
}

void rain9()
{
int k,l;
glPointSize(4.0);
for( k=12;k>=0;k--)
{
if(rainpoint9[k][1]==160.0)
{
rainpoint9[k][1]=400.0;
}
}

for( l=1;l<=12;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint9[l]);
glEnd();
}
for(i=11;i>=0;i--)
{

rainpoint9[i][1]=rainpoint9[i][1]-1;
}
rain11();
}

void rain11()
{
int k,l;
glPointSize(4.0);
for( k=12;k>=0;k--)
{
if(rainpoint11[k][1]==160.0)
{
rainpoint11[k][1]=400.0;
}
}

for( l=1;l<=12;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint11[l]);
glEnd();
}
for(i=11;i>=0;i--)
{

rainpoint11[i][1]=rainpoint11[i][1]-1;
}
rain13();
}


void rain13()
{
int k,l;
glPointSize(4.0);
for( k=11;k>=0;k--)
{
if(rainpoint13[k][1]==200.0)
{
rainpoint13[k][1]=400.0;
}
}

for( l=1;l<=11;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint13[l]);
glEnd();
}
for(i=13;i>=0;i--)
{

rainpoint13[i][1]=rainpoint13[i][1]-2;
}

rain15();
}
void rain15()
{
int k,l;
glPointSize(4.0);
for( k=8;k>=0;k--)
{
if(rainpoint15[k][1]==240.0)
{
rainpoint15[k][1]=400.0;
}
}

for( l=1;l<=8;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint15[l]);
glEnd();
}
for(i=23;i>=0;i--)
{

rainpoint15[i][1]=rainpoint15[i][1]-1;
}
rain17();
}

void rain17()
{
int k,l;
glPointSize(4.0);
for( k=6;k>=0;k--)
{
if(rainpoint17[k][1]==300.0)
{
rainpoint17[k][1]=400.0;
}
}

for( l=1;l<=6;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint17[l]);
glEnd();
}
for(i=23;i>=0;i--)
{

rainpoint17[i][1]=rainpoint17[i][1]-1;
}

rain19();
}

void rain19()
{
int k,l;
glPointSize(4.0);
for( k=24;k>=0;k--)
{
if(rainpoint19[k][1]==-60.0)
{
rainpoint19[k][1]=400.0;
}
}

for( l=1;l<=24;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint19[l]);
glEnd();
}
for(i=23;i>=0;i--)
{

rainpoint19[i][1]=rainpoint19[i][1]-1;
}
rain21();
}

void rain21()
{
int k,l;
glPointSize(4.0);
for( k=7;k>=0;k--)
{
if(rainpoint21[k][1]==280.0)
{
rainpoint21[k][1]=400.0;
}
}

for( l=1;l<=7;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint21[l]);
glEnd();
}
for(i=11;i>=0;i--)
{

rainpoint21[i][1]=rainpoint21[i][1]-2;
}
rain23();
}

void rain23()
{
int k,l;
glPointSize(4.0);
for( k=7;k>=0;k--)
{
if(rainpoint23[k][1]==280.0)
{
rainpoint23[k][1]=400.0;
}
}

for( l=1;l<=7;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint23[l]);
glEnd();
}
for(i=6;i>=0;i--)
{

rainpoint23[i][1]=rainpoint23[i][1]-2;
}
rain25();
}

void rain25()
{
int k,l;
glPointSize(4.0);
for( k=7;k>=0;k--)
{
if(rainpoint25[k][1]==280.0)
{
rainpoint25[k][1]=400.0;
}
}

for( l=1;l<=7;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint25[l]);
glEnd();
}
for(i=6;i>=0;i--)
{

rainpoint25[i][1]=rainpoint25[i][1]-2;
}
rain27();
}

void rain27()
{
int k,l;
glPointSize(4.0);
for( k=7;k>=0;k--)
{
if(rainpoint27[k][1]==280.0)
{
rainpoint27[k][1]=400.0;
}
}

for( l=1;l<=7;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint27[l]);
glEnd();
}
for(i=6;i>=0;i--)
{

rainpoint27[i][1]=rainpoint27[i][1]-1;
}
rain29();
}

void rain29()
{
int k,l;
glPointSize(4.0);
for( k=12;k>=0;k--)
{
if(rainpoint29[k][1]==200.0)
{
rainpoint29[k][1]=400.0;
}
}

for( l=1;l<=12;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint29[l]);
glEnd();
}
for(i=11;i>=0;i--)
{

rainpoint29[i][1]=rainpoint29[i][1]-1;
}
rain31();
}

void rain31()
{
int k,l;
glPointSize(4.0);
for( k=11;k>=0;k--)
{
if(rainpoint31[k][1]==200.0)
{
rainpoint31[k][1]=400.0;
}
}

for( l=1;l<=11;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint31[l]);
glEnd();
}
for(i=10;i>=0;i--)
{

rainpoint31[i][1]=rainpoint31[i][1]-1;
}
rain33();
}

void rain33()
{
int k,l;
glPointSize(4.0);
for( k=11;k>=0;k--)
{
if(rainpoint33[k][1]==180.0)
{
rainpoint33[k][1]=400.0;
}
}

for( l=1;l<=11;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint33[l]);
glEnd();
}
for(i=7;i>=0;i--)
{

rainpoint33[i][1]=rainpoint33[i][1]-1;
}
rain35();
}

void rain35()
{
int k,l;
glPointSize(4.0);
for( k=5;k>=0;k--)
{
if(rainpoint35[k][1]==300.0)
{
rainpoint35[k][1]=400.0;
}
}

for( l=1;l<=8;l++)
{
glBegin(GL_POINTS);
glColor3f(0.0,0.5,1.0);
glVertex2fv(rainpoint35[l]);
glEnd();
}
for(i=7;i>=0;i--)
{

rainpoint35[i][1]=rainpoint35[i][1]-1;
}

}


/* HOUSE AND OTHERS*/


void drawconts()
{
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINE_STRIP);
for(i=0;i<=16;i++)
{
   glVertex2f(conts[0][i],conts[1][i]);
}
glEnd();
if(count1>60)
drawpump();
}
void drawhouse()
{

    count1=count1+1;
glColor3f(0.5,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(house[0][0],house[1][0]);
glVertex2f(house[0][1],house[1][1]);
glVertex2f(house[0][3],house[1][2]);
glVertex2f(house[0][3],house[1][3]);
glEnd();

glColor3f(0.5,1.0,0.5);//door
glBegin(GL_POLYGON);
glVertex2f(house[0][7],house[1][7]);
glVertex2f(house[0][8],house[1][8]);
glVertex2f(house[0][9],house[1][9]);
glVertex2f(house[0][10],house[1][10]);
glEnd();


glColor3f(1.0,0.0,0.5);
glBegin(GL_POLYGON);
glVertex2f(house[0][2],house[1][2]);
glVertex2f(house[0][5],house[1][5]);
glVertex2f(house[0][6],house[1][6]);
glVertex2f(house[0][3],house[1][3]);
glEnd();

glColor3f(1.0,0.5,0.0);//reqd
glBegin(GL_POLYGON);
glVertex2f(house[0][1],house[1][1]);
glVertex2f(house[0][11],house[1][11]);
glVertex2f(house[0][14],house[1][14]);
glVertex2f(house[0][2],house[1][2]);
glEnd();

glBegin(GL_POLYGON);
glVertex2f(house[0][1],house[1][1]);
glVertex2f(house[0][11],house[1][11]);
glVertex2f(house[0][12],house[1][12]);
glVertex2f(house[0][4],house[1][4]);
glEnd();


glBegin(GL_POLYGON);
glVertex2f(house[0][12],house[1][12]);
glVertex2f(house[0][13],house[1][13]);
glVertex2f(house[0][5],house[1][5]);
glVertex2f(house[0][4],house[1][4]);
glEnd();

glBegin(GL_POLYGON);
glVertex2f(house[0][14],house[1][14]);
glVertex2f(house[0][13],house[1][13]);
glVertex2f(house[0][5],house[1][5]);
glVertex2f(house[0][2],house[1][2]);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
  glVertex2f(house[0][14],house[1][14]);
glVertex2f(house[0][13],house[1][13]);
glVertex2f(house1[2][0],house1[2][1]);
glVertex2f(house1[0][0],house1[0][1]);

glEnd();
glColor3f(.5,0.0,.5);
glBegin(GL_POLYGON);
glVertex2f(house[0][11],house[1][11]);
glVertex2f(house[0][14],house[1][14]);
glVertex2f(house1[0][0],house1[0][1]);
glEnd();
if(count1>30)
drawconts();
}
void drawpump()
{
glColor3f(.25,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(pump[0][0],pump[1][0]);
glVertex2f(pump[0][1],pump[1][1]);
glVertex2f(pump[0][2],pump[1][2]);
glVertex2f(pump[0][3],pump[1][3]);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(pump[0][4],pump[1][4]);
glVertex2f(pump[0][5],pump[1][5]);
glVertex2f(pump[0][6],pump[1][6]);
glVertex2f(pump[0][7],pump[1][7]);
glEnd();
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(pump[0][8],pump[1][8]);
glVertex2f(pump[0][9],pump[1][9]);
glVertex2f(pump[0][10],pump[1][10]);
glVertex2f(pump[0][11],pump[1][11]);
glVertex2f(pump[0][12],pump[1][12]);
glVertex2f(pump[0][13],pump[1][13]);
glEnd();
glEnd();
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2f(pump[0][14],pump[1][14]);
glEnd();
if(count1>90)
drawtank();
}
void drawtank()
{
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINE_LOOP);
for(i=0;i<=17;i++)
{
    glVertex2f(tank[0][i],tank[1][i]);
}
glEnd();
circle_draw(-525,-240,10);
circle_draw(-505,-240,10);
circle_draw(-485,-240,10);
circle_draw(-465,-240,10);
circle_draw(-525,-200,10);
circle_draw(-505,-200,10);
circle_draw(-485,-200,10);
circle_draw(-465,-200,10);
circle_draw(-520,-220,10);
circle_draw(-500,-220,10);
circle_draw(-480,-220,10);
circle_draw(-460,-220,10);
circle_draw(-520,-180,10);
circle_draw(-500,-180,10);
circle_draw(-480,-180,10);
circle_draw(-460,-180,10);
circle_draw(-445,-240,10);
circle_draw(-445,-200,10);
circle_draw(-440,-220,10);
circle_draw(-440,-180,10);

}
void bitmap()
{
int a,p,b,o;
glColor3f(1.0,0.0,0.0);
glRasterPos2i(80,350);
for( a=0;a<strlen(str3);a++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,str3[a]);
glColor3f(0.0,1.0,0.0);
glRasterPos2i(100,300);
for( p=0;p<strlen(str5);p++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,str5[p]);
glColor3f(0.0,0.0,1.0);
glRasterPos2i(100,250);
for( b=0;b<strlen(str6);b++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,str6[b]);
glColor3f(1.0,0.0,1.0);
glRasterPos2i(100,200);
for( o=0;o<strlen(str4);o++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,str4[o]);

}
void bitmap1()
{
int l,z,k,m;
glColor3f(1.0,0.0,0.0);

glRasterPos2i(-350,-150);
for( l=0;l<strlen(str1);l++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,str1[l]);
glRasterPos2i(-190,-200);
for( z=0;z<strlen(str2);z++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,str2[z]);
glRasterPos2i(-190,-100);
for( k=0;k<strlen(str7);k++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,str7[k]);

/*glRasterPos2i(-240,250);
for( m=0;m<strlen(str8);m++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,str8[m]);*/


glRasterPos2i(-340,-60);
for( m=0;m<strlen(str8);m++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,str8[m]);
}
void line()
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2f(-440,-180);
glVertex2f(-360,-155);
glVertex2f(-280,-310);
glVertex2f(-200,-210);
glVertex2f(-160,-90);
glVertex2f(-50,-30);

glVertex2f(-290,-40);
glVertex2f(-240,250);

glEnd();
}
void draw_pixel(GLint cx,GLint cy)
{
glColor3f(0.5,0.5,0.5);
glBegin(GL_POINTS);
glVertex2i(cx,cy);
glEnd();
}
void plotpixels(GLint h,GLint k,GLint x,GLint y)
{
draw_pixel(x+h,y+k);
draw_pixel(-x+h,y+k);
draw_pixel(x+h,-y+k);
draw_pixel(-x+h,-y+k);
draw_pixel(y+h,x+k);
draw_pixel(-y+h,x+k);
draw_pixel(y+h,-x+k);
draw_pixel(-y+h,-x+k);
}
void circle_draw(GLint h,GLint k,GLint r)
{
GLint d=1-r,x=0,y=r;
while(y>x)
{
plotpixels(h,k,x,y);
if(d<0) d+=2*x+3;
else
{
d+=2*(x-y)+5;
--y;
}
++x;
}
plotpixels(h,k,x,y);
}

void square()
{
glColor3f(0.0,1.0,1.0);
glBegin(GL_QUADS);
glVertex2f(x,y);
glVertex2f(-y,x);
glVertex2f(-x,-y);
glVertex2f(y,-x);
glEnd();
}
void disprectangle2()
{
glColor3f(0.0,0.25,1.0);
glBegin(GL_POLYGON);
for(l=0;l<=3;l++)
{
glVertex2fv(rectangle2[l]);
}
glEnd();
if(count>450 && count<600)
{
rectangle2[1][1]=rectangle2[1][1]+0.07;
rectangle2[0][1]=rectangle2[0][1]+0.07;
}
if(count>3500&&count<3660)
{
rectangle2[1][1]=rectangle2[1][1]-0.07;
rectangle2[0][1]=rectangle2[0][1]-0.07;
}

}
void disprectangle()
{
glColor3f(0.0,0.25,1.0);

glBegin(GL_POLYGON);
for(l=0;l<=3;l++)
{
glVertex2fv(rectangle[l]);
}
glEnd();
if(count>540 && count<1500)
{
rectangle[2][1]=rectangle[2][1]+0.05;
rectangle[3][1]=rectangle[3][1]+0.05;
}
}
void disprec();
void disprec()
{

if(count>3770 && count<4400)
{
rectangle[2][1]=rectangle[2][1]-0.06;
rectangle[3][1]=rectangle[3][1]-0.06;
}
glColor3f(0.0,0.25,1.0);
glBegin(GL_POLYGON);
glVertex2fv(rectangle[0]);
glVertex2fv(rectangle[1]);
glVertex2fv(rectangle[2]);
glVertex2fv(rectangle[3]);
glEnd();
}
void displayd()
{
glClear(GL_COLOR_BUFFER_BIT);
square();
glutSwapBuffers();
}
unsigned char key2;

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0);
bitmap();
if(key2=='q'||key2=='Q') exit(0);
if(key2=='s'||key2=='S')
{
drawhouse();
if(count1>150)
{
line();
bitmap1();
}
}
if(key2=='r'||key2=='R')
{
count=count+1;
drawhouse();
bitmap();
line();
bitmap1();


if(count<2500)
{
rain1();
}
if(count>370&&count<3600)
flow1();
if(count<3620)
{
  flow2();
  if(count>300&&count<4030)
  {
  disprectangle2();

  }
}
if(count>3770 && count<4200)
disprec();
if(count>500&&count<3730)
flow3();
if(count>520&&count<3750)
flow4();
if(count>540&&count<3770)
disprectangle();
if(count>1530&&count<4250)
flow5();
if(count>1500&&count<4480)
{
square();

}
if(count>1560&&count<4300)
flow6();
if(count>1600&&count<4320)
flow7();
if(count>1620&&count<4340)
flow8();
if(count>1660&&count<4360)
flow9();
if(count>1690&&count<4380)
flow10();
if(count>1790) disprectangle1();

}
glFlush();
glutSwapBuffers();
}

void mykey(unsigned char key,int x,int y)
{
   key2=key;
if(key==27) exit(0);
   if(key=='q'||key=='Q') exit(0);
   if(key=='s'||key=='S') {
drawhouse();
bitmap1();
}
   if(key=='r'||key=='R')
   {
  count=150;
  drawhouse();
  rain1();
  rain35();
   }
}
void myinit()
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-750.0,400.0,-500.0,400.0);
}
void idle()
{
theta=theta+15.0;
if(theta>360.0)
theta=theta-360.0;
x=18.0*cos(radian*theta);
y=18.0*sin(radian*theta);
bitmap();
glutPostRedisplay();
}

int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGB);
    glutInitWindowPosition(0,0);
    glutInitWindowSize(500,500);
    glutCreateWindow("RAIN WATER HARVESTING");
myinit();
glutDisplayFunc(display);
    glutIdleFunc(idle);
glutKeyboardFunc(mykey);
glutMainLoop();
}



hasil gambar nya bila dijalankan akan menghasilkan bentuk sperti ini :
untuk memulainya adalah sebagai berikut:
S = untuk menampilkan rumah dan lain-lainnya
R = untuk menurunkan air hujannya
Q = untuk exit dari animasi tersebut.



demikian semoga bermanfaat dan dapat dimanfaatkan dengan sebaik-baiknya.

Daftar Pustaka
  1. https://www.openglprojects.in/2017/04/rain-water-harvesting-cg-projects.html#gsc.tab=0




Contoh Projek 3D lainnya.


Baca Juga Projek OpenGL dengan C++ Lainnya  dibawah ini :

 Contoh Projek 2D openGL

Baca juga Animasi 2D untuk projek openGL lainnya dibawah ini

Dasar OpenGL

Baca juga Dasar Dari OpenGL dibawah ini jika kalian ingin menekuninya.


Sunday, 15 July 2018

OpenGL projek Teko 3D dengan animasi menggunakan C++ di codeblock

teko adalah benda yang tidak asing lagi bagi orang karena itu adalah sebuah tempat minum atau wadah tempat minum. nah bagimana cara membuatnya dengan openGL dicodeblock, kali ini saya akan membagikan sourcode untuk dipelajari bagi yang pertama kali belajar membuat projek animasi dengan openGL. hasil akhir dari projek ini akan menghasilkan bentuk seperti dibawah ini :


langkah-langkagh untuk membuatnya adalah sebagai berikut.

  1. download terlebih dahulu openGL disini :  View openGL
  2. setting codeblock dengan openGL caranya dapat dilihat disini: view
  3. buka codeblock dan buat projek baru caranya dapat dilihat disini : view
  4. copy semua sourcode yang tersedia di blog ini.
adapun sourcode yang tersedia adalah sebagai berikut ini :


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


GLenum doubleBuffer;

float rotX = 0.0, rotY = 0.0;
int teaList;

long patchData[][16] = {
    {102,103,104,105,4,5,6,7,8,9,10,11,12,13,14,15},
    {12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27},
    {24,25,26,27,29,30,31,32,33,34,35,36,37,38,39,40},
    {96,96,96,96,97,98,99,100,101,101,101,101,0,1,2,3,},
    {0,1,2,3,106,107,108,109,110,111,112,113,114,115,116,117},
    {118,118,118,118,124,122,119,121,123,126,125,120,40,39,38,37},
    {41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56},
    {53,54,55,56,57,58,59,60,61,62,63,64,28,65,66,67},
    {68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83},
    {80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95}
};

float cpData[][3] = {
    {0.2,0,2.7},{0.2,-0.112,2.7},{0.112,-0.2,2.7},{0,-0.2,2.7},
    {1.3375,0,2.53125},{1.3375,-0.749,2.53125},{0.749,-1.3375,2.53125},
    {0,-1.3375,2.53125},{1.4375,0,2.53125},{1.4375,-0.805,2.53125},
    {0.805,-1.4375,2.53125},{0,-1.4375,2.53125},{1.5,0,2.4},{1.5,-0.84,2.4},
    {0.84,-1.5,2.4},{0,-1.5,2.4},{1.75,0,1.875},{1.75,-0.98,1.875},
    {0.98,-1.75,1.875},{0,-1.75,1.875},{2,0,1.35},{2,-1.12,1.35},
    {1.12,-2,1.35},{0,-2,1.35},{2,0,0.9},{2,-1.12,0.9},{1.12,-2,0.9},
    {0,-2,0.9},{-2,0,0.9},{2,0,0.45},{2,-1.12,0.45},{1.12,-2,0.45},
    {0,-2,0.45},{1.5,0,0.225},{1.5,-0.84,0.225},{0.84,-1.5,0.225},
    {0,-1.5,0.225},{1.5,0,0.15},{1.5,-0.84,0.15},{0.84,-1.5,0.15},
    {0,-1.5,0.15},{-1.6,0,2.025},{-1.6,-0.3,2.025},{-1.5,-0.3,2.25},
    {-1.5,0,2.25},{-2.3,0,2.025},{-2.3,-0.3,2.025},{-2.5,-0.3,2.25},
    {-2.5,0,2.25},{-2.7,0,2.025},{-2.7,-0.3,2.025},{-3,-0.3,2.25},
    {-3,0,2.25},{-2.7,0,1.8},{-2.7,-0.3,1.8},{-3,-0.3,1.8},{-3,0,1.8},
    {-2.7,0,1.575},{-2.7,-0.3,1.575},{-3,-0.3,1.35},{-3,0,1.35},
    {-2.5,0,1.125},{-2.5,-0.3,1.125},{-2.65,-0.3,0.9375},{-2.65,0,0.9375},
    {-2,-0.3,0.9},{-1.9,-0.3,0.6},{-1.9,0,0.6},{1.7,0,1.425},
    {1.7,-0.66,1.425},{1.7,-0.66,0.6},{1.7,0,0.6},{2.6,0,1.425},
    {2.6,-0.66,1.425},{3.1,-0.66,0.825},{3.1,0,0.825},{2.3,0,2.1},
    {2.3,-0.25,2.1},{2.4,-0.25,2.025},{2.4,0,2.025},{2.7,0,2.4},
    {2.7,-0.25,2.4},{3.3,-0.25,2.4},{3.3,0,2.4},{2.8,0,2.475},
    {2.8,-0.25,2.475},{3.525,-0.25,2.49375},{3.525,0,2.49375},
    {2.9,0,2.475},{2.9,-0.15,2.475},{3.45,-0.15,2.5125},{3.45,0,2.5125},
    {2.8,0,2.4},{2.8,-0.15,2.4},{3.2,-0.15,2.4},{3.2,0,2.4},{0,0,3.15},
    {0.8,0,3.15},{0.8,-0.45,3.15},{0.45,-0.8,3.15},{0,-0.8,3.15},
    {0,0,2.85},{1.4,0,2.4},{1.4,-0.784,2.4},{0.784,-1.4,2.4},{0,-1.4,2.4},
    {0.4,0,2.55},{0.4,-0.224,2.55},{0.224,-0.4,2.55},{0,-0.4,2.55},
    {1.3,0,2.55},{1.3,-0.728,2.55},{0.728,-1.3,2.55},{0,-1.3,2.55},
    {1.3,0,2.4},{1.3,-0.728,2.4},{0.728,-1.3,2.4},{0,-1.3,2.4},{0,0,0},
    {1.425,-0.798,0},{1.5,0,0.075},{1.425,0,0},{0.798,-1.425,0},
    {0,-1.5,0.075},{0,-1.425,0},{1.5,-0.84,0.075},{0.84,-1.5,0.075}
};


void Teapot(long grid)
{
    float p[4][4][3], q[4][4][3], r[4][4][3], s[4][4][3];
    long i, j, k, l;

    teaList = 1;
    glNewList(teaList, GL_COMPILE);
    glPushMatrix();
    glRotatef(270.0, 1.0, 0.0, 0.0);
    for (i = 0; i < 10; i++) {
for (j = 0; j < 4; j++) {
   for (k = 0; k < 4; k++) {
for (l = 0; l < 3; l++) {
   p[j][k][l] = cpData[patchData[i][j*4+k]][l];
   q[j][k][l] = cpData[patchData[i][j*4+(3-k)]][l];
   if (l == 1) {
q[j][k][l] *= -1.0;
   }
   if (i < 6) {
r[j][k][l] = cpData[patchData[i][j*4+(3-k)]][l];
if (l == 0) {
   r[j][k][l] *= -1.0;
}
s[j][k][l] = cpData[patchData[i][j*4+k]][l];
if (l == 0) {
   s[j][k][l] *= -1.0;
}
if (l == 1) {
   s[j][k][l] *= -1.0;
}
   }
}
   }
}
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &p[0][0][0]);
glEnable(GL_MAP2_VERTEX_3);
glMapGrid2f(grid, 0.0, 1.0, grid, 0.0, 1.0);
glEvalMesh2(GL_FILL, 0, grid, 0, grid);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &q[0][0][0]);
glEvalMesh2(GL_FILL, 0, grid, 0, grid);
if (i < 6) {
   glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &r[0][0][0]);
   glEvalMesh2(GL_FILL, 0, grid, 0, grid);
   glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &s[0][0][0]);
   glEvalMesh2(GL_FILL, 0, grid, 0, grid);
}
    }
    glDisable(GL_MAP2_VERTEX_3);
    glPopMatrix();
    glEndList();
}

static void Init(void)
{
    float position[] = {0.0, 3.0, 3.0, 0.0};
    float local_view[] = {0.0};
    float ambient[] = {0.1745, 0.01175, 0.01175};
    float diffuse[] = {0.61424, 0.04136, 0.04136};
    float specular[] = {0.727811, 0.626959, 0.626959};

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glLightfv(GL_LIGHT0, GL_POSITION, position);
    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);

    glFrontFace(GL_CW);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_AUTO_NORMAL);
    glEnable(GL_NORMALIZE);

    glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
    glMaterialf(GL_FRONT, GL_SHININESS, 0.6*128.0);

    glClearColor(0.5, 0.5, 0.5, 1.0);
    glColor3f(1.0, 1.0, 1.0);

    Teapot(14);
}

static void Reshape(int w, int h)
{

    glViewport(0, 0, (GLint)w, (GLint)h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-6.0, 6.0, -6.0, 6.0, -1.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

static void Key(unsigned char key, int x, int y)
{

    switch (key) {
      case 27:
exit(0);
    }
}

static void SpecialKey(int key, int x, int y)
{

    switch (key) {
      case GLUT_KEY_UP:
rotX -= 20.0;
glutPostRedisplay();
break;
      case GLUT_KEY_DOWN:
rotX += 20.0;
glutPostRedisplay();
break;
      case GLUT_KEY_LEFT:
rotY -= 20.0;
glutPostRedisplay();
break;
      case GLUT_KEY_RIGHT:
rotY += 20.0;
glutPostRedisplay();
break;
    }
}

static void Draw(void)
{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

    glTranslatef(0.0, 0.0, -5.0);
    glRotatef(rotY, 0.0,1.0,0.0);
    glRotatef(rotX, 1.0,0.0,0.0);

    glCallList(teaList);

    glPopMatrix();

    if (doubleBuffer) {
glutSwapBuffers();
    } else {
glFlush();
    }
}

static void Args(int argc, char **argv)
{
    GLint i;

    doubleBuffer = GL_FALSE;

    for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-sb") == 0) {
   doubleBuffer = GL_FALSE;
} else if (strcmp(argv[i], "-db") == 0) {
   doubleBuffer = GL_TRUE;
}
    }
}

int main(int argc, char **argv)
{
    GLenum type;

    glutInit(&argc, argv);
    Args(argc, argv);

    type = GLUT_RGB | GLUT_DEPTH;
    type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
    glutInitDisplayMode(type);
    glutInitWindowSize(300, 300);
    glutCreateWindow("TeaPot");

    Init();

    glutReshapeFunc(Reshape);
    glutKeyboardFunc(Key);
    glutSpecialFunc(SpecialKey);
    glutDisplayFunc(Draw);
    glutMainLoop();
}



kemudian disaat program dijalankan akan menghasilkan animasi berupa teko warna merah seperti dibawah ini. semoga dengan adanya artikel ini dapat membantu anda-anda semua dalam menyelesaikan tugas dari dosen ataupun dari kantoer buat referensi belajar.










Contoh Projek 3D lainnya.


Baca Juga Projek OpenGL dengan C++ Lainnya  dibawah ini :

 Contoh Projek 2D openGL

Baca juga Animasi 2D untuk projek openGL lainnya dibawah ini

Dasar OpenGL

Baca juga Dasar Dari OpenGL dibawah ini jika kalian ingin menekuninya.


membuat animasi mobil 3D dengan OpenGL codeBlock C++

projek openGl banyak di blog tapi kebanyakan sulit dicoba sendiri. berikut ini saya beri satu full sourcode membuat animasi dengan openGL terutama untuk membuat game sederhana beserta tutorial cara penggunakan codeblock sampai bisa menggunakan openGL.
adapun hasil akhir dari projek kali ini adalah sebagai berikut



projek ini membuat mobil dengan beberapa fitur dalam animasinya seperti :

  1. putar arah y
  2. putar arah x
  3. putar arah z
  4. tingkatkan ukuran mobil dalam arah x
  5. tingkatkan ukuran mobil dalam arah y
  6. tingkatkan ukuran mobil dalam arah z
  7. tampilan atas kamera
  8. tampilan samping kamera
  9. gerakan mobil kearah depan
  10. gerakan mobil kearah belakang
  11. tombol keluar dari animasi

Adapun langkah -langkah dalam membuatnya adalah sebagai berikut :

  • Download OpenGL terlebih dahulu disini :  View openGL
  • Setting codeblock dengan opengl. kalau belum tau caranya dapat dilihat disini "cara setting OpenGl ke codeblock" 
  • Buka editor codeblock  cara memulai projeknya dapat dilihat disini : view
  • Copy kode dibawah ini kemudian jalankan.

#include <stdio.h>
#include <stdlib.h>
#include <gl/glut.h>
#include <math.h>
#include <string.h>

/* ASCII code for the escape key. */
#define ESCAPE 27

GLint window;
GLint window2;
GLint Xsize=1000;
GLint Ysize=800;
float i,theta;
GLint nml=0,day=1;

char name3[]="PROJECT:  3D CAR  ANIMATION";

GLfloat xt=0.0,yt=0.0,zt=0.0,xw=0.0;   /* x,y,z translation */
GLfloat tx=295,ty=62;
GLfloat xs=1.0,ys=1.0,zs=1.0;

GLfloat xangle=0.0,yangle=0.0,zangle=0.0,angle=0.0;   /* axis angles */

GLfloat r=0,g=0,b=1;
GLint light=1;
int count=1,flg=1;
int view=0;
int flag1=0,aflag=1;            //to switch car driving mode
int flag2=0,wheelflag=0;   //to switch fog effect
GLUquadricObj *t;

static void SpecialKeyFunc( int Key, int x, int y );

/* Simple  transformation routine */
GLvoid Transform(GLfloat Width, GLfloat Height)
{
  glViewport(0, 0, Width, Height);              /* Set the viewport */
  glMatrixMode(GL_PROJECTION);                  /* Select the projection matrix */
  glLoadIdentity(); /* Reset The Projection Matrix */
  gluPerspective(45.0,Width/Height,0.1,100.0);  /* Calculate The Aspect Ratio Of The Window */
  glMatrixMode(GL_MODELVIEW);                   /* Switch back to the modelview matrix */
}


/* A general OpenGL initialization function.  Sets all of the initial parameters. */
GLvoid InitGL(GLfloat Width, GLfloat Height)
{

  glClearColor(1.0, 1.0, 1.0, 1.0);
  glLineWidth(2.0);              /* Add line width,   ditto */
  Transform( Width, Height ); /* Perform the transformation */
  //newly added
  t=gluNewQuadric();
        gluQuadricDrawStyle(t, GLU_FILL);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

// Create light components
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position[] = { 1.5f, 1.0f, 4.0f, 1.0f };

// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);

}

/* The function called when our window is resized  */
GLvoid ReSizeGLScene(GLint Width, GLint Height)
{
  if (Height==0)     Height=1;                   /* Sanity checks */
  if (Width==0)      Width=1;
  Transform( Width, Height );                   /* Perform the transformation */
}

void init()
{
    glClearColor(0,0,0,0);
glPointSize(5.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,900.0,0.0,600.0,50.0,-50.0);
glutPostRedisplay();  // request redisplay
}


/* The main drawing function

   In here we put all the OpenGL and calls to routines which manipulate
   the OpenGL state and environment.

   This is the function which will be called when a "redisplay" is requested.
*/

void display_string(int x, int y, char *string, int font)
{
    int len,i;
glColor3f(0.8,0.52,1.0);
glRasterPos2f(x, y);
    len = (int) strlen(string);
    for (i = 0; i < len; i++) {
    if(font==1)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,string[i]);
if(font==2)
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,string[i]);
if(font==3)
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,string[i]);
if(font==4)
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10,string[i]);
}

}

void display1(void)
{

glClearColor(1.0,1.0,0.1,1.0);
display_string(180,540,"NAME OF THE ENGINEERING COLLEGE",1); //correct cordinate according to name
display_string(215,500,name3,1);
display_string(390,470,"HELP",2);
display_string(10,450,"MOUSE",2);
display_string(10,410,"PRESS RIGHT BUTTON FOR MENU",3);
display_string(10,370,"KEYBOARD",2);
display_string(10,340,"X-Y-Z KEYS FOR CORRESPONDING ROTATION",3);
display_string(10,310,"A-S-Q CAR CUSTOM SIZE SELECTION",3);
display_string(10,280,"U-F FOR CAMERA VIEW SETTINGS",3);
display_string(10,250,"USE LEFT ARROW(<-) AND RIGHT ARROW(->) TO MOVE CAR",3);
display_string(10,220,"ESCAPE TO EXIT",3);
display_string(250,150,"PRESS SPACE BAR TO ENTER",2);
glutPostRedisplay();
glutSwapBuffers();

}

GLvoid DrawGLScene()
{


  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Clear The Screen And The Depth Buffer */
if(view==0)
{
init();
display1();
}
else
{
  if(count==1)
InitGL(Xsize,Ysize);
  if(aflag==1)/* Initialize our window. */
  glClearColor(1,1,1,1);
  else
  glClearColor(0.1,0.1,0.1,0);
  glPushMatrix();
  glLoadIdentity();
  glTranslatef(-1.0,0.0,-3.5);
  glRotatef(xangle,1.0,0.0,0.0);
  glRotatef(yangle,0.0,1.0,0.0);
  glRotatef(zangle,0.0,0.0,1.0);
  glTranslatef(xt,yt,zt);
  glScalef(xs,ys,zs);
  glEnable(GL_COLOR_MATERIAL);
  glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

  if(flag2==1)
  {
  GLfloat fogcolour[4]={1.0,1.0,1.0,1.0};

  glFogfv(GL_FOG_COLOR,fogcolour);              /* Define the fog colour */
  glFogf(GL_FOG_DENSITY,0.1);                   /* How dense */
  glFogi(GL_FOG_MODE,GL_EXP);                   /* exponential decay */
  glFogf(GL_FOG_START,3.0);                   /* Where wwe start fogging */
  glFogf(GL_FOG_END,100.0);                       /* end */
  glHint(GL_FOG_HINT, GL_FASTEST);              /* compute per vertex */
  glEnable(GL_FOG);/* ENABLE */
  }
  if(flag2==0)
  {
  glDisable(GL_FOG);
  }

if(!aflag){
  glBegin(GL_POINTS);
  glColor3f(1,1,1);
  glPointSize(200.0);
  int ccount=0;
  float x=10,y=10;
  while(ccount<20)
  {
  glVertex2f(x,y);
  x+=10;
  y+=10;
  if(y>Ysize) y-=10;
  if(x>Xsize) x-=10;
  ccount++;
  }
  glEnd();}

  glColor3f(1.0,.75,0.0);
  glPointSize(30.0);
  glBegin(GL_POINTS);
  glVertex3f(0.2,0.3,0.3);
  glVertex3f(0.2,0.3,0.5);
  glEnd();
  glPointSize(200.0);



  glBegin(GL_QUADS);                /* OBJECT MODULE*/

  /* top of cube*/
  //************************FRONT BODY****************************************
  glColor3f(r,g,b);
  glVertex3f( 0.2, 0.4,0.6);
  glVertex3f(0.6, 0.5,0.6);
  glVertex3f(0.6, 0.5,0.2);
  glVertex3f( 0.2,0.4,0.2);

  /* bottom of cube*/
  glVertex3f( 0.2,0.2,0.6);
  glVertex3f(0.6,0.2,0.6);
  glVertex3f(0.6,0.2,0.2);
  glVertex3f( 0.2,0.2,0.2);

  /* front of cube*/
  glVertex3f( 0.2,0.2,0.6);
  glVertex3f(0.2, 0.4,0.6);
  glVertex3f(0.2,0.4,0.2);
  glVertex3f( 0.2,0.2,0.2);

  /* back of cube.*/
  glVertex3f(0.6,0.2,0.6);
  glVertex3f(0.6,0.5,0.6);
  glVertex3f(0.6,0.5,0.2);
  glVertex3f( 0.6,0.2,0.2);

  /* left of cube*/
  glVertex3f(0.2,0.2,0.6);
  glVertex3f(0.6,0.2,0.6);
  glVertex3f(0.6,0.5,0.6);
  glVertex3f(0.2,0.4,0.6);

  /* Right of cube */
  glVertex3f(0.2,0.2,0.2);
  glVertex3f( 0.6,0.2,0.2);
  glVertex3f( 0.6,0.5,0.2);
  glVertex3f( 0.2,0.4,0.2);
//****************************************************************************
  glVertex3f(0.7,0.65,0.6);
  glVertex3f(0.7,0.65,0.2);
  glVertex3f(1.7,0.65,0.2);        //top cover
  glVertex3f(1.7,0.65,0.6);
//***************************back guard******************************
  glColor3f(r,g,b);            /* Set The Color To Blue*/
  glVertex3f( 1.8, 0.5,0.6);
  glVertex3f(1.8, 0.5,0.2);
  glVertex3f(2.1, 0.4, 0.2);
  glVertex3f(2.1,0.4,0.6);

  /* bottom of cube*/
  glVertex3f( 2.1,0.2,0.6);
  glVertex3f(2.1,0.2,0.2);
  glVertex3f(1.8,0.2,0.6);
  glVertex3f( 1.8,0.2,0.6);

  /* back of cube.*/
  glVertex3f(2.1,0.4,0.6);
  glVertex3f(2.1,0.4,0.2);
  glVertex3f(2.1,0.2,0.2);
  glVertex3f(2.1,0.2,0.6);

  /* left of cube*/
  glVertex3f(1.8,0.2,0.2);
  glVertex3f(1.8,0.5,0.2);
  glVertex3f(2.1,0.4,0.2);
  glVertex3f(2.1,0.2,0.2);

  /* Right of cube */
  glVertex3f(1.8,0.2,0.6);
  glVertex3f(1.8,0.5,0.6);
  glVertex3f(2.1,0.4,0.6);
  glVertex3f(2.1,0.2,0.6);
//******************MIDDLE BODY************************************
  glVertex3f( 0.6, 0.5,0.6);
  glVertex3f(0.6, 0.2,0.6);
  glVertex3f(1.8, 0.2, 0.6);
  glVertex3f(1.8,0.5,0.6);

  /* bottom of cube*/
  glVertex3f( 0.6,0.2,0.6);
  glVertex3f(0.6,0.2,0.2);
  glVertex3f(1.8,0.2,0.2);
  glVertex3f( 1.8,0.2,0.6);

  /* back of cube.*/
  glVertex3f(0.6,0.5,0.2);
  glVertex3f(0.6,0.2,0.2);
  glVertex3f(1.8,0.2,0.2);
  glVertex3f(1.8,0.5,0.2);
//*********************ENTER WINDOW**********************************
  glColor3f(0.3,0.3,0.3);
  glVertex3f( 0.77, 0.63,0.2);
  glVertex3f(0.75, 0.5,0.2);        //quad front window
  glVertex3f(1.2, 0.5, 0.2);
  glVertex3f( 1.22,0.63,0.2);

  glVertex3f(1.27,0.63,.2);
  glVertex3f(1.25,0.5,0.2);        //quad back window
  glVertex3f(1.65,0.5,0.2);
  glVertex3f(1.67,0.63,0.2);

  glColor3f(r,g,b);
  glVertex3f(0.7,0.65,0.2);
  glVertex3f(0.7,0.5,.2);       //first separation
  glVertex3f(0.75,0.5,0.2);
  glVertex3f(0.77,0.65,0.2);

  glVertex3f(1.2,0.65,0.2);
  glVertex3f(1.2,0.5,.2);       //second separation
  glVertex3f(1.25,0.5,0.2);
  glVertex3f(1.27,0.65,0.2);

  glVertex3f(1.65,0.65,0.2);
  glVertex3f(1.65,0.5,.2);     //3d separation
  glVertex3f(1.7,0.5,0.2);
  glVertex3f(1.7,0.65,0.2);

  glVertex3f( 0.75, 0.65,0.2);
  glVertex3f(0.75, 0.63,0.2);        //line strip
  glVertex3f(1.7, 0.63, 0.2);
  glVertex3f( 1.7,0.65,0.2);

  glVertex3f( 0.75, 0.65,0.6);
  glVertex3f(0.75, 0.63,0.6);        //line strip
  glVertex3f(1.7, 0.63, 0.6);
  glVertex3f( 1.7,0.65,0.6);

  glColor3f(0.3,0.3,0.3);
  glVertex3f( 0.77, 0.63,0.6);
  glVertex3f(0.75, 0.5,0.6);        //quad front window
  glVertex3f(1.2, 0.5, 0.6);
  glVertex3f( 1.22,0.63,0.6);

  glVertex3f(1.27,0.63,.6);
  glVertex3f(1.25,0.5,0.6);        //quad back window
  glVertex3f(1.65,0.5,0.6);
  glVertex3f(1.67,0.63,0.6);

  glColor3f(r,g,b);
  glVertex3f(0.7,0.65,0.6);
  glVertex3f(0.7,0.5,.6);       //first separation
  glVertex3f(0.75,0.5,0.6);
  glVertex3f(0.77,0.65,0.6);

  glVertex3f(1.2,0.65,0.6);
  glVertex3f(1.2,0.5,.6);       //second separation
  glVertex3f(1.25,0.5,0.6);
  glVertex3f(1.27,0.65,0.6);

  glVertex3f(1.65,0.65,0.6);
  glVertex3f(1.65,0.5,.6);
  glVertex3f(1.7,0.5,0.6);
  glVertex3f(1.7,0.65,0.6);
  glEnd();


 //**************************************************************
  glBegin(GL_QUADS);

  /* top of cube*/
  glColor3f(0.3,0.3,0.3);
  glVertex3f( 0.6, 0.5,0.6);
  glVertex3f(0.6, 0.5,0.2);        //quad front window
  glVertex3f(0.7, 0.65, 0.2);
  glVertex3f( 0.7,0.65,0.6);

  glVertex3f(1.7,0.65,.6);
  glVertex3f(1.7,0.65,0.2);        //quad back window
  glVertex3f(1.8,0.5,0.2);
  glVertex3f(1.8,0.5,0.6);


//*****************************road and surrounding development***********************************
  if(flag1)
  {
glPushMatrix();
glTranslatef(xw,0,0);
  glColor3f(0,1,0);
  glVertex3f(-100,0.1,-100);
  glVertex3f(-100,0.1,0);         //a green surroundings
  glVertex3f(100,0.1,0);
  glVertex3f(100,0.1,-100);

  glColor3f(0.7,0.7,0.7);
  glVertex3f(-100,0.1,0);
  glVertex3f(-100,0.1,0.45);         //a long road
  glVertex3f(100,0.1,0.45);
  glVertex3f(100,0.1,0);

  glColor3f(1.0,0.75,0.0);
  glVertex3f(-100,0.1,0.45);       //a median
  glVertex3f(-100,0.1,0.55);
  glVertex3f(100,0.1,0.55);
  glVertex3f(100,0.1,0.45);

  glColor3f(0.7,0.7,0.7);
  glVertex3f(-100,0.1,0.55);
  glVertex3f(-100,0.1,1);         //a long road
  glVertex3f(100,0.1,1);
  glVertex3f(100,0.1,0.55);

  glColor3f(0,1,0);
  glVertex3f(-100,0.1,1);
  glVertex3f(-100,0.1,100);         //a green surroundings
  glVertex3f(100,0.1,100);
  glVertex3f(100,0.1,1);
    glPopMatrix();
  }
  glEnd();

  if(wheelflag)
  {
  glPushMatrix();
  glTranslatef(xw,0,0);
  glColor3f(0.5,.2,0.3);
  glBegin(GL_QUADS);
  for(i=0;i<200;i+=0.2)
  {
  glVertex3f(-100+i,0,1);
  glVertex3f(-99.9+i,0,1);
  glVertex3f(-99.9+i,0.2,1);
  glVertex3f(-100+i,0.2,1);
  i+=0.5;
  }
  for(i=0;i<200;i+=0.2)
  {
  glVertex3f(-100+i,0,0);
  glVertex3f(-99.9+i,0,0);
  glVertex3f(-99.9+i,0.2,0);
  glVertex3f(-100+i,0.2,0);
  i+=0.5;
  }
  glEnd();
  glPopMatrix();
  }
//*************************************************************************************************
glBegin(GL_TRIANGLES);                /* start drawing the cube.*/

  /* top of cube*/
  glColor3f(0.3,0.3,0.3);
  glVertex3f( 0.6, 0.5,0.6);
  glVertex3f( 0.7,0.65,0.6);       //tri front window
  glVertex3f(0.7,0.5,0.6);

  glVertex3f( 0.6, 0.5,0.2);
  glVertex3f( 0.7,0.65,0.2);       //tri front window
  glVertex3f(0.7,0.5,0.2);

  glVertex3f( 1.7, 0.65,0.2);
  glVertex3f( 1.8,0.5,0.2);       //tri back window
  glVertex3f( 1.7,0.5,0.2);

  glVertex3f( 1.7, 0.65,0.6);
  glVertex3f( 1.8,0.5,0.6);       //tri back window
  glVertex3f(1.7,0.5,0.6);

glEnd();
//************IGNITION SYSTEM**********************************
glPushMatrix();
   glColor3f(0.7,0.7,0.7);
   glTranslatef(1.65,0.2,0.3);
   glRotatef(90.0,0,1,0);
   gluCylinder(t,0.02,0.03,.5,10,10);
glPopMatrix();
//********************WHEEL*********************************************

glColor3f(0.7,0.7,0.7);
glPushMatrix();
  glBegin(GL_LINE_STRIP);
  for(theta=0;theta<360;theta=theta+20)
  {
  glVertex3f(0.6,0.2,0.62);
  glVertex3f(0.6+(0.08*(cos(((theta+angle)*3.14)/180))),0.2+(0.08*(sin(((theta+angle)*3.14)/180))),0.62);
  }
glEnd();

glBegin(GL_LINE_STRIP);
  for(theta=0;theta<360;theta=theta+20)
  {
  glVertex3f(0.6,0.2,0.18);
  glVertex3f(0.6+(0.08*(cos(((theta+angle)*3.14)/180))),0.2+(0.08*(sin(((theta+angle)*3.14)/180))),0.18);
  }
glEnd();

glBegin(GL_LINE_STRIP);
for(theta=0;theta<360;theta=theta+20)
  {
  glVertex3f(1.7,0.2,0.18);
  glVertex3f(1.7+(0.08*(cos(((theta+angle)*3.14)/180))),0.2+(0.08*(sin(((theta+angle)*3.14)/180))),0.18);
  }
glEnd();

glBegin(GL_LINE_STRIP);
for(theta=0;theta<360;theta=theta+20)
  {
  glVertex3f(1.7,0.2,0.62);
 glVertex3f(1.7+(0.08*(cos(((theta+angle)*3.14)/180))),0.2+(0.08*(sin(((theta+angle)*3.14)/180))),0.62);
  }
glEnd();
  glTranslatef(0.6,0.2,0.6);
  glColor3f(0,0,0);
  glutSolidTorus(0.025,0.07,10,25);

  glTranslatef(0,0,-0.4);
  glutSolidTorus(0.025,0.07,10,25);

  glTranslatef(1.1,0,0);
  glutSolidTorus(0.025,0.07,10,25);

  glTranslatef(0,0,0.4);
  glutSolidTorus(0.025,0.07,10,25);
  glPopMatrix();
//*************************************************************
  glPopMatrix();
  glEnable(GL_DEPTH_TEST);
  glutPostRedisplay();
  glutSwapBuffers();
 }
}


/*  The function called whenever a "normal" key is pressed. */
void NormalKey(GLubyte key, GLint x, GLint y)
{
    switch ( key )    {
     case ESCAPE : printf("escape pressed. exit.\n");
               glutDestroyWindow(window); /* Kill our window */
               exit(0);
                                   break;

      case ' ':view=1;
                   DrawGLScene();
                   break;

      case 'x': xangle += 5.0;
                      glutPostRedisplay();
                      break;

       case 'X':xangle -= 5.0;
                       glutPostRedisplay();
                       break;

      case 'y':
        yangle += 5.0;
        glutPostRedisplay();
        break;

     case 'Y':
        yangle -= 5.0;
        glutPostRedisplay();
        break;

     case 'z':
        zangle += 5.0;
        glutPostRedisplay();
        break;

     case 'Z':
        zangle -= 5.0;
        glutPostRedisplay();
        break;

      case 'u':                          /* Move up */
        yt += 0.2;
        glutPostRedisplay();
        break;

      case 'U':
        yt -= 0.2;                      /* Move down */
        glutPostRedisplay();
        break;

      case 'f':                          /* Move forward */
        zt += 0.2;
        glutPostRedisplay();
        break;

      case 'F':
        zt -= 0.2;                      /* Move away */
        glutPostRedisplay();
        break;

  case 's':zs+=.2;
  glutPostRedisplay();
  break;

  case 'S':zs-=0.2;
  glutPostRedisplay();
  break;

  case 'a':ys+=.2;
  glutPostRedisplay();
  break;

  case 'A':ys-=0.2;
  glutPostRedisplay();
  break;

  case 'q':xs+=.2;
  glutPostRedisplay();
  break;

  case 'Q':xs-=0.2;
  glutPostRedisplay();
  break;


     default:
break;
    }

}

static void SpecialKeyFunc( int Key, int x, int y )
{
switch ( Key ) {
case GLUT_KEY_RIGHT:
if(!wheelflag)
xt += 0.2;
if(wheelflag)
{
angle+=5;
xw+=0.2;
}
    glutPostRedisplay();
break;

                    case GLUT_KEY_LEFT:
if(!wheelflag)
xt -= 0.2;
if(wheelflag)
{
angle+=5;
xw-=0.2;
}
        glutPostRedisplay();
break;
}
}

void myMenu(int id)
{
if (id==1)
{
flag1=0;
wheelflag=0;
glutPostRedisplay();

}
if(id ==2)
{
flag1=1;
flag2=0;
wheelflag=0;
xangle += 5.0;
glutPostRedisplay();
}
if(id==3)
{
flag2=1;
wheelflag=0;
xangle += 5.0;
glutPostRedisplay();
}
if (id==4)
{
 wheelflag=1;
 glutPostRedisplay();
}
if (id==5)
{
               if(day)
                {

if(light)
{
count++;
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
light=0;
}
else
{
count--;
light=1;
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
glutPostRedisplay();
                }
               else
                {

                  if(nml==0 && flag2==2)
                     {
                       flag2=0;
                       nml=1;
                     }
                   else
                     {
                         flag2=2;
                 nml=0;

                 aflag=0;
                 day=0;

         glClearColor(0.1,0.1,0.1,0);
         GLfloat fogcolour[4]={0.0,0.0,0.0,1.0};

                 glFogfv(GL_FOG_COLOR,fogcolour);              /* Define the fog colour */
                 glFogf(GL_FOG_DENSITY,0.5);                   /* How dense */
                 glFogi(GL_FOG_MODE,GL_EXP);                   /* exponential decay */
                         /* end */
                glHint(GL_FOG_HINT, GL_FASTEST);              /* compute per vertex */
                glEnable(GL_FOG);


        glutPostRedisplay();
                 }
             }

}


       if(id==12)
{
aflag=1;
        day=1;
glClearColor(1,1,1,1);
glDisable(GL_FOG);
glutPostRedisplay();
}

if(id==13)
{
aflag=0;
        day=0;
flag2=2;
glClearColor(0.1,0.1,0.1,0);
GLfloat fogcolour[4]={0.0,0.0,0.0,1.0};

  glFogfv(GL_FOG_COLOR,fogcolour);              /* Define the fog colour */
  glFogf(GL_FOG_DENSITY,0.5);                   /* How dense */
  glFogi(GL_FOG_MODE,GL_EXP);                   /* exponential decay */
                         /* end */
  glHint(GL_FOG_HINT, GL_FASTEST);              /* compute per vertex */
  glEnable(GL_FOG);

  glutPostRedisplay();
       }
}

void colorMenu(int id)
{
if (id==6)
{
r=g=0;
b=1;
glutPostRedisplay();

}
if(id ==7)
{
r=0.8;
b=g=0;
glutPostRedisplay();
}
if(id==8)
{
    g=1;
r=b=0;
glutPostRedisplay();
}
if (id==9)
{
 r=b=g=0;
 glutPostRedisplay();
}
if(id==10)
{
b=0;
r=g=1;
        glutPostRedisplay();
}
if(id==11)
{
b=r=g=.7;
        glutPostRedisplay();
}

}

void myreshape(int w,int h)
{
        glViewport(0,0,w,h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        if(w<=h)
                glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
        else
                glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,-10.0,10.0);
        glMatrixMode(GL_MODELVIEW);
        glutPostRedisplay();
}


//*************************** Main ***************************************************************

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

/* Initialisation and window creation */

  glutInit(&argc, argv);               /* Initialize GLUT state. */

  glutInitDisplayMode(GLUT_RGBA |      /* RGB and Alpha */
                      GLUT_DOUBLE|     /* double buffer */
                      GLUT_DEPTH);     /* Z buffer (depth) */

  glutInitWindowSize(Xsize,Ysize);     /* set initial window size. */
  glutInitWindowPosition(0,0);         /* upper left corner of the screen. */

  glutCreateWindow("3D CAR ANIMATION"); /* Open a window with a title. */

/* Now register the various callback functions */

  glutReshapeFunc(myreshape);
  glutDisplayFunc(DrawGLScene);        /* Function to do all our OpenGL drawing. */
  glutReshapeFunc(ReSizeGLScene);
  glutKeyboardFunc(NormalKey);         /*Normal key is pressed */
  glutSpecialFunc( SpecialKeyFunc );
  InitGL(Xsize,Ysize);
  int submenu=glutCreateMenu(colorMenu);
  glutAddMenuEntry("blue", 6);
glutAddMenuEntry("red", 7);
glutAddMenuEntry("green",8);
glutAddMenuEntry("black",9);
glutAddMenuEntry("yellow",10);
glutAddMenuEntry("grey",11);
  glutCreateMenu(myMenu);
glutAddMenuEntry("car model mode", 1);
glutAddMenuEntry("car driving mode", 2);
glutAddMenuEntry("fog effect",3);
glutAddMenuEntry("wheel effect",4);
glutAddMenuEntry("toggle light",5);
glutAddSubMenu("car colors",submenu);
glutAddMenuEntry("daymode",12);
glutAddMenuEntry("Night mode",13);
glutAttachMenu(GLUT_RIGHT_BUTTON);

  /* Now drop into the event loop from which we never return */

  glutMainLoop();                      /* Start Event Processing Engine. */
  return 1;
}



hasil akhir ketika dicompile dan di jalankan maka akan menghasilkan tampilan seperti dibawah ini.
semua instruksinya sudah tedapat di layar kalian masing-masing:





  1. Y = putar arah y
  2. X = putar arah x
  3. Z = putar arah z
  4. A = tingkatkan ukuran mobil dalam arah x
  5. S = tingkatkan ukuran mobil dalam arah y
  6. tingkatkan ukuran mobil dalam arah z
  7. U = tampilan atas kamera
  8. F = tampilan samping kamera
  9. tombol arah kiri = gerakan mobil kearah depan
  10. tombol arah kanan =  gerakan mobil kearah belakang
  11. Esc = tombol keluar dari animasi
  12. spasi = masukan layar utama dari layar awal



  1. http://www.openglprojects.in/2017/06/3d-car-animation-opengl-computer-graphics-program.html#more&gsc.tab=0




Contoh Projek 3D lainnya.


Baca Juga Projek OpenGL dengan C++ Lainnya  dibawah ini :

 Contoh Projek 2D openGL

Baca juga Animasi 2D untuk projek openGL lainnya dibawah ini

Dasar OpenGL

Baca juga Dasar Dari OpenGL dibawah ini jika kalian ingin menekuninya.


Newer Posts Older Posts Home

Tingkat keanekaragaman hayati tingkat gen

Tingkat keanekaragaman hayati tingkat gen -- Keanekaragaman hayati adalah sebuah istilah untuk menggambarkan keanekaragaman mahluk hidup di ...