Advertisement
Guest User

Text Rendering issue code

a guest
Apr 14th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.49 KB | None | 0 0
  1. package main;
  2.  
  3. import java.awt.Font;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.nio.FloatBuffer;
  9. import java.nio.IntBuffer;
  10. import java.util.ArrayList;
  11.  
  12. import org.lwjgl.BufferUtils;
  13. import org.lwjgl.LWJGLException;
  14. import org.lwjgl.Sys;
  15. import org.lwjgl.input.Keyboard;
  16. import org.lwjgl.input.Mouse;
  17. import org.lwjgl.opengl.Display;
  18. import org.lwjgl.opengl.DisplayMode;
  19. import org.lwjgl.opengl.GL11;
  20. import org.lwjgl.opengl.GL15;
  21. import org.lwjgl.util.glu.GLU;
  22. import org.newdawn.slick.Color;
  23. import org.newdawn.slick.SlickException;
  24. import org.newdawn.slick.TrueTypeFont;
  25. import org.newdawn.slick.UnicodeFont;
  26. import org.newdawn.slick.font.effects.ColorEffect;
  27. import org.newdawn.slick.opengl.Texture;
  28. import org.newdawn.slick.opengl.TextureLoader;
  29.  
  30. public class Window {
  31.    
  32.     int width = 800, height = 600;
  33.    
  34.     /** time at last frame */
  35.     long lastFrame;
  36.    
  37.     /** frames per second */
  38.     int fps;
  39.     /** last fps time */
  40.     long lastFPS;
  41.     private Texture texture;
  42.     private Vector position = new Vector(0, -1.8f, 0);
  43.     private float yaw = 0;
  44.     private float pitch = 0;
  45.     private Model model;
  46.     private Vector sector;
  47.    
  48.     private float[] cubeVerts;
  49.     private int vHandle;
  50.     private int tHandle;
  51.    
  52.     private Vector focusedBlock;
  53.  
  54.     private TrueTypeFont font;
  55.    
  56.     public void start()
  57.     {
  58.        
  59.         try {
  60.             Display.setDisplayMode(new DisplayMode(width, height));
  61.             Display.create();
  62.             Display.setLocation(500, 0);
  63.         } catch (LWJGLException e) {
  64.             e.printStackTrace();
  65.             System.exit(0);
  66.         }
  67.        
  68.  
  69.         Mouse.setGrabbed(true);
  70.        
  71.         //load textures here and other things
  72.         try {
  73.             texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("texture.png")));
  74.         } catch (FileNotFoundException e) {
  75.             e.printStackTrace();
  76.         } catch (IOException e) {
  77.             e.printStackTrace();
  78.         }
  79.        
  80.         Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
  81.         font = new TrueTypeFont(awtFont, false);
  82.        
  83.  
  84.         model = new Model();
  85.         texture.bind();
  86.        
  87.         setupBasicOpenGL();
  88.         getDelta(); // call once before loop to initialise lastFrame
  89.         lastFPS = getTime(); // call before loop to initialise fps timer
  90.        
  91.         GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
  92.         GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
  93.        
  94.        
  95.         IntBuffer ib = BufferUtils.createIntBuffer(2);
  96.         GL15.glGenBuffers(ib);
  97.         vHandle = ib.get(0);
  98.         tHandle = ib.get(1);
  99.        
  100.        
  101.         while (!Display.isCloseRequested()) {
  102.             set3DOpenGL();
  103.            
  104.             int delta = getDelta();
  105.             focusedBlock = model.hitTest(position, pitch, yaw, 8);
  106.            
  107.             GL11.glLoadIdentity();
  108.             lookThrough();
  109.            
  110.             update(delta);
  111.            
  112.             model.processQueue();
  113.             Vector sector = Model.sectorize(position);
  114.             if (sector != this.sector)
  115.             {
  116.                 model.changeSectors(this.sector, sector);
  117.                 if (this.sector == null)
  118.                 {
  119.                     model.processEntireQueue();
  120.  
  121.                     updateVBO(model.getShownBlocks());
  122.                 }
  123.                 this.sector = sector;
  124.             }
  125.            
  126.             texture.bind();
  127.             renderGL();
  128.             drawFocusedBlock(focusedBlock);
  129.            
  130.            
  131.             set2DOpenGL();
  132.             drawReticle();
  133.             Color.white.bind();
  134.             font.drawString(100, 100, "TEST");
  135.            
  136.             Display.update();
  137.             Display.sync(60); // cap fps to 60fps
  138.         }
  139.        
  140.         GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
  141.         GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
  142.        
  143.         ib.put(0, vHandle);
  144.         ib.put(1, tHandle);
  145.         GL15.glDeleteBuffers(ib);
  146.  
  147.         Display.destroy();
  148.     }
  149.    
  150.     public void renderGL()
  151.     {
  152.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
  153.        
  154.        
  155.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  156.         GL11.glDrawArrays(GL11.GL_QUADS, 0, cubeVerts.length / (3));
  157.     }
  158.    
  159.     public void updateVBO(ArrayList<Block> blocks)
  160.     {
  161.         ArrayList<Float> cubeVertList = new ArrayList<Float>();
  162.         ArrayList<Float> texCoordList = new ArrayList<Float>();
  163.        
  164.         for (Block block : blocks)
  165.         {
  166.             for (float f : block.getCubeVerts())
  167.                 cubeVertList.add(f);
  168.             for (float f : block.getTexCoords())
  169.                 texCoordList.add(f);
  170.         }
  171.        
  172.         cubeVerts = new float[cubeVertList.size()];
  173.         for (int i = 0; i < cubeVertList.size(); i++)
  174.             cubeVerts[i] = cubeVertList.get(i);
  175.        
  176.         FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(cubeVerts.length);
  177.         vertBuffer.put(cubeVerts);
  178.         vertBuffer.flip();
  179.        
  180.         float[] textures = new float[texCoordList.size()];
  181.         for (int i = 0; i < texCoordList.size(); i++)
  182.             textures[i] = texCoordList.get(i);
  183.        
  184.        
  185.         FloatBuffer texBuffer = BufferUtils.createFloatBuffer(textures.length);
  186.         texBuffer.put(textures);
  187.         texBuffer.flip();
  188.        
  189.        
  190.        
  191.        
  192.        
  193.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vHandle);
  194.         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
  195.         GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
  196.        
  197.        
  198.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tHandle);
  199.         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texBuffer, GL15.GL_STATIC_DRAW);
  200.         GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0L);
  201.     }
  202.    
  203.     public void update(int delta)
  204.     {
  205.         float mouseSensitivity = .15f;
  206.         float movementSpeed = 5 * (float)delta / 1000.0f;
  207.        
  208.         if (Mouse.isButtonDown(0))
  209.             model.removeBlock(focusedBlock, true);
  210.        
  211.        
  212.         float dx = Mouse.getDX();
  213.         //distance in mouse movement from the last getDY() call.
  214.         float dy = Mouse.getDY();
  215.        
  216.         if (Mouse.isGrabbed())
  217.         {
  218.             if (pitch - dy * mouseSensitivity < 85 && pitch - dy * mouseSensitivity > -85)
  219.                 pitch -= dy * mouseSensitivity;
  220.             yaw += dx * mouseSensitivity;
  221.         }
  222.        
  223.         if (Keyboard.isKeyDown(Keyboard.KEY_W))
  224.         {
  225.             position.x -= movementSpeed * (float)Math.sin(Math.toRadians(yaw));
  226.             position.z += movementSpeed * (float)Math.cos(Math.toRadians(yaw));
  227.         }
  228.  
  229.         //moves the camera backward relitive to its current rotation (yaw)
  230.         if (Keyboard.isKeyDown(Keyboard.KEY_S))
  231.         {      
  232.             position.x += movementSpeed * (float)Math.sin(Math.toRadians(yaw));
  233.             position.z -= movementSpeed * (float)Math.cos(Math.toRadians(yaw));
  234.         }
  235.  
  236.         //strafes the camera left relitive to its current rotation (yaw)
  237.         if (Keyboard.isKeyDown(Keyboard.KEY_A))
  238.         {
  239.             position.x -= movementSpeed * (float)Math.sin(Math.toRadians(yaw-90));
  240.             position.z += movementSpeed * (float)Math.cos(Math.toRadians(yaw-90));
  241.         }
  242.  
  243.         //strafes the camera right relitive to its current rotation (yaw)
  244.         if (Keyboard.isKeyDown(Keyboard.KEY_D))
  245.         {
  246.             position.x -= movementSpeed * (float)Math.sin(Math.toRadians(yaw+90));
  247.             position.z += movementSpeed * (float)Math.cos(Math.toRadians(yaw+90));
  248.         }
  249.        
  250.         if (Keyboard.isKeyDown(Keyboard.KEY_SPACE))
  251.             position.y -= movementSpeed * 2;
  252.         if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
  253.             position.y += movementSpeed * 2;
  254.        
  255.         if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
  256.         {
  257.             Mouse.setGrabbed(!Mouse.isGrabbed());
  258.         }
  259.        
  260.         updateFPS(); // update FPS Counter
  261.     }
  262.    
  263.    
  264.     /**
  265.      * Calculate how many milliseconds have passed
  266.      * since last frame.
  267.      *
  268.      * @return milliseconds passed since last frame
  269.      */
  270.     public int getDelta()
  271.     {
  272.         long time = getTime();
  273.         int delta = (int) (time - lastFrame);
  274.         lastFrame = time;
  275.      
  276.         return delta;
  277.     }
  278.    
  279.     /**
  280.      * Get the accurate system time
  281.      *
  282.      * @return The system time in milliseconds
  283.      */
  284.     public static long getTime()
  285.     {
  286.         return (Sys.getTime() * 1000) / Sys.getTimerResolution();
  287.     }
  288.    
  289.     /**
  290.      * Calculate the FPS and set it in the title bar
  291.      */
  292.     public void updateFPS()
  293.     {
  294.         if (getTime() - lastFPS > 1000)
  295.         {
  296.             Display.setTitle("FPS: " + fps);
  297.             fps = 0;
  298.             lastFPS += 1000;
  299.         }
  300.         fps++;
  301.     }
  302.    
  303.     public void lookThrough()
  304.     {
  305.         //roatate the pitch around the X axis
  306.         GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
  307.         //roatate the yaw around the Y axis
  308.         GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
  309.         //translate to the position vector's location
  310.         GL11.glTranslatef(position .x, position.y, position.z);
  311.     }
  312.        
  313.     public void drawFocusedBlock(Vector focusedBlock)
  314.     {
  315.         if (focusedBlock != null)
  316.         {
  317.             float[] vertexData = Block.cubeVertices(focusedBlock.x,
  318.                                                     focusedBlock.y,
  319.                                                     focusedBlock.z, .51f);
  320.             GL11.glColor3d(0, 0, 0);
  321.             GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
  322.            
  323.             GL11.glBegin(GL11.GL_QUADS);
  324.             for (int i = 0; i < vertexData.length; i += 3)
  325.             {
  326.                 GL11.glVertex3f(vertexData[i],
  327.                         vertexData[i + 1],
  328.                         vertexData[i + 2]);
  329.             }
  330.             GL11.glEnd();
  331.            
  332.             GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
  333.             GL11.glColor3d(1, 1, 1);
  334.         }
  335.     }
  336.    
  337.     public void setupBasicOpenGL()
  338.     {
  339.         GL11.glEnable(GL11.GL_CULL_FACE);
  340.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
  341.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
  342.        
  343.         setupFog();
  344.     }
  345.    
  346.     private void setupFog()
  347.     {
  348.         float r = (float)(44.0 / 255);
  349.         float g = (float)(12.0 / 255);
  350.         float b = (float)(12.0 / 255);
  351.         GL11.glClearColor(r, g, b, 1f);
  352.    
  353.         FloatBuffer fogColor = BufferUtils.createFloatBuffer(4);
  354.         fogColor.put(r).put(g).put(b).put(1f).flip();
  355.        
  356.         GL11.glEnable(GL11.GL_DEPTH_TEST);
  357.         GL11.glEnable(GL11.GL_FOG);
  358.         GL11.glFog(GL11.GL_FOG_COLOR, fogColor);
  359.         GL11.glHint(GL11.GL_FOG_HINT, GL11.GL_DONT_CARE);
  360.         GL11.glFogf(GL11.GL_FOG_DENSITY, 0.15f);
  361.         GL11.glFogf(GL11.GL_FOG_START, 10.0f);
  362.         GL11.glFogf(GL11.GL_FOG_END, 60.0f);
  363.     }
  364.    
  365.     private void set3DOpenGL()
  366.     {
  367.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  368.         GL11.glLoadIdentity();
  369.         GLU.gluPerspective(45.0f,((float)width)/((float)height),0.1f,100.0f);
  370.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  371.         GL11.glLoadIdentity();
  372.         GL11.glEnable(GL11.GL_TEXTURE_2D);
  373.         GL11.glShadeModel(GL11.GL_SMOOTH);
  374.         GL11.glClearDepth(1.0f);
  375.         GL11.glEnable(GL11.GL_DEPTH_TEST);
  376.         GL11.glDepthFunc(GL11.GL_LEQUAL);
  377.         GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
  378.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
  379.     }
  380.    
  381.     private void set2DOpenGL()
  382.     {
  383.         GL11.glEnable(GL11.GL_TEXTURE_2D);
  384.         GL11.glShadeModel(GL11.GL_SMOOTH);        
  385.         GL11.glDisable(GL11.GL_DEPTH_TEST);
  386.         GL11.glDisable(GL11.GL_LIGHTING);                    
  387.          
  388.         GL11.glClearDepth(1);                                      
  389.  
  390.         GL11.glEnable(GL11.GL_BLEND);
  391.         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  392.  
  393.         GL11.glViewport(0,0,width,height);
  394.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  395.  
  396.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  397.         GL11.glLoadIdentity();
  398.         GL11.glOrtho(0, width, height, 0, 1, -1);
  399.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  400.     }
  401.    
  402.     private void drawReticle()
  403.     {
  404.         GL11.glColor3d(0, 0, 0);
  405.         GL11.glBegin(GL11.GL_LINES);
  406.         float x = width / 2;
  407.         float y = height / 2;
  408.         float size = 10;
  409.        
  410.         GL11.glVertex2f(x - size, y);
  411.         GL11.glVertex2f(x + size, y);
  412.         GL11.glVertex2f(x, y - size);
  413.         GL11.glVertex2f(x, y + size);
  414.        
  415.         GL11.glEnd();
  416.         GL11.glColor3d(1, 1, 1);
  417.     }
  418.    
  419.     public static void main(String[] argv)
  420.     {
  421.         Window timerExample = new Window();
  422.         timerExample.start();
  423.     }
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement