Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.ByteBuffer;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.opengl.GL14;
- import org.lwjgl.opengl.PixelFormat;
- /**
- * This is not the beginners guide to rocket science...
- * It's all you ever need to know about 2D rendering for your silly game.
- * Please don't annoy people in #lwjgl with your incompetence.
- * This tutorial uses old-style immediate mode drawing which is enough for you.
- *
- * For any question you may have now, please refer to the fucking
- * OpenGL documentation, thats what it is for.
- *
- * Thank you.
- *
- * @author Evil[1]
- */
- public class GraphicsForIdiots
- {
- /**
- * Generate a texture image.
- * This is your "image loader" - there are lot's of image loading libraries.
- * Google is always your friend.
- * @return Texture identifier.
- */
- static int checkerboard()
- {
- // Checkerboard data.
- ByteBuffer buffer = BufferUtils.createByteBuffer(64 * 64 * 4);
- // Make a checkerboard.
- for(int i = 0; i < 64; i++)
- {
- for(int j = 0; j < 64; j++)
- {
- int color = ((i & 16) ^ (j & 16)) * 0xFF;
- buffer.put((byte)color);
- buffer.put((byte)color);
- buffer.put((byte)color);
- buffer.put((byte)color);
- }
- }
- // Flip the buffer.
- buffer.flip();
- // Create the texture identifier.
- int id = GL11.glGenTextures();
- // Bind the texture - so we can set the state of the texture object.
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
- // Wrap modes - we use repeat here.
- // For 2D sprites and non-repeating textures, GL13.GL_CLAMP_TO_EDGE is adviced as mode.
- // Homework: Try out different wrap modes.
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
- // Texture filter - we use mipmapping here as this gives the highest quality under minification.
- // For 2D games, often GL11.GL_LINEAR minification is sufficient. For "blocky" games, try GL11.GL_NEAREST.
- // Homework: Try out different filter modes.
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
- // Old style automatic mipmap level creation.
- // This is a part of the texture object state, so set it before uploading.
- // Only needed if you use mipmapping.
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL14.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
- // Upload the image.
- // This loads the base texture level, all other levels are generated by the driver if enabled.
- GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, 64, 64, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
- // Bind the default texture.
- // Because OpenGL is an annoying state machine... you sometimes wonder why a texture is bound.
- // So let's revert to some inital state.
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
- // Done.
- return id;
- }
- /**
- * Your Game.
- * @param args Command line arguments.
- * @throws Exception If something goes boom because you are a bad programmer.
- */
- public static void main(String[] args)
- throws Exception
- {
- // Display properties.
- Display.setTitle("Tutorial 1: Rendering a quad!");
- Display.setDisplayMode(new DisplayMode(800, 600));
- // Let's create the display.
- Display.create(new PixelFormat(24, 0, 0, 0, 0));
- // Some hint to the driver.
- // It says: "If you (the driver) generate mipmaps, I wan't it nice!"
- GL11.glHint(GL14.GL_GENERATE_MIPMAP_HINT, GL11.GL_NICEST);
- // Set the clear color.
- GL11.glClearColor(0.6F, 0.8F, 1.0F, 1.0F);
- // Orthogonal projection, this is the 2D setup.
- GL11.glMatrixMode(GL11.GL_PROJECTION);
- GL11.glLoadIdentity();
- GL11.glOrtho(0.0F, 800.0F, 600.0F, 0.0F, -1.0F, 1.0F);
- // Identity modelview.
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- GL11.glLoadIdentity();
- // States.
- GL11.glDisable(GL11.GL_LIGHTING);
- GL11.glDisable(GL11.GL_CULL_FACE);
- // Create the texture.
- int id = checkerboard();
- // Main loop.
- while(!Display.isCloseRequested())
- {
- // Clear the screen.
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
- // Enable texturing.
- // As we want to draw textures, we need to enable it.
- GL11.glEnable(GL11.GL_TEXTURE_2D);
- // Bind our texture.
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
- // Begin quad rendering.
- GL11.glBegin(GL11.GL_QUADS);
- // Draw a quad with independend vertex colors.
- GL11.glColor3f(1.0F, 0.0F, 0.0F); GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f( 10.0F, 10.0F);
- GL11.glColor3f(0.0F, 1.0F, 0.0F); GL11.glTexCoord2f(0.0F, 1.0F); GL11.glVertex2f( 10.0F, 150.0F);
- GL11.glColor3f(0.0F, 0.0F, 1.0F); GL11.glTexCoord2f(1.0F, 1.0F); GL11.glVertex2f(150.0F, 150.0F);
- GL11.glColor3f(1.0F, 1.0F, 0.0F); GL11.glTexCoord2f(1.0F, 0.0F); GL11.glVertex2f(150.0F, 10.0F);
- // Draw another quad using a single color.
- GL11.glColor3f(1.0F, 0.0F, 1.0F);
- GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f(160.0F, 10.0F);
- GL11.glTexCoord2f(0.0F, 1.0F); GL11.glVertex2f(160.0F, 150.0F);
- GL11.glTexCoord2f(1.0F, 1.0F); GL11.glVertex2f(300.0F, 150.0F);
- GL11.glTexCoord2f(1.0F, 0.0F); GL11.glVertex2f(300.0F, 10.0F);
- // Draw another quad using a single color and repeating textures.
- // Se how mipmaps shine here...
- GL11.glColor3f(1.0F, 1.0F, 1.0F);
- GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f(310.0F, 10.0F);
- GL11.glTexCoord2f(0.0F, 3.0F); GL11.glVertex2f(310.0F, 150.0F);
- GL11.glTexCoord2f(3.0F, 3.0F); GL11.glVertex2f(450.0F, 150.0F);
- GL11.glTexCoord2f(3.0F, 0.0F); GL11.glVertex2f(450.0F, 10.0F);
- // End quad rendering.
- GL11.glEnd();
- // 2D sprite rendering often uses alpha blending, so let's alpha blend.
- // Set the blend modes based on texture (source) alpha.
- GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
- // Enable blending.
- GL11.glEnable(GL11.GL_BLEND);
- // Begin quad rendering.
- GL11.glBegin(GL11.GL_QUADS);
- // Draw a quad with independend vertex colors.
- GL11.glColor3f(1.0F, 0.0F, 0.0F); GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f( 10.0F, 160.0F);
- GL11.glColor3f(0.0F, 1.0F, 0.0F); GL11.glTexCoord2f(0.0F, 1.0F); GL11.glVertex2f( 10.0F, 310.0F);
- GL11.glColor3f(0.0F, 0.0F, 1.0F); GL11.glTexCoord2f(1.0F, 1.0F); GL11.glVertex2f(150.0F, 310.0F);
- GL11.glColor3f(1.0F, 1.0F, 0.0F); GL11.glTexCoord2f(1.0F, 0.0F); GL11.glVertex2f(150.0F, 160.0F);
- // Draw another quad using a single color.
- GL11.glColor3f(1.0F, 0.0F, 1.0F);
- GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f(160.0F, 160.0F);
- GL11.glTexCoord2f(0.0F, 1.0F); GL11.glVertex2f(160.0F, 310.0F);
- GL11.glTexCoord2f(1.0F, 1.0F); GL11.glVertex2f(300.0F, 310.0F);
- GL11.glTexCoord2f(1.0F, 0.0F); GL11.glVertex2f(300.0F, 160.0F);
- // Draw another quad using a single color and repeating textures.
- // Se how mipmaps shine here...
- GL11.glColor3f(1.0F, 1.0F, 1.0F);
- GL11.glTexCoord2f(0.0F, 0.0F); GL11.glVertex2f(310.0F, 160.0F);
- GL11.glTexCoord2f(0.0F, 3.0F); GL11.glVertex2f(310.0F, 310.0F);
- GL11.glTexCoord2f(3.0F, 3.0F); GL11.glVertex2f(450.0F, 310.0F);
- GL11.glTexCoord2f(3.0F, 0.0F); GL11.glVertex2f(450.0F, 160.0F);
- // End quad rendering.
- GL11.glEnd();
- // Disable alpha blending.
- GL11.glDisable(GL11.GL_BLEND);
- // Update.
- Display.update();
- }
- // Destroy.
- Display.destroy();
- }
- }
Add Comment
Please, Sign In to add comment