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.



Wednesday, 13 June 2018

Example Projek OpenGL Lampion merah dengan Codeblock C++

ini adalah sebuah projek membuat lampion dengan efek memutar, menjauhkan layar dengan tombol, mendekatkan dengan layar , merubah putaran ke arah sumbu x, y atau z. hasil akhir dari projek ini adalah seperti gambar dibawah ini.


langkah-langkagh untuk membuatnya adalah sebagai berikut.

  1. download terlebih dahulu openGL disini : View openGL
  2. setting codeblock dengan openGL caranya dapat dilihat disini: view
  3. buka codeblock dan buat projek baru caranya dapat dilihat disini : view
  4. copy semua sourcode yang tersedia di blog ini.
adapun sourcode yang tersedia adalah sebagai berikut ini :


#include <GL/glut.h>
#include <windows.h>
int w=400, h=400, z=0;
int x1=0, y1=0, z1=0, sudut=0;
void renderScene (void) {
static float alpha =0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor (0, 0, 0, 0);
glLoadIdentity ();
glTranslatef (0, 0, z);
glRotatef (sudut, x1, y1, z1);
glRotatef (alpha,1,1,1);
glColor3f(1, 1, 0);
alpha = alpha +0.5;
//glutWireCube (3);//fungsi kubus
//glutSolidCube(3);//kubus penuh warna
glutWireSphere(2,90,90);//fungsi bola
//glutWireCone(2, 4, 25, 25);//fungsi kerucut
glColor3f(1,0,0);
glutWireTorus (4, 2, 90, 30);//fungsi donat
//glutWireTeapot (4);//fungsi ceret
//glutSolidIcosahedron ();//fungsi delima
//glutWireDodecahedron ();//fungsi bola
//glutWireTetrahedron();//fungsi piramida
glutSwapBuffers ();
}
void resize (int w1, int h1) {
glViewport (0, 0, w1, h1);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (45.0,(float) w1/(float) h1,1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
void Keyboard (GLubyte key, GLint x, GLint y) {
if (key == 'a' || key == 'A') z+=2;
if (key == 'd' || key == 'D') z-=2;
if (key == 'x' || key == 'X') {
x1=1;
y1=0;
z1=0;
sudut +=10;
}
if (key == 'y' || key == 'Y') {
x1=0;
y1=1;
z1=0;
sudut +=-10;
}
if (key == 'z'|| key == 'Z') {
x1=0;
y1=0;
z1=1;
sudut +=-10;
}
if ( key == 'f'|| key == 'F') {
glutFullScreen ();
}
{
int foo;
foo = x + y;
if ('q' == key || 'Q' == key || 27 == key)
exit (0);
}
}
void timer (int value) {
glutPostRedisplay ();
glutTimerFunc (1,timer,0);
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition (100,100);
glutInitWindowSize (w,h);
glutCreateWindow ("lampion");
gluOrtho2D (-w/2,w/2,-h/2,h/2);
glutDisplayFunc (renderScene);
glutReshapeFunc (resize);
glutKeyboardFunc (Keyboard);
glutTimerFunc (1,timer,0);
glutMainLoop ();
}


apabila sourcode ini dijalankan akan menghasilkan hasil seperti dibawah ini. berupa lampion merah dengan putaran-putarananya


dan berikut ini adalah tombol yang dapat digunakan untuk merubah arahnya.

a. tombol A untuk zoom (+) objek 
b.tombol D untuk zoom ( - )objek 
c.tombol X untuk rotasi sumbu x 
d. tombol Y untuk rotasi sumbu y 
e. tombol Z untuk rotasi sumbu y



  1. http://arsipbertuah.blogspot.co.id/2015/01/tugas-transformasi-2d-dan-3d-opengl-c.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.


OpenGL projek example Daun 2D dengan codeBlock C++

     kali ini kita akan mendesain sebuah gambar berupa daun sederhana dengan openGL dengan codeblock. sourcode ini sudah sempurna dan mudah digunakan dan dijalankan. projek tersebut akan menghasilkan hasil seperti gambar di bawah ini.


adapun langkah -langkah dalam membuatnya adalah sebagai berikut :
  1. download terlebih dahulu library openglnya disini : Download
  2. setting opengl di codeblock dengan openGl dapat dilihat disini : view
  3. buat projek baru dengan openGL caranya dapat dilihat disini : view
  4. copy semua sourcode yang tersedia di artikel ini
Adapun sourcode dari artikel ini adalah sebagai berikut :

#include<gl/glut.h>
void daun( ) {
       glClear(GL_COLOR_BUFFER_BIT);
       glBegin(GL_QUADS); //background
              glColor3f(1.0,1.0,1.0); // warna putih
              glVertex2f(-15,15);
              glColor3f(1.0,1.0,1.0); // warna putih
              glVertex2f(-15,-15);
              glColor3f(1.0,1.0,1.0); // warna putih
              glVertex2f(15,-15);
              glColor3f(1.0,1.0,1.0); // warna putih
              glVertex2f(15,15);
       glEnd();
       glFlush();


       glBegin(GL_TRIANGLES); //tangkai
              glColor3f(0.0,2.0,0.0); // warna hijau
              glVertex2f(-12.5,10.5);
              glColor3f(0.0,2.0,0.0); // warna hijau
              glVertex2f(-13,10);
              glColor3f(1.0,5.0,0.0); // warna kuning
              glVertex2f(-7,8.5);
       glEnd();
       glFlush();

       glBegin(GL_POLYGON);//bidang 1
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(0,10);
              glColor3f(0.0,1.0,0.0);//warna hijau
              glVertex2f(-3,10.5);
              glColor3f(0.0,1.0,0.0);//warna hijau
              glVertex2f(-6,10.5);
              glColor3f(1.0,1.0,0.0);//warna kuning
              glVertex2f(-9,9);
              glColor3f(1.0,1.0,0.0);//warna kuning
              glVertex2f(-6,8);
       glEnd();
       glFlush();

       glBegin(GL_POLYGON);//bidang 2
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(0,10);
              glColor3f(1.0,1.0,0.0); //warna kuning
              glVertex2f(-6,8);
              glColor3f(1.0,1.0,0.0); //warna kuning
              glVertex2f(-3,6);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(8,4);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(6,6.5);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(4,8);
       glEnd();
       glFlush();

       glBegin(GL_POLYGON);//bidang 3
              glColor3f(1.0,1.0,0.0); //warna kuning
              glVertex2f(-3,6);
              glColor3f(1.0,1.0,0.0); //warna kuning
              glVertex2f(3,0);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(11,-4);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(10,0);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(8,4);
       glEnd();
       glFlush();

       glBegin(GL_POLYGON);//bidang 4
              glColor3f(1.0,1.0,0.0);//warna kuning
              glVertex2f(3,0);
              glColor3f(1.0,1.0,0.0); //warna kuning
              glVertex2f(6,-4);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(11,-7);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(11,-4);
       glEnd();
       glFlush();

       glBegin(GL_POLYGON);//bidang 5
              glColor3f(1.0,1.0,0.0);//warna kuning
              glVertex2f(6,-4);
              glColor3f(1.0,1.0,0.0); //warna kuning
              glVertex2f(9,-9);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(10.5,-10);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(11,-7);
       glEnd();
       glFlush();

       glBegin(GL_POLYGON);//bidang 6
              glColor3f(1.0,1.0,0.0);//warna kuning
              glVertex2f(9,-9);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(10,-13);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(10.5,-10);
       glEnd();
       glFlush();

       glBegin(GL_POLYGON);//bidang 7
              glColor3f(1.0,1.0,0.0);// warna kuning
              glVertex2f(9,-9);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(9,-11);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(10,-13);
       glEnd();
       glFlush();

       glBegin(GL_POLYGON);//bidang 8
              glColor3f(1.0,1.0,0.0);//warna kuning
              glVertex2f(3,0);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(3,-8.5);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(9,-11);
              glColor3f(1.0,1.0,0.0); //warna kuning
              glVertex2f(9,-9);
              glColor3f(1.0,1.0,0.0); //warna kuning
              glVertex2f(6,-4);
       glEnd();
       glFlush();

       glBegin(GL_POLYGON); //bidang 9
              glColor3f(1.0,1.0,0.0);//warna kuning
              glVertex2f(-6,8);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(-5,-4);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(3,-8.5);
              glColor3f(1.0,1.0,0.0); //warna kuning
              glVertex2f(3,0);
              glColor3f(1.0,1.0,0.0); //warna kuning
              glVertex2f(-3,6);
       glEnd();
       glFlush();

       glBegin(GL_POLYGON); //bidang 10
              glColor3f(1.0,1.0,0.0);//warna kuning
              glVertex2f(-9,9);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(-10,6);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(-10,4);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(-9,1);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(-7,-2);
              glColor3f(0.0,1.0,0.0); //warna hijau
              glVertex2f(-5,-4);
              glColor3f(1.0,1.0,0.0); //warna kuning
              glVertex2f(-6,8);
       glEnd();
       glFlush();

       //TULANG
       glBegin(GL_LINE_STRIP);//tulang tengah
              glColor3f(0.0,0.0,0.0);//warna hitam
              glVertex2f(-9,9.1);
              glVertex2i(-6,8);
              glVertex2i(-3,6);
              glVertex2i(3,0);
              glVertex2i(6,-4);
              glVertex2i(9,-9);
              glVertex2i(10,-13);
       glEnd();
       glFlush();

       glBegin(GL_LINES);//tulang daun 1
              glColor3f(0.0,0.0,0.0);
              glVertex2f(-6,8);
              glVertex2f(-7,-2);
       glEnd();
       glFlush();

       glBegin(GL_LINES);//tulang daun 2
              glColor3f(0.0,0.0,0.0);
              glVertex2f(-4.5,7);
              glVertex2f(4,8);
       glEnd();
       glFlush();

       glBegin(GL_LINES);//tulang daun 3
              glColor3f(0.0,0.0,0.0);
              glVertex2f(-2,5);
              glVertex2f(-1.6,-6);
       glEnd();
       glFlush();

       glBegin(GL_LINES);//tulang daun 4
              glColor3f(0.0,0.0,0.0);
              glVertex2f(0,3);
              glVertex2f(9,2);
       glEnd();
       glFlush();

       glBegin(GL_LINES);//tulang daun 5
              glColor3f(0.0,0.0,0.0);
              glVertex2f(3,0);
              glVertex2f(4,-9);
       glEnd();
       glFlush();

       glBegin(GL_LINES);//tulang daun 6
              glColor3f(0.0,0.0,0.0);
              glVertex2f(5,-2.5);
              glVertex2f(11,-4);
       glEnd();
       glFlush();

       glBegin(GL_LINES);//tulang daun 7
              glColor3f(0.0,0.0,0.0);
              glVertex2f(7,-5.7);
              glVertex2f(8,-10.5);
       glEnd();
       glFlush();

       glBegin(GL_LINES);//tulang daun 8
              glColor3f(0.0,0.0,0.0);
              glVertex2f(8,-7.4);
              glVertex2f(10.7,-9);
       glEnd();
       glFlush();

}
int main (int argc, char **argv)
{
       glutInit(&argc, argv);
       glutInitWindowPosition(350,70);
       glutInitWindowSize(400,400);
       glutCreateWindow("Godong");
       gluOrtho2D(-15.0,15.0,-15.0,15.0);
       glutDisplayFunc(daun);
       glutMainLoop();
}




apabila dicompile hasilnya akan seperti berikut ini. berupa daun sederhana yang mungkin bisa membatu kalian dalam menyelesaikan tugas. semoga bermanfaat dan dapat membatu semuanya dalam mendesain sebuah objek di opengl


sekali lagi saya ucapka terimakasih semoga bermanfaat dan berkah ilmunya. selamat mencoba... :)


  1. http://dhamalo.blogspot.co.id/2015/02/cara-membuat-daun-opengl-microsoft.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.


Tuesday, 12 June 2018

Projek OpenGL membuat sendok dengan C++ di codeBlock

     Dalam projek ini kita akan membuat sebuah sendok yang memutar-mutar. dalam hal ini sendok ini dibuat sesederhana mungkin untuk membuatnya menjadi semirip mungkin. untuk mempersingkat penjelasan projek kita yang akan kita hasilkan adalah sebagai berikut :


Adapun langkah-langkah untuk membuatnya adalah sebagai berikut.

  1. download codeblock terlebih dahulu disini : View openGL
  2. setting codeblock dengan openGL Glut caranya dapat dilihat disini : view
  3. buat projek baru dengan codeblock caranya dapat dilihat disini : view
  4. copy semua sourcode di blog ini untuk mencobanya
adapun sourcodenya adalah seperti dibawah ini :


#include <gl/glut.h>
#include <math.h>

void cylinder(float rbase,float rtop,float height);
void blok(float tebal,int ratiol,int ratiop);


int screen_width=400;
int screen_height=400;
int button_up=0,button_down=0;
int Turn=0;

double rotation_y=0,rotation_y_plus=-15,direction;
double Rhead=0,Rheadplus=0;
double rotate_All=0,All_plus=0;
double Angguk=0,Anggukplus=0;
double press=0,pressplus,pressplus1=180,pressplus2=0,pressplus3=0,pressplus4=0,pressplus5=0;



GLfloat ambient_light[]={0.0,0.0,0.45,1.0};
GLfloat  source_light[]={0.8,0.8,0.8,1.0};
GLfloat    light_pos[]={5.0,0.0,6.0,1.0};

void init(void)
{
 glClearColor(1.0,1.0,1.0,0.0);  //putih    //glClearColor(0.0,0.0,0.4,0.0); //back color
 glShadeModel(GL_SMOOTH);
 glViewport(0,0,screen_width,screen_height);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,1.0f,1000.0f);

 glEnable  (GL_DEPTH_TEST);
 glPolygonMode   (GL_FRONT_AND_BACK,GL_FILL);
 glEnable  (GL_LIGHTING);
 glLightModelfv  (GL_LIGHT_MODEL_AMBIENT,ambient_light);
 glLightfv  (GL_LIGHT0,GL_DIFFUSE,source_light);
 glLightfv  (GL_LIGHT0,GL_POSITION,light_pos);
 glEnable  (GL_LIGHT0);
 glEnable  (GL_COLOR_MATERIAL);
 glColorMaterial (GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
}

void resize(int width,int height)
{
 screen_width=width;
 screen_height=height;

 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 glViewport(0,0,screen_width,screen_height);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,1.0f,1000.0f);

 glutPostRedisplay();
}

void display(void)
{
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();

 glTranslatef(-6,3,-70);//atur posisi benda semua
 glPushMatrix();


       Rhead=Rhead+Rheadplus; /* jika cut gambar diam*/

      glRotatef(Rhead,1.0,0.0,1.0);//merubah translasi,rotsi keseluruhan kalo di nokan semua menjadi scaling
    Rheadplus=3;//untuk kecepatan rotasi
        //end turn left-right for fan head

    glTranslatef(0.0,0,0);// untuk pergereakan rotasi
    //cylinder(3,3,6); /* jika cut silinder 2 off */

   glRotatef(0,0,0.0,0.0);
      glTranslatef(0,0,5); // penggabungan obyek
   cylinder(1.9,5,3); // ukuran obyek // jika cut silinder 1 off


   glRotatef(45,1.0,0.0,0.0);
   glTranslatef(0,-1.5,5);// untuk pergereakan rotasi
   blok(0.7,3,6);

   glRotatef(45,1.0,0.0,0.0);
   glTranslatef(0,2.7,2.8);// untuk pergereakan rotasi
   blok(0.7,3,26);


    glRotatef(120,0.0,1.0,0.0);
   glPopMatrix();
   glRotatef(360,0.0,0.0,1.0);
   glTranslatef(0.0,1.0,0);
 glFlush();
 glutSwapBuffers();

}


void cylinder(float rbase,float rtop,float height)
{
 float i;
 glPushMatrix();
 glTranslatef(0.0,0.0,-rbase/4);
 glutSolidCone(rbase,0,32,9);//0 nya buat kerucut,32 nya untuk nutup lubang, 0 nya untuk mlubangi dan membuat jadi tembus (bagian atas)

 for(i=0;i<=height;i+=rbase/8)
 {
  glTranslatef(0.0,0.0,rbase/8);
  glutSolidTorus(rbase/4,rbase-((i*(rbase-rtop))/height),16,16);
 }
 glTranslatef(0.0,0.0,rbase/4);
 glutSolidCone(rtop,0,1,0);//0 nya buat kerucut(jika semua bukan nol),32 nya untuk nutup lubang, 0 nya untuk mlubangi dan membuat jadi tembus ( bagian bawah )
 glPopMatrix();
}

void blok(float tebal,int ratiol,int ratiop)
{
 float i,j;
 glPushMatrix();
 for(i=0;i<ratiop;i++)
 {
  glTranslatef(-(ratiol+1)*tebal/2,0.0,0.0);
  for(j=0;j<ratiol;j++)
  {
   glTranslatef(tebal,0.0,0.0);
   glutSolidCube(tebal);
  }
  glTranslatef(-(ratiol-1)*tebal/2,0.0,tebal);
 }
 glPopMatrix();
}

int main(int argc,char **argv)
{
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
 glutInitWindowSize(screen_width,screen_height);
 glutInitWindowPosition(0,0);
 glutCreateWindow("sendok");
 glutDisplayFunc(display);
 glutIdleFunc(display);
 glutReshapeFunc(resize);
 init();
 glutMainLoop();
 return(0);
}



setelah di running projek akan menghasilkan hasil seperti dibawah ini. bila terjadi kesalahan silahkan dikoreksi terlebih dahulu barang kali ada saourcode yang tertinggal.


demikian semoga bermanfaat dan dapat membantu semuanya yang lagi belajar openGL.




  1. http://kuli-kuliah.blogspot.co.id/2012/04/opengl-sendok.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.


Newer Posts Older Posts Home

Tingkat keanekaragaman hayati tingkat gen

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