Wednesday, May 4, 2016

Membuat Kacamata Dengan OpenGL

Hallo Broo dan Sist, pagi ini saya akan bagi-bagi source code program kacamata dengan openGL, program ini sangat sederhana dan mudah dimengerti.

Langkahnya :
1. Buka aplikasi Visual C++
2. Copy code dibawah ini

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <gl/glut.h>
#include <math.h>
#define Garis(x1,y1,x2,y2)glVertex2f((x1),(y1)); glVertex2f((x2),(y2));

void Kacamata(){
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
    float x0 = -60;
    float y0 = 0;
    float r = 30;
    float s;
    float phi = 3.14;
    int jt = 90;
 for(int i=0;i<=360;i++){
       glColor3f(0.5,0.3,0.0);
  s = i*(2*phi/jt);
       float x = x0+r*cos(s);
        float y = y0+r*sin(s);
        glVertex2f(x/100,y/100);
    }

glEnd();
glFlush();
glBegin(GL_LINES);
Garis(-0.3,0,0.3,0);
Garis(-0.9,0.0,-0.9,0.8);
Garis(0.9,0.0,0.9,0.8);
Garis(-0.9,0.8,-0.7,0.8);
Garis(0.9,0.8,0.7,0.8);
glEnd();
glFlush();
glBegin(GL_POLYGON);
 float x1 = 60;
 float y1 = 0;
 float s1;
 for(int i=0;i<=360;i++){
        glColor3f(0.3,0.3,0.0);
  s1 = i*(2*phi/jt);
        float x = x1+r*cos(s1);
  float y = y1+r*sin(s1);
        glVertex2f(x/100,y/100);
    }
glEnd();
glFlush();
glBegin(GL_POLYGON);
glColor3f(0.2,0.0,0.0);
Garis(-0.9,0.1,-0.3,0.1);
Garis(-0.3,0.1,-0.3,-0.1);
Garis(-0.3,-0.1,-0.9,-0.1);
Garis(-0.9,-0.1,-0.9,0.1);
glEnd();
glFlush();
glBegin(GL_POLYGON);
Garis(0.9,0.1,0.3,0.0);
Garis(0.3,0.1,0.3,-0.1);
Garis(0.3,-0.1,0.9,-0.1);
Garis(0.9,-0.1,0.9,0.1);
glEnd();
glFlush();

}

int main(int argc, char** argv){
    printf("http://kaptenhiu.blogspot.co.id/");
    glutCreateWindow("Kacamata");
    glutDisplayFunc(Kacamata);
    glutMainLoop();
} 
3.Hasilnya seperti ini :

Mudah kan? silahkan di pahami setiap barisnya untuk memperdalam pemahaman tentang grafika komputer.
Jika ada kesulitan silahkan ditanyakan dikomentar yaa bro dan sist.
selamat mencoba dan semoga suksess.

Blogger
Disqus
Pilih Sistem Komentar Yang Anda Sukai

No comments