Rev 185 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 168 | chris | 1 | package com.gebauz.Bauzoid.graphics.model; |
| 2 | |||
| 176 | chris | 3 | import java.io.ByteArrayOutputStream; |
| 4 | import java.nio.ByteBuffer; |
||
| 5 | import java.nio.ByteOrder; |
||
| 168 | chris | 6 | import java.nio.FloatBuffer; |
| 7 | |||
| 176 | chris | 8 | import android.opengl.GLES20; |
| 9 | |||
| 10 | import com.gebauz.Bauzoid.app.Consts; |
||
| 185 | chris | 11 | import com.gebauz.Bauzoid.graphics.Graphics; |
| 12 | import com.gebauz.Bauzoid.graphics.GraphicsObject; |
||
| 168 | chris | 13 | import com.gebauz.Bauzoid.math.Vector2; |
| 14 | import com.gebauz.Bauzoid.math.Vector3; |
||
| 15 | import com.gebauz.Bauzoid.math.Vector4; |
||
| 16 | |||
| 176 | chris | 17 | /** Vertex Stream that supports individual or interleaved vertex attributes. */ |
| 185 | chris | 18 | public class VertexStream extends GraphicsObject |
| 168 | chris | 19 | { |
| 170 | chris | 20 | /** Type of stream - single attribute or multi-attribute interleaved. */ |
| 21 | public enum StreamType |
||
| 22 | { |
||
| 23 | INDIVIDUAL, |
||
| 24 | INTERLEAVED |
||
| 25 | }; |
||
| 26 | |||
| 168 | chris | 27 | private StreamType mStreamType; |
| 28 | private String mName; |
||
| 29 | private FloatBuffer mData; |
||
| 176 | chris | 30 | private int mVertexCount = 0; |
| 31 | private int mCoordsPerElement = 0; |
||
| 32 | private int mBytesPerElement = 0; |
||
| 168 | chris | 33 | |
| 176 | chris | 34 | private int mBufferID = 0; |
| 35 | private static int[] BUFFERID = new int[1]; |
||
| 36 | |||
| 170 | chris | 37 | private VertexAttribute[] mAttribs; |
| 38 | |||
| 168 | chris | 39 | /** Constructor. */ |
| 185 | chris | 40 | public VertexStream(Graphics graphics, StreamType streamType, String name, VertexAttribute[] attribs) |
| 168 | chris | 41 | { |
| 185 | chris | 42 | super(graphics); |
| 168 | chris | 43 | mStreamType = streamType; |
| 44 | mName = name; |
||
| 170 | chris | 45 | mAttribs = attribs; |
| 168 | chris | 46 | } |
| 47 | |||
| 169 | chris | 48 | /** Set the buffer data. */ |
| 176 | chris | 49 | public void setData(byte[] data, int vertexCount, int coordsPerElement, int bytesPerElement) |
| 169 | chris | 50 | { |
| 176 | chris | 51 | mVertexCount = vertexCount; |
| 52 | mCoordsPerElement = coordsPerElement; |
||
| 53 | mBytesPerElement = bytesPerElement; |
||
| 169 | chris | 54 | |
| 179 | chris | 55 | ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length); |
| 176 | chris | 56 | byteBuffer.order(ByteOrder.nativeOrder()); |
| 179 | chris | 57 | byteBuffer.put(data); |
| 176 | chris | 58 | byteBuffer.position(0); |
| 59 | mData = byteBuffer.asFloatBuffer(); |
||
| 169 | chris | 60 | } |
| 61 | |||
| 176 | chris | 62 | /** Upload vertex data to hardware. */ |
| 63 | public final void upload() |
||
| 64 | { |
||
| 65 | GLES20.glGenBuffers(1, BUFFERID, 0); |
||
| 66 | mBufferID = BUFFERID[0]; |
||
| 67 | |||
| 68 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mBufferID); |
||
| 69 | GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mData.capacity() * Consts.SIZEOF_FLOAT, mData, GLES20.GL_STATIC_DRAW); |
||
| 70 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** Unload vertex data from hardware. */ |
||
| 74 | public final void unload() |
||
| 75 | { |
||
| 76 | deactivate(); |
||
| 77 | BUFFERID[0] = mBufferID; |
||
| 78 | GLES20.glDeleteBuffers(1, BUFFERID, 0); |
||
| 79 | mBufferID = 0; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** Activate stream for rendering. */ |
||
| 83 | public final void activate() |
||
| 84 | { |
||
| 85 | // TODO: buffer state tracking |
||
| 86 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mBufferID); |
||
| 87 | |||
| 88 | for (int i = 0; i < mAttribs.length; i++) |
||
| 89 | { |
||
| 90 | VertexAttribute attrib = mAttribs[i]; |
||
| 91 | |||
| 92 | // TODO: attrib to handle adapter |
||
| 93 | GLES20.glVertexAttribPointer(attrib.getAttribType(), attrib.getCoordsPerElement(), GLES20.GL_FLOAT, false, getBytesPerElement(), attrib.getByteOffset()); |
||
| 94 | GLES20.glEnableVertexAttribArray(attrib.getAttribType()); |
||
| 95 | } |
||
| 96 | |||
| 97 | } |
||
| 98 | |||
| 99 | public final void deactivate() |
||
| 100 | { |
||
| 101 | for (int i = 0; i < mAttribs.length; i++) |
||
| 102 | { |
||
| 103 | VertexAttribute attrib = mAttribs[i]; |
||
| 104 | |||
| 105 | // TODO: attrib to handle adapter |
||
| 106 | GLES20.glDisableVertexAttribArray(attrib.getAttribType()); |
||
| 107 | } |
||
| 108 | |||
| 109 | // TODO: buffer state tracking |
||
| 110 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** Get number of bytes per complete element (equals vertex stride). */ |
||
| 114 | public final int getBytesPerElement() |
||
| 115 | { |
||
| 116 | return mBytesPerElement; |
||
| 117 | } |
||
| 118 | |||
| 168 | chris | 119 | /** Get number of elements. */ |
| 176 | chris | 120 | public final int getVertexCount() |
| 168 | chris | 121 | { |
| 176 | chris | 122 | return mVertexCount; |
| 168 | chris | 123 | } |
| 124 | |||
| 125 | /** Set a two dimensional element. */ |
||
| 126 | public final void setElement2(float x, float y) |
||
| 127 | { |
||
| 128 | |||
| 129 | } |
||
| 130 | |||
| 131 | /** Set a three dimensional element. */ |
||
| 132 | public final void setElement3(float x, float y, float z) |
||
| 133 | { |
||
| 134 | |||
| 135 | } |
||
| 136 | |||
| 137 | /** Set a four dimensional element. */ |
||
| 138 | public final void setElement4(float x, float y, float z, float w) |
||
| 139 | { |
||
| 140 | |||
| 141 | } |
||
| 142 | |||
| 143 | /** Get a two dimensional element. */ |
||
| 144 | public final Vector2 getElement2() |
||
| 145 | { |
||
| 146 | return new Vector2(); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** Get a two dimensional element. */ |
||
| 150 | public final Vector3 getElement3() |
||
| 151 | { |
||
| 152 | return new Vector3(); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** Get a two dimensional element. */ |
||
| 156 | public final Vector4 getElement4() |
||
| 157 | { |
||
| 158 | return new Vector4(); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** Get name of vertex stream. */ |
||
| 162 | public String getName() |
||
| 163 | { |
||
| 164 | return mName; |
||
| 165 | } |
||
| 166 | } |
||
| 167 |