Wednesday 26 April 2017

membuat projek openGL C atau c++ itu mudah (box colorfull 2D). . .OpenGL - tutorials

    sangat mudah membuat sebuah projek openGL di C atau C++. semua orang pasti bisa dan akan bisa bila berusaha untuk mencobanya. namun terkadang kita sulit untuk mencari contoh real dari sebuah projek-projek yang ingin kita coba hasilnya. karena kalau orangnya yang lemah ingatannya seperti saya dan tidak tau cara bikin programnya dan seperti apa bentuknya maka akan sangat kesulitan. 
     maka oleh karena itu dengan memberikan salah satu contoh di bawah ini yang membuat sebuah kotak pelangi sebagai dasar untuk pengetahuannya.



Untuk membuat sebuah projek seperti diatas yang perlu disiapkan adalah :
  1.  library openGL glut + codeblock atau bisa menggunakan editor Dev C++.
  2. library dibuat setingannya ke kode block.
  3. setelah semua beres copy paste contoh koding dibawah ini.

Code Program

#include <stdio.h>
#include <stdlib.h>
#include <gl/glut.h>

using namespace std;

void tampil(){
     glClearColor (1.0,1.0,1.0,1.0); //warna dasar putih
     glClear (GL_COLOR_BUFFER_BIT);

     glColor3f (1.0, 1.0, 0.0);//warna kuning

     glBegin(GL_POLYGON); //persegi kuning
     glVertex2f(0,0.5);
     glVertex2f(0.5,0);
     glVertex2f(0,-0.5);
     glVertex2f(-0.5,0);
     glEnd ();

     glColor3f (0.0, 0.0, 1.0); //warna biru

     glBegin(GL_POLYGON); // persegi biru
     glVertex2f(-0.5,1);
     glVertex2f(-1,0.5);
     glVertex2f(-0.5,0);
     glVertex2f(0,0.5);
     glEnd ();

     glColor3f (1.0, 0.0, 0.0); //warna merah

     glBegin(GL_POLYGON); //persegi merah
     glVertex2f(0.5,1);
     glVertex2f(0,0.5);
     glVertex2f(0.5,0);
     glVertex2f(1,0.5);
     glEnd ();

     glColor3f (0.0, 1.0, 0.0); //warna hijau

     glBegin(GL_POLYGON); //persegi hijau
     glVertex2f(-0.5,0);
     glVertex2f(-1,-0.5);
     glVertex2f(-0.5,-1);
     glVertex2f(0,-0.5);
     glEnd ();

     glColor3f (1.0, 0.0, 1.0); //warna ungu

     glBegin(GL_POLYGON); //persegi ungu
     glVertex2f(0.5,0);
     glVertex2f(0,-0.5);
     glVertex2f(0.5,-1);
     glVertex2f(1,-0.5);
     glEnd ();

     glFlush();
     }

int main(int argc, char *argv[])
{


    glutCreateWindow("ini kotak");
    glutDisplayFunc(tampil);
    glutMainLoop();

    system("PAUSE");
    return EXIT_SUCCESS;
}

      Maka setelah koding diatas dijalankan maka akan menghasilkan gambar seperti dibawah ini. yang berhiasakan warna warni sebuah desain primitif namun untuk awal-awal saya sangat bahagia bisa melakukannya.




ini adalah informasi dasar dari kodingan diatas.

  1. glBegin(mode);
       glVertex* (…);
         …..
       glVertex* (…);
    glEnd ();
       Seperti yang kita lihat di koding atas setiap awal sebuah desain gambar diopengl selalu diawali dengan GL_Begin dan di akhiri dengan glEnd, nah diatara glBegin dan glEnd itu isinya adalah sebuah titik dan manipulasi warna ada disini. adapun beberapa contoh dari macam-macam titik tersebut adalah :

GL_POINTS,
GL_LINES,
GL_LINE_STRIP,
GL_LINE_LOOP,
GL_TRIANGLES,
GL_TRIANGLE_STRIP,
GL_TRIANGLE_FAN,
GL_QUADS,
 GL_QUAD_STRIP,
GL_POLYGON

openGL code
function
glClearColor (1.0,1.0,1.0,1.0);
Indicates what color is used as background. Colors expressed in the form of RGBA (red, gree, blue alpha). Each color ranges between 0 and 1 only
glClear (GL_COLOR_BUFFER_BIT);
This function will remove windows and give the color that we have previously defined by using glclearcolor.
glColor3f (1.0, 1.0, 0.0);
Are the 3 main colors (red, green, blue). The number 1 means yes the number 0 means no. So when we will define the colors in the picture. We first define this glColor3f code before the code glBegin () \ glEnd ().
glVertex2f(0.5,1);
To determine a 2 point only on the x and y axes.
glFlush();
To ensure that the image command is executed. Without this command the image can not be loaded.
glutCreateWindow("ini kotak");
Note in the following picture note on the top there is the word "this box". Yes very true its function just for naming project only
glutDisplayFunc(tampil);
Used to call the function appear from the image that we designed above. On the show function
glutMainLoop();
Used to have openGL run the code.

Reverensi

  1. http://www.sholihin.com/2011/10/membuat-persegi-warna-warni-dengan.html
  2. http://yuniarelfrida.blogspot.co.id/2012/10/membuat-garis-dengan-opengl_26.html

1 comment:

Tingkat keanekaragaman hayati tingkat gen

Tingkat keanekaragaman hayati tingkat gen -- Keanekaragaman hayati adalah sebuah istilah untuk menggambarkan keanekaragaman mahluk hidup di ...