Table of contents

Slick-Util mendukung format image PNG, JPG, GIF dan TGA. Image ini kemudian akan kita gunakan untuk membuat texture.
Untuk memuat gambar menggunakan library Slick-Util sangatlah mudah. Rincian image (gambar) akan disimpan di dalam class Texture. Class Texture akan memberikan kita akses ke atribut-atribut gambar seperti lebar, tinggi, dll. Untuk memuat file gambar ke dalam class Texture digunakan method TextureLoader.getTexture( ). method TextureLoader.getTexture( ) ini menerima input berupa tipe file gambar (PNG, JPG, dll) dan path file gambar. berikut ini contoh cara memuat (me-load) gambar ke dalam class Texture:
Texture texture;
public void init() throws IOException {
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/image.png"));
}
contoh lengkapnya diperlihatkan pada code berikut:
Dalam tutorial sebelumnya, kita telah membahas cara membuat kubus. sekarang kita akan menerapkan gambar tekstur pada permukaan kubus. source codenya adalah hasil modifikasi dari Shape 3D. berikut ini source code lengkapnya:
SELAMAT MENCOBA
lihat semua tutorial
Referensi:
- http://wiki.lwjgl.org/wiki/Slick-Util_Library_-_Part_1_-_Loading_Images_for_LWJGL

Slick-Util mendukung format image PNG, JPG, GIF dan TGA. Image ini kemudian akan kita gunakan untuk membuat texture.
Untuk memuat gambar menggunakan library Slick-Util sangatlah mudah. Rincian image (gambar) akan disimpan di dalam class Texture. Class Texture akan memberikan kita akses ke atribut-atribut gambar seperti lebar, tinggi, dll. Untuk memuat file gambar ke dalam class Texture digunakan method TextureLoader.getTexture( ). method TextureLoader.getTexture( ) ini menerima input berupa tipe file gambar (PNG, JPG, dll) dan path file gambar. berikut ini contoh cara memuat (me-load) gambar ke dalam class Texture:
Texture texture;
public void init() throws IOException {
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/image.png"));
}
contoh lengkapnya diperlihatkan pada code berikut:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import org.lwjgl.LWJGLException; | |
import org.lwjgl.opengl.Display; | |
import org.lwjgl.opengl.DisplayMode; | |
import org.lwjgl.opengl.GL11; | |
import org.newdawn.slick.Color; | |
import org.newdawn.slick.opengl.Texture; | |
import org.newdawn.slick.opengl.TextureLoader; | |
import org.newdawn.slick.util.ResourceLoader; | |
public class TutorialLWJGL0016 { | |
/** The texture that will hold the image details */ | |
private Texture texture; | |
/** | |
* Start the example | |
*/ | |
public void start() { | |
initGL(640,480); | |
init(); | |
while (true) { | |
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); | |
render(); | |
Display.update(); | |
Display.sync(100); | |
if (Display.isCloseRequested()) { | |
Display.destroy(); | |
System.exit(0); | |
} | |
} | |
} | |
/** | |
* Initialise the GL display | |
* | |
* @param width The width of the display | |
* @param height The height of the display | |
*/ | |
private void initGL(int width, int height) { | |
try { | |
Display.setDisplayMode(new DisplayMode(width,height)); | |
Display.setTitle("CG[LWJGL0016] || Texture"); | |
Display.create(); | |
Display.setVSyncEnabled(true); | |
} catch (LWJGLException e) { | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
GL11.glEnable(GL11.GL_TEXTURE_2D); | |
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
// enable alpha blending | |
GL11.glEnable(GL11.GL_BLEND); | |
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); | |
GL11.glViewport(0,0,width,height); | |
GL11.glMatrixMode(GL11.GL_MODELVIEW); | |
GL11.glMatrixMode(GL11.GL_PROJECTION); | |
GL11.glLoadIdentity(); | |
GL11.glOrtho(0, width, height, 0, 1, -1); | |
GL11.glMatrixMode(GL11.GL_MODELVIEW); | |
} | |
/** | |
* Initialise resources | |
*/ | |
public void init() { | |
try { | |
// load texture from PNG file | |
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("texture.jpg")); | |
System.out.println("Texture loaded: "+texture); | |
System.out.println(">> Image width: "+texture.getImageWidth()); | |
System.out.println(">> Image height: "+texture.getImageHeight()); | |
System.out.println(">> Texture width: "+texture.getTextureWidth()); | |
System.out.println(">> Texture height: "+texture.getTextureHeight()); | |
System.out.println(">> Texture ID: "+texture.getTextureID()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* draw a quad with the image on it | |
*/ | |
public void render() { | |
Color.white.bind(); | |
texture.bind(); // or GL11.glBind(texture.getTextureID()); | |
GL11.glClearColor(0.20392156862f, 0.59607843137f, 0.85882352941f, 0.0f); | |
GL11.glBegin(GL11.GL_QUADS); | |
GL11.glTexCoord2f(0,0); | |
GL11.glVertex2f(100,100); | |
GL11.glTexCoord2f(1,0); | |
GL11.glVertex2f(100+texture.getTextureWidth(),100); | |
GL11.glTexCoord2f(1,1); | |
GL11.glVertex2f(100+texture.getTextureWidth(),100+texture.getTextureHeight()); | |
GL11.glTexCoord2f(0,1); | |
GL11.glVertex2f(100,100+texture.getTextureHeight()); | |
GL11.glEnd(); | |
} | |
/** | |
* Main Class | |
*/ | |
public static void main(String[] argv) { | |
TutorialLWJGL0016 tutorial = new TutorialLWJGL0016(); | |
tutorial.start(); | |
} | |
} |
Dalam tutorial sebelumnya, kita telah membahas cara membuat kubus. sekarang kita akan menerapkan gambar tekstur pada permukaan kubus. source codenya adalah hasil modifikasi dari Shape 3D. berikut ini source code lengkapnya:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
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; | |
import org.newdawn.slick.Color; | |
import org.newdawn.slick.opengl.Texture; | |
import org.newdawn.slick.opengl.TextureLoader; | |
import org.newdawn.slick.util.ResourceLoader; | |
public class TutorialLWJGL00161 { | |
String windowTitle = "CG[LWJGL0008] || 3D Shapes"; | |
public boolean closeRequested = false; | |
private Texture texture; | |
long lastFrameTime; // digunakan untuk menghitung delta | |
float quadAngle; // sudut rotasi untuk segi empat | |
public void run() { | |
createWindow(); | |
getDelta(); | |
initializeGL(); | |
initializeTexture(); | |
// main loop | |
while (!closeRequested) { | |
pollInput(); | |
updateLogic(getDelta()); | |
renderGL(); | |
Display.update(); | |
} | |
cleanup(); | |
} | |
private void initializeGL() { | |
int width = Display.getDisplayMode().getWidth(); | |
int height = Display.getDisplayMode().getHeight(); | |
GL11.glEnable(GL11.GL_TEXTURE_2D); | |
// enable alpha blending | |
GL11.glEnable(GL11.GL_BLEND); | |
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); | |
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.20392156862f, 0.59607843137f, 0.85882352941f, 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 | |
} | |
public void initializeTexture() { | |
try { | |
// load texture from PNG file | |
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("texture.jpg")); | |
System.out.println("Texture loaded: "+texture); | |
System.out.println(">> Image width: "+texture.getImageWidth()); | |
System.out.println(">> Image height: "+texture.getImageHeight()); | |
System.out.println(">> Texture width: "+texture.getTextureWidth()); | |
System.out.println(">> Texture height: "+texture.getTextureHeight()); | |
System.out.println(">> Texture ID: "+texture.getTextureID()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void updateLogic(int delta) { | |
quadAngle -= 0.08f * delta; // memperkecil variabel sudut rotasi quadAngle | |
} | |
private void renderGL() { | |
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer | |
Color.white.bind(); | |
texture.bind(); | |
GL11.glLoadIdentity(); // Reset The View | |
GL11.glTranslatef(0.0f, 0.0f, -4.0f); //Translasi | |
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.glTexCoord2f(0,0); | |
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top) | |
GL11.glTexCoord2f(1,0); | |
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top) | |
GL11.glTexCoord2f(1,1); | |
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top) | |
GL11.glTexCoord2f(0,1); | |
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top) | |
GL11.glTexCoord2f(0,0); | |
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Top Right Of The Quad (Bottom) | |
GL11.glTexCoord2f(1,0); | |
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad (Bottom) | |
GL11.glTexCoord2f(1,1); | |
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Bottom) | |
GL11.glTexCoord2f(0,1); | |
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Bottom) | |
GL11.glTexCoord2f(0,0); | |
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front) | |
GL11.glTexCoord2f(1,0); | |
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front) | |
GL11.glTexCoord2f(1,1); | |
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Front) | |
GL11.glTexCoord2f(0,1); | |
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Front) | |
GL11.glTexCoord2f(0,0); | |
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Back) | |
GL11.glTexCoord2f(1,0); | |
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Back) | |
GL11.glTexCoord2f(1,1); | |
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back) | |
GL11.glTexCoord2f(0,1); | |
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back) | |
GL11.glTexCoord2f(0,0); | |
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left) | |
GL11.glTexCoord2f(1,0); | |
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left) | |
GL11.glTexCoord2f(1,1); | |
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Left) | |
GL11.glTexCoord2f(0,1); | |
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Left) | |
GL11.glTexCoord2f(0,0); | |
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right) | |
GL11.glTexCoord2f(1,0); | |
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right) | |
GL11.glTexCoord2f(1,1); | |
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Right) | |
GL11.glTexCoord2f(0,1); | |
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Right) | |
GL11.glEnd(); // Done Drawing The Quad | |
} | |
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; | |
} | |
} | |
/** | |
* hitung berapa millisecon aplikasi dijalankan | |
* | |
* @return milliseconds passed since last frame | |
*/ | |
public int getDelta() { | |
long time = (Sys.getTime() * 1000) / Sys.getTimerResolution(); | |
int delta = (int) (time - lastFrameTime); | |
lastFrameTime = time; | |
return delta; | |
} | |
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) { | |
TutorialLWJGL00161 tutorial = new TutorialLWJGL00161(); | |
tutorial.run(); | |
} | |
} |
SELAMAT MENCOBA
lihat semua tutorial
- http://wiki.lwjgl.org/wiki/Slick-Util_Library_-_Part_1_-_Loading_Images_for_LWJGL
Bantu dong mas gak bisa ini saya
BalasHapus