Project

General

Profile

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

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

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 da681e7e Leszek Koltunski
   static final int POS_DATA_SIZE= 3;
43
   static final int NOR_DATA_SIZE= 3;
44
   static final int TEX_DATA_SIZE= 2;
45 6a06a912 Leszek Koltunski
46 da681e7e Leszek Koltunski
   private static final int BYTES_PER_FLOAT = 4;
47 3ef3364d Leszek Koltunski
48 da681e7e Leszek Koltunski
   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 604b2899 Leszek Koltunski
52 da681e7e Leszek Koltunski
   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 a51fe521 Leszek Koltunski
55 6c00149d Leszek Koltunski
   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 da681e7e Leszek Koltunski
   private int mNumVertices;
60
   private FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, TexS,TexT
61 3ef3364d Leszek Koltunski
62 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63 3ef3364d Leszek Koltunski
64 715e7726 Leszek Koltunski
   MeshBase(float factor)
65 3ef3364d Leszek Koltunski
     {
66 466450b5 Leszek Koltunski
     zFactor      = factor;
67 3fc9327a Leszek Koltunski
     mShowNormals = false;
68 42571056 Leszek Koltunski
69 466450b5 Leszek Koltunski
     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 42571056 Leszek Koltunski
     }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74 6c00149d Leszek Koltunski
// when a derived class is done computing its mesh, it has to call this method.
75 42571056 Leszek Koltunski
76 da681e7e Leszek Koltunski
   void setData(int numVertices, float[] vertexAttribs)
77 42571056 Leszek Koltunski
     {
78 da681e7e Leszek Koltunski
     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 42571056 Leszek Koltunski
     }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88 6c00149d Leszek Koltunski
/**
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 da681e7e Leszek Koltunski
   public int getTFO()
94 42571056 Leszek Koltunski
     {
95 da681e7e Leszek Koltunski
     return mTFO.mIndex[0];
96 42571056 Leszek Koltunski
     }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99 6c00149d Leszek Koltunski
/**
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 da681e7e Leszek Koltunski
   public int getNumVertices()
105 226144d0 leszek
     {
106 da681e7e Leszek Koltunski
     return mNumVertices;
107 226144d0 leszek
     }
108 42571056 Leszek Koltunski
109 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 da681e7e Leszek Koltunski
   public float getZFactor()
116 6c00149d Leszek Koltunski
     {
117 da681e7e Leszek Koltunski
     return zFactor;
118 6c00149d Leszek Koltunski
     }
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 da681e7e Leszek Koltunski
   public void bindVertexAttribs(DistortedProgram program)
127 6c00149d Leszek Koltunski
     {
128 da681e7e Leszek Koltunski
     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 6c00149d Leszek Koltunski
     }
147
148 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// PUBLIC API
150 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
151
/**
152
 * When rendering this Mesh, do we want to render the Normal vectors as well?
153 420836fc leszek
 * <p>
154
 * Will work only on OpenGL ES >= 3.0 devices.
155 3fc9327a Leszek Koltunski
 *
156
 * @param show Controls if we render the Normal vectors or not.
157
 */
158
   public void setShowNormals(boolean show)
159
     {
160 420836fc leszek
     mShowNormals = (Distorted.GLSL >= 300 && show);
161 3fc9327a Leszek Koltunski
     }
162 466450b5 Leszek Koltunski
163 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
175
/**
176
 * Release all internal resources.
177
 */
178
   public void markForDeletion()
179
     {
180
     mVertAttribs.clear();
181
182
     mVBO.markForDeletion();
183
     mTFO.markForDeletion();
184
     }
185 6a06a912 Leszek Koltunski
   }
186 226144d0 leszek
187 3fc9327a Leszek Koltunski