Project

General

Profile

Download (8.44 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 6f2d931d

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 26df012c Leszek Koltunski
 * Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions
35 e0b6c593 Leszek Koltunski
 * 2) normals 3) texture coordinates.
36
 * <p>
37 da681e7e Leszek Koltunski
 * If you want to render to a particular shape, extend from here, construct a float array
38
 * containing per-vertex attributes, and call back setData().
39 e0b6c593 Leszek Koltunski
 */
40 715e7726 Leszek Koltunski
public abstract class MeshBase
41 6a06a912 Leszek Koltunski
   {
42 6f2d931d Leszek Koltunski
   private static final int POS_DATA_SIZE= 3;
43
   private static final int NOR_DATA_SIZE= 3;
44
   private static final int INF_DATA_SIZE= 3;
45
   private static final int TEX_DATA_SIZE= 2;
46
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
   static final int VERT_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE + TEX_DATA_SIZE;
52 6a06a912 Leszek Koltunski
53 da681e7e Leszek Koltunski
   private static final int BYTES_PER_FLOAT = 4;
54 3ef3364d Leszek Koltunski
55 6f2d931d Leszek Koltunski
   private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
56
   private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
57
   private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
58
   private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
59 604b2899 Leszek Koltunski
60 6f2d931d Leszek Koltunski
   private static final int TFSIZE  = (POS_DATA_SIZE+POS_DATA_SIZE                            )*BYTES_PER_FLOAT;
61
   private static final int VERTSIZE= (POS_DATA_SIZE+NOR_DATA_SIZE+INF_DATA_SIZE+TEX_DATA_SIZE)*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
   private FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, TexS,TexT
69 3ef3364d Leszek Koltunski
70 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
71 3ef3364d Leszek Koltunski
72 715e7726 Leszek Koltunski
   MeshBase(float factor)
73 3ef3364d Leszek Koltunski
     {
74 466450b5 Leszek Koltunski
     zFactor      = factor;
75 3fc9327a Leszek Koltunski
     mShowNormals = false;
76 42571056 Leszek Koltunski
77 466450b5 Leszek Koltunski
     mVBO = new DistortedBuffer(GLES31.GL_ARRAY_BUFFER             , GLES31.GL_STATIC_READ);
78
     mTFO = new DistortedBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
79 42571056 Leszek Koltunski
     }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82 6c00149d Leszek Koltunski
// when a derived class is done computing its mesh, it has to call this method.
83 42571056 Leszek Koltunski
84 da681e7e Leszek Koltunski
   void setData(int numVertices, float[] vertexAttribs)
85 42571056 Leszek Koltunski
     {
86 da681e7e Leszek Koltunski
     mNumVertices = numVertices;
87
88
     mVertAttribs = ByteBuffer.allocateDirect(mNumVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
89
     mVertAttribs.put(vertexAttribs).position(0);
90
91
     mVBO.setData(numVertices*VERTSIZE, mVertAttribs);
92
     mTFO.setData(numVertices*TFSIZE  , null        );
93 42571056 Leszek Koltunski
     }
94
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96 6c00149d Leszek Koltunski
/**
97
 * Not part of public API, do not document (public only because has to be used from the main package)
98
 *
99
 * @y.exclude
100
 */
101 da681e7e Leszek Koltunski
   public int getTFO()
102 42571056 Leszek Koltunski
     {
103 da681e7e Leszek Koltunski
     return mTFO.mIndex[0];
104 42571056 Leszek Koltunski
     }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107 6c00149d Leszek Koltunski
/**
108
 * Not part of public API, do not document (public only because has to be used from the main package)
109
 *
110
 * @y.exclude
111
 */
112 da681e7e Leszek Koltunski
   public int getNumVertices()
113 226144d0 leszek
     {
114 da681e7e Leszek Koltunski
     return mNumVertices;
115 226144d0 leszek
     }
116 42571056 Leszek Koltunski
117 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118
/**
119
 * Not part of public API, do not document (public only because has to be used from the main package)
120
 *
121
 * @y.exclude
122
 */
123 da681e7e Leszek Koltunski
   public float getZFactor()
124 6c00149d Leszek Koltunski
     {
125 da681e7e Leszek Koltunski
     return zFactor;
126 6c00149d Leszek Koltunski
     }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
/**
130
 * Not part of public API, do not document (public only because has to be used from the main package)
131
 *
132
 * @y.exclude
133
 */
134 da681e7e Leszek Koltunski
   public void bindVertexAttribs(DistortedProgram program)
135 6c00149d Leszek Koltunski
     {
136 da681e7e Leszek Koltunski
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mVBO.mIndex[0] );
137
     GLES31.glVertexAttribPointer(program.mAttribute[0], MeshBase.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET_POS);
138
     GLES31.glVertexAttribPointer(program.mAttribute[1], MeshBase.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET_NOR);
139 6f2d931d Leszek Koltunski
     GLES31.glVertexAttribPointer(program.mAttribute[2], MeshBase.INF_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET_INF);
140
     GLES31.glVertexAttribPointer(program.mAttribute[3], MeshBase.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET_TEX);
141 da681e7e Leszek Koltunski
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
142
     }
143
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
/**
146
 * Not part of public API, do not document (public only because has to be used from the main package)
147
 *
148
 * @y.exclude
149
 */
150
   public void bindTransformAttribs(DistortedProgram program)
151
     {
152
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mTFO.mIndex[0] );
153
     GLES31.glVertexAttribPointer(program.mAttribute[0], MeshBase.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
154
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
155 6c00149d Leszek Koltunski
     }
156
157 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
158
// PUBLIC API
159 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
160
/**
161
 * When rendering this Mesh, do we want to render the Normal vectors as well?
162 420836fc leszek
 * <p>
163
 * Will work only on OpenGL ES >= 3.0 devices.
164 3fc9327a Leszek Koltunski
 *
165
 * @param show Controls if we render the Normal vectors or not.
166
 */
167
   public void setShowNormals(boolean show)
168
     {
169 420836fc leszek
     mShowNormals = (Distorted.GLSL >= 300 && show);
170 3fc9327a Leszek Koltunski
     }
171 466450b5 Leszek Koltunski
172 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
173
/**
174
 * When rendering this mesh, should we also draw the normal vectors?
175
 *
176
 * @return <i>true</i> if we do render normal vectors
177
 */
178
   public boolean getShowNormals()
179
     {
180
     return mShowNormals;
181
     }
182
183 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
184
/**
185
 * Release all internal resources.
186
 */
187
   public void markForDeletion()
188
     {
189
     mVertAttribs.clear();
190
191
     mVBO.markForDeletion();
192
     mTFO.markForDeletion();
193
     }
194 6a06a912 Leszek Koltunski
   }
195 226144d0 leszek
196 3fc9327a Leszek Koltunski