Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalBuffer.java @ 246d021c

1 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 466450b5 Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 466450b5 Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 466450b5 Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.library.main;
21
22 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
23 22e60fba Leszek Koltunski
import java.nio.ByteBuffer;
24
import java.nio.ByteOrder;
25
import java.nio.FloatBuffer;
26 466450b5 Leszek Koltunski
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
/**
29
 * Implements OpenGL buffer object such as GL_ARRAY_BUFFER or GL_TRANSFORM_FEEDBACK_BUFFER.
30
 * Main point: deal with Android lifecycle and recreate the buffer on loss of OpenGL context.
31
 * <p>
32
 * Not part of public API, do not document (public only because has to be used in Meshes)
33
 *
34
 * @y.exclude
35
 */
36 7602a827 Leszek Koltunski
public class InternalBuffer extends InternalObject
37 466450b5 Leszek Koltunski
  {
38 7e53a35f Leszek Koltunski
  private final int[] mIndex;
39 466450b5 Leszek Koltunski
  private int mTarget, mSize, mUsage;
40 342a6773 Leszek Koltunski
  private FloatBuffer mBuffer;
41 466450b5 Leszek Koltunski
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 7602a827 Leszek Koltunski
  public InternalBuffer(int target, int usage)
45 466450b5 Leszek Koltunski
    {
46 7602a827 Leszek Koltunski
    super(InternalObject.TYPE_USER);
47 466450b5 Leszek Koltunski
48
    mIndex  = new int[1];
49
    mTarget = target;
50
    mUsage  = usage;
51 ccd98f1c Leszek Koltunski
    mBuffer = null;
52
    mSize   = 0;
53 466450b5 Leszek Koltunski
54
    recreate();
55
    }
56
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58 22e60fba Leszek Koltunski
// must be called from a thread holding OpenGL Context.
59 466450b5 Leszek Koltunski
60 7e53a35f Leszek Koltunski
  public int createImmediately(int size, float[] buffer)
61 466450b5 Leszek Koltunski
    {
62 22e60fba Leszek Koltunski
    if( mIndex[0]<0 )
63
      {
64
      mSize= size;
65 ccd98f1c Leszek Koltunski
66 22e60fba Leszek Koltunski
      if( buffer!=null )
67
        {
68
        FloatBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()).asFloatBuffer();
69
        buf.put(buffer).position(0);
70
        mBuffer = buf;
71
        }
72
      else
73
        {
74
        mBuffer = null;
75
        }
76
77 b7074bc6 Leszek Koltunski
      GLES30.glGenBuffers( 1, mIndex, 0);
78
      GLES30.glBindBuffer( mTarget, mIndex[0]);
79
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
80
      GLES30.glBindBuffer( mTarget, 0);
81 22e60fba Leszek Koltunski
82
      markWasCreatedImmediately();
83
      }
84 7e53a35f Leszek Koltunski
85
    return mIndex[0];
86 22e60fba Leszek Koltunski
    }
87
88 342a6773 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
89
// buffer non-null!!
90
91
  public void update(float[] buffer)
92
    {
93
    mBuffer.put(buffer).position(0);
94
95
    GLES30.glBindBuffer( mTarget, mIndex[0]);
96
    GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
97
    GLES30.glBindBuffer( mTarget, 0);
98
    }
99
100 22e60fba Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
101
102
  public void invalidate()
103
    {
104
    recreate();
105 466450b5 Leszek Koltunski
    }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
// must be called from a thread holding OpenGL Context
109
110
  void create()
111
    {
112
    if( mIndex[0]<0 )
113
      {
114 b7074bc6 Leszek Koltunski
      GLES30.glGenBuffers( 1, mIndex, 0);
115
      GLES30.glBindBuffer( mTarget, mIndex[0]);
116
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
117
      GLES30.glBindBuffer( mTarget, 0);
118 466450b5 Leszek Koltunski
      }
119
    }
120
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
// must be called from a thread holding OpenGL Context
123
124
  void delete()
125
    {
126
    if( mIndex[0]>=0 )
127
      {
128 b7074bc6 Leszek Koltunski
      GLES30.glDeleteBuffers(1, mIndex, 0);
129 466450b5 Leszek Koltunski
      mIndex[0] = -1;
130
      }
131
    }
132
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
135
  void recreate()
136
    {
137
    mIndex[0] = -1;
138
    }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
// debugging only
142
143
  String printDetails()
144
    {
145
    return getClass().getSimpleName();
146
    }
147
  }