Guest User

Untitled

a guest
Jan 23rd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.56 KB | None | 0 0
  1. import java.nio.ByteBuffer;
  2. import org.lwjgl.BufferUtils;
  3. import org.lwjgl.opengl.Display;
  4. import org.lwjgl.opengl.DisplayMode;
  5. import org.lwjgl.opengl.GL11;
  6. import org.lwjgl.opengl.GL14;
  7. import org.lwjgl.opengl.PixelFormat;
  8.  
  9. /**
  10.  * This is not the beginners guide to rocket science...
  11.  * It's all you ever need to know about 2D rendering for your silly game.
  12.  * Please don't annoy people in #lwjgl with your incompetence.
  13.  * This tutorial uses old-style immediate mode drawing which is enough for you.
  14.  *
  15.  * For any question you may have now, please refer to the fucking
  16.  * OpenGL documentation, thats what it is for.
  17.  *
  18.  * Thank you.
  19.  *
  20.  * @author Evil[1]
  21.  */
  22. public class GraphicsForIdiots
  23. {
  24.     /**
  25.      * Generate a texture image.
  26.      * This is your "image loader" - there are lot's of image loading libraries.
  27.      * Google is always your friend.
  28.      * @return Texture identifier.
  29.      */
  30.     static int checkerboard()
  31.     {
  32.         // Checkerboard data.
  33.         ByteBuffer buffer = BufferUtils.createByteBuffer(64 * 64 * 4);
  34.        
  35.         // Make a checkerboard.
  36.         for(int i = 0; i < 64; i++)
  37.         {
  38.             for(int j = 0; j < 64; j++)
  39.             {
  40.                 int color = ((i & 16) ^ (j & 16)) * 0xFF;
  41.                
  42.                 buffer.put((byte)color);
  43.                 buffer.put((byte)color);
  44.                 buffer.put((byte)color);
  45.                 buffer.put((byte)color);
  46.             }
  47.         }
  48.        
  49.         // Flip the buffer.
  50.         buffer.flip();
  51.        
  52.         // Create the texture identifier.
  53.         int id = GL11.glGenTextures();
  54.        
  55.         // Bind the texture - so we can set the state of the texture object.
  56.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
  57.        
  58.         // Wrap modes - we use repeat here.
  59.         // For 2D sprites and non-repeating textures, GL13.GL_CLAMP_TO_EDGE is adviced as mode.
  60.         // Homework: Try out different wrap modes.
  61.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
  62.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
  63.        
  64.         // Texture filter - we use mipmapping here as this gives the highest quality under minification.
  65.         // For 2D games, often GL11.GL_LINEAR minification is sufficient. For "blocky" games, try GL11.GL_NEAREST.
  66.         // Homework: Try out different filter modes.
  67.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
  68.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
  69.        
  70.         // Old style automatic mipmap level creation.
  71.         // This is a part of the texture object state, so set it before uploading.
  72.         // Only needed if you use mipmapping.
  73.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL14.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
  74.        
  75.         // Upload the image.
  76.         // This loads the base texture level, all other levels are generated by the driver if enabled.
  77.         GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, 64, 64, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
  78.        
  79.         // Bind the default texture.
  80.         // Because OpenGL is an annoying state machine... you sometimes wonder why a texture is bound.
  81.         // So let's revert to some inital state.
  82.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
  83.        
  84.         // Done.
  85.         return id;
  86.     }
  87.    
  88.     /**
  89.      * Your Game.
  90.      * @param args Command line arguments.
  91.      * @throws Exception If something goes boom because you are a bad programmer.
  92.      */
  93.     public static void main(String[] args)
  94.     throws Exception
  95.     {
  96.         // Display properties.
  97.         Display.setTitle("Tutorial 1: Rendering a quad!");
  98.         Display.setDisplayMode(new DisplayMode(800, 600));
  99.        
  100.         // Let's create the display.
  101.         Display.create(new PixelFormat(24, 0, 0, 0, 0));
  102.        
  103.         // Some hint to the driver.
  104.         // It says: "If you (the driver) generate mipmaps, I wan't it nice!"
  105.         GL11.glHint(GL14.GL_GENERATE_MIPMAP_HINT, GL11.GL_NICEST);
  106.        
  107.         // Set the clear color.
  108.         GL11.glClearColor(0.6F, 0.8F, 1.0F, 1.0F);
  109.        
  110.         // Orthogonal projection, this is the 2D setup.
  111.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  112.         GL11.glLoadIdentity();
  113.         GL11.glOrtho(0.0F, 800.0F, 600.0F, 0.0F, -1.0F, 1.0F);
  114.        
  115.         // Identity modelview.
  116.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  117.         GL11.glLoadIdentity();
  118.        
  119.         // States.
  120.         GL11.glDisable(GL11.GL_LIGHTING);
  121.         GL11.glDisable(GL11.GL_CULL_FACE);
  122.        
  123.         // Create the texture.
  124.         int id = checkerboard();
  125.        
  126.         // Main loop.
  127.         while(!Display.isCloseRequested())
  128.         {
  129.             // Clear the screen.
  130.             GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
  131.  
  132.             // Enable texturing.
  133.             // As we want to draw textures, we need to enable it.
  134.             GL11.glEnable(GL11.GL_TEXTURE_2D);
  135.            
  136.             // Bind our texture.
  137.             GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
  138.            
  139.             // Begin quad rendering.
  140.             GL11.glBegin(GL11.GL_QUADS);
  141.            
  142.             // Draw a quad with independend vertex colors.
  143.             GL11.glColor3f(1.0F, 0.0F, 0.0F); GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f( 10.0F,  10.0F);
  144.             GL11.glColor3f(0.0F, 1.0F, 0.0F); GL11.glTexCoord2f(0.0F, 1.0F); GL11.glVertex2f( 10.0F, 150.0F);
  145.             GL11.glColor3f(0.0F, 0.0F, 1.0F); GL11.glTexCoord2f(1.0F, 1.0F); GL11.glVertex2f(150.0F, 150.0F);
  146.             GL11.glColor3f(1.0F, 1.0F, 0.0F); GL11.glTexCoord2f(1.0F, 0.0F); GL11.glVertex2f(150.0F,  10.0F);
  147.  
  148.             // Draw another quad using a single color.
  149.             GL11.glColor3f(1.0F, 0.0F, 1.0F);
  150.             GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f(160.0F,  10.0F);
  151.             GL11.glTexCoord2f(0.0F, 1.0F); GL11.glVertex2f(160.0F, 150.0F);
  152.             GL11.glTexCoord2f(1.0F, 1.0F); GL11.glVertex2f(300.0F, 150.0F);
  153.             GL11.glTexCoord2f(1.0F, 0.0F); GL11.glVertex2f(300.0F,  10.0F);
  154.  
  155.             // Draw another quad using a single color and repeating textures.
  156.             // Se how mipmaps shine here...
  157.             GL11.glColor3f(1.0F, 1.0F, 1.0F);
  158.             GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f(310.0F,  10.0F);
  159.             GL11.glTexCoord2f(0.0F, 3.0F); GL11.glVertex2f(310.0F, 150.0F);
  160.             GL11.glTexCoord2f(3.0F, 3.0F); GL11.glVertex2f(450.0F, 150.0F);
  161.             GL11.glTexCoord2f(3.0F, 0.0F); GL11.glVertex2f(450.0F,  10.0F);
  162.            
  163.             // End quad rendering.
  164.             GL11.glEnd();
  165.  
  166.             // 2D sprite rendering often uses alpha blending, so let's alpha blend.
  167.             // Set the blend modes based on texture (source) alpha.
  168.             GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  169.            
  170.             // Enable blending.
  171.             GL11.glEnable(GL11.GL_BLEND);
  172.            
  173.             // Begin quad rendering.
  174.             GL11.glBegin(GL11.GL_QUADS);
  175.            
  176.             // Draw a quad with independend vertex colors.
  177.             GL11.glColor3f(1.0F, 0.0F, 0.0F); GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f( 10.0F, 160.0F);
  178.             GL11.glColor3f(0.0F, 1.0F, 0.0F); GL11.glTexCoord2f(0.0F, 1.0F); GL11.glVertex2f( 10.0F, 310.0F);
  179.             GL11.glColor3f(0.0F, 0.0F, 1.0F); GL11.glTexCoord2f(1.0F, 1.0F); GL11.glVertex2f(150.0F, 310.0F);
  180.             GL11.glColor3f(1.0F, 1.0F, 0.0F); GL11.glTexCoord2f(1.0F, 0.0F); GL11.glVertex2f(150.0F, 160.0F);
  181.            
  182.             // Draw another quad using a single color.
  183.             GL11.glColor3f(1.0F, 0.0F, 1.0F);
  184.             GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f(160.0F, 160.0F);
  185.             GL11.glTexCoord2f(0.0F, 1.0F); GL11.glVertex2f(160.0F, 310.0F);
  186.             GL11.glTexCoord2f(1.0F, 1.0F); GL11.glVertex2f(300.0F, 310.0F);
  187.             GL11.glTexCoord2f(1.0F, 0.0F); GL11.glVertex2f(300.0F, 160.0F);
  188.            
  189.             // Draw another quad using a single color and repeating textures.
  190.             // Se how mipmaps shine here...
  191.             GL11.glColor3f(1.0F, 1.0F, 1.0F);
  192.             GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f(310.0F, 160.0F);
  193.             GL11.glTexCoord2f(0.0F, 3.0F); GL11.glVertex2f(310.0F, 310.0F);
  194.             GL11.glTexCoord2f(3.0F, 3.0F); GL11.glVertex2f(450.0F, 310.0F);
  195.             GL11.glTexCoord2f(3.0F, 0.0F); GL11.glVertex2f(450.0F, 160.0F);
  196.            
  197.             // End quad rendering.
  198.             GL11.glEnd();
  199.            
  200.             // Disable alpha blending.
  201.             GL11.glDisable(GL11.GL_BLEND);
  202.            
  203.             // Update.
  204.             Display.update();
  205.         }
  206.        
  207.         // Destroy.
  208.         Display.destroy();
  209.     }
  210. }
Add Comment
Please, Sign In to add comment