Project

General

Profile

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

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

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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
22 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
23 42571056 Leszek Koltunski
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 604b2899 Leszek Koltunski
46 3fc9327a Leszek Koltunski
   static final int TFSIZE  = (POS_DATA_SIZE+POS_DATA_SIZE              )*BYTES_PER_FLOAT;
47 cab7c165 Leszek Koltunski
   static final int VERTSIZE= (POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
48 a51fe521 Leszek Koltunski
49 3fc9327a Leszek Koltunski
   boolean mShowNormals;
50
51 a51fe521 Leszek Koltunski
   int numVertices;
52
   FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX, NorY,NorZ, TexS, TexT
53
   int[] mAttVBO = new int[1]; // server-side packed vertex attributes
54 cab7c165 Leszek Koltunski
   int[] mAttTFO = new int[1]; // transform feedback
55 a51fe521 Leszek Koltunski
56
   final float zFactor;        // strange workaround for the fact that we need to somehow store the 'depth'
57
                               // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
58 3ef3364d Leszek Koltunski
59 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60 3ef3364d Leszek Koltunski
61 26df012c Leszek Koltunski
   MeshObject(float factor)
62 3ef3364d Leszek Koltunski
     {
63 226144d0 leszek
     super(DistortedObject.NOT_CREATED_YET,DistortedObject.TYPE_USER);
64 42571056 Leszek Koltunski
65 226144d0 leszek
     zFactor = factor;
66 3fc9327a Leszek Koltunski
     mShowNormals = false;
67 42571056 Leszek Koltunski
     recreate();
68
     }
69
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
// must be called from a thread holding OpenGL Context
72
//
73 a51fe521 Leszek Koltunski
// Do NOT release mVertAttribs etc as we will need them when we need to re-create the buffers after
74 42571056 Leszek Koltunski
// a loss of OpenGL context!
75
76 226144d0 leszek
   void create()
77 42571056 Leszek Koltunski
     {
78 a51fe521 Leszek Koltunski
     if( mAttVBO[0]<0 )
79 42571056 Leszek Koltunski
       {
80 e6519ac8 Leszek Koltunski
       GLES31.glGenBuffers(1, mAttVBO, 0);
81
       GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mAttVBO[0]);
82
       GLES31.glBufferData(GLES31.GL_ARRAY_BUFFER, numVertices*VERTSIZE, mVertAttribs, GLES31.GL_STATIC_READ);
83
       GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
84 42571056 Leszek Koltunski
       }
85 420836fc leszek
     if( mAttTFO[0]<0 && Distorted.GLSL >= 300 )
86 cab7c165 Leszek Koltunski
       {
87 e6519ac8 Leszek Koltunski
       GLES31.glGenBuffers(1, mAttTFO, 0);
88
       GLES31.glBindBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, mAttTFO[0]);
89
       GLES31.glBufferData(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, numVertices*TFSIZE, null, GLES31.GL_STATIC_READ);
90
       GLES31.glBindBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0);
91 cab7c165 Leszek Koltunski
       }
92 42571056 Leszek Koltunski
     }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
// must be called from a thread holding OpenGL Context
96
97 226144d0 leszek
   void delete()
98 42571056 Leszek Koltunski
     {
99 a51fe521 Leszek Koltunski
     if( mAttVBO[0]>=0 )
100 42571056 Leszek Koltunski
       {
101 e6519ac8 Leszek Koltunski
       GLES31.glDeleteBuffers(1, mAttVBO, 0);
102 a51fe521 Leszek Koltunski
       mAttVBO[0] = -1;
103 42571056 Leszek Koltunski
       }
104 cab7c165 Leszek Koltunski
     if( mAttTFO[0]>=0 )
105
       {
106 e6519ac8 Leszek Koltunski
       GLES31.glDeleteBuffers(1, mAttTFO, 0);
107 cab7c165 Leszek Koltunski
       mAttTFO[0] = -1;
108
       }
109 42571056 Leszek Koltunski
     }
110
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
113 226144d0 leszek
   void recreate()
114 42571056 Leszek Koltunski
     {
115 a51fe521 Leszek Koltunski
     mAttVBO[0] = -1;
116 cab7c165 Leszek Koltunski
     mAttTFO[0] = -1;
117 42571056 Leszek Koltunski
     }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120 226144d0 leszek
// debugging only
121 42571056 Leszek Koltunski
122 226144d0 leszek
   String printDetails()
123
     {
124 a51fe521 Leszek Koltunski
     return getClass().getSimpleName()+" vertices:"+ numVertices;
125 226144d0 leszek
     }
126 42571056 Leszek Koltunski
127 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
128
/**
129
 * When rendering this Mesh, do we want to render the Normal vectors as well?
130 420836fc leszek
 * <p>
131
 * Will work only on OpenGL ES >= 3.0 devices.
132 3fc9327a Leszek Koltunski
 *
133
 * @param show Controls if we render the Normal vectors or not.
134
 */
135
   public void setShowNormals(boolean show)
136
     {
137 420836fc leszek
     mShowNormals = (Distorted.GLSL >= 300 && show);
138 3fc9327a Leszek Koltunski
     }
139 6a06a912 Leszek Koltunski
   }
140 226144d0 leszek
141 3fc9327a Leszek Koltunski