Wednesday 8 March 2017

Membuat Nama Menggunakan OpenGL

Cara membuat sebuah program OpenGL sederhana yang menampilkan nama Anda.
Gunakan objek-objek primitif sesuai kreativitas Anda. (tampilan berikut hanya saran dan contoh).



Fakultas FMIPA Departemen Ilmu Komputer IPB.

#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <windows.h>


static void key_callback(GLFWwindow* window,int key,int scancode,int action,int mods){
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)// Tombol Buat Exit
glfwSetWindowShouldClose(window,GL_TRUE);//nutup
}

void setup_viewport(GLFWwindow* window){
    float ratio;
    int width,height;
    glfwGetFramebufferSize(window,&width,&height);
    ratio = width / (float) height;
    glViewport(0,0,width,height);

    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-ratio,ratio,-1.f,1.f,1.f,-1.f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void  display() {

//-----------BACKGROUND-------------------------------------------

 glBegin(GL_POLYGON); //mulai menggambar persegi empat
    glColor3ub(255,255,255);
    glVertex2f(-2.8, -2.8); //menentukan vertex
    //glVertex2f(-2.8, 2.8);
    glVertex2f(2.849, 2.8);
    glVertex2f(2.849, -2.8);
    glEnd(); //menyelesaikan
    glFlush();

glBegin(GL_POLYGON); //mulai menggambar persegi empat
    glColor3ub(0,0,0);
    glVertex2f(-1.8, -0.8); //menentukan vertex
    glVertex2f(-1.8, 0.8);
    //glVertex2f(1.849, 0.8);
    glVertex2f(1.849, -0.8);
    glEnd(); //menyelesaikan
    glFlush();


//------------HURUF--------------------------------------------------

    //glRotatef((float)  glfwGetTime() * 50.f,0.f,0.f,1.f);
    //Huruf S
     //1
     glBegin(GL_POLYGON);
     glVertex2f(-1.5,0.5);
     glColor3ub(218,165,32);
     glVertex2f(-1.5,0.3);
     glColor3ub(218,165,32);
     glVertex2f(-1.8,0.3);
     glColor3ub(218,165,32);
     glVertex2f(-1.8,0.5);
     glEnd();
     //2
     glBegin(GL_POLYGON);
     glVertex2f(-1.8,0.3);
     glColor3ub(218,165,32);
     glVertex2f(-1.7,0.3);
     glVertex2f(-1.7,0.0);
     glVertex2f(-1.8,0.0);
     glEnd();


  //3
     glBegin(GL_POLYGON);
     glVertex2f(-1.8,0.0);
     glColor3ub(218,165,32);
     glVertex2f(-1.5,0.0);
     glVertex2f(-1.5,-0.2);
     glVertex2f(-1.8,-0.2);
     glEnd();


     //4
     glBegin(GL_POLYGON);
     glVertex2f(-1.5,0.0);
     glColor3ub(218,165,32);
     glVertex2f(-1.6,0.0);
     glVertex2f(-1.6,-0.4);
     glVertex2f(-1.5,-0.4);
     glEnd();
     //5
     glBegin(GL_POLYGON);
     glVertex2f(-1.5,-0.4);
     glVertex2f(-1.8,-0.4);
     glColor3ub(218,165,32);
     glVertex2f(-1.8,-0.6);
     glVertex2f(-1.5,-0.6);
     glEnd();



    //HURUF A
    glBegin(GL_QUADS);
    glVertex3f(-1.25f,-0.6f,0.f);
    glColor3ub(218,165,32);
    glVertex3f(-1.15f,-0.6f,0.f);
    glColor3ub(218,165,32);
    glVertex3f(-0.85f,0.5f,0.f);
    glColor3ub(218,165,32);
    glVertex3f(-0.95f,0.5f,0.f);
    glEnd();
    glBegin(GL_QUADS);
    glVertex3f(-0.65f,-0.6f,0.f);
    glVertex3f(-0.55f,-0.6f,0.f);
    glColor3ub(218,165,32);
    glVertex3f(-0.85f,0.5f,0.f);
    glVertex3f(-0.95f,0.5f,0.f);
    glEnd();
    glBegin(GL_QUADS);
    glVertex3f(-1.05f,-0.1f,0.f);
    glVertex3f(-0.75f,-0.1f,0.f);
    glVertex3f(-0.75f,0.0f,0.f);
    glVertex3f(-1.05f,0.0f,0.f);
    glEnd();

//Huruf T

    glBegin(GL_POLYGON);
     glVertex2f(0.0,0.4);
     glColor3ub(218,165,32);
     glVertex2f(0.0,0.5);
     glColor3ub(218,165,32);
     glVertex2f(-0.5,0.5);
     glColor3ub(218,165,32);
     glVertex2f(-0.5,0.4);
     glEnd();
     //2
    glBegin(GL_POLYGON);
     glVertex2f(-0.2,0.4);
     glColor3ub(218,165,32);
     glVertex2f(-0.3,0.4);
     glVertex2f(-0.3,-0.6);
     glVertex2f(-0.2,-0.6);
     glEnd();

    //Huruf R
   //1
     glBegin(GL_POLYGON);
     glVertex2f(0.3,-0.6);
     glVertex2f(0.3,0.5);
     glVertex2f(0.2,0.4);
     glColor3ub(218,165,32);
     glVertex2f(0.2,-0.6);
     glEnd();
     //2
     glBegin(GL_POLYGON);
     glVertex2f(0.3,0.5);
     glVertex2f(0.5,0.5);
     glColor3ub(218,165,32);
     glVertex2f(0.6,0.3);
     glVertex2f(0.3,0.3);
     glEnd();
     //3
      glBegin(GL_POLYGON);
     glVertex2f(0.6,0.4);
     glColor3ub(218,165,32);
     glVertex2f(0.5,0.5);
     glVertex2f(0.5,-0.1);
     glVertex2f(0.6,-0.1);
     glEnd();
     //4
      glBegin(GL_POLYGON);
     glVertex2f(0.3,0.0);
     glColor3ub(218,165,32);
     glVertex2f(0.6,0.0);
     glVertex2f(0.6,-0.2);
     glVertex2f(0.3,-0.2);
     glEnd();


     //5
     glBegin(GL_POLYGON);
     glVertex2f(0.3,-0.2);
     glVertex2f(0.6,-0.6);
     glVertex2f(0.6,-0.4);
     glColor3ub(218,165,32);
     glVertex2f(0.4,-0.2);
     glEnd();


    //Huruf I
   /* glBegin(GL_POLYGON);
     glVertex2f(0.9,0.3);
     glColor3f(1.0,-0.1,0.0);
     glVertex2f(0.9,0.4);
     glColor3f(0.0,-0.9,0.0);
     glVertex2f(0.8,0.4);
     glColor3f(0.0,-0.1,1.0);
     glVertex2f(0.8,0.3);
     glEnd(); */
     //2
    glBegin(GL_POLYGON);
     glVertex2f(0.95,0.5);
     glColor3ub(218,165,32);
     glVertex2f(0.85,0.5);
     glColor3ub(218,165,32);
     glVertex2f(0.85,-0.6);
     glColor3ub(218,165,32);
     glVertex2f(0.95,-0.6);
     glEnd();

     //Huruf A
     glBegin(GL_QUADS);
     glVertex3f(1.8f,-0.6f,0.f);
     glColor3ub(218,165,32);
     glVertex3f(1.7f,-0.6f,0.f);
     glVertex3f(1.4f,0.5f,0.f);
     glColor3ub(218,165,32);
     glVertex3f(1.5f,0.5f,0.f);
     glEnd();
     glBegin(GL_QUADS);
     glVertex3f(1.2f,-0.6f,0.f);
     glColor3ub(218,165,32);
     glVertex3f(1.1f,-0.6f,0.f);
     glColor3ub(218,165,32);
     glVertex3f(1.4f,0.5f,0.f);
     glColor3ub(218,165,32);
     glVertex3f(1.5f,0.5f,0.f);
     glEnd();
     glBegin(GL_QUADS);
     glVertex3f(1.6f,-0.1f,0.f);
     glColor3ub(218,165,32);
     glVertex3f(1.3f,-0.1f,0.f);
     glColor3ub(218,165,32);
     glVertex3f(1.3f,0.0f,0.f);
     glColor3ub(218,165,32);
     glVertex3f(1.6f,0.0f,0.f);
     glEnd();

//----------------------------------------
//Bayangan

//Huruf S
     //1
     glBegin(GL_POLYGON);
     glVertex2f(-1.45,0.5);
     glColor3ub(128,0,0);
     glVertex2f(-1.45,0.3);
     glColor3ub(128,0,0);
     glVertex2f(-1.75,0.3);
     glColor3ub(128,0,0);
     glVertex2f(-1.75,0.5);
     glEnd();
     //2
     glBegin(GL_POLYGON);
     glVertex2f(-1.75,0.3);
     glColor3ub(128,0,0);
     glVertex2f(-1.65,0.3);
     glVertex2f(-1.65,0.0);
     glVertex2f(-1.75,0.0);
     glEnd();


  //3
     glBegin(GL_POLYGON);
     glVertex2f(-1.75,0.0);
     glColor3ub(128,0,0);
     glVertex2f(-1.45,0.0);
     glVertex2f(-1.45,-0.2);
     glVertex2f(-1.75,-0.2);
     glEnd();


     //4
     glBegin(GL_POLYGON);
     glVertex2f(-1.45,0.0);
     glColor3ub(128,0,0);
     glVertex2f(-1.55,0.0);
     glVertex2f(-1.55,-0.4);
     glVertex2f(-1.45,-0.4);
     glEnd();
     //5
     glBegin(GL_POLYGON);
     glVertex2f(-1.45,-0.4);
     glVertex2f(-1.75,-0.4);
     glColor3ub(128,0,0);
     glVertex2f(-1.75,-0.6);
     glVertex2f(-1.45,-0.6);
     glEnd();



    //HURUF A
    glBegin(GL_QUADS);
    glVertex3f(-1.2f,-0.6f,0.f);
    glColor3ub(128,0,0);
    glVertex3f(-1.1f,-0.6f,0.f);
    glColor3ub(128,0,0);
    glVertex3f(-0.8f,0.5f,0.f);
    glColor3ub(128,0,0);
    glVertex3f(-0.9f,0.5f,0.f);
    glEnd();
    glBegin(GL_QUADS);
    glVertex3f(-0.6f,-0.6f,0.f);
    glVertex3f(-0.5f,-0.6f,0.f);
    glColor3ub(128,0,0);
    glVertex3f(-0.8f,0.5f,0.f);
    glVertex3f(-0.9f,0.5f,0.f);
    glEnd();
    glBegin(GL_QUADS);
    glVertex3f(-1.0f,-0.1f,0.f);
    glVertex3f(-0.7f,-0.1f,0.f);
    glVertex3f(-0.7f,0.0f,0.f);
    glVertex3f(-1.0f,0.0f,0.f);
    glEnd();

//Huruf T

     glBegin(GL_POLYGON);
     glVertex2f(0.05,0.4);
     glColor3ub(128,0,0);
     glVertex2f(0.05,0.5);
     glColor3ub(128,0,0);
     glVertex2f(-0.45,0.5);
     glColor3ub(128,0,0);
     glVertex2f(-0.45,0.4);
     glEnd();
     //2
     glBegin(GL_POLYGON);
     glVertex2f(-0.15,0.4);
     glColor3ub(128,0,0);
     glVertex2f(-0.25,0.4);
     glVertex2f(-0.25,-0.6);
     glVertex2f(-0.15,-0.6);
     glEnd();


    //Huruf R
   //1
     glBegin(GL_POLYGON);
     glVertex2f(0.35,-0.6);
     glVertex2f(0.35,0.5);
     glVertex2f(0.25,0.4);
     glColor3ub(128,0,0);
     glVertex2f(0.25,-0.6);
     glEnd();
     //2
     glBegin(GL_POLYGON);
     glVertex2f(0.35,0.5);
     glVertex2f(0.55,0.5);
     glColor3ub(128,0,0);
     glVertex2f(0.65,0.3);
     glVertex2f(0.35,0.3);
     glEnd();
     //3
     glBegin(GL_POLYGON);
     glVertex2f(0.65,0.4);
     glColor3ub(128,0,0);
     glVertex2f(0.55,0.5);
     glVertex2f(0.55,-0.1);
     glVertex2f(0.65,-0.1);
     glEnd();
     //4
     glBegin(GL_POLYGON);
     glVertex2f(0.35,0.0);
     glColor3ub(128,0,0);
     glVertex2f(0.65,0.0);
     glVertex2f(0.65,-0.2);
     glVertex2f(0.35,-0.2);
     glEnd();


     //5
     glBegin(GL_POLYGON);
     glVertex2f(0.35,-0.2);
     glVertex2f(0.65,-0.6);
     glVertex2f(0.65,-0.4);
     glColor3ub(128,0,0);
     glVertex2f(0.45,-0.2);
     glEnd();


    //Huruf I
   /* glBegin(GL_POLYGON);
     glVertex2f(0.9,0.3);
     glColor3f(1.0,-0.1,0.0);
     glVertex2f(0.9,0.4);
     glColor3f(0.0,-0.9,0.0);
     glVertex2f(0.8,0.4);
     glColor3f(0.0,-0.1,1.0);
     glVertex2f(0.8,0.3);
     glEnd(); */
     //2
     glBegin(GL_POLYGON);
     glVertex2f(1.0,0.5);
     glColor3ub(128,0,0);
     glVertex2f(0.9,0.5);
     glColor3ub(128,0,0);
     glVertex2f(0.9,-0.6);
     glColor3ub(128,0,0);
     glVertex2f(1.0,-0.6);
     glEnd();

     //Huruf A
     glBegin(GL_QUADS);
     glVertex3f(1.85f,-0.6f,0.f);
     glColor3ub(128,0,0);
     glVertex3f(1.75f,-0.6f,0.f);
     glVertex3f(1.45f,0.5f,0.f);
     glColor3ub(128,0,0);
     glVertex3f(1.55f,0.5f,0.f);
     glEnd();
     glBegin(GL_QUADS);
     glVertex3f(1.25f,-0.6f,0.f);
     glColor3ub(128,0,0);
     glVertex3f(1.15f,-0.6f,0.f);
     glColor3ub(128,0,0);
     glVertex3f(1.45f,0.5f,0.f);
     glColor3ub(128,0,0);
     glVertex3f(1.55f,0.5f,0.f);
     glEnd();
     glBegin(GL_QUADS);
     glVertex3f(1.65f,-0.1f,0.f);
     glColor3ub(128,0,0);
     glVertex3f(1.35f,-0.1f,0.f);
     glColor3ub(128,0,0);
     glVertex3f(1.35f,0.0f,0.f);
     glColor3ub(128,0,0);
     glVertex3f(1.65f,0.0f,0.f);
     glEnd();

    glFlush();

}

int main(void){
    GLFWwindow* window;
    if (!glfwInit()) exit(EXIT_FAILURE);//Init pertama kali, cek jalan apa enggak

    window = glfwCreateWindow(480,640,"Default",NULL,NULL);
            //glfwCreateWindow panjang,lebar,title window,....,...
    if(!window){
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);
    glfwSetKeyCallback(window,key_callback);

    while(!glfwWindowShouldClose(window)){
        setup_viewport(window);
        display();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();//Mutus task GLFW
    exit(EXIT_SUCCESS);
}

No comments:

Post a Comment