1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| package YOUR.PACKAGE.HERE;
import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL12;
import static org.lwjgl.opengl.GL11.*;
public class BasicTextureTest { private static final int WIDTH = 800, HEIGHT = 600; public static void main(String[] args){ try{ Display.setDisplayMode(new DisplayMode(800, 600)); Display.create(); }catch(LWJGLException e){ e.printStackTrace(); } glMatrixMode(GL_PROJECTION); glOrtho(0, WIDTH, HEIGHT, 0, -1, 1); glMatrixMode(GL_MODELVIEW); glClearColor(0, 1, 0, 0); BufferedImage test = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = test.createGraphics();
g2d.setColor(new Color(1.0f, 1.0f, 1.0f, 0.5f)); g2d.fillRect(0, 0, 128, 128); g2d.setColor(Color.red); g2d.drawRect(0, 0, 127, 127); g2d.fillRect(10, 10, 10, 10); g2d.setColor(Color.blue); g2d.drawString("Test image", 10, 64); int textureID = loadTexture(test); glEnable(GL_TEXTURE_2D); while(!Display.isCloseRequested()){ glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glPushMatrix(); glTranslatef(100, 100, 0); glBindTexture(GL_TEXTURE_2D, textureID); glBegin(GL_QUADS); { glTexCoord2f(0, 0); glVertex2f(0, 0); glTexCoord2f(1, 0); glVertex2f(128, 0); glTexCoord2f(1, 1); glVertex2f(128, 128); glTexCoord2f(0, 1); glVertex2f(0, 128); } glEnd(); glPopMatrix(); Display.update(); } } private static final int BYTES_PER_PIXEL = 4; public static int loadTexture(BufferedImage image){ int[] pixels = new int[image.getWidth() * image.getHeight()]; image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * BYTES_PER_PIXEL); for(int y = 0; y < image.getHeight(); y++){ for(int x = 0; x < image.getWidth(); x++){ int pixel = pixels[y * image.getWidth() + x]; buffer.put((byte) ((pixel >> 16) & 0xFF)); buffer.put((byte) ((pixel >> 8) & 0xFF)); buffer.put((byte) (pixel & 0xFF)); buffer.put((byte) ((pixel >> 24) & 0xFF)); } }
buffer.flip(); int textureID = glGenTextures(); glBindTexture(GL_TEXTURE_2D, textureID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); return textureID; } } |