Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 227b9bca

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.library.mesh;
21

    
22
import android.opengl.GLES31;
23

    
24
import org.distorted.library.main.Distorted;
25
import org.distorted.library.main.DistortedBuffer;
26
import org.distorted.library.program.DistortedProgram;
27

    
28
import java.nio.ByteBuffer;
29
import java.nio.ByteOrder;
30
import java.nio.FloatBuffer;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
/**
34
 * Abstract class which represents a Mesh, i.e. an array of vertices (rendered as a TRIANGLE_STRIP)
35
 * <p>
36
 * If you want to render to a particular shape, extend from here, construct a float array
37
 * containing per-vertex attributes, and call back setData().
38
 */
39
public abstract class MeshBase
40
   {
41
   // 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
   private static final int TEX_DATA_SIZE= 2; // texture coordinates
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;  // 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

    
54
   private static final int BYTES_PER_FLOAT = 4;
55

    
56
   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
   private static final int TRAN_SIZE  = TRAN_ATTRIBS*BYTES_PER_FLOAT;
61
   private static final int VERT_SIZE  = VERT_ATTRIBS*BYTES_PER_FLOAT;
62

    
63
   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
   private int mNumVertices;
68
   private FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
   MeshBase(float factor)
73
     {
74
     zFactor      = factor;
75
     mShowNormals = false;
76

    
77
     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
     }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
// when a derived class is done computing its mesh, it has to call this method.
83

    
84
   void setAttribs(float[] vertexAttribs)
85
     {
86
     mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
87

    
88
     mVertAttribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
89
     mVertAttribs.put(vertexAttribs).position(0);
90

    
91
     mVBO.setData(mNumVertices*VERT_SIZE, mVertAttribs);
92
     mTFO.setData(mNumVertices*TRAN_SIZE, null        );
93
     }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
/**
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
   public int getTFO()
102
     {
103
     return mTFO.mIndex[0];
104
     }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
/**
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
   public int getNumVertices()
113
     {
114
     return mNumVertices;
115
     }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
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
   public float getZFactor()
124
     {
125
     return zFactor;
126
     }
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
   public void bindVertexAttribs(DistortedProgram program)
135
     {
136
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mVBO.mIndex[0] );
137
     GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_POS);
138
     GLES31.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR);
139
     GLES31.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_INF);
140
     GLES31.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_TEX);
141
     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], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
154
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
155
     }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
// PUBLIC API
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
/**
161
 * When rendering this Mesh, do we want to render the Normal vectors as well?
162
 * <p>
163
 * Will work only on OpenGL ES >= 3.0 devices.
164
 *
165
 * @param show Controls if we render the Normal vectors or not.
166
 */
167
   public void setShowNormals(boolean show)
168
     {
169
     mShowNormals = (Distorted.GLSL >= 300 && show);
170
     }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
184
/**
185
 * Release all internal resources.
186
 */
187
   public void markForDeletion()
188
     {
189
     mVertAttribs.clear();
190

    
191
     mVBO.markForDeletion();
192
     mTFO.markForDeletion();
193
     }
194
   }
195

    
196

    
197

    
(1-1/3)