Guest User

Le Java de OpenGL

a guest
Jan 17th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. package test;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.nio.ByteBuffer;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9. import javax.imageio.ImageIO;
  10. import org.lwjgl.BufferUtils;
  11. import org.lwjgl.LWJGLException;
  12. import org.lwjgl.opengl.Display;
  13. import org.lwjgl.opengl.DisplayMode;
  14. import static org.lwjgl.opengl.GL11.*;
  15.  
  16. public class Main
  17. {  
  18.     public static void main(String[] args)
  19.     {
  20.         try
  21.         {        
  22.             BufferedImage image = null;
  23.            
  24.             try
  25.             {
  26.                 Path currentRelativePath = Paths.get("");
  27.                
  28.                 String currentPathString = currentRelativePath.toAbsolutePath().toString();
  29.  
  30.                 image = ImageIO.read(new File(currentPathString + File.separator + "res" + File.separator + "texture.png"));
  31.             }
  32.            
  33.             catch (IOException exception)
  34.             {
  35.                 exception.printStackTrace();
  36.             }
  37.            
  38.             int[] pixels = new int[image.getWidth() * image.getHeight() * 4];
  39.  
  40.             image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
  41.            
  42.             ByteBuffer buffer = BufferUtils.createByteBuffer(pixels.length);
  43.            
  44.             for (int y = 0; y < image.getHeight(); y++)
  45.             {
  46.                 for (int x = 0; x < image.getWidth(); x++)
  47.                 {
  48.                     int pixel = pixels[y * image.getWidth() + x];
  49.  
  50.                     buffer.put((byte) ((pixel >> 16) & 0xFF));
  51.  
  52.                     buffer.put((byte) ((pixel >> 8) & 0xFF));
  53.  
  54.                     buffer.put((byte) (pixel & 0xFF));
  55.  
  56.                     buffer.put((byte) ((pixel >> 24) & 0xFF));
  57.                 }
  58.             }
  59.  
  60.             buffer.flip();
  61.  
  62.             Display.setDisplayMode(new DisplayMode(640, 480));
  63.             Display.setTitle("Title");
  64.             Display.create();
  65.            
  66.             glEnable(GL_TEXTURE_2D);
  67.            
  68.             glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  69.             glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  70.            
  71.             glClearColor(0, 0, 0, 1);
  72.            
  73.             glMatrixMode(GL_PROJECTION);
  74.            
  75.             glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 0, 1);
  76.            
  77.             glMatrixMode(GL_MODELVIEW);
  78.            
  79.             glColor3d(1, 0, 0);
  80.            
  81.             int textureId = glGenTextures();
  82.            
  83.             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  84.            
  85.             System.out.println(glGetError());
  86.  
  87.             while (true)
  88.             {
  89.                 if (Display.isCloseRequested())
  90.                     return;
  91.                
  92.                 glClear(GL_COLOR_BUFFER_BIT);
  93.                
  94.                 glBindTexture(GL_TEXTURE_2D, textureId);
  95.                
  96.                 glBegin(GL_QUADS);
  97.                     glVertex2d(50, 50);
  98.                     glVertex2d(100, 100);
  99.                     glVertex2d(300, 10);
  100.                     glVertex2d(20, 100);
  101.                 glEnd();
  102.                
  103.                 glBindTexture(GL_TEXTURE_2D, 0);
  104.                
  105.                 Display.update();
  106.                
  107.                 try
  108.                 {
  109.                     Thread.sleep(10);
  110.                 }
  111.                
  112.                 catch (InterruptedException exception)
  113.                 {
  114.                     exception.printStackTrace();
  115.                 }
  116.             }
  117.         }
  118.        
  119.         catch (LWJGLException exception)
  120.         {
  121.             exception.printStackTrace();
  122.         }
  123.        
  124.         Display.destroy();
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment