Project

General

Profile

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

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

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 6a06a912 Leszek Koltunski
package org.distorted.library;
21
22 42571056 Leszek Koltunski
import android.opengl.GLES30;
23
24 6a06a912 Leszek Koltunski
import java.nio.FloatBuffer;
25
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27 e0b6c593 Leszek Koltunski
/**
28 26df012c Leszek Koltunski
 * Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions
29 e0b6c593 Leszek Koltunski
 * 2) normals 3) texture coordinates.
30
 * <p>
31 a51fe521 Leszek Koltunski
 * If you want to render to a particular shape, extend from here, construct the attrib FloatBuffer
32
 * and provide correct numVertices.
33 e0b6c593 Leszek Koltunski
 */
34 226144d0 leszek
public abstract class MeshObject extends DistortedObject
35 6a06a912 Leszek Koltunski
   {
36 a51fe521 Leszek Koltunski
   private static final int BYTES_PER_FLOAT = 4;
37 6a06a912 Leszek Koltunski
38 cab7c165 Leszek Koltunski
   static final int POS_DATA_SIZE= 3;
39
   static final int NOR_DATA_SIZE= 3;
40
   static final int TEX_DATA_SIZE= 2;
41 3ef3364d Leszek Koltunski
42 cab7c165 Leszek Koltunski
   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 a51fe521 Leszek Koltunski
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 cab7c165 Leszek Koltunski
   int[] mAttTFO = new int[1]; // transform feedback
51 a51fe521 Leszek Koltunski
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 3ef3364d Leszek Koltunski
55 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
56 3ef3364d Leszek Koltunski
57 26df012c Leszek Koltunski
   MeshObject(float factor)
58 3ef3364d Leszek Koltunski
     {
59 226144d0 leszek
     super(DistortedObject.NOT_CREATED_YET,DistortedObject.TYPE_USER);
60 42571056 Leszek Koltunski
61 226144d0 leszek
     zFactor = factor;
62 42571056 Leszek Koltunski
     recreate();
63
     }
64
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
// must be called from a thread holding OpenGL Context
67
//
68 a51fe521 Leszek Koltunski
// Do NOT release mVertAttribs etc as we will need them when we need to re-create the buffers after
69 42571056 Leszek Koltunski
// a loss of OpenGL context!
70
71 226144d0 leszek
   void create()
72 42571056 Leszek Koltunski
     {
73 a51fe521 Leszek Koltunski
     if( mAttVBO[0]<0 )
74 42571056 Leszek Koltunski
       {
75 a51fe521 Leszek Koltunski
       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 42571056 Leszek Koltunski
       }
80 cab7c165 Leszek Koltunski
     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 42571056 Leszek Koltunski
     }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
// must be called from a thread holding OpenGL Context
91
92 226144d0 leszek
   void delete()
93 42571056 Leszek Koltunski
     {
94 a51fe521 Leszek Koltunski
     if( mAttVBO[0]>=0 )
95 42571056 Leszek Koltunski
       {
96 a51fe521 Leszek Koltunski
       GLES30.glDeleteBuffers(1, mAttVBO, 0);
97
       mAttVBO[0] = -1;
98 42571056 Leszek Koltunski
       }
99 cab7c165 Leszek Koltunski
     if( mAttTFO[0]>=0 )
100
       {
101
       GLES30.glDeleteBuffers(1, mAttTFO, 0);
102
       mAttTFO[0] = -1;
103
       }
104 42571056 Leszek Koltunski
     }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108 226144d0 leszek
   void recreate()
109 42571056 Leszek Koltunski
     {
110 a51fe521 Leszek Koltunski
     mAttVBO[0] = -1;
111 cab7c165 Leszek Koltunski
     mAttTFO[0] = -1;
112 42571056 Leszek Koltunski
     }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115 226144d0 leszek
// debugging only
116 42571056 Leszek Koltunski
117 226144d0 leszek
   String printDetails()
118
     {
119 a51fe521 Leszek Koltunski
     return getClass().getSimpleName()+" vertices:"+ numVertices;
120 226144d0 leszek
     }
121 42571056 Leszek Koltunski
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123 69ed1eb4 Leszek Koltunski
/**
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 6a06a912 Leszek Koltunski
   }