Project

General

Profile

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

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

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
27 6a06a912 Leszek Koltunski
import java.nio.FloatBuffer;
28
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30 e0b6c593 Leszek Koltunski
/**
31 26df012c Leszek Koltunski
 * Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions
32 e0b6c593 Leszek Koltunski
 * 2) normals 3) texture coordinates.
33
 * <p>
34 a51fe521 Leszek Koltunski
 * If you want to render to a particular shape, extend from here, construct the attrib FloatBuffer
35
 * and provide correct numVertices.
36 e0b6c593 Leszek Koltunski
 */
37 715e7726 Leszek Koltunski
public abstract class MeshBase
38 6a06a912 Leszek Koltunski
   {
39 a51fe521 Leszek Koltunski
   private static final int BYTES_PER_FLOAT = 4;
40 6a06a912 Leszek Koltunski
41 6c00149d Leszek Koltunski
   public static final int POS_DATA_SIZE= 3;
42
   public static final int NOR_DATA_SIZE= 3;
43
   public static final int TEX_DATA_SIZE= 2;
44 3ef3364d Leszek Koltunski
45 6c00149d Leszek Koltunski
   public static final int OFFSET0 =                                                           0;
46
   public static final int OFFSET1 = (POS_DATA_SIZE                            )*BYTES_PER_FLOAT;
47
   public static final int OFFSET2 = (POS_DATA_SIZE+NOR_DATA_SIZE              )*BYTES_PER_FLOAT;
48 604b2899 Leszek Koltunski
49 6c00149d Leszek Koltunski
   public static final int TFSIZE  = (POS_DATA_SIZE+POS_DATA_SIZE              )*BYTES_PER_FLOAT;
50
   public static final int VERTSIZE= (POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
51 a51fe521 Leszek Koltunski
52 6c00149d Leszek Koltunski
   private boolean mShowNormals;       // when rendering this mesh, draw normal vectors?
53
   private DistortedBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
54
   private final float zFactor;        // strange workaround for the fact that we need to somehow store the 'depth'
55
                                       // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
56 3fc9327a Leszek Koltunski
57 6c00149d Leszek Koltunski
   ////////////////////////////////////////////////////////////////////////////////
58
   // derived classes need to compute and fill up those 2 variables with data
59
   // describing a particular shape. Having done so, call setData()
60 e92785ba Leszek Koltunski
   int mNumVertices;
61 466450b5 Leszek Koltunski
   FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, TexS,TexT
62 6c00149d Leszek Koltunski
   ////////////////////////////////////////////////////////////////////////////////
63 3ef3364d Leszek Koltunski
64 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65 3ef3364d Leszek Koltunski
66 715e7726 Leszek Koltunski
   MeshBase(float factor)
67 3ef3364d Leszek Koltunski
     {
68 466450b5 Leszek Koltunski
     zFactor      = factor;
69 3fc9327a Leszek Koltunski
     mShowNormals = false;
70 42571056 Leszek Koltunski
71 466450b5 Leszek Koltunski
     mVBO = new DistortedBuffer(GLES31.GL_ARRAY_BUFFER             , GLES31.GL_STATIC_READ);
72
     mTFO = new DistortedBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
73 42571056 Leszek Koltunski
     }
74
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76 6c00149d Leszek Koltunski
// when a derived class is done computing its mesh, it has to call this method.
77 42571056 Leszek Koltunski
78 e92785ba Leszek Koltunski
   void setData(int numVertices, FloatBuffer vertAttribs)
79 42571056 Leszek Koltunski
     {
80 e92785ba Leszek Koltunski
     mVBO.setData(numVertices*VERTSIZE, vertAttribs);
81
     mTFO.setData(numVertices*TFSIZE  , null       );
82 42571056 Leszek Koltunski
     }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85 6c00149d Leszek Koltunski
/**
86
 * Not part of public API, do not document (public only because has to be used from the main package)
87
 *
88
 * @y.exclude
89
 */
90
   public int getVBO()
91 42571056 Leszek Koltunski
     {
92 466450b5 Leszek Koltunski
     return mVBO.mIndex[0];
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
   public int getTFO()
102 226144d0 leszek
     {
103 466450b5 Leszek Koltunski
     return mTFO.mIndex[0];
104 226144d0 leszek
     }
105 42571056 Leszek Koltunski
106 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e92785ba Leszek Koltunski
     return mNumVertices;
115 6c00149d Leszek Koltunski
     }
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 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
129
// PUBLIC API
130 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
131
/**
132
 * When rendering this Mesh, do we want to render the Normal vectors as well?
133 420836fc leszek
 * <p>
134
 * Will work only on OpenGL ES >= 3.0 devices.
135 3fc9327a Leszek Koltunski
 *
136
 * @param show Controls if we render the Normal vectors or not.
137
 */
138
   public void setShowNormals(boolean show)
139
     {
140 420836fc leszek
     mShowNormals = (Distorted.GLSL >= 300 && show);
141 3fc9327a Leszek Koltunski
     }
142 466450b5 Leszek Koltunski
143 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
144
/**
145
 * When rendering this mesh, should we also draw the normal vectors?
146
 *
147
 * @return <i>true</i> if we do render normal vectors
148
 */
149
   public boolean getShowNormals()
150
     {
151
     return mShowNormals;
152
     }
153
154 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
155
/**
156
 * Release all internal resources.
157
 */
158
   public void markForDeletion()
159
     {
160
     mVertAttribs.clear();
161
162
     mVBO.markForDeletion();
163
     mTFO.markForDeletion();
164
     }
165 6a06a912 Leszek Koltunski
   }
166 226144d0 leszek
167 3fc9327a Leszek Koltunski