Created
January 17, 2018 16:42
-
-
Save abdullahainun/2eb7556cc348634786ed9eb748fdfaab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math.h> | |
#include <GL/glut.h> | |
#include <time.h> | |
//#include <fstream.h> | |
#include <stdarg.h> | |
#include <stdio.h> | |
#include <string.h> | |
typedef struct { | |
float v[3]; | |
}vector2D_t; | |
typedef struct { | |
float m[3][3]; | |
}matrix2D_t; | |
typedef struct { | |
float m[4][4]; | |
} matrix3D_t; | |
typedef struct { | |
float v[4]; | |
} vector3D_t; | |
typedef struct { | |
float x; | |
float y; | |
float z; | |
} point3D_t; | |
typedef struct { | |
float x; | |
float y; | |
} point2D_t; | |
typedef struct { | |
float r; | |
float g; | |
float b; | |
} color_t; | |
matrix2D_t createIdentity2dim(void) { | |
matrix2D_t u; | |
int i,j; | |
for (i=0;i<3;i++) { | |
for(j=0;j<3;j++) | |
{ | |
u.m[i][j]=0.; | |
u.m[i][i]=1.; | |
} | |
} | |
return u; | |
} | |
matrix2D_t translation2dimMTX(float dx,float dy) | |
{ | |
matrix2D_t trans=createIdentity2dim(); | |
trans.m[0][2]=dx; | |
trans.m[1][2]=dy; | |
return trans; | |
} | |
matrix2D_t rotation2dimMTX(float theta) | |
{ | |
matrix2D_t rotate=createIdentity2dim(); | |
float cs=cos(theta); | |
float sn=sin(theta); | |
rotate.m[0][0]=cs; rotate.m[0][1]=-sn; | |
rotate.m[1][0]=sn; rotate.m[1][1]=cs; | |
return rotate; | |
} | |
matrix2D_t scaling2dimMTX(float mx,float my) | |
{ | |
matrix2D_t scale=createIdentity2dim(); | |
scale.m[0][0]=mx; | |
scale.m[1][1]=my; | |
return scale; | |
} | |
matrix2D_t operator * (matrix2D_t a, matrix2D_t b) | |
{ | |
matrix2D_t c;//c=a*b | |
int i,j,k; | |
for (i=0;i<3;i++) for (j=0;j<3;j++) { | |
c.m[i][j]=0; | |
for (k=0;k<3;k++) | |
c.m[i][j]+=a.m[i][k]*b.m[k][j]; | |
} | |
return c; | |
} | |
vector2D_t operator * (matrix2D_t a, vector2D_t b) | |
{ | |
vector2D_t c;//c=a*b | |
int i,j; | |
for (i=0;i<3;i++) { | |
c.v[i]=0; | |
for (j=0;j<3;j++) | |
c.v[i]+=a.m[i][j]*b.v[j]; | |
} | |
return c; | |
} | |
vector2D_t point2vector2dim(point2D_t pnt) | |
{ | |
vector2D_t vec; | |
vec.v[0]=pnt.x; | |
vec.v[1]=pnt.y; | |
vec.v[2]=1.; | |
return vec; | |
} | |
point2D_t vector2point2dim(vector2D_t vec) | |
{ | |
point2D_t pnt; | |
pnt.x=vec.v[0]; | |
pnt.y=vec.v[1]; | |
return pnt; | |
} | |
void setColor2(color_t col) | |
{ | |
glColor3f(col.r, col.g, col.b); | |
} | |
void drawPolygon2dim(point2D_t pnt[],int n) | |
{ | |
int i; | |
glBegin(GL_LINE_LOOP); | |
for (i=0;i<n;i++) { | |
glVertex2f(pnt[i].x, pnt[i].y); | |
} | |
glEnd(); | |
} | |
void drawPolygon12dim(point2D_t pnt[],int n,float tick1) | |
{ | |
int i; | |
glBegin(GL_LINE_LOOP); | |
for (i=0;i<n;i++) { | |
glVertex2f(pnt[i].x+250*cos(tick1), pnt[i].y+250*sin(tick1)); | |
} | |
glEnd(); | |
} | |
void GradatePolygon2dim(point2D_t pnt[],int n, color_t color[]) | |
{ | |
int i; | |
setColor2(color[0]); | |
glBegin(GL_POLYGON); | |
for (i=0;i<n;i++) { | |
setColor2(color[i]); | |
glVertex2f(pnt[i].x, pnt[i].y); | |
} | |
glEnd(); | |
} | |
void fillPolygon2dim(point2D_t pnt[],int n, color_t color) | |
{ | |
int i; | |
setColor2(color); | |
glBegin(GL_POLYGON); | |
for (i=0;i<n;i++) { | |
glVertex2f(pnt[i].x, pnt[i].y); | |
} | |
glEnd(); | |
} | |
void GradatePolygon12dim(point2D_t pnt[],int n, color_t color[]) | |
{ | |
int i; | |
setColor2(color[0]); | |
glBegin(GL_POLYGON); | |
for (i=0;i<n;i++) { | |
setColor2(color[i]); | |
glVertex2f(pnt[i].x, pnt[i].y); | |
} | |
glEnd(); | |
} | |
////////////////// matrices and vectors 3D ver 2 ///////////////// | |
matrix3D_t createIdentity(void) | |
{ | |
matrix3D_t u; | |
int i,j; | |
for (i=0;i<4;i++) { | |
for(j=0;j<4;j++) u.m[i][j]=0.; | |
u.m[i][i]=1.; | |
} | |
return u; | |
} | |
matrix3D_t operator * (matrix3D_t a,matrix3D_t b) | |
{ | |
matrix3D_t c;//c=a*b | |
int i,j,k; | |
for (i=0;i<4;i++) for (j=0;j<4;j++) { | |
c.m[i][j]=0; | |
for (k=0;k<4;k++) c.m[i][j]+=a.m[i][k]*b.m[k][j]; | |
} | |
return c; | |
} | |
vector3D_t operator * (matrix3D_t a, vector3D_t b) | |
{ | |
vector3D_t c;//c=a*b | |
int i,j; | |
for (i=0;i<4;i++) { | |
c.v[i]=0; | |
for (j=0;j<4;j++) c.v[i]+=a.m[i][j]*b.v[j]; | |
} | |
return c; | |
} | |
matrix3D_t translationMTX(float dx,float dy,float dz) | |
{ | |
matrix3D_t trans=createIdentity(); | |
trans.m[0][3]=dx; | |
trans.m[1][3]=dy; | |
trans.m[2][3]=dz; | |
return trans; | |
} | |
matrix3D_t rotationXMTX(float theta) | |
{ | |
matrix3D_t rotate=createIdentity(); | |
float cs=cos(theta); | |
float sn=sin(theta); | |
rotate.m[1][1]=cs; rotate.m[1][2]=-sn; | |
rotate.m[2][1]=sn; rotate.m[2][2]=cs; | |
return rotate; | |
} | |
matrix3D_t rotationYMTX(float theta) | |
{ | |
matrix3D_t rotate=createIdentity(); | |
float cs=cos(theta); | |
float sn=sin(theta); | |
rotate.m[0][0]=cs; rotate.m[0][2]=sn; | |
rotate.m[2][0]=-sn; rotate.m[2][2]=cs; | |
return rotate; | |
} | |
matrix3D_t rotationZMTX(float theta) | |
{ | |
matrix3D_t rotate=createIdentity(); | |
float cs=cos(theta); | |
float sn=sin(theta); | |
rotate.m[0][0]=cs; rotate.m[0][1]=-sn; | |
rotate.m[1][0]=sn; rotate.m[1][1]=cs; | |
return rotate; | |
} | |
matrix3D_t scalingMTX(float factorx,float factory,float factorz) | |
{ | |
matrix3D_t scale=createIdentity(); | |
scale.m[0][0]=factorx; | |
scale.m[1][1]=factory; | |
scale.m[2][2]=factorz; | |
return scale; | |
} | |
matrix3D_t perspectiveMTX(float eyelength) | |
{ | |
matrix3D_t perspective=createIdentity(); | |
perspective.m[3][2]=-1./eyelength; | |
return perspective; | |
} | |
point2D_t Vector2Point2D(vector3D_t vec) | |
{ | |
point2D_t pnt; | |
pnt.x=vec.v[0]; | |
pnt.y=vec.v[1]; | |
return pnt; | |
} | |
point3D_t Vector2Point3D(vector3D_t vec) | |
{ | |
point3D_t pnt; | |
pnt.x=vec.v[0]; | |
pnt.y=vec.v[1]; | |
pnt.z=vec.v[2]; | |
return pnt; | |
} | |
vector3D_t Point2Vector(point3D_t pnt) | |
{ | |
vector3D_t vec; | |
vec.v[0]=pnt.x; | |
vec.v[1]=pnt.y; | |
vec.v[2]=pnt.z; | |
vec.v[3]=1.; | |
return vec; | |
} | |
//func write | |
void bitmap_output(int x, int y, char *string, void *font) | |
{ | |
int len, i; | |
glRasterPos2f(x, y); | |
len = (int) strlen(string); | |
for (i = 0; i < len; i++) { | |
glutBitmapCharacter(font, string[i]); | |
} | |
} | |
vector3D_t homogenizeVector(vector3D_t vec) | |
{ | |
int i; | |
for (i=0;i<3;i++) { | |
vec.v[i]/=vec.v[3]; | |
} | |
vec.v[3]=1.; | |
return vec; | |
} | |
vector3D_t unitVector(vector3D_t vec) | |
{ | |
int i; | |
float vec2=0.; | |
float vec1,invvec1; | |
for (i=0;i<3;i++) { | |
vec2+=vec.v[i]*vec.v[i]; | |
} | |
vec1=sqrt(vec2); | |
if (vec1!=0.) { | |
invvec1=1./vec1; | |
for (i=0;i<3;i++) { | |
vec.v[i]*=invvec1; | |
} | |
} | |
vec.v[3]=1.; | |
return vec; | |
} | |
// inner product (dot product) of homogeneous vector | |
float operator * (vector3D_t a, vector3D_t b) | |
{ | |
float c;//c=a*b | |
int i; | |
c=0; | |
for (i=0;i<3;i++) { | |
c+=a.v[i]*b.v[i]; | |
} | |
return c; | |
} | |
// outer product (cross product ) of homogeneous vector | |
// i j k | |
// a0 a1 a2 | |
// b0 b1 b2 | |
vector3D_t operator ^ (vector3D_t a, vector3D_t b) | |
{ | |
vector3D_t c;//c=a*b | |
c.v[0]=a.v[1]*b.v[2]-a.v[2]*b.v[1]; | |
c.v[1]=a.v[2]*b.v[0]-a.v[0]*b.v[2]; | |
c.v[2]=a.v[0]*b.v[1]-a.v[1]*b.v[0]; | |
c.v[3]=1.; | |
return c; | |
} | |
vector3D_t operator - (vector3D_t v1,vector3D_t v0) | |
{ | |
vector3D_t c;//c=v1-v0 | |
c.v[0]=v1.v[0]-v0.v[0]; | |
c.v[1]=v1.v[1]-v0.v[1]; | |
c.v[2]=v1.v[2]-v0.v[2]; | |
c.v[3]=1.; | |
return c; | |
} | |
vector3D_t operator - (vector3D_t v) | |
{ | |
vector3D_t c;//c=-v | |
c.v[0]=-v.v[0]; | |
c.v[1]=-v.v[1]; | |
c.v[2]=-v.v[2]; | |
c.v[3]=1.; | |
return c; | |
} | |
vector3D_t operator * (float r, vector3D_t b) | |
{ | |
vector3D_t c;//c=r*b | |
int i; | |
for (i=0;i<3;i++) { | |
c.v[i]=r*b.v[i]; | |
} | |
c.v[3]=1.; | |
return c; | |
} | |
vector3D_t operator * (vector3D_t b, float r) | |
{ | |
vector3D_t c;//c=r*b | |
int i; | |
for (i=0;i<3;i++) { | |
c.v[i]=r*b.v[i]; | |
} | |
c.v[3]=1.; | |
return c; | |
} | |
float funcPositive(float x) | |
{ | |
if (0.<x) return x; | |
else return 0.; | |
} | |
// x to yth power | |
float power(float x,float y) | |
{ | |
//ln z = y ln x z = exp (y ln x) | |
if (x==0.) return 0; | |
return exp(y*log(x)); | |
} | |
color_t operator + (color_t c1, color_t c2) | |
{ | |
color_t col; | |
col.r=c1.r+c2.r; | |
col.g=c1.g+c2.g; | |
col.b=c1.b+c2.b; | |
return col; | |
} | |
color_t operator * (float r, color_t c) | |
{ | |
color_t col; | |
col.r=r*c.r; | |
col.g=r*c.g; | |
col.b=r*c.b; | |
return col; | |
} | |
color_t operator * (color_t c, float r) | |
{ | |
color_t col; | |
col.r=r*c.r; | |
col.g=r*c.g; | |
col.b=r*c.b; | |
return col; | |
} | |
//PhongModel color calculation | |
// LightVector, NormalVector, ViewVector, ColorofObject | |
color_t PhongModel(vector3D_t Light,vector3D_t Normal,vector3D_t View,color_t col) | |
{ | |
float kspe=0.7; // specular reflection coefficient | |
float kdif=0.6; // diffuse reflection coefficient | |
float kamb=0.4; // ambient light coefficient | |
float tmp,NL,RV; | |
color_t ColWhite={1,1,1}; | |
vector3D_t ReflectionVector=(2.*(Light*Normal)*Normal)-Light; | |
tmp=Normal*Light; | |
NL=funcPositive(tmp); | |
tmp=ReflectionVector*View; | |
RV=funcPositive(tmp); | |
return kdif*NL*col+kspe*power(RV,4)*ColWhite+kamb*col; | |
} | |
////////////// End of matrices and vectors 3D ver 2 ////////////// | |
////////////// OpenGL drawShape Functions ver 1 ///////////////// | |
void setColor(float red,float green,float blue) | |
{ | |
glColor3f(red, green, blue); | |
} | |
void setColor(color_t col) | |
{ | |
glColor3f(col.r, col.g, col.b); | |
} | |
void drawDot(float x,float y) | |
{ | |
glBegin(GL_POINTS); | |
glVertex2f(x, y); | |
glEnd(); | |
} | |
void drawLine(float x1, float y1, float x2, float y2) | |
{ | |
glBegin(GL_LINES); | |
glVertex2f(x1, y1); | |
glVertex2f(x2, y2); | |
glEnd(); | |
} | |
void drawLine(point2D_t p1,point2D_t p2) | |
{ | |
drawLine(p1.x,p1.y,p2.x,p2.y); | |
} | |
//n: number of points | |
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(); | |
} | |
//n: number of vertices | |
void drawPolygon(point2D_t pnt[],int n) | |
{ | |
int i; | |
glBegin(GL_LINE_LOOP); | |
for (i=0;i<n;i++) { | |
glVertex2f(pnt[i].x, pnt[i].y); | |
} | |
glEnd(); | |
} | |
// The function fillPolygon can fills only convex polygons | |
//n: number of vertices | |
void fillPolygon(point2D_t pnt[],int n,color_t color) | |
{ | |
int i; | |
setColor(color); | |
glBegin(GL_POLYGON); | |
for (i=0;i<n;i++) { | |
glVertex2f(pnt[i].x, pnt[i].y); | |
} | |
glEnd(); | |
} | |
// The function gradatePolygon can fills only convex polygons | |
// The vertices will be painted with corresponding given colors. | |
// The points inside the polygon will be painted with the mixed color. | |
//n: number of vertices | |
void gradatePolygon(point2D_t pnt[],color_t col[],int num) | |
{ | |
int i; | |
glBegin(GL_POLYGON); | |
for (i=0;i<num;i++) { | |
setColor(col[i]); | |
glVertex2f(pnt[i].x, pnt[i].y); | |
} | |
glEnd(); | |
} | |
//////////// End of OpenGL drawShape Functions ver 1 //////////// | |
void userdraw(void); | |
void display(void) | |
{ | |
glClear( GL_COLOR_BUFFER_BIT); | |
userdraw(); | |
glutSwapBuffers(); | |
} | |
////////////////////////////////////////////////////////////////// | |
void drawcharX(float x,float y) | |
{ | |
drawLine(x,y,x+10,y+12);drawLine(x,y+12,x+10,y); | |
} | |
void drawcharY(float x,float y) | |
{ | |
drawLine(x+5,y,x+5,y+7);drawLine(x,y+12,x+5,y+7);drawLine(x+10,y+12,x+5,y+7); | |
} | |
void drawcharZ(float x,float y) | |
{ | |
drawLine(x,y+12,x+10,y+12);drawLine(x+10,y+12,x,y);drawLine(x,y,x+10,y); | |
} | |
void drawAxes(matrix3D_t view) | |
{ | |
#define HALFAXIS 220 | |
#define HALFAXIS1 (HALFAXIS-10) | |
point3D_t axes[14]={ | |
{-HALFAXIS,0,0},{HALFAXIS,0,0},{HALFAXIS1,5,0},{HALFAXIS1,0,0},{0,0,0}, | |
{0,-HALFAXIS,0},{0,HALFAXIS,0},{0,HALFAXIS1,5},{0,HALFAXIS1,0},{0,0,0}, | |
{0,0,-HALFAXIS},{0,0,HALFAXIS},{5,0,HALFAXIS1},{0,0,HALFAXIS1} | |
}; | |
vector3D_t vec[14]; | |
point2D_t buff[14]; | |
int i; | |
for (i=0;i<14;i++) { | |
vec[i]=Point2Vector(axes[i]); | |
vec[i]=view*vec[i]; | |
buff[i]=Vector2Point2D(vec[i]); | |
} | |
drawPolyline(buff,14); | |
drawcharX(buff[1].x,buff[1].y); | |
drawcharY(buff[6].x,buff[6].y); | |
drawcharZ(buff[11].x-14,buff[11].y); | |
} | |
////////////////////////////////////////////////////////////////// | |
typedef struct { | |
int NumberofVertices; //in the face | |
short int pnt[50]; | |
color_t col; | |
} face_t; | |
typedef struct { | |
int NumberofVertices; //menyatakan jumlah titik yang membentuk obyek | |
point3D_t pnt[2096]; //titik yang membentuk face, dengan max ......? | |
color_t col[1600]; | |
int NumberofFaces; //menyatakan jumlah face yang membentuk obyek; | |
face_t fc[1024]; //menyatkan face2 yang membentuk obyek | |
float z; | |
} object3D_t; | |
float zRata(object3D_t obyek,matrix3D_t mat){ | |
int i; | |
float z=0; | |
vector3D_t vec; | |
for(i=0;i<obyek.NumberofVertices;i++){ | |
vec=Point2Vector(obyek.pnt[i]); | |
vec=mat*vec; | |
z=z+vec.v[2]; | |
} | |
z=z/obyek.NumberofVertices; | |
return z; | |
} | |
void draw3D(object3D_t obyek, matrix3D_t mat, color_t warna) | |
{ | |
int i,j; | |
vector3D_t vec[1000],vb[360]; | |
vector3D_t LightVector={0,0,1,1}; | |
vector3D_t ViewVector={0,0,1,1}; | |
vector3D_t NormalVector; | |
point2D_t p[360]; | |
color_t fillcol,wb={0.1,0.1,0.2}; | |
for(i=0;i<obyek.NumberofVertices;i++) | |
{ | |
vec[i]=Point2Vector(obyek.pnt[i]); | |
vec[i]=mat*vec[i]; | |
} | |
// Face invisible | |
for(i=0;i<obyek.NumberofFaces;i++) | |
{ | |
for(j=0;j<obyek.fc[i].NumberofVertices;j++) | |
{ | |
vb[j]=vec[obyek.fc[i].pnt[j]]; | |
p[j]=Vector2Point2D(vb[j]); | |
} | |
NormalVector=(vb[1]-vb[0])^(vb[2]-vb[0]); | |
if(NormalVector.v[2]<=0.) | |
{ | |
NormalVector=unitVector(NormalVector); | |
fillcol=PhongModel(LightVector,NormalVector,ViewVector,wb); | |
fillPolygon(p,obyek.fc[i].NumberofVertices,fillcol); | |
} | |
} | |
// face visible | |
for(i=0;i<obyek.NumberofFaces;i++) | |
{ | |
for(j=0;j<obyek.fc[i].NumberofVertices;j++) | |
{ | |
vb[j]=vec[obyek.fc[i].pnt[j]]; | |
p[j]=Vector2Point2D(vb[j]); | |
} | |
NormalVector=(vb[1]-vb[0])^(vb[2]-vb[0]); | |
if(NormalVector.v[2]>0.) | |
{ | |
NormalVector=unitVector(NormalVector); | |
fillcol=PhongModel(LightVector,NormalVector,ViewVector,warna); | |
fillPolygon(p,obyek.fc[i].NumberofVertices,fillcol); | |
} | |
} | |
} | |
void draw3Dw(object3D_t obyek,matrix3D_t mat,color_t col){ | |
vector3D_t vec[1600], vecbuff[50]; | |
vector3D_t vecNormal; | |
vector3D_t lightVector={0,0,1,1},viewVector={0,0,1,1}; | |
color_t colbuff; | |
point2D_t p[50]; | |
int i,j; | |
for(i=0;i<obyek.NumberofVertices;i++){ | |
vec[i]=Point2Vector(obyek.pnt[i]); | |
vec[i]=mat*vec[i]; | |
} | |
for(i=0;i<obyek.NumberofFaces;i++){ | |
for(j=0;j<obyek.fc[i].NumberofVertices;j++) | |
vecbuff[j]=vec[obyek.fc[i].pnt[j]]; | |
vecNormal=(vecbuff[1]-vecbuff[0])^(vecbuff[2]-vecbuff[0]); | |
if(vecNormal.v[2]<0){ | |
for(j=0;j<obyek.fc[i].NumberofVertices;j++){ | |
p[j]=Vector2Point2D(vecbuff[j]); | |
} | |
vecNormal=unitVector(vecNormal); | |
colbuff=PhongModel(lightVector,vecNormal,viewVector,col); | |
fillPolygon(p,obyek.fc[i].NumberofVertices,colbuff); | |
} | |
} | |
for(i=0;i<obyek.NumberofFaces;i++){ | |
for(j=0;j<obyek.fc[i].NumberofVertices;j++) | |
vecbuff[j]=vec[obyek.fc[i].pnt[j]]; | |
vecNormal=(vecbuff[1]-vecbuff[0])^(vecbuff[2]-vecbuff[0]); | |
if(vecNormal.v[2]>=0){ | |
for(j=0;j<obyek.fc[i].NumberofVertices;j++){ | |
p[j]=Vector2Point2D(vecbuff[j]); | |
} | |
vecNormal=unitVector(vecNormal); | |
colbuff=PhongModel(lightVector,vecNormal,viewVector,col); | |
fillPolygon(p,obyek.fc[i].NumberofVertices,colbuff); | |
} | |
} | |
} | |
void draw3Dc(object3D_t obyek,matrix3D_t mat){ | |
vector3D_t vec[1600], vecbuff[50]; | |
vector3D_t vecNormal; | |
vector3D_t lightVector={0,0,1,1},viewVector={0,0,1,1}; | |
color_t colbuff; | |
point2D_t p[50]; | |
int i,j; | |
for(i=0;i<obyek.NumberofVertices;i++){ | |
vec[i]=Point2Vector(obyek.pnt[i]); | |
vec[i]=mat*vec[i]; | |
} | |
for(i=0;i<obyek.NumberofFaces;i++){ | |
for(j=0;j<obyek.fc[i].NumberofVertices;j++) | |
vecbuff[j]=vec[obyek.fc[i].pnt[j]]; | |
vecNormal=(vecbuff[1]-vecbuff[0])^(vecbuff[2]-vecbuff[0]); | |
if(vecNormal.v[2]<0){ | |
for(j=0;j<obyek.fc[i].NumberofVertices;j++){ | |
p[j]=Vector2Point2D(vecbuff[j]); | |
} | |
vecNormal=unitVector(vecNormal); | |
colbuff=PhongModel(lightVector,vecNormal,viewVector,obyek.fc[i].col); | |
fillPolygon(p,obyek.fc[i].NumberofVertices,colbuff); | |
} | |
} | |
for(i=0;i<obyek.NumberofFaces;i++){ | |
for(j=0;j<obyek.fc[i].NumberofVertices;j++) | |
vecbuff[j]=vec[obyek.fc[i].pnt[j]]; | |
vecNormal=(vecbuff[1]-vecbuff[0])^(vecbuff[2]-vecbuff[0]); | |
if(vecNormal.v[2]>=0){ | |
for(j=0;j<obyek.fc[i].NumberofVertices;j++){ | |
p[j]=Vector2Point2D(vecbuff[j]); | |
} | |
vecNormal=unitVector(vecNormal); | |
colbuff=PhongModel(lightVector,vecNormal,viewVector,obyek.fc[i].col); | |
fillPolygon(p,obyek.fc[i].NumberofVertices,colbuff); | |
} | |
} | |
} | |
static void makeCone(object3D_t &kerucut,float h,float r,int n){ | |
float teta=6.28/n; | |
int i; | |
kerucut.NumberofVertices=n+1; | |
kerucut.pnt[0].x=0; | |
kerucut.pnt[0].y=h; | |
kerucut.pnt[0].z=0; | |
for(i=0;i<n;i++){ | |
kerucut.pnt[i+1].x=r*cos(i*teta); | |
kerucut.pnt[i+1].z=r*sin(i*teta); | |
kerucut.pnt[i+1].y=0; | |
} | |
kerucut.NumberofFaces=n+1; | |
for(i=0;i<n-1;i++){ | |
kerucut.fc[i].NumberofVertices=3; | |
kerucut.fc[i].pnt[0]=0; | |
kerucut.fc[i].pnt[1]=i+2; | |
kerucut.fc[i].pnt[2]=i+1; | |
} | |
kerucut.fc[n-1].NumberofVertices=3; | |
kerucut.fc[n-1].pnt[0]=0; | |
kerucut.fc[n-1].pnt[1]=1; | |
kerucut.fc[n-1].pnt[2]=n; | |
kerucut.fc[n].NumberofVertices=n; | |
for(i=0;i<n;i++){ | |
kerucut.fc[n].pnt[i]=i+1; | |
} | |
} | |
static void makeCylinder(object3D_t &silinder,float h, float r, int n){ | |
float a=6.28/n; | |
int i; | |
silinder.NumberofVertices=2*n; | |
for(i=0;i<n;i++){ | |
silinder.pnt[i].x=r*cos((float)i*a); | |
silinder.pnt[i].z=r*sin((float)i*a); | |
silinder.pnt[i].y=0; | |
silinder.pnt[n+i].x=r*cos((float)i*a); | |
silinder.pnt[n+i].z=r*sin((float)i*a); | |
silinder.pnt[n+i].y=h; | |
} | |
silinder.NumberofFaces=n+2; | |
for(i=0;i<n-1;i++){ | |
silinder.fc[i].NumberofVertices=4; | |
silinder.fc[i].pnt[0]=i; | |
silinder.fc[i].pnt[1]=n+i; | |
silinder.fc[i].pnt[2]=n+i+1; | |
silinder.fc[i].pnt[3]=i+1; | |
} | |
silinder.fc[n-1].NumberofVertices=4; | |
silinder.fc[n-1].pnt[0]=n; | |
silinder.fc[n-1].pnt[1]=0; | |
silinder.fc[n-1].pnt[2]=n-1; | |
silinder.fc[n-1].pnt[3]=2*n-1; | |
silinder.fc[n].NumberofVertices=n; | |
for(i=0;i<n;i++) | |
silinder.fc[n].pnt[i]=2*n-i-1; | |
silinder.fc[n+1].NumberofVertices=n; | |
for(i=0;i<n;i++) | |
silinder.fc[n+1].pnt[i]=i; | |
} | |
static void makeCylinderN(object3D_t &silinder,float h[],float r[], int n, int m) | |
{ | |
float d=6.28/n; | |
int i,j; | |
for(j=0;j<m;j++) | |
for(i=0;i<n;i++) { | |
silinder.pnt[i+j*n].x=r[j]*cos(i*d); | |
silinder.pnt[i+j*n].z=r[j]*sin(i*d); | |
if(j==0) | |
silinder.pnt[i+j*n].y=0; | |
else | |
silinder.pnt[i+j*n].y=h[j-1]; | |
} | |
silinder.NumberofVertices=m*n; | |
for(j=0;j<m-1;j++){ | |
for(i=0;i<n-1;i++) { | |
silinder.fc[i+j*n].NumberofVertices=4; | |
silinder.fc[i+j*n].pnt[0]=j*n+i; | |
silinder.fc[i+j*n].pnt[1]=(j+1)*n+i; | |
silinder.fc[i+j*n].pnt[2]=(j+1)*n+i+1; | |
silinder.fc[i+j*n].pnt[3]=j*n+i+1; | |
} | |
silinder.fc[(j+1)*n-1].NumberofVertices=4; | |
silinder.fc[(j+1)*n-1].pnt[0]=(j+1)*n-1; | |
silinder.fc[(j+1)*n-1].pnt[1]=(j+2)*n-1; | |
silinder.fc[(j+1)*n-1].pnt[2]=(j+1)*n; | |
silinder.fc[(j+1)*n-1].pnt[3]=j*n; | |
} | |
silinder.fc[(m-1)*n].NumberofVertices=n; | |
for(i=0;i<n;i++) silinder.fc[(m-1)*n].pnt[i]=i; | |
silinder.fc[(m-1)*n+1].NumberofVertices=n; | |
for(i=0;i<n;i++) silinder.fc[(m-1)*n+1].pnt[i]=m*n-1-i; | |
silinder.NumberofFaces=(m-1)*n+2; | |
} | |
float findZ(object3D_t obyek,matrix3D_t mat){ | |
vector3D_t vec; | |
int i; | |
float z=0; | |
for(i=0;i<obyek.NumberofVertices;i++) | |
{ | |
vec=Point2Vector(obyek.pnt[i]); | |
vec=mat*vec; | |
z=z+vec.v[2]; | |
} | |
return z/obyek.NumberofVertices; | |
} | |
static void makeBall(object3D_t &bola,float r, int n, int m) | |
{ | |
float d=6.28/n,rl,h; | |
int i,j; | |
for(j=0;j<m;j++){ | |
h=r*cos(j*3.14/(m-1)); | |
rl=r*sin(j*3.14/(m-1)); | |
for(i=0;i<n;i++) { | |
bola.pnt[i+j*n].x=rl*cos(i*d); | |
bola.pnt[i+j*n].z=rl*sin(i*d); | |
bola.pnt[i+j*n].y=h; | |
}} | |
bola.NumberofVertices=m*n; | |
for(j=0;j<m-1;j++){ | |
for(i=0;i<n-1;i++) { | |
bola.fc[i+j*n].NumberofVertices=4; | |
bola.fc[i+j*n].pnt[0]=j*n+i; | |
bola.fc[i+j*n].pnt[1]=(j+1)*n+i; | |
bola.fc[i+j*n].pnt[2]=(j+1)*n+i+1; | |
bola.fc[i+j*n].pnt[3]=j*n+i+1; | |
} | |
bola.fc[(j+1)*n-1].NumberofVertices=4; | |
bola.fc[(j+1)*n-1].pnt[0]=(j+1)*n-1; | |
bola.fc[(j+1)*n-1].pnt[1]=(j+2)*n-1; | |
bola.fc[(j+1)*n-1].pnt[2]=(j+1)*n; | |
bola.fc[(j+1)*n-1].pnt[3]=j*n; | |
} | |
bola.NumberofFaces=(m-1)*n; | |
} | |
static void makeSetBall(object3D_t &bola,float r, int n, int m) | |
{ | |
float d=3.14/n,rl,h; | |
int i,j; | |
for(j=0;j<m;j++){ | |
h=r*cos(j*3.14/(m-1)); | |
rl=r*sin(j*3.14/(m-1)); | |
for(i=0;i<n;i++) { | |
bola.pnt[i+j*n].x=rl*cos(i*d); | |
bola.pnt[i+j*n].z=rl*sin(i*d); | |
bola.pnt[i+j*n].y=h; | |
}} | |
bola.NumberofVertices=m*n; | |
for(j=0;j<m-1;j++){ | |
for(i=0;i<n-1;i++) { | |
bola.fc[i+j*n].NumberofVertices=4; | |
bola.fc[i+j*n].pnt[0]=j*n+i; | |
bola.fc[i+j*n].pnt[1]=(j+1)*n+i; | |
bola.fc[i+j*n].pnt[2]=(j+1)*n+i+1; | |
bola.fc[i+j*n].pnt[3]=j*n+i+1; | |
} | |
bola.fc[(j+1)*n-1].NumberofVertices=4; | |
bola.fc[(j+1)*n-1].pnt[0]=(j+1)*n-1; | |
bola.fc[(j+1)*n-1].pnt[1]=(j+2)*n-1; | |
bola.fc[(j+1)*n-1].pnt[2]=(j+1)*n; | |
bola.fc[(j+1)*n-1].pnt[3]=j*n; | |
} | |
bola.NumberofFaces=(m-1)*n; | |
} | |
void dasar() { | |
point2D_t lgt[4] = {{-400, 300}, {-400, -100}, {400, -100}, {400, 300}}; | |
point2D_t jalan[4]={{-400, -100}, {-400, -300}, {400, -300}, {400, -100}}; | |
color_t col[4] = {{0.9,0.7,0.1}, {0,0.4,1},{0.0,0.4,1},{0.9,0.8,0.1}}; | |
color_t abu2={0.4,0.4,0.4}; | |
gradatePolygon(lgt,col,4); | |
fillPolygon(jalan,4,abu2); | |
} | |
void pohon(){ | |
float r[4]={20,20}; | |
float h[4]={50,20}; | |
color_t wrn={0.5,0.4,0.4}; | |
color_t krem={0.93,1.96,0.57}; | |
object3D_t silinder; | |
matrix3D_t mat; | |
matrix3D_t tilting=rotationXMTX(0.2); | |
mat=tilting*rotationYMTX(1)*translationMTX(-190.,-70.,0.); | |
makeCylinderN(silinder,h,r,20,2); | |
draw3Dw(silinder,mat,wrn); | |
float r1[2]={50,0.2}; | |
float h1[2]={40,40}; | |
color_t daun={0,1,0}; | |
object3D_t silinder1; | |
matrix3D_t mat1; | |
matrix3D_t tilting1=rotationXMTX(0.2); | |
//pohon 1 | |
mat1=tilting1*rotationYMTX(1)*scalingMTX(2.5,2.5,2.5)*translationMTX(-75.,0.,0.); | |
makeCylinderN(silinder1,h1,r1,20,2); | |
draw3Dw(silinder1,mat1,daun); | |
mat1=tilting1*rotationYMTX(1)*scalingMTX(2.3,2.3,2.3)*translationMTX(-82.,30.,0.); | |
makeCylinderN(silinder1,h1,r1,20,2); | |
draw3Dw(silinder1,mat1,daun); | |
mat1=tilting1*rotationYMTX(1)*scalingMTX(2,2,2)*translationMTX(-90.,60.,0.); | |
makeCylinderN(silinder1,h1,r1,20,2); | |
draw3Dw(silinder1,mat1,daun); | |
mat1=tilting1*rotationYMTX(1)*scalingMTX(1.8,1.8,1.8)*translationMTX(-94.,95.,0.); | |
makeCylinderN(silinder1,h1,r1,20,2); | |
draw3Dw(silinder1,mat1,daun); | |
mat1=tilting1*rotationYMTX(1)*scalingMTX(1.2,1.2,1.2)*translationMTX(-140.,180.,0.); | |
makeCylinderN(silinder1,h1,r1,20,2); | |
draw3Dw(silinder1,mat1,daun); | |
//pohon 2 | |
mat=tilting*rotationYMTX(1)*translationMTX(400.,-170.,0.); | |
makeCylinderN(silinder,h,r,20,2); | |
draw3Dw(silinder,mat,wrn); | |
mat1=tilting1*rotationYMTX(1)*scalingMTX(2.3,2.3,2.3)*translationMTX(175.,-45.,0.); | |
makeCylinderN(silinder1,h1,r1,20,2); | |
draw3Dw(silinder1,mat1,daun); | |
mat1=tilting1*rotationYMTX(1)*scalingMTX(2,2,2)*translationMTX(205.,-30.,0.); | |
makeCylinderN(silinder1,h1,r1,20,2); | |
draw3Dw(silinder1,mat1,daun); | |
mat1=tilting1*rotationYMTX(1)*scalingMTX(1.8,1.8,1.8)*translationMTX(230.,-10.,0.); | |
makeCylinderN(silinder1,h1,r1,20,2); | |
draw3Dw(silinder1,mat1,daun); | |
mat1=tilting1*rotationYMTX(1)*scalingMTX(1.4,1.4,1.4)*translationMTX(290.,20.,0.); | |
makeCylinderN(silinder1,h1,r1,20,2); | |
draw3Dw(silinder1,mat1,daun); | |
mat1=tilting1*rotationYMTX(1)*scalingMTX(1.2,1.2,1.2)*translationMTX(340.,50.,0.); | |
makeCylinderN(silinder1,h1,r1,20,2); | |
draw3Dw(silinder1,mat1,daun); | |
} | |
void orang(){ | |
static float kec=0.01; | |
static float tick=0,tick1=0,tick2=0,tick3=0, tick4=0, tick5=0; | |
float high[4]={100,160,200}; | |
float jari2link[4]={30,20,20,10}; | |
static int timer=0; | |
matrix3D_t mat[13]; | |
color_t warna[1]={0.7,0.7,0.7}; | |
static object3D_t silinder,kubus,bola,kerucut,bola2,silinder2; | |
static object3D_t silinder_2,kubus_2,bola_2,kerucut_2,bola2_2,silinder2_2, silinder_3; | |
if(tick==0){ | |
makeCylinderN(silinder2,high,jari2link,20,4); | |
makeCylinder(silinder,100,50,20); | |
makeCylinder(kubus,100,70,4); | |
makeBall(bola,30,20,20); | |
makeCylinderN(silinder2_2,high,jari2link,20,4); | |
makeCylinder(silinder_2,100,50,20); | |
makeCylinder(silinder_3,100,50,20); | |
makeCylinder(kubus_2,100,70,4); | |
makeBall(bola_2,30,20,20); | |
} | |
mat[0]=translationMTX(-250,-50,0)*translationMTX(tick4,0,0)*rotationZMTX(0.15)*scalingMTX(0.25,1.0,0.5); | |
mat[1]=translationMTX(-250,-50,0)*translationMTX(tick4,0,0)*rotationZMTX(2.5)*rotationZMTX(tick5)*scalingMTX(0.25,1.5,0.25); | |
mat[2]=translationMTX(-250,-50,0)*translationMTX(tick4,0,0)*rotationZMTX(3.7)*rotationZMTX(-tick5)*scalingMTX(0.25,1.5,0.25); | |
mat[3]=translationMTX(-250,-50,0)*translationMTX(tick4,0,0)*rotationZMTX(2.5)*scalingMTX(0.5,0.5,0.5); | |
mat[4]=translationMTX(-265,73,0)*translationMTX(tick4,0,0)*scalingMTX(0.8,0.8,0.8); | |
mat[5]=translationMTX(-265,30,0)*translationMTX(tick4,0,0)*rotationZMTX(7.4)*rotationZMTX(-tick2)*scalingMTX(0.25,0.75,0.25); | |
// mat[6]=translationMTX(230,-150,-300)*translationMTX(tick3,0,0)*rotationZMTX(0.15)*scalingMTX(0.25,1.0,0.5); | |
// mat[7]=translationMTX(230,-150,-300)*translationMTX(tick3,0,0)*rotationZMTX(2.5)*rotationZMTX(tick2)*scalingMTX(0.25,1.5,0.25); | |
// mat[8]=translationMTX(230,-150,-300)*translationMTX(tick3,0,0)*rotationZMTX(3.7)*rotationZMTX(-tick2)*scalingMTX(0.25,1.5,0.25); | |
// mat[9]=translationMTX(230,-150,-300)*translationMTX(tick3,0,0)*rotationZMTX(2.5)*scalingMTX(0.5,0.5,0.5); | |
// mat[10]=translationMTX(215,-27,-300)*translationMTX(tick3,0,0)*scalingMTX(0.8,0.8,0.8); | |
// mat[11]=translationMTX(215,-70,-300)*translationMTX(tick3,0,0)*rotationZMTX(3.7)*rotationZMTX(-tick2)*scalingMTX(0.25,0.75,0.25); | |
// mat[12]=translationMTX(215,-70,100)*translationMTX(tick3,0,0)*rotationZMTX(-3.7)*rotationZMTX(-tick2)*scalingMTX(0.25,0.75,0.25); | |
mat[6]=translationMTX(230,-50,0)*translationMTX(tick4,0,0)*rotationZMTX(0.15)*scalingMTX(0.25,1.0,0.5); | |
mat[7]=translationMTX(230,-50,0)*translationMTX(tick4,0,0)*rotationZMTX(2.5)*rotationZMTX(tick5)*scalingMTX(0.25,1.5,0.25); | |
mat[8]=translationMTX(230,-50,0)*translationMTX(tick4,0,0)*rotationZMTX(3.7)*rotationZMTX(-tick5)*scalingMTX(0.25,1.5,0.25); | |
mat[9]=translationMTX(230,-50,0)*translationMTX(tick4,0,0)*rotationZMTX(2.5)*scalingMTX(0.5,0.5,0.5); | |
mat[10]=translationMTX(215,73,0)*translationMTX(tick4,0,0)*scalingMTX(0.8,0.8,0.8); | |
mat[11]=translationMTX(215,30,0)*translationMTX(tick4,0,0)*rotationZMTX(7.4)*rotationZMTX(-tick2)*scalingMTX(0.25,0.75,0.25); | |
draw3D(silinder,mat[2],warna[0]); | |
draw3D(kubus,mat[0],warna[0]); | |
draw3D(bola,mat[4],warna[0]); | |
draw3D(silinder,mat[1],warna[0]); | |
draw3D(bola,mat[3],warna[0]); | |
draw3D(silinder,mat[5],warna[0]); | |
draw3D(silinder_2,mat[8],warna[0]); | |
draw3D(kubus_2,mat[6],warna[0]); | |
draw3D(bola_2,mat[10],warna[0]); | |
draw3D(silinder_2,mat[7],warna[0]); | |
draw3D(bola_2,mat[9],warna[0]); | |
draw3D(silinder_2,mat[11],warna[0]); | |
draw3D(silinder_3,mat[12],warna[0]); | |
tick2=tick2+kec; | |
tick+=.1; | |
if(tick>=900) tick=-800; | |
if(tick2<=0) kec=0.01; | |
else if(tick2>=1.3) kec=-0.01; | |
tick3+=-2; | |
if(tick3<=-900) tick3=800; | |
} | |
void sepada(){ | |
static float tick=0,tick1=0,tick2=0,tick3; | |
static float kec=-2; | |
float high[4]={100,160,200}; | |
float jari2link[4]={30,20,20,10}; | |
float r1[2]={50,0.2}; | |
float h1[2]={40,40}; | |
static int timer=0; | |
matrix3D_t mat[100]; | |
color_t warna[6]={{0.6,0.5,0.3},{0.7,0.7,0.7},{0.5,0,0},{0,0.5,0},{0.2,0.2,1},{0.2,0.5,0.8}}; | |
static object3D_t silinder,kubus,bola,kerucut,bola2,silinder1,silinder2; | |
if(tick==0){ | |
makeCylinderN(silinder2,high,jari2link,20,4); | |
makeCylinder(silinder,100,50,20); | |
makeCylinder(kubus,100,70,4); | |
makeBall(bola,30,20,20); | |
makeSetBall(bola2,30,20,20); | |
makeCone(kerucut,70,70,10); | |
makeCylinderN(silinder1,h1,r1,20,2); | |
} | |
//sepeda | |
mat[1]=translationMTX(-250,-200,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(5)*rotationYMTX(1)*scalingMTX(1.5,0.15,1.5); | |
mat[2]=translationMTX(0,-200,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(5)*rotationYMTX(1)*scalingMTX(1.5,0.15,1.5); | |
mat[3]=translationMTX(-250,-200,0)*translationMTX(tick1,0,0)*rotationZMTX(4.7)*scalingMTX(0.15,2.5,0.5); | |
mat[4]=translationMTX(-90,-200,0)*translationMTX(tick1,0,0)*scalingMTX(0.15,1.0,0.5); | |
mat[5]=translationMTX(0,-200,0)*translationMTX(tick1,0,0)*rotationZMTX(1)*scalingMTX(0.15,1.0,0.5); | |
mat[6]=translationMTX(-250,-200,0)*translationMTX(tick1,0,0)*scalingMTX(0.5,0.5,0.5); | |
mat[7]=translationMTX(0,-200,0)*translationMTX(tick1,0,0)*scalingMTX(0.5,0.5,0.5); | |
mat[8]=translationMTX(-90,-100,0)*translationMTX(tick1,0,0)*scalingMTX(0.5,0.25,0.5); | |
mat[9]=translationMTX(-250,-200,0)*translationMTX(tick1,0,0)*scalingMTX(0.15,1.4,0.5); | |
mat[10]=translationMTX(-250,-120,0)*translationMTX(tick1,0,0)*rotationZMTX(4.7)*scalingMTX(0.15,1.6,0.5); | |
mat[11]=translationMTX(-150,-200,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(5)*rotationYMTX(1)*scalingMTX(0.5,0.15,0.5); | |
mat[12]=translationMTX(-200,-50,0)*translationMTX(tick1,0,0)*rotationXMTX(2)*rotationZMTX(0.9)*scalingMTX(0.15,1.3,0.15); | |
//orang naik sepada | |
mat[13]=translationMTX(-90,-75,0)*translationMTX(tick1,0,0)*rotationYMTX(1)*rotationZMTX(0.6)*scalingMTX(0.25,1.3,0.5); | |
mat[14]=translationMTX(-130,60,0)*translationMTX(tick1,0,0)*rotationZMTX(5)*rotationYMTX(1)*scalingMTX(1.0,1.0,1.0); | |
mat[15]=translationMTX(-110,0,0)*translationMTX(tick1,0,0)*rotationZMTX(2)*rotationYMTX(0)*scalingMTX(0.25,1.0,0.25); | |
mat[16]=translationMTX(-200,-40,0)*translationMTX(tick1,0,0)*scalingMTX(0.5,0.5,0.5); | |
mat[17]=translationMTX(-110,0,0)*translationMTX(tick1,0,0)*rotationZMTX(3)*rotationYMTX(0)*scalingMTX(0.25,1.0,0.25); | |
mat[18]=translationMTX(-125,-90,0)*translationMTX(tick1,0,0)*scalingMTX(0.5,0.5,0.5); | |
mat[19]=translationMTX(-80,-80,0)*translationMTX(tick1,0,0)*rotationZMTX(2+tick3)*rotationYMTX(0)*scalingMTX(0.25,0.85,0.25); | |
mat[20]=translationMTX(tick2,tick2,0)*translationMTX(-150,-200,0)*translationMTX(tick1,0,0)*scalingMTX(0.25,0.75,0.25); | |
mat[21]=translationMTX(tick2,tick2,0)*translationMTX(-150,-115,0)*translationMTX(tick1,0,0)*scalingMTX(0.5,0.5,0.5); | |
mat[22]=translationMTX(-80,-80,0)*translationMTX(tick1,0,0)*rotationZMTX(2-tick3)*rotationYMTX(0)*scalingMTX(0.25,0.75,0.25); | |
mat[23]=translationMTX(0,-tick2,0)*translationMTX(-150,-200,0)*translationMTX(tick1,0,0)*scalingMTX(0.25,0.75,0.25); | |
mat[24]=translationMTX(0,-tick2,0)*translationMTX(-150,-115,0)*translationMTX(tick1,0,0)*scalingMTX(0.5,0.5,0.5); | |
mat[25]=translationMTX(tick2,tick2,0)*translationMTX(-150,-210,0)*translationMTX(tick1,0,0)*scalingMTX(0.25,0.25,0.25); | |
mat[26]=translationMTX(-130,70,0)*translationMTX(tick1,0,0)*scalingMTX(1.2,1.2,1.2); | |
//sepeda | |
draw3D(silinder,mat[1],warna[4]); | |
draw3D(silinder,mat[2],warna[4]); | |
draw3D(silinder,mat[22],warna[1]); | |
draw3D(silinder,mat[23],warna[1]); | |
draw3D(bola,mat[24],warna[1]); | |
draw3D(silinder,mat[3],warna[2]); | |
draw3D(silinder,mat[10],warna[2]); | |
draw3D(silinder,mat[4],warna[2]); | |
draw3D(silinder,mat[5],warna[2]); | |
draw3D(silinder,mat[9],warna[2]); | |
draw3D(bola,mat[6],warna[5]); | |
draw3D(bola,mat[7],warna[5]); | |
draw3D(kubus,mat[8],warna[1]); | |
draw3D(silinder,mat[11],warna[3]); | |
draw3D(silinder,mat[12],warna[2]); | |
//orang naik sepada | |
draw3D(silinder,mat[15],warna[1]); | |
draw3D(bola,mat[16],warna[1]); | |
draw3D(kubus,mat[13],warna[1]); | |
draw3D(bola,mat[14],warna[1]); | |
draw3D(silinder,mat[19],warna[1]); | |
draw3D(silinder,mat[20],warna[1]); | |
draw3D(bola,mat[21],warna[1]); | |
draw3D(kubus,mat[25],warna[0]); | |
draw3D(silinder,mat[17],warna[1]); | |
draw3D(bola,mat[18],warna[1]); | |
draw3Dw(silinder1,mat[26],warna[1]); | |
tick+=0.1; | |
tick1+=-5; | |
if(tick1<=-500) tick1=800; | |
tick2=tick2+kec; | |
if(tick2<=-10) kec=2; | |
else if(tick2>=0) kec=-2; | |
} | |
void userdraw(void){ | |
static float tick=0,tick1=0,tick2=0,tick3; | |
static float kec=-2; | |
float high[4]={100,160,200}; | |
float jari2link[4]={30,20,20,10}; | |
float r1[2]={50,0.2}; | |
float h1[2]={40,40}; | |
static int timer=0; | |
matrix3D_t mat[100]; | |
color_t warna[6]={{0.6,0.5,0.3},{0.7,0.7,0.7},{0.5,0,0},{0,0.5,0},{0.2,0.2,1},{0.2,0.5,0.8}}; | |
static object3D_t silinder,kubus,bola,kerucut,bola2,silinder1,silinder2, kursi1bawah,kursi1atas, kursi2bawah, kursi2atas; | |
static object3D_t meja_atas, kaki1, kaki2, kaki3, kaki4; | |
if(tick==0){ | |
// kursi 1 | |
makeCylinder(kursi1bawah,500,50,20); | |
makeCylinder(kursi1atas,300,55,20); | |
// kursi 2 | |
makeCylinder(kursi2bawah,500,50,20); | |
makeCylinder(kursi2atas,300,55,20); | |
// atap meja | |
makeCylinder(kubus,100,110,20); | |
// kaki meja | |
makeCylinder(kaki2,600,10,10); | |
makeCylinder(kaki1,50,40,10); | |
// | |
} | |
//sepeda | |
// kursi 1 | |
mat[1]=translationMTX(-250,-100,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(-200)*rotationYMTX(1)*scalingMTX(1.5,0.15,1.5); | |
mat[2]=translationMTX(-250,-75,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(-200)*rotationYMTX(1)*scalingMTX(1.5,0.15,1.5); | |
// kursi 2 | |
mat[3]=translationMTX(250,0,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(-200)*rotationYMTX(1)*scalingMTX(1.5,0.15,1.5); | |
mat[4]=translationMTX(250,25,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(-200)*rotationYMTX(1)*scalingMTX(1.5,0.15,1.5); | |
// meja | |
// kursi 1 | |
mat[5]=translationMTX(0,100,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(-200)*rotationYMTX(1)*scalingMTX(1.5,0.15,1.5); | |
mat[6]=translationMTX(0,-70,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(-200)*rotationYMTX(1)*scalingMTX(1.5,0.15,1.5); | |
mat[7]=translationMTX(0,-90,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(-200)*rotationYMTX(1)*scalingMTX(1.5,0.15,1.5); | |
// mat[8]=translationMTX(0,0,0)*translationMTX(tick1,-20,0)*rotationZMTX(tick)*rotationXMTX(5)*rotationYMTX(200)*scalingMTX(1.5,0.15,1.5); | |
mat[8]=translationMTX(0,100,0)*translationMTX(tick1,-20,0)*rotationZMTX(tick)*rotationXMTX(10)*rotationYMTX(200)*scalingMTX(1.5,0.15,1.5); | |
mat[26]=translationMTX(-130,70,0)*translationMTX(tick1,0,0)*scalingMTX(1.2,1.2,1.2); | |
mat[11]=translationMTX(0,-70,0)*translationMTX(tick1,-20,3)*rotationZMTX(tick)*rotationXMTX(-100)*rotationYMTX(1); | |
// | |
mat[12]=translationMTX(-40,0,40)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(0)*rotationYMTX(1); | |
mat[13]=translationMTX(-250,-100,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(-200)*rotationYMTX(1)*scalingMTX(1.5,0.15,1.5); | |
mat[14]=translationMTX(-250,-100,0)*translationMTX(tick1,0,0)*rotationZMTX(tick)*rotationXMTX(-200)*rotationYMTX(1)*scalingMTX(1.5,0.15,1.5); | |
// membuat objek 2 kursi | |
// kursi 1 | |
draw3D(kursi1bawah,mat[1],warna[4]); | |
draw3D(kursi1atas,mat[2],warna[2]); | |
//kursi 2 | |
draw3D(kursi2bawah,mat[3],warna[4]); | |
draw3D(kursi2atas,mat[4],warna[2]); | |
// kaki meja | |
draw3D(kaki1,mat[7],warna[1]); | |
draw3D(kaki2,mat[6],warna[4]); | |
// meja | |
draw3D(kubus,mat[5],warna[1]); | |
// orang | |
orang(); | |
} | |
int main(int argc, char **argv){ | |
glutInit(&argc,argv); | |
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB ); | |
glutInitWindowPosition(100,100); | |
glutInitWindowSize(800,600); | |
glutCreateWindow ("Meja dan Kursi"); | |
glClearColor(0.0, 1.0, 0.24, 0.0); | |
gluOrtho2D(-800., 800., -600.0, 600.0); | |
// Define the dimensions of the Orthographic Viewing Volume | |
glutIdleFunc(display); // idle event call back | |
glutDisplayFunc(display); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment