Menggambar 3 Dimensi Menggunakan OpenGL

Posted in Komputer, Kuliah, Pemrograman C by derwan dani on 16 October , 2010

Berikut ini Gambar hasil dari program ini :

Download Program here


Source Codenya :

/*
Nama : Derwan dani
NRP : G64096018
Deskripsi : Tugas Komputer grafik membuat gambar 3 dimensi (PENSIL)
*/

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>

//variabel statis
double size=0.8; // menentukan besarnya pensil
static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2;

void batang(){
     int n =6;  double t;

    //hitam
    glBegin(GL_QUAD_STRIP);
    glColor3f(0.0, 0.0, 0.0);
			for  (int i=0;  i <= n;  ++i) {
					t = i * 2.0 * 3.14159 / n;
					glVertex3f( size*cos(t), 15.3,size*sin(t) );
					glVertex3f( size*cos(t), 13.0,size*sin(t) );
			}
    glEnd();

    //putih
    glBegin(GL_QUAD_STRIP);
    glColor3f(0.8, 0.8, 0.9);
			for  (int i=0;  i <= n;  ++i) {
					t = i * 2.0 * 3.14159 / n;
					glVertex3f( size*cos(t), 13.0,size*sin(t) );
					glVertex3f( size*cos(t), 12.3,size*sin(t) );
			}
     glEnd();

    //biru
	glBegin(GL_QUAD_STRIP);

			for  (int i=0;  i <= n;  ++i) {

				if (i % 2 !=0){

						t = i * 2.0 * 3.14159 / n;
						glColor3f(0.0, 0.0, 1.0);
						glVertex3f( size*cos(t), 12.3,size*sin(t) );
						glColor3f(0.0, 0.0, 1.0);
						glVertex3f( size*cos(t), -6.5,size*sin(t) );

				}
				else
						glColor3f(0.1, 0.2, 0.6);
						t = i * 2.0 * 3.14159 / n;
						glVertex3f( size*cos(t), 12.3,size*sin(t) );
						glVertex3f( size*cos(t), -6.5,size*sin(t) );
				}
     glEnd();

}

//mata pensil
void matapensil(){

    double x=0, y=-9.5, z=0 ;

    //Mata pensil warna hitam
	glBegin(GL_TRIANGLE_FAN);
    glColor3f(0.0, 0.0, 0.0);
    glVertex3d(x,y,z);

			int a = 10;  double s;
			for  (int i=0;  i <= a;  ++i) {
					s = i * 2.0 * 3.14159 / a;
					glVertex3f( 0.27*cos(s), -8.5,0.27*sin(s) );
			}
     glEnd();

	//Mata pensil warna kuning
	glBegin(GL_TRIANGLE_FAN);
	glColor3f(0.8, 0.6, 0.0);
    glVertex3d(x,y,z);

			int n = 10;  double t;
			for  (int i=0;  i <= n;  ++i) {
					t = i * 2.0 * 3.14159 / n;
					glVertex3f( size*cos(t), -6.5,size*sin(t) );
			}
    glEnd();

}

//Tutup atas
void tutupatas(){

	double x=0,y=15.5,z=0;

	glBegin(GL_TRIANGLE_FAN);
    glColor3f(0.0, 0.0, 0.0);
    glVertex3d(x,y,z);

    int n = 6;  double t;
    for  (int i=0;  i <= n;  ++i) {
			t = i * 2.0 * 3.14159 / n;
			glVertex3f( size*cos(t), 15.3,size*sin(t) );
	}
    glEnd();
}

//Tulisan
void Text(double x,double y,double z,double scale,char *s)
{
   int i;

   glPushMatrix();
   glTranslatef(x,y,z);
   glScalef(scale,scale,scale);
   for (i=0;i<strlen(s);i++)
      glutStrokeCharacter(GLUT_STROKE_ROMAN,s[i]);
   glPopMatrix();
}

void display(void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glRotatef(theta[0], 1.0, 0.0, 0.0);
	glRotatef(theta[1], 0.0, 1.0, 0.0);
	glRotatef(theta[2], 0.0, 0.0, 1.0);
	glColor3f(1.0, 0.0, 0.0);
    Text(-7.82,3,0,0.01,"PensilKu");
	gluLookAt(2.5,2.5,2.5,0,0,0, 0, 1, 0);
    batang();
	matapensil();
	tutupatas();
	glFlush();
	glutSwapBuffers();
}

//Pergerakan Mouse
void mouse(int btn, int state, int x, int y)
{
	if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) theta[0]+=15;
	if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) theta[1]+=15;
	if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) theta[2]+=15;
	glutPostRedisplay();
}

//Menampilkan objek kelayar
void myReshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
        glOrtho(-10.0, 10.0, -10.0 * (GLfloat) h / (GLfloat) w,
            10.0 * (GLfloat) h / (GLfloat) w, -50.0, 50.0);
    else
        glOrtho(-10.0 * (GLfloat) w / (GLfloat) h,
            10.0 * (GLfloat) w / (GLfloat) h, -10.0, 10.0, -50.0, 50.0);
    glMatrixMode(GL_MODELVIEW);
}

//Fungsi utama program
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 700);
    glutCreateWindow("PENSIL [ Derwan dani - G64096018 ]");
    glutReshapeFunc(myReshape);
    glutDisplayFunc(display);

    glutIdleFunc(NULL);
    glutMouseFunc(mouse);
    glEnable(GL_DEPTH_TEST);
    glClearColor(1.0, 1.0, 1.0, 1.0);

	//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
	glutMainLoop();

}

donwload program

2 Responses

Subscribe to comments with RSS.

  1. ndang said, on 17 October , 2010 at 8:57 am

    okehhh …

    • derwan dani said, on 17 October , 2010 at 12:45 pm

      thanks pak😀


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: