Project

General

Profile

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

library / src / main / java / org / distorted / library / MeshObject.java @ 604b2899

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
   static final int NDC_DATA_SIZE= 2;
42

    
43
   static final int OFFSET0 =                                                           0;
44
   static final int OFFSET1 = (POS_DATA_SIZE                            )*BYTES_PER_FLOAT;
45
   static final int OFFSET2 = (POS_DATA_SIZE+NOR_DATA_SIZE              )*BYTES_PER_FLOAT;
46

    
47
   static final int TFSIZE  = (POS_DATA_SIZE+NOR_DATA_SIZE+NDC_DATA_SIZE)*BYTES_PER_FLOAT;
48
   static final int VERTSIZE= (POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
49

    
50
   int numVertices;
51
   FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX, NorY,NorZ, TexS, TexT
52
   int[] mAttVBO = new int[1]; // server-side packed vertex attributes
53
   int[] mAttTFO = new int[1]; // transform feedback
54

    
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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
   MeshObject(float factor)
61
     {
62
     super(DistortedObject.NOT_CREATED_YET,DistortedObject.TYPE_USER);
63

    
64
     zFactor = factor;
65
     recreate();
66
     }
67

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

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
// must be called from a thread holding OpenGL Context
94

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

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
   void recreate()
112
     {
113
     mAttVBO[0] = -1;
114
     mAttTFO[0] = -1;
115
     }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
// debugging only
119

    
120
   String printDetails()
121
     {
122
     return getClass().getSimpleName()+" vertices:"+ numVertices;
123
     }
124

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

    
(26-26/26)