Guest User

Untitled

a guest
Jun 8th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.BufferedImage;
  5. import java.nio.ByteBuffer;
  6.  
  7. import org.lwjgl.BufferUtils;
  8. import org.lwjgl.opengl.GL12;
  9.  
  10. import fr.openrp_cl.main.Main;
  11. import static org.lwjgl.opengl.GL11.*;
  12.  
  13. public class Render{
  14.    
  15.     public static int loadTexture(BufferedImage Image){
  16.         int[] Pixels = new int[Image.getWidth() * Image.getHeight()];
  17.         Image.getRGB(0, 0, Image.getWidth(), Image.getHeight(), Pixels, 0, Image.getWidth());
  18.         ByteBuffer Buffer = BufferUtils.createByteBuffer(Image.getWidth() * Image.getHeight() * 4);
  19.         for(int y = 0; y < Image.getHeight(); y++){
  20.             for(int x = 0; x < Image.getWidth(); x++){
  21.                 int Pixel = Pixels[y * Image.getWidth() + x];
  22.                 Buffer.put((byte) ((Pixel >> 16) & 0xFF));
  23.                 Buffer.put((byte) ((Pixel >> 8) & 0xFF));
  24.                 Buffer.put((byte) (Pixel & 0xFF));
  25.                 Buffer.put((byte) ((Pixel >> 24) & 0xFF));
  26.             }
  27.         }
  28.         Buffer.flip();
  29.         int textureID = glGenTextures();
  30.         glBindTexture(GL_TEXTURE_2D, textureID);
  31.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
  32.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
  33.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  34.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  35.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, Image.getWidth(), Image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, Buffer);
  36.         glBindTexture(GL_TEXTURE_2D, 0);
  37.        
  38.         return textureID;
  39.     }
  40.    
  41.     public static void drawLine(float x, float y, float x2, float y2){
  42.         glBegin(GL_LINES);
  43.         glVertex2f(x, y);
  44.         glVertex2f(x2, y2);
  45.         glEnd();
  46.     }
  47.    
  48.     public static void drawQuad(float x, float y, float width, float height){
  49.         glBegin(GL_TRIANGLES);
  50.         glVertex2f(x + width, y);
  51.         glVertex2f(x, y);
  52.         glVertex2f(x, y + height);
  53.         glVertex2f(x, y + height);
  54.         glVertex2f(x + width, y + height);
  55.         glVertex2f(x + width, y);
  56.         glEnd();
  57.     }
  58.    
  59.     public static void drawTexturedQuad(int textureID, float x, float y, float width, float height, float alpha){
  60.         glLoadIdentity();
  61.         glPushMatrix();
  62.         glBindTexture(GL_TEXTURE_2D, textureID);
  63.         glColor4f(1, 1, 1, alpha);
  64.         glBegin(GL_TRIANGLES);
  65.         glTexCoord2f(1, 0);
  66.         glVertex2f(x + width, y);
  67.         glTexCoord2f(0, 0);
  68.         glVertex2f(x, y);
  69.         glTexCoord2f(0, 1);
  70.         glVertex2f(x, y + height);
  71.         glTexCoord2f(0, 1);
  72.         glVertex2f(x, y + height);
  73.         glTexCoord2f(1, 1);
  74.         glVertex2f(x + width, y + height);
  75.         glTexCoord2f(1, 0);
  76.         glVertex2f(x + width, y);
  77.         glEnd();
  78.         glLoadIdentity();
  79.         glPopAttrib();
  80.         glPushMatrix();
  81.         glBindTexture(GL_TEXTURE_2D, 0);
  82.         glColor4f(1, 1, 1, 1);
  83.     }
  84.    
  85.     public static void drawTexturedQuad(int textureID, float x, float y, float width, float height){
  86.         drawTexturedQuad(textureID, x, y, width, height, 1);
  87.     }
  88.    
  89.     public static void drawColoredQuad(float x, float y, float width, float height, float r, float g, float b, float alpha){
  90.         glColor4f(r, g, b, alpha);
  91.         drawQuad(x, y, width, height);
  92.         glColor4f(1, 1, 1, 1);
  93.     }
  94.    
  95.     public static void drawColoredQuad(float x, float y, float width, float height, float r, float g, float b){
  96.         drawColoredQuad(x, y, width, height, r, g, b, 1);
  97.     }
  98.    
  99.     public static void drawString(float x, float y, String String, Font Font, int r, int g, int b, int alpha){
  100.         BufferedImage Image = new BufferedImage(Main.Width, Main.Height, BufferedImage.TYPE_INT_ARGB);
  101.         Graphics2D Graphics2D = Image.createGraphics();
  102.         Graphics2D.setFont(Font);
  103.         Graphics2D.setColor(new Color(r, g, b, alpha));
  104.         Graphics2D.drawString(String, x, y);
  105.         Graphics2D.dispose();
  106.         int textureID = loadTexture(Image);
  107.         drawTexturedQuad(textureID, 0, 0, Main.Width, Main.Height);
  108.         glDeleteTextures(textureID);
  109.     }
  110.    
  111.     public static void drawString(float x, float y, String String, Font Font, int r, int g, int b){
  112.         drawString(x, y, String, Font, r, g, b, 1);
  113.     }
  114.    
  115.     public static void drawString(float x, float y, String String, Font Font){
  116.         drawString(x, y, String, Font, 1, 1, 1, 1);
  117.     }
  118.    
  119. }
Advertisement
Add Comment
Please, Sign In to add comment