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/DistortedBuffer.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 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.main;
21

  
22
import android.opengl.GLES31;
23
import java.nio.Buffer;
24

  
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
/**
27
 * Implements OpenGL buffer object such as GL_ARRAY_BUFFER or GL_TRANSFORM_FEEDBACK_BUFFER.
28
 * Main point: deal with Android lifecycle and recreate the buffer on loss of OpenGL context.
29
 * <p>
30
 * Not part of public API, do not document (public only because has to be used in Meshes)
31
 *
32
 * @y.exclude
33
 */
34
public class DistortedBuffer extends DistortedObject
35
  {
36
  int[] mIndex;
37

  
38
  private int mTarget, mSize, mUsage;
39
  private Buffer mBuffer;
40

  
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

  
43
  DistortedBuffer(int target, int usage)
44
    {
45
    super(DistortedObject.NOT_CREATED_YET,DistortedObject.TYPE_USER);
46

  
47
    mIndex  = new int[1];
48
    mTarget = target;
49
    mUsage  = usage;
50

  
51
    recreate();
52
    }
53

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

  
56
  void setData(int size, Buffer buffer)
57
    {
58
    mSize   = size;
59
    mBuffer = buffer;
60
    }
61

  
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
// must be called from a thread holding OpenGL Context
64

  
65
  void create()
66
    {
67
    if( mIndex[0]<0 )
68
      {
69
      GLES31.glGenBuffers( 1, mIndex, 0);
70
      GLES31.glBindBuffer( mTarget, mIndex[0]);
71
      GLES31.glBufferData( mTarget, mSize, mBuffer, mUsage);
72
      GLES31.glBindBuffer( mTarget, 0);
73
      }
74
    }
75

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
// must be called from a thread holding OpenGL Context
78

  
79
  void delete()
80
    {
81
    if( mIndex[0]>=0 )
82
      {
83
      GLES31.glDeleteBuffers(1, mIndex, 0);
84
      mIndex[0] = -1;
85
      }
86
    }
87

  
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

  
90
  void recreate()
91
    {
92
    mIndex[0] = -1;
93
    }
94

  
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// debugging only
97

  
98
  String printDetails()
99
    {
100
    return getClass().getSimpleName();
101
    }
102
  }
src/main/java/org/distorted/library/main/DistortedEffects.java
424 424

  
425 425
  private void displayNormals(MeshObject mesh)
426 426
    {
427
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
427
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.getTFO() );
428 428
    GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
429 429
    DistortedRenderState.switchOffDrawing();
430 430
    GLES31.glDrawArrays( GLES31.GL_POINTS, 0, mesh.numVertices);
......
434 434

  
435 435
    mNormalProgram.useProgram();
436 436
    GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
437
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
437
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getTFO() );
438 438
    GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
439 439
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
440 440
    GLES31.glLineWidth(8.0f);
......
467 467
    GLES31.glUniform2ui(mMainOITSizeH, surface.mWidth, surface.mHeight);
468 468
    GLES31.glUniform1ui(mMainOITNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
469 469

  
470
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
470
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getVBO() );
471 471
    GLES31.glVertexAttribPointer(mMainOITProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
472 472
    GLES31.glVertexAttribPointer(mMainOITProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
473 473
    GLES31.glVertexAttribPointer(mMainOITProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
......
505 505
    mMainProgram.useProgram();
506 506
    GLES31.glUniform1i(mMainTextureH, 0);
507 507

  
508
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
508
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getVBO() );
509 509
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
510 510
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
511 511
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
src/main/java/org/distorted/library/main/EffectQueuePostprocess.java
169 169

  
170 170
      mPreProgram.useProgram();
171 171

  
172
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
172
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getVBO() );
173 173
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
174 174
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
175 175
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
src/main/java/org/distorted/library/main/MeshCubes.java
713 713
     if( remainingVert!=0 )
714 714
       android.util.Log.e("MeshCubes", "remainingVert " +remainingVert );
715 715

  
716
/*
717
     float[] attribs_good =
718
     { -0.5f, +0.5f, -0.5f,  +0.0f, +1.0f, +0.0f,  +0.0f, +0.0f,
719
       -0.5f, +0.5f, -0.5f,  +0.0f, +1.0f, +0.0f,  +0.0f, +0.0f,
720
       -0.5f, +0.5f, +0.5f,  +0.0f, +1.0f, +0.0f,  +0.0f, +1.0f,
721
       +0.5f, +0.5f, -0.5f,  +0.0f, +1.0f, +0.0f,  +1.0f, +0.0f,
722
       +0.5f, +0.5f, +0.5f,  +0.0f, +1.0f, +0.0f,  +1.0f, +1.0f };
723

  
724
     float[] attribs_hmm =
725
         {   -0.5f, -0.1f, -0.5f,  +0.0f, -1.0f, +0.0f,  +0.0f, +1.0f,
726
             -0.5f, -0.1f, -0.5f,  +0.0f, -1.0f, +0.0f,  +0.0f, +1.0f,
727
             -0.5f, -0.1f, +0.5f,  +0.0f, -1.0f, +0.0f,  +0.0f, +0.5f,
728
             +0.5f, -0.1f, -0.5f,  +0.0f, -1.0f, +0.0f,  +1.0f, +1.0f,
729
             +0.5f, -0.1f, +0.5f,  +0.0f, -1.0f, +0.0f,  +1.0f, +0.5f };
730

  
731
     float[] attribs_bad =
732
     { -0.5f, +0.0f, -0.5f,  +0.0f, +1.0f, +0.0f,  +0.0f, +1.0f,
733
       -0.5f, +0.0f, -0.5f,  +0.0f, +1.0f, +0.0f,  +0.0f, +1.0f,
734
       -0.5f, +0.0f, +0.5f,  +0.0f, +1.0f, +0.0f,  +0.0f, +0.5f,
735
       +0.5f, +0.0f, -0.5f,  +0.0f, +1.0f, +0.0f,  +1.0f, +1.0f,
736
       +0.5f, +0.0f, +0.5f,  +0.0f, +1.0f, +0.0f,  +1.0f, +0.5f };
737

  
738
     numVertices=5;
739
*/
740 716
     mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
741 717
     mVertAttribs.put(attribs).position(0);
718

  
719
     setData(numVertices, mVertAttribs);
742 720
     }
743 721

  
744 722
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/main/MeshFlat.java
52 52
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
53 53
       }
54 54

  
55
     //android.util.Log.e("BITMAP","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
55
     //android.util.Log.e("MeshFlat","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
56 56

  
57 57
     remainingVert = numVertices;
58 58
     }
......
77 77

  
78 78
     return vertex+1;
79 79
     }
80

  
80 81
///////////////////////////////////////////////////////////////////////////////////////////////////
81 82

  
82 83
  private int repeatLast(int vertex, float[] attribs)
83 84
     {
84 85
     remainingVert--;
85 86

  
86
     //android.util.Log.e("BITMAP", "repeating last vertex!");
87
     //android.util.Log.e("MeshFlat", "repeating last vertex!");
87 88

  
88 89
     if( vertex>0 )
89 90
       {
......
114 115
     final float X = 1.0f/mCols;
115 116
     final float Y = 1.0f/mRows;
116 117

  
117
     //android.util.Log.d("BITMAP", "buildGrid");
118
     //android.util.Log.d("MeshFlat", "buildGrid");
118 119

  
119 120
     y = 0.0f;
120 121

  
......
144 145
       y+=Y;
145 146
       }
146 147

  
147
     //android.util.Log.d("BITMAP", "buildGrid done");
148
     //android.util.Log.d("MeshFlat", "buildGrid done");
148 149
     }
149 150

  
150 151
///////////////////////////////////////////////////////////////////////////////////////////////////
......
185 186
    //android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,8) );
186 187

  
187 188
    if( remainingVert!=0 )
188
      android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
189
      android.util.Log.d("MeshFlat", "remainingVert " +remainingVert );
189 190

  
190 191
    mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
191 192
    mVertAttribs.put(attribs).position(0);
193

  
194
    setData(numVertices, mVertAttribs);
192 195
    }
193 196
 }
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