1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| public class Sprite { public float x; public float y; public Texture texture; }
public void run() {
Sprite [] sprite = new Sprite[2]; sprite[0] = new Sprite(); sprite[0].x = 20; sprite[0].y = 20; sprite[0].texture = square; sprite[1] = new Sprite(); sprite[1].x = 125; sprite[1].y = 125; sprite[1].texture = smile; final int amountOfVertex = 4; final int vertexSize = 2; / 2 for x and y cord only
FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertex * vertexSize);
FloatBuffer texCoordData = BufferUtils.createFloatBuffer(amountOfVertex * vertexSize); texCoordData.put(new float[]{ 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f}); texCoordData.flip(); int vboVertexHandle = glGenBuffers(); glBindTexture(GL_TEXTURE_2D, square.getTextureID()); glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle); glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); int texCoordHandle = glGenBuffers(); glBindTexture(GL_TEXTURE_2D, texCoordHandle); glBindBuffer(GL_ARRAY_BUFFER, texCoordHandle); glBufferData(GL_ARRAY_BUFFER, texCoordData, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0);
while(!Display.isCloseRequested()) { gamepad.pollInput(); glClear(GL_COLOR_BUFFER_BIT); for(int i = 0; i < 2; i++) { vertexData.put(new float[]{ sprite[i].x, sprite[i].y, sprite[i].x + sprite[i].texture.getImageWidth(), sprite[i].y, sprite[i].x + sprite[i].texture.getImageWidth(), sprite[i].y + sprite[i].texture.getImageHeight(), sprite[i].x, sprite[i].y + sprite[i].texture.getImageHeight()}); vertexData.flip(); glBindTexture(GL_TEXTURE_2D, sprite[i].texture.getTextureID()); glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle); glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle); glVertexPointer(vertexSize, GL_FLOAT, 0, 0L); glBindBuffer(GL_ARRAY_BUFFER, texCoordHandle); glTexCoordPointer(vertexSize, GL_FLOAT, 0, 0L); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glDrawArrays(GL_QUADS, 0, amountOfVertex); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); } Display.update(); Display.sync(60); } |