Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ da681e7e

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, ie 3 arrays of Vertex attributes: 1) positions
35
 * 2) normals 3) texture coordinates.
36
 * <p>
37
 * 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
 */
40
public abstract class MeshBase
41
   {
42
   static final int POS_DATA_SIZE= 3;
43
   static final int NOR_DATA_SIZE= 3;
44
   static final int TEX_DATA_SIZE= 2;
45

    
46
   private static final int BYTES_PER_FLOAT = 4;
47

    
48
   private static final int OFFSET_POS =                                                        0;
49
   private static final int OFFSET_NOR = (POS_DATA_SIZE                         )*BYTES_PER_FLOAT;
50
   private static final int OFFSET_TEX = (POS_DATA_SIZE+NOR_DATA_SIZE           )*BYTES_PER_FLOAT;
51

    
52
   private static final int TFSIZE  = (POS_DATA_SIZE+POS_DATA_SIZE              )*BYTES_PER_FLOAT;
53
   private static final int VERTSIZE= (POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
54

    
55
   private boolean mShowNormals;       // when rendering this mesh, draw normal vectors?
56
   private DistortedBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
57
   private final float zFactor;        // strange workaround for the fact that we need to somehow store the 'depth'
58
                                       // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
59
   private int mNumVertices;
60
   private FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, TexS,TexT
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
   MeshBase(float factor)
65
     {
66
     zFactor      = factor;
67
     mShowNormals = false;
68

    
69
     mVBO = new DistortedBuffer(GLES31.GL_ARRAY_BUFFER             , GLES31.GL_STATIC_READ);
70
     mTFO = new DistortedBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
71
     }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
// when a derived class is done computing its mesh, it has to call this method.
75

    
76
   void setData(int numVertices, float[] vertexAttribs)
77
     {
78
     mNumVertices = numVertices;
79

    
80
     mVertAttribs = ByteBuffer.allocateDirect(mNumVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
81
     mVertAttribs.put(vertexAttribs).position(0);
82

    
83
     mVBO.setData(numVertices*VERTSIZE, mVertAttribs);
84
     mTFO.setData(numVertices*TFSIZE  , null        );
85
     }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
/**
89
 * Not part of public API, do not document (public only because has to be used from the main package)
90
 *
91
 * @y.exclude
92
 */
93
   public int getTFO()
94
     {
95
     return mTFO.mIndex[0];
96
     }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
/**
100
 * Not part of public API, do not document (public only because has to be used from the main package)
101
 *
102
 * @y.exclude
103
 */
104
   public int getNumVertices()
105
     {
106
     return mNumVertices;
107
     }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
/**
111
 * Not part of public API, do not document (public only because has to be used from the main package)
112
 *
113
 * @y.exclude
114
 */
115
   public float getZFactor()
116
     {
117
     return zFactor;
118
     }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
/**
122
 * Not part of public API, do not document (public only because has to be used from the main package)
123
 *
124
 * @y.exclude
125
 */
126
   public void bindVertexAttribs(DistortedProgram program)
127
     {
128
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mVBO.mIndex[0] );
129
     GLES31.glVertexAttribPointer(program.mAttribute[0], MeshBase.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET_POS);
130
     GLES31.glVertexAttribPointer(program.mAttribute[1], MeshBase.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET_NOR);
131
     GLES31.glVertexAttribPointer(program.mAttribute[2], MeshBase.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET_TEX);
132
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
133
     }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
/**
137
 * Not part of public API, do not document (public only because has to be used from the main package)
138
 *
139
 * @y.exclude
140
 */
141
   public void bindTransformAttribs(DistortedProgram program)
142
     {
143
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mTFO.mIndex[0] );
144
     GLES31.glVertexAttribPointer(program.mAttribute[0], MeshBase.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
145
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
146
     }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// PUBLIC API
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
/**
152
 * When rendering this Mesh, do we want to render the Normal vectors as well?
153
 * <p>
154
 * Will work only on OpenGL ES >= 3.0 devices.
155
 *
156
 * @param show Controls if we render the Normal vectors or not.
157
 */
158
   public void setShowNormals(boolean show)
159
     {
160
     mShowNormals = (Distorted.GLSL >= 300 && show);
161
     }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
/**
165
 * When rendering this mesh, should we also draw the normal vectors?
166
 *
167
 * @return <i>true</i> if we do render normal vectors
168
 */
169
   public boolean getShowNormals()
170
     {
171
     return mShowNormals;
172
     }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
/**
176
 * Release all internal resources.
177
 */
178
   public void markForDeletion()
179
     {
180
     mVertAttribs.clear();
181

    
182
     mVBO.markForDeletion();
183
     mTFO.markForDeletion();
184
     }
185
   }
186

    
187

    
188

    
(1-1/3)