TUGAS GKV-1 A
PEMBENTUKAN BANGUN 3D
BOLA, SILINDER, BOTOL, dan GELAS
1. BOLA
for(i=0;i<n;i++) sphere.fc[n*n+1].pnt[i]=(n+1)*n-1-i;
color_t c={1,1,0};
for(i=0;i<sphere.NumberofFaces;i++) sphere.fc[i].col=c;
for(i=0;i<sphere.NumberofVertices;i++)
sphere.col[i]=c;
}
b. Code yang menunjukan procedure userdraw dan object 3d_t bola untuk membentuk o
bjek 3d bola. CreateSphere (bola,20,100) digunakan untuk membentuk objek 3d bola
dengan n=20 dan r=100.
void userdraw(void){
matrix3D_t tilting=rotationXMTX(0.25)*rotationYMTX(-0.5);
setColor(0,1,0);
drawAxes(tilting);
object3D_t bola;
createSphere(bola,20,100);
setColor(0,0,0);
draw3D(bola,tilting);
}
d. Code berikut untuk menginisialisasi main, ukuran windows juga akan diatur menjadi
640x480 serta akan meminta windows untuk bersifat buffered, dengan warna RGBA.
glutInit(&argc,argv);
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize(680,480);
e. Code diatas akan menampilkan sebuah windows dengan judul “Bola”, dan akan mem
anggil prosedur yang telah dibuat diatas. glClearColor(0.231, 0.0, 0.0, 0.0);
akan membuat background menjadi warna hijau tua dan glutMainLoop(); akan men
jalankan program.
glutCreateWindow ("Bola");
glClearColor(0.231, 0.0, 0.0, 0.0);
gluOrtho2D(-500., 500., -350.0, 350.0);
glutIdleFunc(display); // idle event call back
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Full Source code untuk Bola :
NATASHA PUTRI – 24060119130073 GKV A
#include <windows.h>
#include <math.h>
#include <GL/glut.h>
#include <fstream>
#include <stdlib.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;
{
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;
}
for (i=0;i<3;i++) {
vec.v[i]/=vec.v[3];
}
vec.v[3]=1.;
return vec;
}
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));
}
//return kdif*NL*col+kamb*col;
}
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);
}
//////////////////////////////////////////////////////////////////
typedef struct {
int NumberofVertices; //in the face
short int pnt[50];
color_t col;
} face_t;
typedef struct {
int NumberofVertices; //of the object
point3D_t pnt[1600];
color_t col[1600];
int NumberofFaces; //of the object
face_t fc[1000];
} object3D_t;
NATASHA PUTRI – 24060119130073 GKV A
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;
}
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);
}
}
}
vector3D_t lightVector={0,0,1,1},viewVector={0,0,1,1};
color_t colbuff[50];
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(vecbuff[j]);
colbuff[j]=PhongModel(lightVector,vecNormal,viewVector,obyek.col[obyek.fc[i].pnt[j]]);
}
gradatePolygon(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(vecbuff[j]);
colbuff[j]=PhongModel(lightVector,vecNormal,viewVector,obyek.col[obyek.fc[i].pnt[j]]);
}
gradatePolygon(p,obyek.fc[i].NumberofVertices,colbuff);
}
}
}
sphere.col[i]=c;
}
void userdraw(void)
{
matrix3D_t tilting=rotationXMTX(0.25)*rotationYMTX(-0.5);
setColor(0,1,0);
drawAxes(tilting);
object3D_t bola;
createSphere(bola,20,90);
setColor(0,0,0);
draw3D(bola,tilting);
}
2. SILINDER
NATASHA PUTRI – 24060119130073 GKV A
Code berikut menunjukan silinder bertumupuk dengan m silinder dan n titik pembentuk
lingkaran mempunyai r [] sebanyak m+1 dan h [] sebanyak m. Pembuatan silinder bertumpuk
dengan m silinder dan n buah titik untuk menghasilkan lingkaran adalah:
Koordinat titik-titik:
Xi=ri.cos(iθ) B=∑ ℎ
Yi=b Zi=ri.sin(iθ)
2. Pembuatan face
Jumlah Face = m×n+2, m×n buah face untuk bagian tepi, 2 buah face
P0=in+j
P1=(i+1).n+j
P2=(i+1).n+j+1
P3=i.n+j+1
Pi=i
Pi=(m+1).n-i-1
silinder.fc[i*n+j].pnt[0]=i*n+j; silinder.fc[i*n+j].pnt[1]=(i+1)*n+j;
silinder.fc[i*n+j].pnt[2]=(i+1)*n+j+1;
silinder.fc[i*n+j].pnt[3]=i*n+j+1; if(j==(n-1)){
silinder.fc[i*n+j].pnt[2]=i*n+j+1; silinder.fc[i*n+j].pnt[3]=(i-
1)*n+j+1;
}
}
}
silinder.fc[m*n].NumberofVertices=n; for(i=0;i<n;i++)
silinder.fc[m*n].pnt[i]=i;
silinder.fc[m*n+1].NumberofVertices=n; for(i=0;i<n;i+
+)silinder.fc[m*n+1].pnt[i]=(m+1)*n-1-i;
}
c. Code berikut untuk menginisialisasi main, ukuran windows juga akan diatur menjadi
640x480 serta akan meminta windows untuk bersifat buffered, dengan warna RGBA.
glutInit(&argc,argv);
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize(680,480);
d. Code berikut menampilkan sebuah windows dengan judul “Silinder”, dan akan mema
nggil prosedur yang telah dibuat diatas. glClearColor(0.534, 0.0, 0.342, 0.0);
akan membuat background menjadi warna ungu tua dan glutMainLoop(); akan men
jalankan program.
glutCreateWindow ("Silinder");
glClearColor(0.534, 0.0, 0.342, 0.0);
gluOrtho2D(-500., 500., -350.0, 350.0);
glutIdleFunc(display); // idle event call back
glutDisplayFunc(display);
glutMainLoop();
return 0;
Full Source Code Silinder (menggunakan drawshape function dan matrices vector 3D
yang sama dengan bola)
3. BOTOL
void userdraw(void){
matrix3D_t tilting=rotationXMTX(0.5)*rotationYMTX(-0.25);
setColor(0,1,0);
drawAxes(tilting);
object3D_t o;
float r[12]={40,50,50,45,50,50,20,20,17,20,20,17};
float h[11]={5,60,5,5,70,50,40,4,4,10,4};
NATASHA PUTRI – 24060119130073 GKV A
createCylinderN(o,11,20,r,h);
setColor(1,0,0);
draw3D(o,tilting);
c. Code berikut menginisialisasi main, ukuran windows juga akan diatur menjadi 640x4
80 serta akan meminta windows untuk bersifat buffered, dengan warna RGBA.
glutInit(&argc,argv);
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize(680,480);
d. Code berikut menampilkan sebuah windows dengan judul “Botol”, dan akan memang
gil prosedur yang telah dibuat diatas. glClearColor(0.657, 0.0, 0.0, 0.0); aka
n membuat background menjadi warna merah tua dan glutMainLoop(); akan menja
lankan program.
glutCreateWindow ("Botol");
glClearColor(0.657, 0.0, 0.0, 0.0);
gluOrtho2D(-500., 500., -350.0, 350.0);
glutIdleFunc(display); // idle event call back
glutDisplayFunc(display);
glutMainLoop();
return 0;
Full Source Code Botol (menggunakan drawshape function dan matrices vector 3D
yang sama dengan bola)
silinder.pnt[i*n+j].z=r[i]*sin(j*a);
}}
silinder.NumberofFaces=m*n+2;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
silinder.fc[i*n+j].NumberofVertices=4;
silinder.fc[i*n+j].pnt[0]=i*n+j;
silinder.fc[i*n+j].pnt[1]=(i+1)*n+j;
silinder.fc[i*n+j].pnt[2]=(i+1)*n+j+1;
silinder.fc[i*n+j].pnt[3]=i*n+j+1;
if(j==(n-1)){
silinder.fc[i*n+j].pnt[2]=i*n+j+1;
silinder.fc[i*n+j].pnt[3]=(i-1)*n+j+1;
} }}
silinder.fc[m*n].NumberofVertices=n;
for(i=0;i<n;i++) silinder.fc[m*n].pnt[i]=i;
silinder.fc[m*n+1].NumberofVertices=n;
for(i=0;i<n;i++)
silinder.fc[m*n+1].pnt[i]=(m+1)*n-1-i;
}
void userdraw(void){
matrix3D_t tilting=rotationXMTX(0.5)*rotationYMTX(-0.25);
setColor(0,1,0);
drawAxes(tilting);
object3D_t o;
float r[12]={40,50,50,45,50,50,20,20,17,20,20,17};
float h[11]={5,60,5,5,70,50,40,4,4,10,4};
createCylinderN(o,11,20,r,h);
setColor(1,0,0);
draw3D(o,tilting);
}
int main(int argc, char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize(680,480);
glutCreateWindow ("Botol");
glClearColor(0.657, 0.0, 0.0, 0.0);
gluOrtho2D(-500., 500., -350.0, 350.0);
glutIdleFunc(display); // idle event call back
glutDisplayFunc(display);
NATASHA PUTRI – 24060119130073 GKV A
glutMainLoop();
return 0;
}
4. GELAS
obyek 3Dnya, m adalah jumlah silinder, n adalah jumlah titik pembentuk lingkaran
atas, r adalah jari-jari pada setiap lingkaran yang berupa array sejumlah m+1, dan h
Pembuatan silinder bertumpuk dengan m silinder dan n buah titik untuk menghasilkan
lingkaran adalah:
Koordinat titik-titik :
Xi=ri.cos(iθ) B=∑ ℎ
Yi=b Zi=ri.sin(iθ)
2. Pembuatan face
Jumlah Face = m×n+2, m×n buah face untuk bagian tepi, 2 buah face
P0=in+j
P1=(i+1).n+j
P2=(i+1).n+j+1
P3=i.n+j+1
Pi=i
Pi=(m+1).n-i-1
Perbedaan gelas dan botol hanya terdapat pada jari-jari setiap bagian silindernya, berikut
source codenya :
}}
silinder.NumberofFaces=m*n+2;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
silinder.fc[i*n+j].NumberofVertices=4;
silinder.fc[i*n+j].pnt[0]=i*n+j;
silinder.fc[i*n+j].pnt[1]=(i+1)*n+j;
silinder.fc[i*n+j].pnt[2]=(i+1)*n+j+1;
silinder.fc[i*n+j].pnt[3]=i*n+j+1;
if(j==(n-1)){
silinder.fc[i*n+j].pnt[2]=i*n+j+1;
silinder.fc[i*n+j].pnt[3]=(i-1)*n+j+1;
} }}
silinder.fc[m*n].NumberofVertices=n;
for(i=0;i<n;i++) silinder.fc[m*n].pnt[i]=i;
silinder.fc[m*n+1].NumberofVertices=n;
for(i=0;i<n;i++)
silinder.fc[m*n+1].pnt[i]=(m+1)*n-1-i;
}
c. Code berikut untuk menginisialisasi main, ukuran windows juga akan diatur menjadi
640x480 serta akan meminta windows untuk bersifat buffered, dengan warna RGBA.
glutInit(&argc,argv);
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize(680,480);
d. Code berikut akan menampilkan sebuah windows dengan judul “Gelas”, dan akan me
manggil prosedur yang telah dibuat diatas. glClearColor(1.0, 1.0, 1.0, 0.0);
akan membuat background menjadi warna putih dan glutMainLoop(); akan menjala
nkan program.
glutCreateWindow ("Gelas");
glClearColor(0.231, 1.0, 1.0, 0.0);
gluOrtho2D(-500., 500., -350.0, 350.0);
glutIdleFunc(display); // idle event call back
glutDisplayFunc(display);
glutMainLoop();
return 0;
Full Source Code Gelas (menggunakan drawshape functions dan matrices vector 3D
yang sama dengan Bola)
NATASHA PUTRI – 24060119130073 GKV A
draw3D(o,tilting);
}
int main(int argc, char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize(680,480);
glutCreateWindow ("Gelas");
glClearColor(1.0, 1.0, 1.0, 0.0);
gluOrtho2D(-500., 500., -350.0, 350.0);
glutIdleFunc(display); // idle event call back
glutDisplayFunc(display);
glutMainLoop();
return 0;
}