Wednesday 11 July 2018

projek OpenGL "pemandangan 2D" dengan codeblock c++

ini adalah projek 2D yaitu menggambar/ mendisain pemandangan alam sederhana dengan openGL codeblock. gambar ini sangat simple dan mudah difahami dan dicoba sourcodenya secara langsung menggunakan editor codeblock.

adapun hasil akhir dari projek ini adalah sebagai berikut :


adapun langkah-langkah untuk membuat projek sederhana ini adalah sebagai berikut :

  • Download OpenGL terlebih dahulu disini : View openGL

  • Setting codeblock dengan opengl. kalau belum tau caranya dapat dilihat disini "cara setting OpenGl ke codeblock
  • Buka editor codeblock  cara memulai projeknya dapat dilihat disini : view
  • Copy kode dibawah ini kemudian jalankan.



#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <math.h>


void PohonKelapa(void){
   glClear(GL_COLOR_BUFFER_BIT);

   //Gradasi Langit Senja
   glBegin(GL_POLYGON);
      glColor3ub(255,165,0);    glVertex2f(3.0, 3.0);
      glColor3ub(255,255,255);   glVertex2f(-1.7, 3.0);
      glColor3ub(255,255,255);   glVertex2f(3.0, 1.0);
   glEnd();

   //Gradasi Langit
   glBegin(GL_POLYGON);
      glColor3ub(0,206,209);           glVertex2f(-3.0, 3.0);
      glColor3ub(255,165,0);           glVertex2f(3.0, 3.0);
      glColor3ub(255,165,0);           glVertex2f(3.0, 2.5);
      glColor3ub(255,228,181);         glVertex2f(-3.0, 2.5);
   glEnd();

   //Matahari ketika Senja
   glBegin(GL_POLYGON);
      glColor3ub(255,165,0);           glVertex2f(3.0, 3.0);
      glColor3ub(255,248,220);         glVertex2f(2.7, 3.0);
      glColor3ub(255,248,220);         glVertex2f(3.0, 2.7);
   glEnd();

   //Burung (dari kanan ke kiri)
   glLineWidth(0.5);
   glBegin(GL_LINE_STRIP);
      glColor3ub(139,139,131);         glVertex2f(-0.1, 2.3);
      glColor3ub(139,139,131);         glVertex2f(0.0, 2.4);
      glColor3ub(255,255,240);         glVertex2f(0.1, 2.3);
      glColor3ub(255,255,240);         glVertex2f(0.0, 2.3);
      glColor3ub(139,139,131);         glVertex2f(0.1, 2.4);
      glColor3ub(139,139,131);         glVertex2f(0.2, 2.3);
   glEnd();
   glBegin(GL_LINE_STRIP);
      glColor3ub(139,139,131);         glVertex2f(-1.7, 2.1);
      glColor3ub(139,139,131);         glVertex2f(-1.6, 2.2);
      glColor3ub(255,255,240);         glVertex2f(-1.5, 2.1);
      glColor3ub(255,255,240);         glVertex2f(-1.6, 2.1);
      glColor3ub(139,139,131);         glVertex2f(-1.5, 2.2);
      glColor3ub(139,139,131);         glVertex2f(-1.4, 2.1);
   glEnd();
   glBegin(GL_LINE_STRIP);
      glColor3ub(139,139,131);         glVertex2f(-1.9, 2.3);
      glColor3ub(139,139,131);         glVertex2f(-1.8, 2.4);
      glColor3ub(255,255,240);         glVertex2f(-1.7, 2.3);
      glColor3ub(255,255,240);         glVertex2f(-1.8, 2.3);
      glColor3ub(139,139,131);         glVertex2f(-1.7, 2.4);
      glColor3ub(139,139,131);         glVertex2f(-1.6, 2.3);
   glEnd();
   glBegin(GL_LINE_STRIP);
      glColor3ub(139,139,131);         glVertex2f(-2.4, 2.2);
      glColor3ub(139,139,131);         glVertex2f(-2.3, 2.3);
      glColor3ub(255,255,240);         glVertex2f(-2.2, 2.2);
      glColor3ub(255,255,240);         glVertex2f(-2.3, 2.2);
      glColor3ub(139,139,131);         glVertex2f(-2.2, 2.3);
      glColor3ub(139,139,131);         glVertex2f(-2.1, 2.2);
   glEnd();

   //Pesisir
   glBegin(GL_TRIANGLES);
      glColor3ub(255,255,255);   glVertex2f(-3.0, 0.0);
      glColor3ub(205,133,63);    glVertex2f(-3.0, -3.0);
      glColor3ub(255,255,255);   glVertex2f(3.0, -3.0);
   glEnd();

   //Air Laut
   glBegin(GL_TRIANGLES);
      glColor3ub(135,206,250);   glVertex2f(3.0, 0.3);
      glColor3ub(255,255,255);   glVertex2f(-2.8, 0.3);
      glColor3ub(255,255,255);   glVertex2f(3.0, -2.8);
   glEnd();

   //Garis Tepi Laut atas
   glLineWidth(1);
   glBegin(GL_LINE_STRIP);
      glColor3ub(139,137,137);   glVertex2f(-3.0, 0.3);
      glColor3ub(238,233,233);   glVertex2f(3.0, 0.3);
   glEnd();

    //Garis Tepi Laut
   glLineWidth(2);
   glBegin(GL_LINE_STRIP);
      glColor3ub(245,245,245);   glVertex2f(-3.0, 0.3);
      glColor3ub(238,233,233);   glVertex2f(-2.6, 0.2);
      glColor3ub(205,201,201);   glVertex2f(-1.8, -0.2);
      glColor3ub(0,0,0);         glVertex2f(-1.8, -0.3);
      glColor3ub(139,131,120);   glVertex2f(-1.9, -0.4);
      glColor3ub(0,0,0);         glVertex2f(-1.8, -0.5);
      glColor3ub(105,105,105);   glVertex2f(-1.2, -0.7);
      glColor3ub(105,105,105);   glVertex2f(-1.0, -0.8);
      glColor3ub(0,0,0);         glVertex2f(-0.1, -0.9);
      glColor3ub(0,0,0);         glVertex2f(0.0, -1.0);
      glColor3ub(105,105,105);   glVertex2f(-0.1, -1.1);
      glColor3ub(0,0,0);         glVertex2f(-0.3, -1.2);
      glColor3ub(0,0,0);         glVertex2f(-0.4, -1.3);
      glColor3ub(0,0,0);         glVertex2f(-0.1, -1.4);
      glColor3ub(105,105,105);   glVertex2f(1.0, -1.4);
      glColor3ub(0,0,0);         glVertex2f(1.1, -1.5);
      glColor3ub(105,105,105);   glVertex2f(1.0, -1.5);
      glColor3ub(0,0,0);         glVertex2f(0.8, -1.6);
      glColor3ub(105,105,105);   glVertex2f(1.1, -1.7);
      glColor3ub(0,0,0);         glVertex2f(1.6, -1.9);
      glColor3ub(0,0,0);         glVertex2f(3.0, -2.8);
   glEnd();

   //Batang Kelapa
   glBegin(GL_POLYGON);
      glColor3ub(0,0,0);         glVertex2f(-1.6, -1.7);
      glColor3ub(222, 133, 63);  glVertex2f(-1.5, -1.5);
      glColor3ub(210, 105, 30);  glVertex2f(-1.4, -1.2);
      glColor3ub(255, 222, 173); glVertex2f(-1.3, -0.9);
      glColor3ub(205,133,63);    glVertex2f(-1.2, -0.7);
      glColor3ub(205, 133, 63);  glVertex2f(-1.1, -0.5);
      glColor3ub(222, 184, 135); glVertex2f(-0.8, 0.0);
      glColor3ub(205,133,63);    glVertex2f(-0.6, 0.2);
      glColor3ub(210, 105, 30);  glVertex2f(-0.7, -0.1);
      glColor3ub(255, 222, 173); glVertex2f(-0.8, -0.3);
      glColor3ub(205,133,63);    glVertex2f(-0.9, -0.5);
      glColor3ub(160, 82, 45);   glVertex2f(-1.0, -0.7);
      glColor3ub(205, 133, 63);  glVertex2f(-1.1, -1.0);
      glColor3ub(210, 105, 30);  glVertex2f(-1.2, -1.4);
      glColor3ub(0,0,0);         glVertex2f(-1.1, -1.7);
   glEnd();

   //Daun Kelapa kanan bawah
   glBegin(GL_POLYGON);
      glColor3ub(107,142,35);    glVertex2f(-0.6, 0.2);
      glColor3ub(107,142,35);    glVertex2f(-0.4, 0.0);
      glColor3ub(107,142,35);    glVertex2f(-0.4, -0.1);
      glColor3ub(107,142,35);    glVertex2f(-0.5, -0.3);
      glColor3ub(154,205,50);    glVertex2f(-0.6, -0.5);
      glColor3ub(107,142,35);    glVertex2f(-0.8, -0.7);
      glColor3ub(154,205,50);    glVertex2f(-0.7, -0.5);
      glColor3ub(107,142,35);    glVertex2f(-0.8, -0.5);
      glColor3ub(124,252,0);     glVertex2f(-0.7, -0.1);
      glColor3ub(107,142,35);    glVertex2f(-0.5, 0.1);
   glEnd();

   //Daun Kelapa kanan bawah2
   glBegin(GL_POLYGON);
      glColor3ub(107,142,35);    glVertex2f(-0.6, 0.2);
      glColor3ub(107,142,35);    glVertex2f(-0.4, 0.3);
      glColor3ub(107,142,35);    glVertex2f(0.0, 0.2);
      glColor3ub(107,142,35);    glVertex2f(0.1, 0.0);
      glColor3ub(154,205,50);    glVertex2f(0.2, -0.3);
      glColor3ub(107,142,35);    glVertex2f(0.2, -0.4);
      glColor3ub(154,205,50);    glVertex2f(-0.1, -0.2);
      glColor3ub(107,142,35);    glVertex2f(0.0, -0.1);
      glColor3ub(124,252,0);     glVertex2f(-0.3, 0.0);
      glColor3ub(107,142,35);    glVertex2f(-0.2, 0.1);
   glEnd();


    //Daun Kelapa tengah
   glBegin(GL_POLYGON);
      glColor3ub(107,142,35);    glVertex2f(-0.6, 0.2);
      glColor3ub(124,252,0);     glVertex2f(-0.5, 0.5);
      glColor3ub(255,255,224);   glVertex2f(-0.4, 1.0);
      glColor3ub(173,255,47);    glVertex2f(-0.4, 0.5);
      glColor3ub(250,250,210);   glVertex2f(-0.1, 0.9);
      glColor3ub(173,255,47);    glVertex2f(-0.3, 0.4);
   glEnd();

   //Daun Kelapa kiri
   glBegin(GL_POLYGON);
      glColor3ub(107,142,35);    glVertex2f(-0.6, 0.2);
      glColor3ub(173,255,47);    glVertex2f(-0.8, 0.3);
      glColor3ub(255,255,224);   glVertex2f(-0.9, 0.3);
      glColor3ub(173,255,47);    glVertex2f(-1.1, 0.2);
      glColor3ub(173,255,47);    glVertex2f(-1.1, 0.0);
      glColor3ub(173,255,47);    glVertex2f(-1.2, -0.1);
      glColor3ub(107,142,35);    glVertex2f(-1.1, -0.3);
      glColor3ub(173,255,47);    glVertex2f(-1.3, -0.7);
      glColor3ub(173,255,47);    glVertex2f(-1.1, -0.3);
      glColor3ub(173,255,47);    glVertex2f(-1.0, -0.3);
      glColor3ub(107,142,35);    glVertex2f(-1.0, -0.1);
      glColor3ub(173,255,47);    glVertex2f(-0.9, -0.1);
      glColor3ub(107,142,35);    glVertex2f(-0.9, 0.1);
   glEnd();

   //Daun kiri2
   glBegin(GL_POLYGON);
      glColor3ub(107,142,35);    glVertex2f(-0.6, 0.2);
      glColor3ub(173,255,47);    glVertex2f(-0.7, 0.4);
      glColor3ub(255,255,224);   glVertex2f(-0.9, 0.5);
      glColor3ub(173,255,47);    glVertex2f(-1.0, 0.5);
      glColor3ub(173,255,47);    glVertex2f(-1.3, 0.5);
      glColor3ub(173,255,47);    glVertex2f(-1.4, 0.5);
      glColor3ub(107,142,35);    glVertex2f(-1.5, 0.3);
      glColor3ub(173,255,47);    glVertex2f(-1.3, 0.4);
      glColor3ub(173,255,47);    glVertex2f(-1.2, 0.5);
      glColor3ub(173,255,47);    glVertex2f(-1.0, 0.6);
      glColor3ub(107,142,35);    glVertex2f(-1.0, 0.5);
      glColor3ub(173,255,47);    glVertex2f(-0.8, 0.5);
      glColor3ub(107,142,35);    glVertex2f(-0.8, 0.4);
      glColor3ub(107,142,35);    glVertex2f(-0.6, 0.2);
   glEnd();

   //Tikar
   glBegin(GL_POLYGON);
      glColor3ub(238,233,233);   glVertex2f(-2.6, -0.8);
      glColor3ub(85,107,47);     glVertex2f(-2.0, -0.8);
      glColor3ub(85,107,47);     glVertex2f(-2.2, -1.0);
      glColor3ub(85,107,47);     glVertex2f(-2.8, -1.0);
   glEnd();

    //Kerangka Tikar
   glLineWidth(1);
   glBegin(GL_LINE_STRIP);
      glColor3ub(0,0,0);         glVertex2f(-2.0, -0.8);
      glColor3ub(0,0,0);         glVertex2f(-2.2, -1.0);
      glColor3ub(0,0,0);         glVertex2f(-2.8, -1.0);
   glEnd();

   //Tiang Payung
   glLineWidth(3);
   glBegin(GL_LINES);
      glColor3ub(0,0,0);         glVertex2f(-2.7, -0.9);
      glColor3ub(0,0,0);         glVertex2f(-2.6, -0.4);
   glEnd();

   //Payung
   glBegin(GL_TRIANGLE_FAN);
      glColor3ub(176,224,230);   glVertex2f(-2.6, -0.2);
      glColor3ub(25,25,112);     glVertex2f(-2.9, -0.4);
      glColor3ub(255,0,0);       glVertex2f(-2.8, -0.5);
      glColor3ub(34,139,34);     glVertex2f(-2.6, -0.6);
      glColor3ub(25,25,112);     glVertex2f(-2.5, -0.6);
      glColor3ub(255,69,0);      glVertex2f(-2.3, -0.5);
      glColor3ub(148,0,211);     glVertex2f(-2.2, -0.4);
   glEnd();

   //Kerangka Payung
   glLineWidth(1);
   glBegin(GL_LINE_STRIP);
      glColor3ub(0,0,0);         glVertex2f(-2.6, -0.2);
      glColor3ub(0,0,0);         glVertex2f(-2.9, -0.4);
      glColor3ub(0,0,0);         glVertex2f(-2.8, -0.5);
      glColor3ub(0,0,0);         glVertex2f(-2.6, -0.6);
      glColor3ub(0,0,0);         glVertex2f(-2.5, -0.6);
      glColor3ub(0,0,0);         glVertex2f(-2.3, -0.5);
      glColor3ub(0,0,0);         glVertex2f(-2.2, -0.4);
   glEnd();

   //Gunung kanan
   glBegin(GL_TRIANGLES);
      glColor3ub(112,138,144);   glVertex2f(1.6, 0.2);
      glColor3ub(112,138,144);   glVertex2f(3.0, 0.2);
      glColor3ub(0,0,128);       glVertex2f(2.3, 1.0);
   glEnd();

   //Kerangka Gunung kanan
   glLineWidth(1);
   glBegin(GL_LINE_LOOP);
      glColor3ub(0,0,0);         glVertex2f(1.6, 0.2);
      glColor3ub(0,0,0);         glVertex2f(3.0, 0.2);
      glColor3ub(0,0,0);         glVertex2f(2.3, 1.0);
   glEnd();

   //Gunung kiri
   glBegin(GL_TRIANGLES);
      glColor3ub(112,138,144);   glVertex2f(0.6, 0.2);
      glColor3ub(112,138,144);   glVertex2f(2.0, 0.2);
      glColor3ub(0,0,128);       glVertex2f(1.3, 1.0);
   glEnd();

   //Kerangka Gunung kiri
   glLineWidth(1);
   glBegin(GL_LINE_LOOP);
      glColor3ub(0,0,0);         glVertex2f(0.6, 0.2);
      glColor3ub(0,0,0);         glVertex2f(2.0, 0.2);
      glColor3ub(0,0,0);         glVertex2f(1.3, 1.0);
   glEnd();

   //Area bawah Gunung
   glBegin(GL_TRIANGLES);
      glColor3ub(0,0,0);         glVertex2f(0.5, 0.2);
      glColor3ub(0,0,0);         glVertex2f(3.0, 0.2);
      glColor3ub(5,105,105);     glVertex2f(2.9, 0.3);
   glEnd();

   //Perahu
   glBegin(GL_POLYGON);
      glColor3ub(160,82,45);     glVertex2f(-0.1, -2.2);
      glColor3ub(160,82,45);     glVertex2f(-0.2, -2.3);
      glColor3ub(205,133,63);    glVertex2f(-0.1, -2.5);
      glColor3ub(160,82,45);     glVertex2f(0.0, -2.6);
      glColor3ub(205,133,63);    glVertex2f(0.3, -2.7);
      glColor3ub(160,82,45);     glVertex2f(1.4, -2.7);
      glColor3ub(139,69,19);     glVertex2f(1.7, -2.6);
      glColor3ub(160,82,45);     glVertex2f(1.8, -2.5);
      glColor3ub(255,255,255);   glVertex2f(1.9, -2.3);
      glColor3ub(255,255,255);   glVertex2f(1.8, -2.2);
      glColor3ub(255,255,255);   glVertex2f(0.9, -2.1);
   glEnd();
   glLineWidth(2);
   glBegin(GL_LINE_STRIP); //Kerangka Perahu
      glColor3ub(0,0,0);         glVertex2f(1.9, -2.3);
      glColor3ub(0,0,0);         glVertex2f(1.7, -2.4);
      glColor3ub(0,0,0);         glVertex2f(-0.1, -2.4);
      glColor3ub(165,42,42);     glVertex2f(-0.2, -2.3);
      glColor3ub(165,42,42);     glVertex2f(-0.1, -2.5);
      glColor3ub(0,0,0);         glVertex2f(0.0, -2.6);
      glColor3ub(0,0,0);         glVertex2f(0.3, -2.7);
      glColor3ub(0,0,0);         glVertex2f(1.4, -2.7);
      glColor3ub(0,0,0);         glVertex2f(1.7, -2.6);
      glColor3ub(0,0,0);         glVertex2f(1.8, -2.5);
      glColor3ub(0,0,0);         glVertex2f(1.9, -2.3);
      glColor3ub(165,42,42);     glVertex2f(1.8, -2.2);
      glColor3ub(165,42,42);     glVertex2f(0.9, -2.1);
      glColor3ub(165,42,42);     glVertex2f(-0.1, -2.2);
   glEnd();
   glLineWidth(9);
   glBegin(GL_LINE_STRIP);
      glColor3ub(139,69,19);     glVertex2f(1.2, -2.6);
      glColor3ub(139,69,19);     glVertex2f(1.5, -2.8);
      glColor3ub(0,0,0);         glVertex2f(1.6, -2.9);
   glEnd();
   glBegin(GL_LINE_STRIP);
      glColor3ub(139,69,19);     glVertex2f(0.4, -2.6);
      glColor3ub(139,69,19);     glVertex2f(0.7, -2.8);
      glColor3ub(0,0,0);         glVertex2f(0.8, -2.9);
   glEnd();
   glBegin(GL_LINES);
      glColor3ub(139,69,19);     glVertex2f(0.4, -2.9);
      glColor3ub(205,133,63);    glVertex2f(1.8, -2.9);
   glEnd();
   glLineWidth(2);//kerangka
   glBegin(GL_LINES);
      glColor3ub(0,0,0);         glVertex2f(0.4, -2.9);
      glColor3ub(0,0,0);         glVertex2f(1.8, -2.9);
   glEnd();



   glFlush();
}

void display(void){
   glClear (GL_COLOR_BUFFER_BIT);
   PohonKelapa();
   glEnd();
   glFlush();
}

int main(int argc, char *argv[]){
    glutInitWindowSize(400,400);                // ukuran jendela
    glutInitWindowPosition(15,15);             // sumbu x : kiri ke kanan // sumbu y : atas ke bawah
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); // mode tampilan warna RGB dan banyak frame
    glutCreateWindow("pemandangan sorehari");
    gluOrtho2D(-3, 3., -3, 3);                   // Plot batas koodinat x dan y (dari -1 sd 1)
 glClearColor(1.0, 1.0, 1.0, 1.0);     // warna background window R G B A
    glutDisplayFunc(display);                    // fungsi untuk menampilkan display
    glutMainLoop();                              // fungsi untuk mengulangi program utama secara terus menerus
    return 0;
}



untuk hasil akhir dari projek ini dapat anda lihat di deskripsi awal. projek ini akan menghasilkan pemandangan ala gambar anak SD.demikian tutorial kali ini semoga bermanfaat bagi kalian yang sedang mengerjakan tugas dari dosen mengenai openGL.




  1. http://3mimo3t.blogspot.co.id/2013/11/grafika-komputer.html




Contoh Projek 3D lainnya.


Baca Juga Projek OpenGL dengan C++ Lainnya  dibawah ini :


 Contoh Projek 2D openGL

Baca juga Animasi 2D untuk projek openGL lainnya dibawah ini


Dasar OpenGL

Baca juga Dasar Dari OpenGL dibawah ini jika kalian ingin menekuninya.



No comments:

Post a Comment

Tingkat keanekaragaman hayati tingkat gen

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