Gedung 3dimensi Visual Studio

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

PROGRAM

#include<stdio.h>
#include<math.h>
#include<glut.h>
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;
//matrices and vectors 3D vec 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;
}
//Perkalian matrik dengan matrik
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;
}
//Perkalian matrik dengan vector,hasilnya vektor
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;

1
}
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 scallingMTX(float factorx,float factory,float factorz)
{
matrix3D_t scale=createIdentity();
scale.m[0][0]=factorx;
scale.m[1][1]=factory;
scale.m[2][2]=factory;
return scale;
}
point2D_t Vector2Point2D(vector3D_t vec)
{
point2D_t pnt;
pnt.x=vec.v[0];
pnt.y=vec.v[1];
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;
}
//inner product (dot product) of homogeous vector

2
float operator *(vector3D_t a, vector3D_t b)
{
float 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 homogeous vector
// i j l
// 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)

3
{
if(0.<x) return x;
else return 0;
}
//x to yth power
float power(float x, float y)
{
//ln z=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;
}
///End of matrices and vectors 3D ver 3//
//OpenGL drawShape Function ver1//
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_LINES);
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);
}

4
//n:number of points
void drawPolyline(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();
}
//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();
}
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();
}
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();
}
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);

5
}

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}, {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];
}face_t;

typedef struct {
int NumberofVertices; //of the object
point3D_t pnt[100];
int NumberofFaces; //of the object
face_t fc[50];
}object3D_t;

//Fungsi untuk membuat kode program untuk menghasilkan suatu gambar


void userdraw(void){
//mendeklarasikan variable tick dengan nilai awal 0
static int tick=0;
//mendeklarasikan variable theta dengan nilai awal -0,5 dan nilai theta =
0.1*sin(0.05*tick)-0.5
float theta=-0.5;
theta = 0.1*sin(0.05*tick)-0.5;
//Dalam deklarasi ini, matrik tilting adalah rotasi terhadap sumbu Y sebesar -0.5 rad
dan rotasi terhadap sumbu X sebesar 0.5 rad.
matrix3D_t tilting=rotationXMTX(0.5)*rotationYMTX(theta);
//untuk mengatur warna menjadi warna hitam
setColor(0,0,0);
//pemanggilan tilting pada fungsi drawAxes
drawAxes(tilting);
//untuk membuat objek prisma yang terdiri dari 5 vertek dan 5 sisi prisma. Setiap sisi
terdiri dari 3 vertek dan satu 4 vertek.
object3D_t kubus = {
8,
{
{50,70,50},{50,70,-50},{-50,70,-50},{-50,70,50},
{50,-70,50},{50,-70,-50},{-50,-70,-50}, {-50,-70,50},
},
6,

6
{
{4,{0,4,5,1}}, {4,{2,6,7,3}}, {4,{0,1,2,3}}, {4,{4,7,6,5}},
{4,{0,3,7,4}}, {4,{1,5,6,2}}
}
};

object3D_t kubus2 = {
4,
{{-55,0,0},{-35,0,0},{-35,25,0},{-55,25,0},},
1,
{{4,{0,1,2,3}}}
};

object3D_t kubus3 = {
4,
{{0,0,0},{-20,0,0},{-20,25,0},{0,25,0},},
1,
{{4,{0,1,2,3}}}
};

object3D_t kubus4 = {
4,
{{-55,-10,0},{-35,-10,0},{-35,-35,0},{-55,-35,0},},
1,
{{4,{0,1,2,3}}}
};

object3D_t kubus5 = {
4,
{{0,-10,0},{-20,-10,0},{-20,-35,0},{0,-35,0},},
1,
{{4,{0,1,2,3}}}
};

object3D_t pintu = {
4,
{{0,-50,0},{-25,-50,0},{-25,-100,0},{0,-100,0},},
1,
{{4,{0,1,2,3}}}
};

object3D_t pintuu = {
4,
{{0,-50,0},{-25,-50,0},{-25,-100,0},{0,-100,0},},
1,
{{4,{0,1,2,3}}}
};

object3D_t pintu2 = {
4,
{{-55,-50,0},{-25,-50,0},{-25,-100,0},{-55,-100,0},},
1,
{{4,{0,1,2,3}}}
};

object3D_t pintu2u = {
4,
{{-55,-50,0},{-25,-50,0},{-25,-100,0},{-55,-100,0},},
1,
{{4,{0,1,2,3}}}
};

7
object3D_t jendela1 = {
8,
{
{80,70,50},{80,70,20},{60,70,20},{60,70,50},
{80,45,50},{80,45,20},{60,45,20}, {60,45,50},
},
1,
{
{4,{2,6,7,3}}
}
};

object3D_t jendela2 = {
8,
{
{80,70,-20},{80,70,10},{60,70,10},{60,70,-20},
{80,45,-20},{80,45,10},{60,45,10}, {60,45,-20},
},
1,
{
{4,{2,6,7,3}}
}
};

object3D_t jendela3 = {
8,
{
{80,5,50},{80,5,20},{60,5,20},{60,5,50},
{80,30,50},{80,30,20},{60,30,20}, {60,30,50},
},
1,
{
{4,{2,6,7,3}}
}
};

object3D_t jendela4 = {
8,
{
{80,5,-20},{80,5,10},{60,5,10},{60,5,-20},
{80,30,-20},{80,30,10},{60,30,10}, {60,30,-20},
},
1,
{
{4,{2,6,7,3}}
}
};

object3D_t door = {
8,
{
{80,-10,50},{80,-10,20},{60,-10,20},{60,-10,50},
{80,-50,50},{80,-50,20},{60,-50,20}, {60,-50,50},
},
1,
{
{4,{2,6,7,3}}
}
};

object3D_t door2 = {
8,

8
{
{80,-10,-20},{80,-10,10},{60,-10,10},{60,-10,-20},
{80,-50,-20},{80,-50,10},{60,-50,10}, {60,-50,-20},
},
1,
{
{4,{2,6,7,3}}
}
};

vector3D_t vec[50];
point2D_t buff[50];
vector3D_t vecbuff[50];
vector3D_t NormalVector;
float normalz[50];
matrix3D_t trans,rot,mat,scale;
int i,j,loop;
color_t merah={0.8f,0.1f,0.2f};//gedung
color_t abu={0.6f,0.6f,0.6f};//pintu
color_t biru={0.7f,0.9f,0.9f};//jendela

setColor(0,0,0);
scale=scallingMTX(1.2,1.2,1);
mat=tilting*scale;

for(loop=0;loop<4;loop++){
for(i=0;i<kubus.NumberofVertices;i++){
vec[i]=Point2Vector(kubus.pnt[i]);
vec[i]=mat*vec[i];
}
//menggambar sisi yang tidak tampak dengan warna putih
setColor(0,0,0);
for(i=0;i<kubus.NumberofFaces; i++){
for(j=0;j<kubus.fc[i].NumberofVertices; j++){
vecbuff[j]=vec[kubus.fc[i].pnt[j]];
}

NormalVector=(vecbuff[1]-vecbuff[0])^(vecbuff[2]-vecbuff[0]);
normalz[i]=NormalVector.v[2];
if(normalz[i]<0.){
for(j=0;j<kubus.fc[i].NumberofVertices;j++){
buff[j]=Vector2Point2D(vecbuff[j]);
}
fillPolygon(buff,kubus.fc[i].NumberofVertices,merah);
drawPolygon(buff,kubus.fc[i].NumberofVertices);
}
}
//menggambar sisi yang tampak dengan hitam
setColor(0,0,0);
for(i=0;i<kubus.NumberofFaces; i++){
if(0.<=normalz[i]){
for(j=0;j<kubus.fc[i].NumberofVertices;j++){
vecbuff[j]=vec[kubus.fc[i].pnt[j]];
buff[j]=Vector2Point2D(vecbuff[j]);
}
drawPolygon(buff,kubus.fc[i].NumberofVertices);
}
}

//kubus2
for (i = 0; i<kubus2.NumberofVertices; i++){

9
vec[i] = Point2Vector(kubus2.pnt[i]);
vec[i] = mat*vec[i];
}
for (i=0; i<kubus2.NumberofVertices; i++) {
for (j=0; j<kubus2.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[kubus2.fc[i].pnt[j]]);
}
fillPolygon(buff,kubus2.fc[i].NumberofVertices,biru);
drawPolygon(buff, kubus2.fc[i].NumberofVertices);
}

//kubus3
for (i = 0; i<kubus3.NumberofVertices; i++){
vec[i] = Point2Vector(kubus3.pnt[i]);
vec[i] = mat*vec[i];
}
for (i=0; i<kubus3.NumberofVertices; i++) {
for (j=0; j<kubus3.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[kubus3.fc[i].pnt[j]]);
}
fillPolygon(buff,kubus3.fc[i].NumberofVertices,biru);
drawPolygon(buff, kubus3.fc[i].NumberofVertices);
}

//kubus4
for (i = 0; i<kubus4.NumberofVertices; i++){
vec[i] = Point2Vector(kubus4.pnt[i]);
vec[i] = mat*vec[i];
}
for (i=0; i<kubus3.NumberofVertices; i++) {
for (j=0; j<kubus4.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[kubus4.fc[i].pnt[j]]);
}
fillPolygon(buff,kubus4.fc[i].NumberofVertices,biru);
drawPolygon(buff, kubus4.fc[i].NumberofVertices);
}

//kubus 5
for (i = 0; i<kubus5.NumberofVertices; i++){
vec[i] = Point2Vector(kubus5.pnt[i]);
vec[i] = mat*vec[i];
}
for (i=0; i<kubus3.NumberofVertices; i++) {
for (j=0; j<kubus5.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[kubus5.fc[i].pnt[j]]);
}
fillPolygon(buff,kubus5.fc[i].NumberofVertices,biru);
drawPolygon(buff, kubus5.fc[i].NumberofVertices);
}

//pintu
for (i = 0; i<pintu.NumberofVertices; i++){
vec[i] = Point2Vector(pintu.pnt[i]);
vec[i] = mat*vec[i];
}
for (i=0; i<pintu.NumberofVertices; i++) {
for (j=0; j<pintu.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[pintu.fc[i].pnt[j]]);
}
fillPolygon(buff,pintu.fc[i].NumberofVertices,abu);
drawPolygon(buff, pintu.fc[i].NumberofVertices);
}

10
//pintuu
setColor(0,0,0);
for (i = 0; i<pintuu.NumberofVertices; i++){
vec[i] = Point2Vector(pintuu.pnt[i]);
vec[i] = mat*vec[i];
}
for (i=0; i<pintuu.NumberofVertices; i++) {
for (j=0; j<pintuu.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[pintuu.fc[i].pnt[j]]);
}
drawPolygon(buff, pintuu.fc[i].NumberofVertices);
}
//pintu2
setColor(1,1,1);
for (i = 0; i<pintu2.NumberofVertices; i++){
vec[i] = Point2Vector(pintu2.pnt[i]);
vec[i] = mat*vec[i];
}
for (i=0; i<pintu2.NumberofVertices; i++) {
for (j=0; j<pintu2.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[pintu2.fc[i].pnt[j]]);
}
fillPolygon(buff,pintu2.fc[i].NumberofVertices,abu);
drawPolygon(buff, pintu2.fc[i].NumberofVertices);
}
//pintu2u
setColor(0,0,0);
for (i = 0; i<pintu2u.NumberofVertices; i++){
vec[i] = Point2Vector(pintu2u.pnt[i]);
vec[i] = mat*vec[i];
}
for (i=0; i<pintu2u.NumberofVertices; i++) {
for (j=0; j<pintu2u.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[pintu2u.fc[i].pnt[j]]);
}
drawPolygon(buff, pintu2u.fc[i].NumberofVertices);
}
//jendela1
setColor(0,0,0);
for (i = 0; i<jendela1.NumberofVertices; i++){
vec[i] = Point2Vector(jendela1.pnt[i]);
vec[i] = mat*vec[i];
}

for (i=0; i<jendela1.NumberofVertices; i++) {


for (j=0; j<jendela1.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[jendela1.fc[i].pnt[j]]);
}
fillPolygon(buff,jendela1.fc[i].NumberofVertices,biru);
drawPolygon(buff, jendela1.fc[i].NumberofVertices);
}
//jendela 2
setColor(0,0,0);
for (i = 0; i<jendela2.NumberofVertices; i++){
vec[i] = Point2Vector(jendela2.pnt[i]);
vec[i] = mat*vec[i];
}

for (i=0; i<jendela2.NumberofVertices; i++) {


for (j=0; j<jendela2.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[jendela2.fc[i].pnt[j]]);
}

11
fillPolygon(buff,jendela2.fc[i].NumberofVertices,biru);
drawPolygon(buff, jendela2.fc[i].NumberofVertices);
}
//jendela3
setColor(0,0,0);
for (i = 0; i<jendela3.NumberofVertices; i++){
vec[i] = Point2Vector(jendela3.pnt[i]);
vec[i] = mat*vec[i];
}

for (i=0; i<jendela3.NumberofVertices; i++) {


for (j=0; j<jendela3.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[jendela3.fc[i].pnt[j]]);
}
fillPolygon(buff,jendela3.fc[i].NumberofVertices,biru);
drawPolygon(buff, jendela3.fc[i].NumberofVertices);
}
//jendela4
setColor(0,0,0);
for (i = 0; i<jendela4.NumberofVertices; i++){
vec[i] = Point2Vector(jendela4.pnt[i]);
vec[i] = mat*vec[i];
}

for (i=0; i<jendela4.NumberofVertices; i++) {


for (j=0; j<jendela4.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[jendela4.fc[i].pnt[j]]);
}
fillPolygon(buff,jendela4.fc[i].NumberofVertices,biru);
drawPolygon(buff, jendela4.fc[i].NumberofVertices);
}
//door
setColor(0,0,0);
for (i = 0; i<door.NumberofVertices; i++){
vec[i] = Point2Vector(door.pnt[i]);
vec[i] = mat*vec[i];
}

for (i=0; i<door.NumberofVertices; i++) {


for (j=0; j<door.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[door.fc[i].pnt[j]]);
}
fillPolygon(buff,door.fc[i].NumberofVertices,biru);
drawPolygon(buff, door.fc[i].NumberofVertices);
}
//door2
setColor(0,0,0);
for (i = 0; i<door2.NumberofVertices; i++){
vec[i] = Point2Vector(door2.pnt[i]);
vec[i] = mat*vec[i];
}

for (i=0; i<door2.NumberofVertices; i++) {


for (j=0; j<door2.fc[i].NumberofVertices;j++) {
buff[j]=Vector2Point2D(vec[door2.fc[i].pnt[j]]);
}
fillPolygon(buff,door2.fc[i].NumberofVertices,biru);
drawPolygon(buff, door2.fc[i].NumberofVertices);
}
mat=mat*scale;
}
tick++;

12
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition (100, 100);
glutInitWindowSize (700, 480);
glutCreateWindow ("Gedung 3D by ");
glClearColor(0.8, 0.2, 0.4, 0.0);
gluOrtho2D(-320., 320., -240.0, 240.0);
//Define the dimensions of the Orthograhpic Viewing Volume
glutIdleFunc(display); //idle event call back
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

HASIL PROGRAM

13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy