Last active
August 29, 2015 14:14
-
-
Save sugiartocokrowibowo/05dbbe200f3c046810f3 to your computer and use it in GitHub Desktop.
Source code tutorial CG[LWJGL0016] || Membuat Texture dengan bantuan Library Slick-Util Oleh Sugiarto Cokrowibowo
This file contains hidden or 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(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment