Project

General

Profile

« Previous | Next » 

Revision a51fe521

Added by Leszek Koltunski about 7 years ago

Move from unpacked to packad server-side Vertex attribute buffer.

View differences:

src/main/java/org/distorted/library/MeshObject.java
28 28
 * Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions
29 29
 * 2) normals 3) texture coordinates.
30 30
 * <p>
31
 * If you want to render to a particular shape, extend from here, construct the three FloatBuffers and
32
 * provide correct dataLength, i.e. the number of vertices.
31
 * If you want to render to a particular shape, extend from here, construct the attrib FloatBuffer
32
 * and provide correct numVertices.
33 33
 */
34 34
public abstract class MeshObject extends DistortedObject
35 35
   {
36
   static final int BYTES_PER_FLOAT   = 4; //
37
   static final int POSITION_DATA_SIZE= 3; // Size of the position data in elements
38
   static final int NORMAL_DATA_SIZE  = 3; // Size of the normal data in elements.
39
   static final int TEX_DATA_SIZE     = 2; // Size of the texture coordinate data in elements.
36
   private static final int BYTES_PER_FLOAT = 4;
40 37

  
41
   int dataLength;
42
   FloatBuffer mMeshPositions, mMeshNormals, mMeshTexture;
43
   int[] mPosVBO = new int[1];
44
   int[] mNorVBO = new int[1];
45
   int[] mTexVBO = new int[1];
38
   static final int POSITION_DATA_SIZE= 3;
39
   static final int NORMAL_DATA_SIZE  = 3;
40
   static final int TEX_DATA_SIZE     = 2;
46 41

  
47
   final float zFactor; // strange workaround for the fact that we need to somehow store the 'depth'
48
                        // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
42
   static final int OFFSET0 =                                                                   0;
43
   static final int OFFSET1 = (POSITION_DATA_SIZE                               )*BYTES_PER_FLOAT;
44
   static final int OFFSET2 = (POSITION_DATA_SIZE+NORMAL_DATA_SIZE              )*BYTES_PER_FLOAT;
45
   static final int VERTSIZE= (POSITION_DATA_SIZE+NORMAL_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
46

  
47
   int numVertices;
48
   FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX, NorY,NorZ, TexS, TexT
49
   int[] mAttVBO = new int[1]; // server-side packed vertex attributes
50

  
51
   final float zFactor;        // strange workaround for the fact that we need to somehow store the 'depth'
52
                               // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
49 53

  
50 54
///////////////////////////////////////////////////////////////////////////////////////////////////
51 55

  
......
60 64
///////////////////////////////////////////////////////////////////////////////////////////////////
61 65
// must be called from a thread holding OpenGL Context
62 66
//
63
// Do NOT release mMeshPositions etc as we will need them when we need to re-create the buffers after
67
// Do NOT release mVertAttribs etc as we will need them when we need to re-create the buffers after
64 68
// a loss of OpenGL context!
65 69

  
66 70
   void create()
67 71
     {
68
     if( mPosVBO[0]<0 )
69
       {
70
       GLES30.glGenBuffers(1, mPosVBO, 0);
71
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mPosVBO[0]);
72
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength*POSITION_DATA_SIZE*BYTES_PER_FLOAT, mMeshPositions, GLES30.GL_STATIC_READ);
73
       }
74
     if( mNorVBO[0]<0 )
75
       {
76
       GLES30.glGenBuffers(1, mNorVBO, 0);
77
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mNorVBO[0]);
78
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength*  NORMAL_DATA_SIZE*BYTES_PER_FLOAT, mMeshNormals  , GLES30.GL_STATIC_READ);
79
       }
80
     if( mTexVBO[0]<0 )
72
     if( mAttVBO[0]<0 )
81 73
       {
82
       GLES30.glGenBuffers(1, mTexVBO, 0);
83
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mTexVBO[0]);
84
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength*    TEX_DATA_SIZE*BYTES_PER_FLOAT, mMeshTexture  , GLES30.GL_STATIC_READ);
74
       GLES30.glGenBuffers(1, mAttVBO, 0);
75
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mAttVBO[0]);
76
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, numVertices*VERTSIZE, mVertAttribs, GLES30.GL_STATIC_READ);
77
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
85 78
       }
86

  
87
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
88 79
     }
89 80

  
90 81
///////////////////////////////////////////////////////////////////////////////////////////////////
......
92 83

  
93 84
   void delete()
94 85
     {
95
     if( mPosVBO[0]>=0 )
96
       {
97
       GLES30.glDeleteBuffers(1, mPosVBO, 0);
98
       mPosVBO[0] = -1;
99
       }
100
     if( mNorVBO[0]>=0 )
101
       {
102
       GLES30.glDeleteBuffers(1, mNorVBO, 0);
103
       mNorVBO[0] = -1;
104
       }
105
     if( mTexVBO[0]>=0 )
86
     if( mAttVBO[0]>=0 )
106 87
       {
107
       GLES30.glDeleteBuffers(1, mTexVBO, 0);
108
       mTexVBO[0] = -1;
88
       GLES30.glDeleteBuffers(1, mAttVBO, 0);
89
       mAttVBO[0] = -1;
109 90
       }
110 91
     }
111 92

  
......
113 94

  
114 95
   void recreate()
115 96
     {
116
     mPosVBO[0] = -1;
117
     mNorVBO[0] = -1;
118
     mTexVBO[0] = -1;
97
     mAttVBO[0] = -1;
119 98
     }
120 99

  
121 100
///////////////////////////////////////////////////////////////////////////////////////////////////
......
123 102

  
124 103
   String printDetails()
125 104
     {
126
     return getClass().getSimpleName()+" vertices:"+dataLength;
105
     return getClass().getSimpleName()+" vertices:"+ numVertices;
127 106
     }
128 107

  
129 108
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff