Selasa, 04 Desember 2012
Membuat Rumah 3D
//by wendel H selsily (672011094)
#include <gl\gl.h>
#include <gl\glut.h>
#include <gl\glu.h> /* GLU extention library */
void init(void);
void display(void);
void keyboard(unsigned char, int, int);
void resize(int, int);
int is_depth; /* depth testing flag */
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(40, 40);
glutCreateWindow("Rumah 3D Berwarna");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutReshapeFunc(resize);
glutMainLoop();
return 0;
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
is_depth = 1;
glMatrixMode(GL_MODELVIEW);
glPointSize(9.0);
glLineWidth(6.0f);
}
void display(void)
{
if (is_depth)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
else
glClear(GL_COLOR_BUFFER_BIT);
//ini untuk buat lantainya
glBegin(GL_QUADS);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-100.0, 0.0, -100.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-100.0, 0.0, 100.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(100.0, 0.0, 100.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(100.0, 0.0, -100.0);
glEnd();
glBegin(GL_QUADS);//depan rumah
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-50.0, 0.0, 100.0);
glVertex3f(-50.0, 50.0, 100.0);
glVertex3f(50.0, 50.0, 100.0);
glVertex3f(50.0, 0.0, 100.0);
glEnd();
glBegin(GL_QUADS);//garis depan rumah`
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-50.0, 0.0, 100.1);
glVertex3f(-50.0, 50.0, 100.1);
glVertex3f(50.0, 50.0, 100.1);
glVertex3f(50.0, 0.0, 100.1);
glEnd();
glBegin(GL_QUADS);// pintu rumah
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-15.1, 0.0, 100.5);
glVertex3f(-15.1, 40.0, 100.5);
glVertex3f(15.1, 40.0, 100.5);
glVertex3f(15.1, 0.0, 100.5);
glEnd();
glBegin(GL_LINE_LOOP);//garis pintu rumah
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-15.1, 0.0, 100.6);
glVertex3f(-15.1, 40.0, 100.6);
glVertex3f(15.1, 40.0, 100.6);
glVertex3f(15.1, 0.0, 100.6);
glEnd();
glBegin(GL_POINTS);// tangan pintu
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-13.0, 20.0, 100.8);
glEnd();
glBegin(GL_TRIANGLES);//atap depan rumah
glColor3f(1.0f,0.6f,0.0f);
glVertex3f(0.0, 100.0, 100.0);
glVertex3f(50.0, 50.0, 100.0);
glVertex3f(-50.0, 50.0, 100.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis atap depan rumah
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(0.0, 100.0, 100.0);
glVertex3f(50.0, 50.0, 100.0);
glVertex3f(-50.0, 50.0, 100.0);
glEnd();
glBegin(GL_QUADS);//samping kiri
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-50.0, 0.0, 100.0);
glVertex3f(-50.0, 50.0, 100.0);
glVertex3f(-50.0, 50.0, -100.0);
glVertex3f(-50.0, 0.0, -100.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis samping kiri
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.0, 0.0, 100.0);
glVertex3f(-50.0, 50.0, 100.0);
glVertex3f(-50.0, 50.0, -100.0);
glVertex3f(-50.0, 0.0, -100.0);
glEnd();
glBegin(GL_QUADS);//jendela samping kiri (kiri atas)
glColor3f(0.7f,0.7f,0.7f);
glVertex3f(-51.1, 25.0, 0.0);
glVertex3f(-51.1, 40.0, 0.0);
glVertex3f(-51.1, 40.0, -50.0);
glVertex3f(-51.1, 25.0, -50.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis jendela samping kiri (kiri atas)
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-51.1, 25.0, 0.0);
glVertex3f(-51.1, 40.0, 0.0);
glVertex3f(-51.1, 40.0, -50.0);
glVertex3f(-51.1, 25.0, -50.0);
glEnd();
glBegin(GL_QUADS);//jendela samping kiri (kiri bawah)
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-51.1, 10.0, 0.0);
glVertex3f(-51.1, 25.0, 0.0);
glVertex3f(-51.1, 25.0, -50.0);
glVertex3f(-51.1, 10.0, -50.0);
glEnd();
glBegin(GL_QUADS);//garis jendela samping kiri (kiri bawah)
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-51.1, 10.0, 0.0);
glVertex3f(-51.1, 25.0, 0.0);
glVertex3f(-51.1, 25.0, -50.0);
glVertex3f(-51.1, 10.0, -50.0);
glEnd();
glBegin(GL_QUADS);//jendela samping kiri (kanan bawah)
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(-51.1, 10.0, 50.0);
glVertex3f(-51.1, 25.0, 50.0);
glVertex3f(-51.1, 25.0, -50.0);
glVertex3f(-51.1, 10.0, -50.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis jendela samping kiri (kanan bawah)
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-51.1, 10.0, 50.0);
glVertex3f(-51.1, 25.0, 50.0);
glVertex3f(-51.1, 25.0, -50.0);
glVertex3f(-51.1, 10.0, -50.0);
glEnd();
glBegin(GL_QUADS);//jendela samping kiri (kanan atas)
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-51.1, 25.0, 0.0);
glVertex3f(-51.1, 40.0, 0.0);
glVertex3f(-51.1, 40.0, 50.0);
glVertex3f(-51.1, 25.0, 50.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis jendela samping kiri (kanan atas)
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(-51.1, 25.0, 0.0);
glVertex3f(-51.1, 40.0, 0.0);
glVertex3f(-51.1, 40.0, 50.0);
glVertex3f(-51.1, 25.0, 50.0);
glEnd();
glBegin(GL_QUADS);//atap samping kiri
glColor3f(1.0f,0.4f,0.0f);
glVertex3f(-50.0, 50.0, -100.0);
glVertex3f(0.0, 100.0, -100.0);
glVertex3f(0.0, 100.0, 100.0);
glVertex3f(-50.0, 50.0, 100.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis atap samping kiri
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(-50.0, 50.0, -100.0);
glVertex3f(0.0, 100.0, -100.0);
glVertex3f(0.0, 100.0, 100.0);
glVertex3f(-50.0, 50.0, 100.0);
glEnd();
glBegin(GL_QUADS);//samping kanan
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(50.0, 0.0, 100.0);
glVertex3f(50.0, 50.0, 100.0);
glVertex3f(50.0, 50.0, -100.0);
glVertex3f(50.0, 0.0, -100.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis samping kanan
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(50.0, 0.0, 100.0);
glVertex3f(50.0, 50.0, 100.0);
glVertex3f(50.0, 50.0, -100.0);
glVertex3f(50.0, 0.0, -100.0);
glEnd();
glBegin(GL_QUADS);//jendela samping kanan (kanan atas)
glColor3f(0.7f,0.7f,0.7f);
glVertex3f(51.1, 25.0, 0.0);
glVertex3f(51.1, 40.0, 0.0);
glVertex3f(51.1, 40.0, -50.0);
glVertex3f(51.1, 25.0, -50.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis jendela samping kanan (kanan atas)
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(51.1, 25.0, 0.0);
glVertex3f(51.1, 40.0, 0.0);
glVertex3f(51.1, 40.0, -50.0);
glVertex3f(51.1, 25.0, -50.0);
glEnd();
glBegin(GL_QUADS);//jendela samping kanan (kanan bawah)
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(51.1, 10.0, 0.0);
glVertex3f(51.1, 25.0, 0.0);
glVertex3f(51.1, 25.0, -50.0);
glVertex3f(51.1, 10.0, -50.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis jendela samping kanan (kanan bawah)
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(51.1, 10.0, 0.0);
glVertex3f(51.1, 25.0, 0.0);
glVertex3f(51.1, 25.0, -50.0);
glVertex3f(51.1, 10.0, -50.0);
glEnd();
glBegin(GL_QUADS);//jendela samping kiri (kiri bawah)
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(51.1, 10.0, 50.0);
glVertex3f(51.1, 25.0, 50.0);
glVertex3f(51.1, 25.0, -50.0);
glVertex3f(51.1, 10.0, -50.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis jendela samping kiri (kiri bawah)
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(51.1, 10.0, 50.0);
glVertex3f(51.1, 25.0, 50.0);
glVertex3f(51.1, 25.0, -50.0);
glVertex3f(51.1, 10.0, -50.0);
glEnd();
glBegin(GL_QUADS);//jendela samping kiri (kiri atas)
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(51.1, 25.0, 0.0);
glVertex3f(51.1, 40.0, 0.0);
glVertex3f(51.1, 40.0, 50.0);
glVertex3f(51.1, 25.0, 50.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis jendela samping kiri (kiri atas)
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(51.1, 25.0, 0.0);
glVertex3f(51.1, 40.0, 0.0);
glVertex3f(51.1, 40.0, 50.0);
glVertex3f(51.1, 25.0, 50.0);
glEnd();
glBegin(GL_QUADS);//atap samping kanan
glColor3f(1.0f,0.4f,0.0f);
glVertex3f(50.0, 50.0, 100.0);
glVertex3f(0.0, 100.0, 100.0);
glVertex3f(0.0, 100.0, -100.0);
glVertex3f(50.0, 50.0, -100.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis atap samping kanan
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(50.0, 50.0, 100.0);
glVertex3f(0.0, 100.0, 100.0);
glVertex3f(0.0, 100.0, -100.0);
glVertex3f(50.0, 50.0, -100.0);
glEnd();
glBegin(GL_QUADS);//belakang
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-50.0, 0.0, -100.0);
glVertex3f(-50.0, 50.0, -100.0);
glVertex3f(50.0, 50.0, -100.0);
glVertex3f(50.0, 0.0, -100.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis belakang
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.0, 0.0, -100.0);
glVertex3f(-50.0, 50.0, -100.0);
glVertex3f(50.0, 50.0, -100.0);
glVertex3f(50.0, 0.0, -100.0);
glEnd();
glBegin(GL_TRIANGLES);//atap belakang
glColor3f(1.0f,0.6f,0.0f);
glVertex3f(0.0, 100.0, -100.0);
glVertex3f(-50.0, 50.0, -100.0);
glVertex3f(50.0, 50.0, -100.0);
glEnd();
glBegin(GL_LINE_LOOP);//garis atap belakang
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(0.0, 100.0, -100.0);
glVertex3f(-50.0, 50.0, -100.0);
glVertex3f(50.0, 50.0, -100.0);
glEnd();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
/* Untuk kontrol dengan keyboard:
"a": ke kiri
"d": ke kanan
"w": ke depan
"s": ke belakang
"t": toggle depth-testing
"u" : ke atas
"n" : ke bawah
"i" : putar ke bawah terhadap sumbu X
"j" : putar ke atas terhadap sumbu X
"o" : putar ke kiri terhadap sumbu Y
"k" : putar ke kanan terhadap sumbu Y
"p" : putar ke kiri terhadap sumbu Z
"l" : putar ke kanan terhadap sumbu Z
*/
switch (key)
{
case 'a':
case 'A':
glTranslatef(5.0, 0.0, 0.0);
break;
case 'd':
case 'D':
glTranslatef(-5.0, 0.0, 0.0);
break;
case 'u' :
case 'U' :
glTranslatef(0.0,5.0,0.0);
break;
case 'n' :
case 'N' :
glTranslatef(0.0,-5.0, 0.0);
break;
case 'w':
case 'W':
glTranslatef(0.0, 0.0, 5.0);
break;
case 's':
case 'S':
glTranslatef(0.0, 0.0, -5.0);
break;
case 'i':
case 'I':
glRotatef(3.0,1.0,0.0,0.0);
break;
case 'j':
case 'J':
glRotatef(-3.0,1.0,0.0,0.0);
break;
case 'o':
case 'O':
glRotatef(3.0,0.0,1.0,0.0);
break;
case 'k':
case 'K':
glRotatef(-3.0,0.0,1.0,0.0);
break;
case 'p':
case 'P':
glRotatef(3.0,0.0,0.0,1.0);
break;
case 'l':
case 'L':
glRotatef(-3.0,0.0,0.0,1.0);
break;
case 't':
case 'T':
if (is_depth)
{
is_depth = 0;
glDisable(GL_DEPTH_TEST);
}
else
{
is_depth = 1;
glEnable(GL_DEPTH_TEST);
}
}
display();
}
void resize(int width, int height)
{
if (height == 0) height = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, width / height, 1.0, 400.0);
/* Inisialisasi posisi */
glTranslatef(0.0, -5.0, -150.0);
glMatrixMode(GL_MODELVIEW);
}
ini adalah tombol Untuk kontrol posisi Rumah di atas dengan menggunakan keyboard :
"a": ke kiri
"d": ke kanan
"w": ke depan
"s": ke belakang
"t": toggle depth-testing
"u" : ke atas
"n" : ke bawah
"i" : putar ke bawah terhadap sumbu X
"j" : putar ke atas terhadap sumbu X
"o" : putar ke kiri terhadap sumbu Y
"k" : putar ke kanan terhadap sumbu Y
"p" : putar ke kiri terhadap sumbu Z
"l" : putar ke kanan terhadap sumbu Z
silahkan download coding dan project jadinya klik di sini
Cara Membuat 2 Garis dengan Titik Potong
#include <windows.h>
#include <gl/GL.h>
#include <gl/glut.h>
#include <stdio.h>
// pendefinisian variabel
int g=0,j=0,a,b,c,d,e,f,;
static float m1,c1,x;
static float m2,c2,y;
static float ax,cx;
static float ay,cy;
static float bx,dx;
static float by,dy;
void drawLine(int x,int y);
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(8.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void grad()
{
//mencetak gradien warna background
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.0);
glVertex2i(0, 0);
glColor3f(0.0, 1.0, 1.0);
glVertex2i(640, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex2i(640, 480);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(0, 480);
glEnd();
}
//rumus
void potong()
{
glColor3f(0,0,0);
glBegin(GL_LINES);
glVertex2f(ax,ay);
glVertex2f(bx,by);
glVertex2f(cx,cy);
glVertex2f(dx,dy);
glEnd();
glutSwapBuffers();
//persamaan 1
m1=((by-ay)/(bx-ax));
c1=ay-(m1*ax);
////persamaan 2
m2=((dy-cy)/(dx-cx));
c2=cy-(m2*cx);
//rumus utama
x=((c2-c1)/(m1-m2));
y=(m1*x)+c1;
a=x;
b=y;
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 0.0);
glVertex2i(a,b);
glEnd();
glutSwapBuffers();
}
void drawDot()
{
glBegin(GL_POINTS);
glColor3f(1.0, .0, 0.0);
glVertex2i(a, b);
glEnd();
glutSwapBuffers();
}
void drawLine(int x,int y)
{
grad();
if(g==1) {
ax=x;
ay=480-y;
bx=c;
by=d;
glClear(GL_COLOR_BUFFER_BIT);
grad();
glBegin(GL_LINE_STRIP);
glColor3f(0.0, 0.0, 0.0);
glVertex2i(c, d);
glVertex2i(x, 480-y);
glEnd();
glutSwapBuffers();
}
if(g>=2) {
dx=x;
dy=480-y;
cx=d;
cy=e;
potong();
drawDot();
glBegin(GL_LINE_STRIP);
glColor3f(0.0, 1.0, 0.0);
glVertex2i(d, e);
glVertex2i(x, 480-y);
glEnd();
glFlush();
}
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
grad();
d=x;
e=480-y;
g+=1;
if(g==1)
{
c=x;
d=480-y;
}
if(g==2) {
g=0;
}
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{
g+=1;
grad();
}
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
exit(-1);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
grad();
glutSwapBuffers();
}
main(int argc, char**argv)
{
glutInit(&argc,argv);// inisialisasi toolkit
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);// set modus display
glutInitWindowSize(640,480);// set ukuran window
glutInitWindowPosition(100,150);// set posisi window di layar
glutCreateWindow("titikpotong_672011094");// buka screen window
glutDisplayFunc(myDisplay);// registrasi fungsi menggambar
glutMotionFunc(drawLine);//memanggil fungsi garis
glutMouseFunc(mouse);//memanggil fungsi mouse
myInit();
glutMainLoop();// berulang terus-menerus
}
silahkan download projectnya disini
Membuat Bentuk Obat Nyamuk persegi 6 (spiral hexagon) di OpenGL
#include <gl/Gl.h>
#include <gl/glut.h>
#include <math.h>
void obat(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_STRIP);
glColor3f(0.0, 0.0, 1.0);
double angle , angleInc, cx, cy, radius, rotAngle;
int n=6;
rotAngle=0;
if(n < 3) return;
angle = rotAngle* 3.14159265/180;
angleInc= 2 * 3.14159265/n;
for(int i = 0; i < 10; i++){
for(int j = 0; j < n; j++){
angle += angleInc;
glVertex2f((radius+i) * cos(angle) + cx, (radius+i) * sin(angle) + cy);
}
}
glEnd();
glFlush();
}
main(int argc, char *argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("Obat Nyamuk");
glutDisplayFunc(obat);
glClearColor(1.0,1.0,1.0,0.0);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10.0,10.0,-11.0,11.0);
glViewport(40, 400, 60, 300);
glutMainLoop();
}
silah kan download codingnya disini
Langganan:
Postingan (Atom)