Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1051 | chris | 1 | package com.gebauz.bauzoid.graphics.model; |
| 2 | |||
| 3 | import java.nio.ByteBuffer; |
||
| 4 | import java.nio.ByteOrder; |
||
| 5 | import java.nio.IntBuffer; |
||
| 6 | import java.nio.ShortBuffer; |
||
| 7 | import java.util.ArrayList; |
||
| 8 | import java.util.List; |
||
| 9 | |||
| 10 | import com.badlogic.gdx.Gdx; |
||
| 11 | import com.badlogic.gdx.graphics.GL20; |
||
| 12 | import com.badlogic.gdx.utils.BufferUtils; |
||
| 13 | import com.gebauz.bauzoid.app.Consts; |
||
| 14 | import com.gebauz.bauzoid.graphics.Graphics; |
||
| 15 | import com.gebauz.bauzoid.graphics.GraphicsObject; |
||
| 16 | |||
| 17 | |||
| 18 | /** Index stream. */ |
||
| 19 | public class IndexStream extends GraphicsObject |
||
| 20 | { |
||
| 21 | private static List<IndexStream> mManagedStreams = new ArrayList<IndexStream>(); |
||
| 22 | |||
| 23 | private int mIndexCount = 0; |
||
| 24 | private ShortBuffer mData; |
||
| 25 | |||
| 26 | private int mBufferID = 0; |
||
| 27 | //private static int[] BUFFERID = new int[1]; |
||
| 28 | private static final IntBuffer BUFFERID = BufferUtils.newIntBuffer(1); |
||
| 29 | |||
| 30 | /** Constructor. */ |
||
| 31 | public IndexStream(Graphics graphics) |
||
| 32 | { |
||
| 33 | super(graphics); |
||
| 34 | |||
| 35 | mManagedStreams.add(this); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** Destroy internal data. */ |
||
| 39 | public void dispose() |
||
| 40 | { |
||
| 41 | mManagedStreams.remove(this); |
||
| 42 | |||
| 43 | unload(); |
||
| 44 | mIndexCount = 0; |
||
| 45 | mData = null; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** Set the data. */ |
||
| 49 | public void setData(byte data[]) |
||
| 50 | { |
||
| 51 | if (data == null) |
||
| 52 | { |
||
| 53 | dispose(); |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | |||
| 57 | ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length); |
||
| 58 | byteBuffer.order(ByteOrder.nativeOrder()); |
||
| 59 | byteBuffer.put(data); |
||
| 60 | byteBuffer.position(0); |
||
| 61 | mData = byteBuffer.asShortBuffer(); |
||
| 62 | |||
| 63 | mIndexCount = data.length; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** Set the data. */ |
||
| 67 | public void setData(short data[]) |
||
| 68 | { |
||
| 69 | if (data == null) |
||
| 70 | { |
||
| 71 | dispose(); |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | mData = BufferUtils.newShortBuffer(data.length); |
||
| 76 | mData.put(data); |
||
| 77 | mData.position(0); |
||
| 78 | |||
| 79 | mIndexCount = data.length; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** Reload all managed index streams. */ |
||
| 83 | public static void reloadManagedStreams() |
||
| 84 | { |
||
| 85 | for (int i = 0; i < mManagedStreams.size(); i++) |
||
| 86 | { |
||
| 87 | IndexStream stream = mManagedStreams.get(i); |
||
| 88 | stream.upload(); |
||
| 89 | Gdx.app.log(Consts.LOG_TAG, "IndexStream reloaded!"); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** Create handle and upload indices to hardware. */ |
||
| 94 | public final void upload() |
||
| 95 | { |
||
| 96 | if (mData == null) |
||
| 97 | return; |
||
| 98 | |||
| 99 | Gdx.gl20.glGenBuffers(1, BUFFERID); |
||
| 100 | mBufferID = BUFFERID.get(0); |
||
| 101 | |||
| 102 | Gdx.gl20.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, mBufferID); |
||
| 103 | Gdx.gl20.glBufferData(GL20.GL_ELEMENT_ARRAY_BUFFER, mData.capacity() * Consts.SIZEOF_SHORT, mData, GL20.GL_STATIC_DRAW); |
||
| 104 | Gdx.gl20.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** Reload indices to hardware. */ |
||
| 108 | public final void reupload() |
||
| 109 | { |
||
| 110 | if (mBufferID == 0) |
||
| 111 | { |
||
| 112 | // perform initial upload |
||
| 113 | upload(); |
||
| 114 | return; |
||
| 115 | } |
||
| 116 | |||
| 117 | if (mData == null) |
||
| 118 | return; |
||
| 119 | |||
| 120 | Gdx.gl20.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, mBufferID); |
||
| 121 | Gdx.gl20.glBufferSubData(GL20.GL_ELEMENT_ARRAY_BUFFER, 0, mData.capacity() * Consts.SIZEOF_SHORT, mData); |
||
| 122 | Gdx.gl20.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** Unload indices from hardware. */ |
||
| 126 | public final void unload() |
||
| 127 | { |
||
| 128 | BUFFERID.put(0, mBufferID); |
||
| 129 | Gdx.gl20.glDeleteBuffers(1, BUFFERID); |
||
| 130 | mBufferID = 0; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** Activate index stream. */ |
||
| 134 | public final void activate() |
||
| 135 | { |
||
| 136 | // TODO: state tracking |
||
| 137 | Gdx.gl20.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, mBufferID); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** Deactivate index stream. */ |
||
| 141 | public final void deactivate() |
||
| 142 | { |
||
| 143 | // TODO: state tracking |
||
| 144 | Gdx.gl20.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** Get number of indices. */ |
||
| 148 | public final int getIndexCount() |
||
| 149 | { |
||
| 150 | return mIndexCount; |
||
| 151 | } |
||
| 152 | |||
| 153 | } |