86
4. Jenis - Jenis Texture pada OpenGl
• GL_TEXTURE_1D
: Gambar
pada texture
ini semuanya
1 dimensi.Mempunyai lebar tetapi tidak memiliki tinggi atau kedalaman
• GL_TEXTURE_2D
: Gambar
pada texture
ini semuanya
2 dimensi.Mempunyai lebar dan tinggi tetapi tidak memiliki kedalaman
• GL_TEXTURE_3D
: Gambar
pada texture
ini semuanya
3 dimensi.Mempunyai lebar, tinggi dan kedalaman.
• GL_TEXTURE_RECTANGLE : Gambar dalam teksture ini 2 dimensi
hanya satu gambar,tidak memiliki multimapping.Koordinat teksture digunakan untuk teksture yang tidak dengan titik yang sebenarnya.
• GL_TEXTURE_BUFFER: Gambar dalam teksture ini 1 dimensi hanya
satu gambar,tidak memiliki multimapping.Penyimpanan untuk data ini dating dari sebuah buffer object.
• GL_TEXTURE_CUBE_MAP : Terdapat 6 sisi dari gambar 2D ,dan
semua memiliki ukuran yang sama •
GL_TEXTURE_1D_ARRAY : Gambar di teksture ini semuanya 1 dimensi.Bagaimanapun terdiri dari banyak set dari 1 gambar dimensi,
semuanya dengan satu teksture. Panjang array adalah bagian dari ukuran teksture
• GL_TEXTURE_2D_ARRAY : Gambar di teksture ini semuanya 2
dimensi.Bagaimanapun terdiri dari banyak set dari 2 gambar dimensi, semuanya dengan satu teksture. Panjang array adalah bagian dari
ukuran teksture. •
GL_TEXTURE_CUBE_MAP_ARRAY : Gambar di teksture ini semuanya peta kubus. Terdiri dari banyak set peta kubus,semuanya dengan satu
teksture.Panjang array 6 angka dari sisi kubus adalah bagian dari ukuran teksture
• GL_TEXTURE_2D_MULTISAMPLE : Gambar di teksture ini 2 dimensi
hanya satu gambar,tidak ada multimapping.Kebanyakan pixel di gambar ini terdiri dari banyak contoh di dalamnya hanya satu nilai
• GL_TEXTURE_2D_MULTISAMPLE_ARRAY : Kombinasi 2D array dan
jenis 2D multisample.tidak ada multimapping
87 Pembuatan kode program untuk texture terhadapa objek 3 dimensi dirancang
dengan melibatkan beberapa file java untuk memudahkan dalam pengaturan dan operasi transformasi geometri. Beberapa file pendukung akan disertakan dalam
lampiran.
Kegiatan Praktikum 6.1
Keluaran Program
Perancangan class diagram
D. Aktivitas Pembelajaran
88 Texture.java
import static org.lwjgl.opengl.GL11.; import static org.lwjgl.util.glu.GLU.;
import java.awt.Font; import org.lwjgl.LWJGLException;
import org.lwjgl.Sys; import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode;
public abstract class CGApplication { protected long lastFrame;
protected int fps; protected long last;
protected String windowTitle = ; protected int width;
protected int height; protected boolean fullscreen;
protected boolean vsync; public final int DEFAULT_FONT_SIZE = 14;
protected boolean showFPS; protected int oldfps;
protected int activeFontTextureId; public void startint width, int height, boolean fullscreen,
boolean vsync,
boolean showFPS, String windowTitle { this.windowTitle = windowTitle;
this.height = height; this.width = width;
this.fullscreen = fullscreen; this.vsync = vsync;
this.showFPS = showFPS; try {
89
createWindow; init;
while Display.isCloseRequested Keyboard.isKeyDownKeyboard.KEY_ESCAPE
{ render;
Display.update; }
deinit; Display.destroy;
} catch Exception e { e.printStackTrace;
System.exit0; }
} public abstract void render;
public abstract void deinit; private void createWindow throws LWJGLException {
Display.setTitlewindowTitle; Display.setFullscreenfullscreen;
Display.setVSyncEnabledvsync; DisplayMode displayMode = null;
DisplayMode d[] = Display.getAvailableDisplayModes; for int i = 0; i d.length; i++ {
if d[i].getWidth == width d[i].getHeight == height d[i].getBitsPerPixel == 32 {
displayMode = d[i]; break;
} }
Display.setDisplayModedisplayMode; Display.create;
} private void setOrthoMode {
glViewport0, 0, width, height; glMatrixModeGL_PROJECTION;
glPushMatrix; glLoadIdentity;
gluOrtho2D0, width, height, 0; glMatrixModeGL_MODELVIEW;
glPushMatrix; glLoadIdentity;
} private void unsetOrthoMode {
glPopMatrix; glMatrixModeGL_PROJECTION;
glPopMatrix; glMatrixModeGL_MODELVIEW;
} public abstract void init;
}
GCApplication.java
import java.awt.Color; import java.awt.Graphics;
90
import java.awt.color.ColorSpace; import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage;
import java.awt.image.ColorModel; import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer; import java.awt.image.DataBufferByte;
import java.awt.image.Raster; import java.awt.image.WritableRaster;
import java.io.IOException; import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteOrder;
import java.util.Hashtable; import javax.imageio.ImageIO;
import org.newdawn.slick.util.ResourceLoader; public class Texture {
private ColorModel glAlphaColorModel = new ComponentColorModel
ColorSpace.getInstanceColorSpace.CS_sRGB, new int[] { 8, 8, 8, 8 }, true, false,
ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE; private ColorModel glColorModel = new ComponentColorModel
ColorSpace.getInstanceColorSpace.CS_sRGB, new int[] { 8, 8, 8, 0 }, false, false,
ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE;
private ByteBuffer imageData; private int width;
private int height; private ByteBuffer convertImageDataBufferedImage
bufferedImage {
ByteBuffer imageBuffer; WritableRaster raster;
BufferedImage texImage;
int texWidth = bufferedImage.getWidth; int texHeight = bufferedImage.getHeight;
if bufferedImage.getColorModel.hasAlpha { raster= Raster.createInterleavedRaster
DataBuffer.TYPE_BYTE,texWidth, texHeight,
4, null;
texImage = new BufferedImageglAlphaColorModel, raster, false,new HashtableString, Object;
} else { raster =Raster.createInterleavedRaster
DataBuffer.TYPE_BYTE,texWidth,texHeight,3,null; texImage= new BufferedImageglColorModel,
raster, false,new HashtableString, Object;
91
} AffineTransform tx =
AffineTransform.getScaleInstance1, -1; tx.translate0, -bufferedImage.getHeightnull;
AffineTransformOp op = new AffineTransformOptx,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR; bufferedImage = op.filterbufferedImage, null;
Graphics g = texImage.getGraphics; g.setColornew Color0f, 0f, 0f, 0f;
g.fillRect0, 0, texWidth, texHeight; g.drawImagebufferedImage, 0, 0, null;
this.width = texWidth; this.height = texHeight;
byte[] data=DataBufferByte texImage.getRaster.getDataBuffer.getData;
imageBuffer = ByteBuffer.allocateDirectdata.length; imageBuffer.orderByteOrder.nativeOrder;
imageBuffer.putdata, 0, data.length; imageBuffer.flip;
return imageBuffer; }
public boolean loadString ref { URL url = ResourceLoader.getResourceref;
if url == null { return false;
} try {
BufferedImage bufferedImage
= ImageIO.readurl;
imageData = convertImageDatabufferedImage; } catch IOException e {
return false; }
return true; }
public ByteBuffer getImageData { return imageData;
} public int getWidth {
return width; }
public int getHeight { return height;
} }
TextureMappingTranformasi.java
import org.lwjgl.BufferUtils; import setting.CGApplication;
import setting.Texture;
92
import static org.lwjgl.opengl.GL11.; import static org.lwjgl.util.glu.GLU.;
public class TextureMappingTransformasi extends CGApplication { private Texture m_texture;
private int m_texID; private float angle;
Override public void init {
Load image data m_texture = new Texture;
if m_texture.loadpictree.png { System.out.printlnFailed to load texture\n;
System.exit1; return;
} Generate ID m_texID
IntBuffer textureIDBuffer = BufferUtils.createIntBuffer1;
glGenTexturestextureIDBuffer; m_texID = textureIDBuffer.get0;
Activate texture m_texID glBindTextureGL_TEXTURE_2D, m_texID;
Copy image data into texture memory glTexImage2DGL_TEXTURE_2D, 0, GL_RGBA,
m_texture.getWidth,m_texture.getHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,m_texture.getImageData;
Set magnification and minification filter glTexParameteriGL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR; glTexParameteriGL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR; glDisableGL_TEXTURE_2D;
} Override
public void updateint delta { angle += 0.05f;
} private void drawTexturedQuads {
glBeginGL_QUADS; glTexCoord2d0, 0;
glVertex3f-2.0f, -2.0f, 0.0f; glTexCoord2d1, 0;
glVertex3f2.0f, -2.0f, 0.0f; glTexCoord2d1, 1;
glVertex3f2.0f, 2.0f, 0.0f; glTexCoord2d0, 1;
glVertex3f-2.0f, 2.0f, 0.0f; glEnd;
93
} Override
public void render { glViewport0, 0, width, height;
glMatrixModeGL_PROJECTION; glLoadIdentity;
gluPerspective45.0f, width height, 1.0f, 100.0f;
glClearGL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; glClearColor0.0f, 0.0f, 0.0f, 1.0f;
Transformasi glMatrixModeGL_MODELVIEW;
glLoadIdentity; Zoom-out
glTranslatef0.0f, 0.0f, -7.0f; Rotasi
glRotatefangle, 0, 1, 0; glEnableGL_DEPTH_TEST;
glEnableGL_ALPHA_TEST; glAlphaFuncGL_GREATER, 0.1f;
glEnableGL_BLEND; glBlendFuncGL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
Texturing glEnableGL_TEXTURE_2D;
glBindTextureGL_TEXTURE_2D, m_texID; drawTexturedQuads;
glRotatef90, 0, 1, 0; drawTexturedQuads;
glDisableGL_DEPTH_TEST; glDisableGL_ALPHA_TEST;
glDisableGL_TEXTURE_2D; glDisableGL_BLEND;
renderText;
} private void renderText {
drawString0, height – getDefaultFont.getLineHeight,Tekxture dengan
Transformasi.; }
public void deinit { glDeleteTexturesm_texID;
} public static void mainString[] args {
TextureMappingTransformasi app = new TextureMappingTransformasi;
app.start800, 600, false, false, true,Mapping Teksturing;
} }
94 Buatlah ilustrasi teksture mapping pada sebuah gedung seperti dalam keluaran
program di bawah ini :
Disediakan dua buah file gambar seperti berikut ini :
E. Latihan Tugas
95
gedungA.png gedungB.png
Texturing merupakan proses mewarnai, memberi tekstur, atau memberi efek material pada sebuah model 3D. Texture mapping adalah teknik shading untuk
pengolahan gambar yang memetakan sebuah fungsi pada permukaan tiga dimensi dalam scene. Fungsi yang dipetakan mencakup satu dimensi, dua
dimensi, dan tiga dimensi dan dapat digambarkan sebagai array atau fungsi matematika atau gambar.
Texture mapping merupakan teknik pemetaan sebuah tekstur pada pola gambar wireframe, dimana wireframe yang telah dibuat akan ditampilkan memiliki kulit
luar seperti tekstur yang diinginkan. OpenGl menyediakan banyak sekali jenis texture yang dapat digunakan untuk membuat objek 2 dimensi dan 3 dimensi
menjadi sangat menarik.
F. Rangkuman
96
Perancangan class diagram
CGApplication.java
Sama dengan kode program pada kegiatan praktikum 6.1
Texture.java
Sama dengan kode program pada kegiatan praktikum 6.1
TextureGedung.java
import setting.CfgApplication; import setting.Texture;
import java.awt.geom.Point2D; import static org.lwjgl.opengl.GL11.;
import static org.lwjgl.util.glu.GLU.; import java.io.IOException;
import java.nio.IntBuffer; import java.util.logging.FileHandler;
import java.util.logging.Level; import java.util.logging.Logger;
import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display; import static org.lwjgl.opengl.Display.update;
import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL12.GL_CLAMP_TO_EDGE;
G. Kunci Jawaban
97
import org.lwjgl.util.glu.GLU; public class TeksturGedung extends CGApplication {
public static final int DISPLAY_HEIGHT = 480; public static final int DISPLAY_WIDTH = 640;
private final int[] m_texID = new int[14]; public final static int NO_WRAP = -1;
private Texture m_texture;
public static void mainString[] args { TODO code application logic here
TeksturGedung main = new TeksturGedung; main.start800,600,false,false,true,Tekstur Gedung;
} public void render {
glViewport0, 0, width, height; glMatrixModeGL_PROJECTION;
glLoadIdentity; gluPerspective45.0f,widthheight, 1.0f, 700.0f;
glMatrixModeGL_MODELVIEW; glLoadIdentity;
glClearGL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; glClearColor0.0f, 0.0f, 0.0f, 0.0f;
glEnableGL_DEPTH_TEST; glEnableGL_ALPHA_TEST;
glAlphaFuncGL_GREATER, 0.1f; glEnableGL_BLEND;
glBlendFuncGL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; glEnableGL_TEXTURE_2D;
glPushMatrix; glTranslatef-50, 0, 25;
for int row = 0; row 2; row++ {
glPushMatrix; for int col = 0; col 6; col++ {
drawBuilding4, 6, 10; glTranslatef6, 0, 0;
drawBuilding4, 12, 11; glTranslatef6, 0, 0;
drawBuilding4, 10, 10; glTranslatef4, 0, 0;
} glPopMatrix;
glTranslatef0, 0, 12; }
glPopMatrix; glPushMatrix;
glTranslatef-50, 0, -50; for int row = 0; row 2; row++ {
glPushMatrix; for int col = 0; col 6; col++ {
drawBuilding4, 6, 10; glTranslatef6, 0, 0;
drawBuilding4, 12, 11; glTranslatef6, 0, 0;