Project

General

Profile

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

library / src / main / java / org / distorted / library / main / MeshObject.java @ 466450b5

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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
22 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
23 6a06a912 Leszek Koltunski
import java.nio.FloatBuffer;
24
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26 e0b6c593 Leszek Koltunski
/**
27 26df012c Leszek Koltunski
 * Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions
28 e0b6c593 Leszek Koltunski
 * 2) normals 3) texture coordinates.
29
 * <p>
30 a51fe521 Leszek Koltunski
 * If you want to render to a particular shape, extend from here, construct the attrib FloatBuffer
31
 * and provide correct numVertices.
32 e0b6c593 Leszek Koltunski
 */
33 466450b5 Leszek Koltunski
public abstract class MeshObject
34 6a06a912 Leszek Koltunski
   {
35 a51fe521 Leszek Koltunski
   private static final int BYTES_PER_FLOAT = 4;
36 6a06a912 Leszek Koltunski
37 cab7c165 Leszek Koltunski
   static final int POS_DATA_SIZE= 3;
38
   static final int NOR_DATA_SIZE= 3;
39
   static final int TEX_DATA_SIZE= 2;
40 3ef3364d Leszek Koltunski
41 cab7c165 Leszek Koltunski
   static final int OFFSET0 =                                                           0;
42
   static final int OFFSET1 = (POS_DATA_SIZE                            )*BYTES_PER_FLOAT;
43
   static final int OFFSET2 = (POS_DATA_SIZE+NOR_DATA_SIZE              )*BYTES_PER_FLOAT;
44 604b2899 Leszek Koltunski
45 3fc9327a Leszek Koltunski
   static final int TFSIZE  = (POS_DATA_SIZE+POS_DATA_SIZE              )*BYTES_PER_FLOAT;
46 cab7c165 Leszek Koltunski
   static final int VERTSIZE= (POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
47 a51fe521 Leszek Koltunski
48 3fc9327a Leszek Koltunski
   boolean mShowNormals;
49
50 a51fe521 Leszek Koltunski
   int numVertices;
51 466450b5 Leszek Koltunski
   FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, TexS,TexT
52
53
   DistortedBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
54 a51fe521 Leszek Koltunski
55
   final float zFactor;        // strange workaround for the fact that we need to somehow store the 'depth'
56
                               // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
57 3ef3364d Leszek Koltunski
58 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59 3ef3364d Leszek Koltunski
60 26df012c Leszek Koltunski
   MeshObject(float factor)
61 3ef3364d Leszek Koltunski
     {
62 466450b5 Leszek Koltunski
     zFactor      = factor;
63 3fc9327a Leszek Koltunski
     mShowNormals = false;
64 42571056 Leszek Koltunski
65 466450b5 Leszek Koltunski
     mVBO = new DistortedBuffer(GLES31.GL_ARRAY_BUFFER             , GLES31.GL_STATIC_READ);
66
     mTFO = new DistortedBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
67 42571056 Leszek Koltunski
     }
68
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 466450b5 Leszek Koltunski
   void setData(int size, FloatBuffer buffer)
72 42571056 Leszek Koltunski
     {
73 466450b5 Leszek Koltunski
     mVBO.setData(size*VERTSIZE, buffer);
74
     mTFO.setData(size*TFSIZE  , null  );
75 42571056 Leszek Koltunski
     }
76
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79 466450b5 Leszek Koltunski
   int getVBO()
80 42571056 Leszek Koltunski
     {
81 466450b5 Leszek Koltunski
     return mVBO.mIndex[0];
82 42571056 Leszek Koltunski
     }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
86 466450b5 Leszek Koltunski
   int getTFO()
87 226144d0 leszek
     {
88 466450b5 Leszek Koltunski
     return mTFO.mIndex[0];
89 226144d0 leszek
     }
90 42571056 Leszek Koltunski
91 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
92
// PUBLIC API
93 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
94
/**
95
 * When rendering this Mesh, do we want to render the Normal vectors as well?
96 420836fc leszek
 * <p>
97
 * Will work only on OpenGL ES >= 3.0 devices.
98 3fc9327a Leszek Koltunski
 *
99
 * @param show Controls if we render the Normal vectors or not.
100
 */
101
   public void setShowNormals(boolean show)
102
     {
103 420836fc leszek
     mShowNormals = (Distorted.GLSL >= 300 && show);
104 3fc9327a Leszek Koltunski
     }
105 466450b5 Leszek Koltunski
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
/**
108
 * Release all internal resources.
109
 */
110
   public void markForDeletion()
111
     {
112
     mVertAttribs.clear();
113
114
     mVBO.markForDeletion();
115
     mTFO.markForDeletion();
116
     }
117 6a06a912 Leszek Koltunski
   }
118 226144d0 leszek
119 3fc9327a Leszek Koltunski