Created
January 26, 2015 03:51
-
-
Save sugiartocokrowibowo/e39ab53b531a85f6e8ba 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.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(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment