Project

General

Profile

« Previous | Next » 

Revision 466450b5

Added by Leszek Koltunski over 5 years ago

Beginnings of a separate package only with Meshes.

View differences:

src/main/java/org/distorted/library/main/MeshObject.java
20 20
package org.distorted.library.main;
21 21

  
22 22
import android.opengl.GLES31;
23

  
24 23
import java.nio.FloatBuffer;
25 24

  
26 25
///////////////////////////////////////////////////////////////////////////////////////////////////
......
31 30
 * If you want to render to a particular shape, extend from here, construct the attrib FloatBuffer
32 31
 * and provide correct numVertices.
33 32
 */
34
public abstract class MeshObject extends DistortedObject
33
public abstract class MeshObject
35 34
   {
36 35
   private static final int BYTES_PER_FLOAT = 4;
37 36

  
......
49 48
   boolean mShowNormals;
50 49

  
51 50
   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
   int[] mAttTFO = new int[1]; // transform feedback
51
   FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, TexS,TexT
52

  
53
   DistortedBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
55 54

  
56 55
   final float zFactor;        // strange workaround for the fact that we need to somehow store the 'depth'
57 56
                               // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
......
60 59

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

  
65
     zFactor = factor;
62
     zFactor      = factor;
66 63
     mShowNormals = false;
67
     recreate();
68
     }
69 64

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

  
76
   void create()
77
     {
78
     if( mAttVBO[0]<0 )
79
       {
80
       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
       }
85
     if( mAttTFO[0]<0 && Distorted.GLSL >= 300 )
86
       {
87
       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
       }
65
     mVBO = new DistortedBuffer(GLES31.GL_ARRAY_BUFFER             , GLES31.GL_STATIC_READ);
66
     mTFO = new DistortedBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
92 67
     }
93 68

  
94 69
///////////////////////////////////////////////////////////////////////////////////////////////////
95
// must be called from a thread holding OpenGL Context
96 70

  
97
   void delete()
71
   void setData(int size, FloatBuffer buffer)
98 72
     {
99
     if( mAttVBO[0]>=0 )
100
       {
101
       GLES31.glDeleteBuffers(1, mAttVBO, 0);
102
       mAttVBO[0] = -1;
103
       }
104
     if( mAttTFO[0]>=0 )
105
       {
106
       GLES31.glDeleteBuffers(1, mAttTFO, 0);
107
       mAttTFO[0] = -1;
108
       }
73
     mVBO.setData(size*VERTSIZE, buffer);
74
     mTFO.setData(size*TFSIZE  , null  );
109 75
     }
110 76

  
111 77
///////////////////////////////////////////////////////////////////////////////////////////////////
112 78

  
113
   void recreate()
79
   int getVBO()
114 80
     {
115
     mAttVBO[0] = -1;
116
     mAttTFO[0] = -1;
81
     return mVBO.mIndex[0];
117 82
     }
118 83

  
119 84
///////////////////////////////////////////////////////////////////////////////////////////////////
120
// debugging only
121 85

  
122
   String printDetails()
86
   int getTFO()
123 87
     {
124
     return getClass().getSimpleName()+" vertices:"+ numVertices;
88
     return mTFO.mIndex[0];
125 89
     }
126 90

  
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
// PUBLIC API
127 93
///////////////////////////////////////////////////////////////////////////////////////////////////
128 94
/**
129 95
 * When rendering this Mesh, do we want to render the Normal vectors as well?
......
136 102
     {
137 103
     mShowNormals = (Distorted.GLSL >= 300 && show);
138 104
     }
105

  
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
/**
108
 * Release all internal resources.
109
 */
110
   public void markForDeletion()
111
     {
112
     mVertAttribs.clear();
113

  
114
     mVBO.markForDeletion();
115
     mTFO.markForDeletion();
116
     }
139 117
   }
140 118

  
141 119

  

Also available in: Unified diff