Project

General

Profile

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

library / src / main / java / org / distorted / library / MeshObject.java @ cab7c165

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;
21

    
22
import android.opengl.GLES30;
23

    
24
import java.nio.FloatBuffer;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
/**
28
 * Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions
29
 * 2) normals 3) texture coordinates.
30
 * <p>
31
 * If you want to render to a particular shape, extend from here, construct the attrib FloatBuffer
32
 * and provide correct numVertices.
33
 */
34
public abstract class MeshObject extends DistortedObject
35
   {
36
   private static final int BYTES_PER_FLOAT = 4;
37

    
38
   static final int POS_DATA_SIZE= 3;
39
   static final int NOR_DATA_SIZE= 3;
40
   static final int TEX_DATA_SIZE= 2;
41

    
42
   static final int OFFSET0 =                                                           0;
43
   static final int OFFSET1 = (POS_DATA_SIZE                            )*BYTES_PER_FLOAT;
44
   static final int OFFSET2 = (POS_DATA_SIZE+NOR_DATA_SIZE              )*BYTES_PER_FLOAT;
45
   static final int VERTSIZE= (POS_DATA_SIZE+NOR_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
   int[] mAttTFO = new int[1]; // transform feedback
51

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
   MeshObject(float factor)
58
     {
59
     super(DistortedObject.NOT_CREATED_YET,DistortedObject.TYPE_USER);
60

    
61
     zFactor = factor;
62
     recreate();
63
     }
64

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

    
71
   void create()
72
     {
73
     if( mAttVBO[0]<0 )
74
       {
75
       GLES30.glGenBuffers(1, mAttVBO, 0);
76
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mAttVBO[0]);
77
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, numVertices*VERTSIZE, mVertAttribs, GLES30.GL_STATIC_READ);
78
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
79
       }
80
     if( mAttTFO[0]<0 )
81
       {
82
       GLES30.glGenBuffers(1, mAttTFO, 0);
83
       GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mAttTFO[0]);
84
       GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, numVertices*VERTSIZE, null, GLES30.GL_STATIC_READ);
85
       GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0);
86
       }
87
     }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
// must be called from a thread holding OpenGL Context
91

    
92
   void delete()
93
     {
94
     if( mAttVBO[0]>=0 )
95
       {
96
       GLES30.glDeleteBuffers(1, mAttVBO, 0);
97
       mAttVBO[0] = -1;
98
       }
99
     if( mAttTFO[0]>=0 )
100
       {
101
       GLES30.glDeleteBuffers(1, mAttTFO, 0);
102
       mAttTFO[0] = -1;
103
       }
104
     }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
   void recreate()
109
     {
110
     mAttVBO[0] = -1;
111
     mAttTFO[0] = -1;
112
     }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
// debugging only
116

    
117
   String printDetails()
118
     {
119
     return getClass().getSimpleName()+" vertices:"+ numVertices;
120
     }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
/**
124
 * Get the minimal set of Vertices which have the same convex hull as the whole set.
125
 * <p>
126
 * In case of Flat Meshes, the set is obviously just the 4 corners. In case of the Cubes Mesh,
127
 * it is a subset of the set of each rightmost- and leftmost- corners in each row.
128
 * <p>
129
 * This is used to be able to quickly compute, in window coordinates, the Mesh'es bounding rectangle.
130
 */
131
   abstract float[] getBoundingVertices();
132
   }
133

    
(26-26/26)