Senin, 19 Januari 2015

CG[LWJGL0003] || Inisialisasi Jendela OpenGL

Table of contents


Pada postingan sebelumnya kita telah membahas pembuatan window OpenGl menggunakan library LWJGL. Selanjutnya pada postingan kali ini kita akan membahas tentang inisialisasi window OpenGL. tanpa perlu banya basa-basi kita langsung ke TKP

Buatlah sebuah class java baru komudian copy-paste code berikut:

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;

import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;


public class TutorialLWJGL0003 {

String windowTitle = "CG[LWJGL0003] || Inisialisasi Jendela OpenGL";
public boolean closeRequested = false;

public void run() {
createWindow();
initializeGL();

// main loop
while (!closeRequested) {
pollInput();
updateLogic();
renderGL();
Display.update();
}

cleanup();
}

private void initializeGL() {
int width = Display.getDisplayMode().getWidth();
int height = Display.getDisplayMode().getHeight();

GL11.glViewport(0, 0, width, height); // Reset The Current Viewport
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix
GLU.gluPerspective(45.0f, ((float) width / (float) height), 0.1f, 100.0f); // Calculate The Aspect Ratio Of The Window
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
GL11.glLoadIdentity(); // Reset The Modelview Matrix

GL11.glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
GL11.glClearDepth(1.0f); // Depth Buffer Setup
GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Test To Do
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
}

private void updateLogic() {
//
}

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

public void pollInput() {

// scroll through key events
while (Keyboard.next()) {
if (Keyboard.getEventKeyState()) {
if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE)
closeRequested = true;
}
}

if (Display.isCloseRequested()) {
closeRequested = true;
}
}

private void createWindow() {
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setVSyncEnabled(true);
Display.setTitle(windowTitle);
Display.create();
} catch (LWJGLException e) {
Sys.alert("Error", "Initialization failed!\n\n" + e.getMessage());
System.exit(0);
}
}

private void cleanup() {
Display.destroy();
}

public static void main(String[] args) {
TutorialLWJGL0003 tutorial = new TutorialLWJGL0003();
tutorial.run();
}

}

source code di atas sebernarnya tidak jauh berbeda dengan source code pada tutorial sebelumnya. yang berbeda di sini hanyalah pada method private void initializeGL() { }.  metode ini berisi operasi operasi inisialisasi window OpenGL yang akan kita gunakan.


sebagai contoh jika kita ingin membuat window OpenGL dengan warna biru dapat kita lakukan dengan mengubah nilai input method
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
menjadi
GL11.glClearColor(0.20392156862f, 0.59607843137f, 0.85882352941f, 0.0f);
dan hasilnya akan menjadi sebagai berikut:


SELAMAT NGODING


lihat semua tutorial

Referensi:
http://wiki.lwjgl.org/index.php?title=Main_Page
http://nehe.gamedev.net/
http://computergraphicsindonesia.blogspot.com/2015/01/cglwjgl0002-membuat-jendela-opengl.html
http://computergraphicsindonesia.blogspot.com/2015/01/cglwjgl0001-memasukkan-library-lwjgl-ke.html

Tidak ada komentar:

Posting Komentar

/*SYNTAX HIGHLIGHTER*/