Project

General

Profile

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

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

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 POSITION_DATA_SIZE= 3;
39
   static final int NORMAL_DATA_SIZE  = 3;
40
   static final int TEX_DATA_SIZE     = 2;
41

    
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().
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

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

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

    
70
   void create()
71
     {
72
     if( mAttVBO[0]<0 )
73
       {
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);
78
       }
79
     }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
// must be called from a thread holding OpenGL Context
83

    
84
   void delete()
85
     {
86
     if( mAttVBO[0]>=0 )
87
       {
88
       GLES30.glDeleteBuffers(1, mAttVBO, 0);
89
       mAttVBO[0] = -1;
90
       }
91
     }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
   void recreate()
96
     {
97
     mAttVBO[0] = -1;
98
     }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
// debugging only
102

    
103
   String printDetails()
104
     {
105
     return getClass().getSimpleName()+" vertices:"+ numVertices;
106
     }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
/**
110
 * Get the minimal set of Vertices which have the same convex hull as the whole set.
111
 * <p>
112
 * In case of Flat Meshes, the set is obviously just the 4 corners. In case of the Cubes Mesh,
113
 * it is a subset of the set of each rightmost- and leftmost- corners in each row.
114
 * <p>
115
 * This is used to be able to quickly compute, in window coordinates, the Mesh'es bounding rectangle.
116
 */
117
   abstract float[] getBoundingVertices();
118
   }
119

    
(26-26/26)