Advertisement
Guest User

BasicTextureTest.java

a guest
Jun 5th, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.image.BufferedImage;
  4. import java.awt.Font;
  5. import java.nio.ByteBuffer;
  6.  
  7. import org.lwjgl.BufferUtils;
  8. import org.lwjgl.LWJGLException;
  9. import org.lwjgl.opengl.Display;
  10. import org.lwjgl.opengl.DisplayMode;
  11. import org.lwjgl.opengl.GL12;
  12.  
  13. import static org.lwjgl.opengl.GL11.*;
  14.  
  15. public class BasicTextureTest {
  16.    
  17.    private static final int WIDTH = 800, HEIGHT = 600;
  18.    
  19.    public static void main(String[] args){
  20.      
  21.       try{
  22.          Display.setDisplayMode(new DisplayMode(800, 600));
  23.          Display.create();
  24.       }catch(LWJGLException e){
  25.          e.printStackTrace();
  26.       }
  27.      
  28.       glMatrixMode(GL_PROJECTION);
  29.       glOrtho(0, WIDTH, HEIGHT, 0, -1, 1); //2D projection matrix
  30.       glMatrixMode(GL_MODELVIEW);
  31.      
  32.       glClearColor(0, 1, 0, 0); //Green clear color
  33.      
  34.      
  35.       //Generate a small test image by drawing to a BufferedImage
  36.       //It's of course also possible to just load an image using ImageIO.load()
  37.       BufferedImage test = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
  38.       Graphics2D g2d = test.createGraphics();
  39.       g2d.setFont(new Font("Arial", Font.PLAIN, 50));
  40.       g2d.setColor(Color.blue);
  41.       g2d.drawString("Test image", 20, 20); //Some blue text
  42.       //g2d.drawRect(10, 10, 10, 10);
  43.      
  44.       int textureID = loadTexture(test);
  45.      
  46.       glEnable(GL_TEXTURE_2D); //Enable texturing
  47.      
  48.      
  49.       while(!Display.isCloseRequested()){
  50.          glClear(GL_COLOR_BUFFER_BIT);
  51.          
  52.          //Enable blending so the green background can be seen through the texture
  53.          glEnable(GL_BLEND);
  54.          glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  55.          
  56.          glPushMatrix();
  57.          glTranslatef(100, 100, 0);
  58.          glBindTexture(GL_TEXTURE_2D, textureID);
  59.          glBegin(GL_QUADS);
  60.          {
  61.             glTexCoord2f(0, 0);
  62.             glVertex2f(0, 0);
  63.            
  64.             glTexCoord2f(1, 0);
  65.             glVertex2f(128, 0);
  66.            
  67.             glTexCoord2f(1, 1);
  68.             glVertex2f(128, 128);
  69.            
  70.             glTexCoord2f(0, 1);
  71.             glVertex2f(0, 128);
  72.          }
  73.          glEnd();
  74.          glPopMatrix();
  75.          
  76.          Display.update();
  77.       }
  78.    }
  79.    
  80.    private static final int BYTES_PER_PIXEL = 4;
  81.    public static int loadTexture(BufferedImage image){
  82.      
  83.       int[] pixels = new int[image.getWidth() * image.getHeight()];
  84.         image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
  85.  
  86.         ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * BYTES_PER_PIXEL); //4 for RGBA, 3 for RGB
  87.        
  88.         for(int y = 0; y < image.getHeight(); y++){
  89.             for(int x = 0; x < image.getWidth(); x++){
  90.                 int pixel = pixels[y * image.getWidth() + x];
  91.                 buffer.put((byte) ((pixel >> 16) & 0xFF));     // Red component
  92.                 buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
  93.                 buffer.put((byte) (pixel & 0xFF));               // Blue component
  94.                 buffer.put((byte) ((pixel >> 24) & 0xFF));    // Alpha component. Only for RGBA
  95.             }
  96.         }
  97.  
  98.         buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS
  99.  
  100.         // You now have a ByteBuffer filled with the color data of each pixel.
  101.         // Now just create a texture ID and bind it. Then you can load it using
  102.         // whatever OpenGL method you want, for example:
  103.  
  104.       int textureID = glGenTextures(); //Generate texture ID
  105.         glBindTexture(GL_TEXTURE_2D, textureID); //Bind texture ID
  106.        
  107.         //Setup wrap mode
  108.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
  109.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
  110.  
  111.         //Setup texture scaling filtering
  112.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  113.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  114.         glTexCoord2f(20, 20);
  115.         //Send texel data to OpenGL
  116.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  117.      
  118.         //Return the texture ID so we can bind it later again
  119.       return textureID;
  120.    }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement