Advertisement
Guest User

An OpenGL texture font example.

a guest
Nov 10th, 2013
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.79 KB | None | 0 0
  1. package com.quew8.testapp;
  2.  
  3. import java.awt.Graphics2D;
  4. import java.awt.color.ColorSpace;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.image.ColorModel;
  7. import java.awt.image.ComponentColorModel;
  8. import java.awt.image.DataBuffer;
  9. import java.awt.image.DataBufferByte;
  10. import java.awt.image.Raster;
  11. import java.awt.image.WritableRaster;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.nio.ByteBuffer;
  15. import javax.imageio.ImageIO;
  16. import org.lwjgl.BufferUtils;
  17. import org.lwjgl.LWJGLException;
  18. import org.lwjgl.opengl.Display;
  19. import org.lwjgl.opengl.DisplayMode;
  20.  
  21. import static org.lwjgl.opengl.GL11.*;
  22. import org.lwjgl.util.glu.GLU;
  23.  
  24. /**
  25.  *
  26.  * @author Quew8
  27.  */
  28. public class TestApplication {
  29.     private static String[] text;
  30.     private static int charSheetTextureId;
  31.     private static final ColorModel glColourModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
  32.                                             new int[] {8,8,8,8},
  33.                                             true,
  34.                                             false,
  35.                                             ComponentColorModel.TRANSLUCENT,
  36.                                             DataBuffer.TYPE_BYTE);
  37.    
  38.     public static void init() throws LWJGLException, IOException {
  39.         Display.setDisplayMode(new DisplayMode(800, 600));
  40.         Display.setTitle("Test App");
  41.         Display.create();
  42.        
  43.         glEnable(GL_BLEND);
  44.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  45.         glEnable(GL_TEXTURE_2D);
  46.         glClearColor(1, 0, 1, 0);
  47.         charSheetTextureId = loadTexture(TestApplication.class.getClassLoader().getResourceAsStream("resources/normal.png"));
  48.     }
  49.    
  50.     public static void loop() {
  51.         int error;
  52.        
  53.         while(!Display.isCloseRequested()) {
  54.             glViewport(0, 0, 800, 600);
  55.            
  56.             glClear(GL_COLOR_BUFFER_BIT);
  57.            
  58.             glMatrixMode(GL_PROJECTION);
  59.             glLoadIdentity();
  60.             glOrtho(0, 200, 0, 200, -1, 1);
  61.            
  62.             glMatrixMode(GL_MODELVIEW);
  63.             glLoadIdentity();
  64.            
  65.             for(int i = 0; i < text.length; i++) {
  66.                 drawLineOfText(text[i], 5, 180 - (i * 15), 9, 10, 1);
  67.             }
  68.            
  69.             if( (error = glGetError()) != GL_NO_ERROR) {
  70.                 throw new RuntimeException("Something Bad Has Happened: " + GLU.gluGetString(error));
  71.             }
  72.            
  73.             Display.update();
  74.             Display.sync(60);
  75.         }
  76.     }
  77.    
  78.     public static void drawLineOfText(String line, float xPos, float yPos, float charWidth, float charHeight, float xGap) {
  79.         glBindTexture(GL_TEXTURE_2D, charSheetTextureId);
  80.         glBegin(GL_QUADS);
  81.         glColor3f(1, 1, 1);
  82.         for(int i = 0; i < line.length(); i++) {
  83.             int code = (int) line.charAt(i);
  84.             int gridX = code % 16;
  85.             int gridY = ( code - gridX ) / 16;
  86.             float texX = (float) gridX / 16;
  87.             float texY = (float) gridY / 16;
  88.            
  89.             glTexCoord2f(texX, texY);
  90.             glVertex2f( ( ( xGap + charWidth ) * i ) + xPos, yPos);
  91.            
  92.             glTexCoord2f(texX, texY + (1f / 16f) );
  93.             glVertex2f( ( ( xGap + charWidth ) * i ) + xPos, yPos - charHeight);
  94.  
  95.             glTexCoord2f(texX + (1f / 16f), texY + (1f / 16f));
  96.             glVertex2f( ( ( xGap + charWidth ) * i ) + xPos + charWidth, yPos - charHeight);
  97.  
  98.             glTexCoord2f(texX + (1f / 16f), texY);
  99.             glVertex2f( ( ( xGap + charWidth ) * i ) + xPos + charWidth, yPos);
  100.         }
  101.         glEnd();
  102.     }
  103.    
  104.     public static int loadTexture(InputStream is) throws IOException {
  105.         int texId = glGenTextures();
  106.         glBindTexture(GL_TEXTURE_2D, texId);
  107.        
  108.         BufferedImage imgIn = ImageIO.read(is);
  109.         int texWidth = imgIn.getWidth(); //Assumes POT textures.
  110.         int texHeight = imgIn.getHeight(); //Assumes POT textures.
  111.        
  112.         WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
  113.         BufferedImage texImage = new BufferedImage(glColourModel, raster, false, null);
  114.  
  115.         Graphics2D g = texImage.createGraphics();
  116.         g.setColor(new java.awt.Color(0f, 0f, 0f, 0f));
  117.         g.fillRect(0, 0, texWidth, texHeight);
  118.         g.drawImage(imgIn, 0, 0, null);
  119.        
  120.         byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
  121.         ByteBuffer bb = BufferUtils.createByteBuffer(data.length);
  122.         bb.put(data);
  123.         bb.flip();
  124.        
  125.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  126.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  127.    
  128.         glTexImage2D(GL_TEXTURE_2D,
  129.                 0,
  130.                 GL_RGBA,
  131.                 texWidth,
  132.                 texHeight,
  133.                 0,
  134.                 GL_RGBA,
  135.                 GL_UNSIGNED_BYTE,
  136.                 bb);
  137.        
  138.         glBindTexture(GL_TEXTURE_2D, 0);
  139.         return texId;
  140.     }
  141.    
  142.     public static void deinit() {
  143.         glDeleteTextures(charSheetTextureId);
  144.         Display.destroy();
  145.     }
  146.    
  147.     public static void main(String[] args) throws LWJGLException, IOException {
  148.         text =
  149.                 args.length > 0 ?
  150.                 args :
  151.                 new String[]{
  152.                     "Some Default Text",
  153.                     "Some More Text",
  154.                     "Something about",
  155.                     "A Lazy Fox. I think",
  156.                     "it jumped about or",
  157.                     "not I don't know."
  158.                 };
  159.         TestApplication.init();
  160.         TestApplication.loop();
  161.         TestApplication.deinit();
  162.     }
  163.    
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement