Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package RenderingModes;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.input.Keyboard;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.opengl.GL12;
- import org.newdawn.slick.opengl.Texture;
- import org.newdawn.slick.opengl.TextureLoader;
- import org.newdawn.slick.util.ResourceLoader;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.ObjectInputStream.GetField;
- import java.nio.ByteBuffer;
- import java.nio.FloatBuffer;
- import javax.imageio.ImageIO;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.opengl.GL15.*;
- /**
- * Renders a colored triangle using Vertex Buffer Objects
- *
- * @author Oskar
- */
- public class VertexBufferObjectDemo {
- private static ByteBuffer dataFromImage(BufferedImage source){
- int[] pixels = new int[source.getWidth() * source.getHeight()];
- source.getRGB(0, 0, source.getWidth(), source.getHeight(), pixels, 0, source.getWidth());
- ByteBuffer buffer = BufferUtils.createByteBuffer(source.getWidth() * source.getHeight() * 4);
- for (int y = 0; y < source.getHeight(); y++) {
- for (int x = 0; x < source.getWidth(); x++) {
- int pixel = pixels[y * source.getWidth() + x];
- buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
- buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
- buffer.put((byte) (pixel & 0xFF)); // Blue component
- buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component.
- }
- }buffer.flip();
- return buffer;
- }
- public static void main(String[] args) {
- try {
- Display.setDisplayMode(new DisplayMode(640, 480));
- Display.setTitle("Vertex Buffer Object Demo");
- Display.create();
- } catch (LWJGLException e) {
- e.printStackTrace();
- Display.destroy();
- System.exit(1);
- }
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(1, -1, 1, -1, 1, -1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- final int amountOfVertices = 6;
- final int vertexSize = 3;
- final int colorSize = 3;
- FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
- vertexData.put(new float[]{ -0.5f, 0.5f, 0,//LEFT BOTTOM
- -0.5f, -0.5f, 0,
- 0.5f, -0.5f, 0,
- 0.5f, -0.5f, 0,//RIGHT TOP
- 0.5f, 0.5f, 0,
- -0.5f, 0.5f, 0,
- });
- vertexData.flip();
- FloatBuffer colorData = BufferUtils.createFloatBuffer(amountOfVertices * colorSize);
- colorData.put(new float[]{1, 1, 1, 1, 1, 1, 1, 1, 1, /**/1, 1, 1, 1, 1, 1, 1, 1, 1});
- colorData.flip();
- FloatBuffer textureUVData = BufferUtils.createFloatBuffer(amountOfVertices * 2); // amount of vertices * two texture coordinates per-vertex
- textureUVData.put(new float[]{0,1 , 0,0 , 1,0 , 1,1 /**/, 0,1 , 0,0 });
- textureUVData.flip();
- int vboVertexHandle = glGenBuffers();
- glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
- glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- int vboColorHandle = glGenBuffers();
- glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
- glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- String path = "/ufo.png";
- InputStream in1 = VertexBufferObjectDemo.class.getResourceAsStream(path);
- BufferedImage image = null;
- try {
- image = ImageIO.read(in1);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //During setup ONLY!!!
- int myTextureID = glGenTextures();
- glBindTexture(GL_TEXTURE_2D, myTextureID);
- GL11.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, dataFromImage(image));
- int vboTextureUVHandle = glGenBuffers();
- glBindBuffer(GL_ARRAY_BUFFER, vboTextureUVHandle);
- glBufferData(GL_ARRAY_BUFFER, textureUVData, GL_DYNAMIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- while (!Display.isCloseRequested()) {
- glClear(GL_COLOR_BUFFER_BIT);
- vertexData.clear();
- //Specifies how the texture will be handled when out - of - range
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
- //Specifies how the texture should interpolate scaling (GL_NEAREST for nearest-neighbour, and GL11.GL_LINEAR for blurry stuffs)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
- GL11.glEnable(GL11.GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, myTextureID);
- GL11.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, dataFromImage(image));
- glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
- glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
- glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
- glColorPointer(colorSize, GL_FLOAT, 0, 0L);
- glBindBuffer(GL_ARRAY_BUFFER, vboTextureUVHandle);
- // 2 Because we have 2 texture coordinates per-vertex
- glTexCoordPointer(2, GL_FLOAT, 0, 0L);
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_COLOR_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glBindTexture(GL_TEXTURE_2D, myTextureID);
- glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);
- glDisableClientState(GL_COLOR_ARRAY);
- glDisableClientState(GL_VERTEX_ARRAY);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- Display.update();
- Display.sync(60);
- }
- glDeleteBuffers(vboVertexHandle);
- glDeleteBuffers(vboColorHandle);
- glDeleteBuffers(vboTextureUVHandle);
- Display.destroy();
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment