Selasa, 20 Januari 2015

CG[LWJGL0008] || 3D Shapes

Table of contents


Dalam tutorial kali ini kita akan membahas 3D Shape. 3D Shape adalah teknik menggambar objek 3D menggunakan beberapa shape 2D. Sebagai fakta bahwa kita dapat membuat objek kubus (objek 3D) dengan cara menggambar enam buah persegi (objek 2D) di keenam sisi kubus tersebut.

contoh dalam tutorial kali ini akan kita kembangkan dari tutorial sebelumnya dengan melakukan beberapa tambahan untuk method renderGL() sebagai berikut:

private void renderGL() {
  GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
  GL11.glLoadIdentity(); // Reset The View

  GL11.glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left And Into The Screen

  GL11.glRotatef(triangleAngle, 0.0f, 1.0f, 0.0f); // Rotate The Pyramid On It's Y Axis

  GL11.glBegin(GL11.GL_TRIANGLES); // Start Drawing The Pyramid
  GL11.glColor3f(1.0f, 0.0f, 0.0f); // Red
  GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
  GL11.glColor3f(0.0f, 1.0f, 0.0f); // Green
  GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Left Of Triangle (Front)
  GL11.glColor3f(0.0f, 0.0f, 1.0f); // Blue
  GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Right Of Triangle (Front)

  GL11.glColor3f(1.0f, 0.0f, 0.0f); // Red
  GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
  GL11.glColor3f(0.0f, 0.0f, 1.0f); // Blue
  GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Left Of Triangle (Right)
  GL11.glColor3f(0.0f, 1.0f, 0.0f); // Green
  GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Right Of Triangle (Right)

  GL11.glColor3f(1.0f, 0.0f, 0.0f); // Red
  GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
  GL11.glColor3f(0.0f, 1.0f, 0.0f); // Green
  GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Left Of Triangle (Back)
  GL11.glColor3f(0.0f, 0.0f, 1.0f); // Blue
  GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Right Of Triangle (Back)

  GL11.glColor3f(1.0f, 0.0f, 0.0f); // Red
  GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
  GL11.glColor3f(0.0f, 0.0f, 1.0f); // Blue
  GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Left Of Triangle (Left)
  GL11.glColor3f(0.0f, 1.0f, 0.0f); // Green
  GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Right Of Triangle (Left)
  GL11.glEnd(); // Done Drawing The Pyramid

  GL11.glLoadIdentity(); // Reset The View
  GL11.glTranslatef(1.5f, 0.0f, -7.0f); // Move Right And Into The Screen

  GL11.glRotatef(quadAngle, 1.0f, 1.0f, 1.0f); // Rotate The Cube On X, Y & Z

  GL11.glBegin(GL11.GL_QUADS); // Start Drawing The Cube
  GL11.glColor3f(0.0f, 1.0f, 0.0f); // Set The Color To Green
  GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
  GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
  GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
  GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)

   GL11.glColor3f(1.0f, 0.5f, 0.0f); // Set The Color To Orange
  GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Top Right Of The Quad (Bottom)
  GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad (Bottom)
  GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Bottom)
  GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Bottom)

  GL11.glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red
  GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
  GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
  GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Front)
  GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Front)

  GL11.glColor3f(1.0f, 1.0f, 0.0f); // Set The Color To Yellow
  GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Back)
  GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Back)
  GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back)
  GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back)

  GL11.glColor3f(0.0f, 0.0f, 1.0f); // Set The Color To Blue
  GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
  GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left)
  GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Left)
  GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Left)

  GL11.glColor3f(1.0f, 0.0f, 1.0f); // Set The Color To Violet
  GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right)
  GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
  GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Right)
  GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Right)
  GL11.glEnd(); // Done Drawing The Quad
 }

berikut ini preview hasilnya:



lihat semua tutorial

Referensi:
http://wiki.lwjgl.org/index.php?title=Main_Page
http://nehe.gamedev.net/
http://computergraphicsindonesia.blogspot.com/2015/01/cglwjgl0006-rotasi.html


Tidak ada komentar:

Posting Komentar

/*SYNTAX HIGHLIGHTER*/