| 1 |
d333eb6b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
| 3 |
|
|
// //
|
| 4 |
|
|
// This file is part of Distorted. //
|
| 5 |
|
|
// //
|
| 6 |
|
|
// Distorted is free software: you can redistribute it and/or modify //
|
| 7 |
|
|
// it under the terms of the GNU General Public License as published by //
|
| 8 |
|
|
// the Free Software Foundation, either version 2 of the License, or //
|
| 9 |
|
|
// (at your option) any later version. //
|
| 10 |
|
|
// //
|
| 11 |
|
|
// Distorted is distributed in the hope that it will be useful, //
|
| 12 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
| 13 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
| 14 |
|
|
// GNU General Public License for more details. //
|
| 15 |
|
|
// //
|
| 16 |
|
|
// You should have received a copy of the GNU General Public License //
|
| 17 |
|
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
| 18 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 19 |
|
|
|
| 20 |
6c00149d
|
Leszek Koltunski
|
package org.distorted.library.mesh;
|
| 21 |
6a06a912
|
Leszek Koltunski
|
|
| 22 |
e6519ac8
|
Leszek Koltunski
|
import android.opengl.GLES31;
|
| 23 |
6c00149d
|
Leszek Koltunski
|
|
| 24 |
|
|
import org.distorted.library.main.Distorted;
|
| 25 |
|
|
import org.distorted.library.main.DistortedBuffer;
|
| 26 |
da681e7e
|
Leszek Koltunski
|
import org.distorted.library.program.DistortedProgram;
|
| 27 |
6c00149d
|
Leszek Koltunski
|
|
| 28 |
da681e7e
|
Leszek Koltunski
|
import java.nio.ByteBuffer;
|
| 29 |
|
|
import java.nio.ByteOrder;
|
| 30 |
6a06a912
|
Leszek Koltunski
|
import java.nio.FloatBuffer;
|
| 31 |
|
|
|
| 32 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 33 |
e0b6c593
|
Leszek Koltunski
|
/**
|
| 34 |
227b9bca
|
Leszek Koltunski
|
* Abstract class which represents a Mesh, i.e. an array of vertices (rendered as a TRIANGLE_STRIP)
|
| 35 |
e0b6c593
|
Leszek Koltunski
|
* <p>
|
| 36 |
da681e7e
|
Leszek Koltunski
|
* If you want to render to a particular shape, extend from here, construct a float array
|
| 37 |
7a5e538a
|
Leszek Koltunski
|
* containing per-vertex attributes, and call back setAttribs().
|
| 38 |
e0b6c593
|
Leszek Koltunski
|
*/
|
| 39 |
715e7726
|
Leszek Koltunski
|
public abstract class MeshBase
|
| 40 |
6a06a912
|
Leszek Koltunski
|
{
|
| 41 |
227b9bca
|
Leszek Koltunski
|
// sizes of attributes of an individual vertex.
|
| 42 |
|
|
private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
|
| 43 |
|
|
private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
|
| 44 |
|
|
private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
|
| 45 |
7a5e538a
|
Leszek Koltunski
|
private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
|
| 46 |
6f2d931d
|
Leszek Koltunski
|
|
| 47 |
|
|
static final int POS_ATTRIB = 0;
|
| 48 |
|
|
static final int NOR_ATTRIB = POS_DATA_SIZE;
|
| 49 |
|
|
static final int INF_ATTRIB = POS_DATA_SIZE + NOR_DATA_SIZE;
|
| 50 |
|
|
static final int TEX_ATTRIB = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;
|
| 51 |
227b9bca
|
Leszek Koltunski
|
static final int VERT_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE + TEX_DATA_SIZE; // number of attributes of a 'normal' vertex
|
| 52 |
|
|
static final int TRAN_ATTRIBS = POS_DATA_SIZE + POS_DATA_SIZE; // number of attributes of a transform feedback vertex
|
| 53 |
6a06a912
|
Leszek Koltunski
|
|
| 54 |
da681e7e
|
Leszek Koltunski
|
private static final int BYTES_PER_FLOAT = 4;
|
| 55 |
3ef3364d
|
Leszek Koltunski
|
|
| 56 |
6f2d931d
|
Leszek Koltunski
|
private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
|
| 57 |
|
|
private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
|
| 58 |
|
|
private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
|
| 59 |
|
|
private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
|
| 60 |
227b9bca
|
Leszek Koltunski
|
private static final int TRAN_SIZE = TRAN_ATTRIBS*BYTES_PER_FLOAT;
|
| 61 |
|
|
private static final int VERT_SIZE = VERT_ATTRIBS*BYTES_PER_FLOAT;
|
| 62 |
a51fe521
|
Leszek Koltunski
|
|
| 63 |
6c00149d
|
Leszek Koltunski
|
private boolean mShowNormals; // when rendering this mesh, draw normal vectors?
|
| 64 |
|
|
private DistortedBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
|
| 65 |
|
|
private final float zFactor; // strange workaround for the fact that we need to somehow store the 'depth'
|
| 66 |
|
|
// of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
|
| 67 |
da681e7e
|
Leszek Koltunski
|
private int mNumVertices;
|
| 68 |
227b9bca
|
Leszek Koltunski
|
private FloatBuffer mVertAttribs; // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
|
| 69 |
7a5e538a
|
Leszek Koltunski
|
private float mInflate;
|
| 70 |
3ef3364d
|
Leszek Koltunski
|
|
| 71 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 72 |
3ef3364d
|
Leszek Koltunski
|
|
| 73 |
715e7726
|
Leszek Koltunski
|
MeshBase(float factor)
|
| 74 |
3ef3364d
|
Leszek Koltunski
|
{
|
| 75 |
466450b5
|
Leszek Koltunski
|
zFactor = factor;
|
| 76 |
3fc9327a
|
Leszek Koltunski
|
mShowNormals = false;
|
| 77 |
42571056
|
Leszek Koltunski
|
|
| 78 |
466450b5
|
Leszek Koltunski
|
mVBO = new DistortedBuffer(GLES31.GL_ARRAY_BUFFER , GLES31.GL_STATIC_READ);
|
| 79 |
|
|
mTFO = new DistortedBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
|
| 80 |
42571056
|
Leszek Koltunski
|
}
|
| 81 |
|
|
|
| 82 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 83 |
6c00149d
|
Leszek Koltunski
|
// when a derived class is done computing its mesh, it has to call this method.
|
| 84 |
42571056
|
Leszek Koltunski
|
|
| 85 |
227b9bca
|
Leszek Koltunski
|
void setAttribs(float[] vertexAttribs)
|
| 86 |
42571056
|
Leszek Koltunski
|
{
|
| 87 |
227b9bca
|
Leszek Koltunski
|
mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
|
| 88 |
da681e7e
|
Leszek Koltunski
|
|
| 89 |
227b9bca
|
Leszek Koltunski
|
mVertAttribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
| 90 |
da681e7e
|
Leszek Koltunski
|
mVertAttribs.put(vertexAttribs).position(0);
|
| 91 |
|
|
|
| 92 |
227b9bca
|
Leszek Koltunski
|
mVBO.setData(mNumVertices*VERT_SIZE, mVertAttribs);
|
| 93 |
|
|
mTFO.setData(mNumVertices*TRAN_SIZE, null );
|
| 94 |
42571056
|
Leszek Koltunski
|
}
|
| 95 |
|
|
|
| 96 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 97 |
6c00149d
|
Leszek Koltunski
|
/**
|
| 98 |
|
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
| 99 |
|
|
*
|
| 100 |
|
|
* @y.exclude
|
| 101 |
|
|
*/
|
| 102 |
da681e7e
|
Leszek Koltunski
|
public int getTFO()
|
| 103 |
42571056
|
Leszek Koltunski
|
{
|
| 104 |
da681e7e
|
Leszek Koltunski
|
return mTFO.mIndex[0];
|
| 105 |
42571056
|
Leszek Koltunski
|
}
|
| 106 |
|
|
|
| 107 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 108 |
6c00149d
|
Leszek Koltunski
|
/**
|
| 109 |
|
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
| 110 |
|
|
*
|
| 111 |
|
|
* @y.exclude
|
| 112 |
|
|
*/
|
| 113 |
da681e7e
|
Leszek Koltunski
|
public int getNumVertices()
|
| 114 |
226144d0
|
leszek
|
{
|
| 115 |
da681e7e
|
Leszek Koltunski
|
return mNumVertices;
|
| 116 |
226144d0
|
leszek
|
}
|
| 117 |
42571056
|
Leszek Koltunski
|
|
| 118 |
6c00149d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 119 |
|
|
/**
|
| 120 |
|
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
| 121 |
|
|
*
|
| 122 |
|
|
* @y.exclude
|
| 123 |
|
|
*/
|
| 124 |
da681e7e
|
Leszek Koltunski
|
public float getZFactor()
|
| 125 |
6c00149d
|
Leszek Koltunski
|
{
|
| 126 |
da681e7e
|
Leszek Koltunski
|
return zFactor;
|
| 127 |
6c00149d
|
Leszek Koltunski
|
}
|
| 128 |
|
|
|
| 129 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 130 |
|
|
/**
|
| 131 |
|
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
| 132 |
|
|
*
|
| 133 |
|
|
* @y.exclude
|
| 134 |
|
|
*/
|
| 135 |
da681e7e
|
Leszek Koltunski
|
public void bindVertexAttribs(DistortedProgram program)
|
| 136 |
6c00149d
|
Leszek Koltunski
|
{
|
| 137 |
da681e7e
|
Leszek Koltunski
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mVBO.mIndex[0] );
|
| 138 |
227b9bca
|
Leszek Koltunski
|
GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_POS);
|
| 139 |
|
|
GLES31.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR);
|
| 140 |
|
|
GLES31.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_INF);
|
| 141 |
|
|
GLES31.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_TEX);
|
| 142 |
da681e7e
|
Leszek Koltunski
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
|
| 143 |
|
|
}
|
| 144 |
|
|
|
| 145 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 146 |
|
|
/**
|
| 147 |
|
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
| 148 |
|
|
*
|
| 149 |
|
|
* @y.exclude
|
| 150 |
|
|
*/
|
| 151 |
|
|
public void bindTransformAttribs(DistortedProgram program)
|
| 152 |
|
|
{
|
| 153 |
|
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mTFO.mIndex[0] );
|
| 154 |
227b9bca
|
Leszek Koltunski
|
GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
|
| 155 |
da681e7e
|
Leszek Koltunski
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
|
| 156 |
6c00149d
|
Leszek Koltunski
|
}
|
| 157 |
|
|
|
| 158 |
7a5e538a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 159 |
|
|
/**
|
| 160 |
|
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
| 161 |
|
|
*
|
| 162 |
|
|
* @y.exclude
|
| 163 |
|
|
*/
|
| 164 |
|
|
public void setInflate(float inflate)
|
| 165 |
|
|
{
|
| 166 |
|
|
mInflate = inflate;
|
| 167 |
|
|
}
|
| 168 |
|
|
|
| 169 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 170 |
|
|
/**
|
| 171 |
|
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
| 172 |
|
|
*
|
| 173 |
|
|
* @y.exclude
|
| 174 |
|
|
*/
|
| 175 |
|
|
public float getInflate()
|
| 176 |
|
|
{
|
| 177 |
|
|
return mInflate;
|
| 178 |
|
|
}
|
| 179 |
|
|
|
| 180 |
466450b5
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 181 |
|
|
// PUBLIC API
|
| 182 |
3fc9327a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 183 |
|
|
/**
|
| 184 |
|
|
* When rendering this Mesh, do we want to render the Normal vectors as well?
|
| 185 |
420836fc
|
leszek
|
* <p>
|
| 186 |
|
|
* Will work only on OpenGL ES >= 3.0 devices.
|
| 187 |
3fc9327a
|
Leszek Koltunski
|
*
|
| 188 |
|
|
* @param show Controls if we render the Normal vectors or not.
|
| 189 |
|
|
*/
|
| 190 |
|
|
public void setShowNormals(boolean show)
|
| 191 |
|
|
{
|
| 192 |
420836fc
|
leszek
|
mShowNormals = (Distorted.GLSL >= 300 && show);
|
| 193 |
3fc9327a
|
Leszek Koltunski
|
}
|
| 194 |
466450b5
|
Leszek Koltunski
|
|
| 195 |
6c00149d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 196 |
|
|
/**
|
| 197 |
|
|
* When rendering this mesh, should we also draw the normal vectors?
|
| 198 |
|
|
*
|
| 199 |
|
|
* @return <i>true</i> if we do render normal vectors
|
| 200 |
|
|
*/
|
| 201 |
|
|
public boolean getShowNormals()
|
| 202 |
|
|
{
|
| 203 |
|
|
return mShowNormals;
|
| 204 |
|
|
}
|
| 205 |
|
|
|
| 206 |
466450b5
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 207 |
|
|
/**
|
| 208 |
|
|
* Release all internal resources.
|
| 209 |
|
|
*/
|
| 210 |
|
|
public void markForDeletion()
|
| 211 |
|
|
{
|
| 212 |
|
|
mVertAttribs.clear();
|
| 213 |
|
|
|
| 214 |
|
|
mVBO.markForDeletion();
|
| 215 |
|
|
mTFO.markForDeletion();
|
| 216 |
|
|
}
|
| 217 |
6a06a912
|
Leszek Koltunski
|
}
|
| 218 |
226144d0
|
leszek
|
|
| 219 |
3fc9327a
|
Leszek Koltunski
|
|