Project

General

Profile

« Previous | Next » 

Revision 23ee8a18

Added by Leszek Koltunski over 7 years ago

Simplify Transform Feedback App.

View differences:

src/main/java/org/distorted/examples/feedback/FeedbackRenderer.java
19 19

  
20 20
package org.distorted.examples.feedback;
21 21

  
22
import android.content.res.Resources;
23
import android.opengl.GLES30;
22 24
import android.opengl.GLSurfaceView;
25
import android.util.Log;
23 26

  
24 27
import org.distorted.library.Distorted;
28
import org.distorted.library.program.DistortedProgram;
29

  
30
import java.io.InputStream;
31
import java.nio.Buffer;
32
import java.nio.ByteBuffer;
33
import java.nio.ByteOrder;
34
import java.nio.FloatBuffer;
25 35

  
26 36
import javax.microedition.khronos.egl.EGLConfig;
27 37
import javax.microedition.khronos.opengles.GL10;
......
31 41
class FeedbackRenderer implements GLSurfaceView.Renderer
32 42
{
33 43
    private GLSurfaceView mView;
34
    private TransformFeedback mTF;
44
    private DistortedProgram mFeedbackProgram;
45
    private int mBufferLength;
46
    private int[] mVBO, mTBO;
35 47

  
36 48
///////////////////////////////////////////////////////////////////////////////////////////////////
37 49

  
......
44 56
   
45 57
    public void onDrawFrame(GL10 glUnused) 
46 58
      {
47
      mTF.render();
59
      mFeedbackProgram.useProgram();
60

  
61
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mVBO[0]);
62
      GLES30.glVertexAttribPointer(mFeedbackProgram.mAttribute[0], 1, GLES30.GL_FLOAT, false, 4, 0);
63
      GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mTBO[0]);
64

  
65
      GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD);
66
      GLES30.glBeginTransformFeedback(GLES30.GL_POINTS);
67
      GLES30.glDrawArrays(GLES30.GL_POINTS, 0, mBufferLength);
68
      GLES30.glEndTransformFeedback();
69
      GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD);
70
      GLES30.glFlush();
71

  
72
      Buffer mappedBuffer =  GLES30.glMapBufferRange(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mBufferLength, GLES30.GL_MAP_READ_BIT);
73

  
74
      if (mappedBuffer!=null)
75
        {
76
        ByteBuffer bb = ((ByteBuffer) mappedBuffer);
77
        bb.order(ByteOrder.nativeOrder());
78
        FloatBuffer transformedData = bb.asFloatBuffer();
79

  
80
        Log.d( "TransformFeedback", String.format("output values = %f %f %f %f %f %f\n",
81
                  transformedData.get(), transformedData.get(), transformedData.get(),
82
                  transformedData.get(), transformedData.get(), transformedData.get() ));
83
        }
84

  
85
      GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER);
48 86
      }
49 87

  
50 88
///////////////////////////////////////////////////////////////////////////////////////////////////
......
67 105
        android.util.Log.e("Feedback", ex.getMessage() );
68 106
        }
69 107

  
70
      mTF = new TransformFeedback(mView.getContext());
108
      final Resources resources = mView.getContext().getResources();
109
      final InputStream vertStream = resources.openRawResource(org.distorted.library.R.raw.feedback_vertex_shader);
110
      final InputStream fragStream = resources.openRawResource(org.distorted.library.R.raw.feedback_fragment_shader);
111

  
112
      String vertHeader= Distorted.GLSL_VERSION;
113
      String fragHeader= Distorted.GLSL_VERSION;
114
      String[] feedback = { "outValue" };
115

  
116
      try
117
        {
118
        mFeedbackProgram = new DistortedProgram(vertStream,fragStream, vertHeader, fragHeader, Distorted.GLSL, feedback );
119
        }
120
      catch(Exception ex)
121
        {
122
        Log.e("TransformFeedback", "exception creating feedback program: "+ex.getMessage());
123
        }
124

  
125
      float[] floatData = { 2.0f, 4.0f, 9.0f, 16.0f, 25.0f, 100.0f };
126
      mBufferLength = floatData.length * 4;
127
      FloatBuffer data = ByteBuffer.allocateDirect(mBufferLength).order(ByteOrder.nativeOrder()).asFloatBuffer();
128
      data.put(floatData).position(0);
129

  
130
      mVBO = new int[1];
131
      GLES30.glGenBuffers(1, mVBO, 0);
132
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mVBO[0]);
133
      GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, mBufferLength, data, GLES30.GL_STATIC_READ);
134

  
135
      mTBO = new int[1];
136
      GLES30.glGenBuffers(1, mTBO, 0);
137
      GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mTBO[0]);
138
      GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mBufferLength, null, GLES30.GL_STATIC_READ);
71 139
      }
72 140
}
src/main/java/org/distorted/examples/feedback/TransformFeedback.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.examples.feedback;
21

  
22
import android.content.Context;
23
import android.content.res.Resources;
24
import android.opengl.GLES30;
25
import android.util.Log;
26

  
27
import org.distorted.library.Distorted;
28
import org.distorted.library.program.DistortedProgram;
29

  
30
import java.io.InputStream;
31
import java.nio.Buffer;
32
import java.nio.ByteBuffer;
33
import java.nio.ByteOrder;
34
import java.nio.FloatBuffer;
35

  
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

  
38
class TransformFeedback
39
    {
40
    private DistortedProgram mFeedbackProgram;
41
    private int mBufferLength;
42
    private int[] mVBO, mTBO;
43

  
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

  
46
    TransformFeedback(final Context context)
47
        {
48
        final Resources resources = context.getResources();
49
        final InputStream vertStream = resources.openRawResource(org.distorted.library.R.raw.feedback_vertex_shader);
50
        final InputStream fragStream = resources.openRawResource(org.distorted.library.R.raw.feedback_fragment_shader);
51

  
52
        String vertHeader= Distorted.GLSL_VERSION;
53
        String fragHeader= Distorted.GLSL_VERSION;
54
        String[] feedback = { "outValue" };
55

  
56
        try
57
          {
58
          mFeedbackProgram = new DistortedProgram(vertStream,fragStream, vertHeader, fragHeader, Distorted.GLSL, feedback );
59
          }
60
        catch(Exception ex)
61
          {
62
          Log.e("TransformFeedback", "exception creating feedback program: "+ex.getMessage());
63
          }
64

  
65
        float[] floatData = { 2.0f, 4.0f, 9.0f, 16.0f, 25.0f, 100.0f };
66
        mBufferLength = floatData.length * 4;
67
        FloatBuffer data = ByteBuffer.allocateDirect(mBufferLength).order(ByteOrder.nativeOrder()).asFloatBuffer();
68
        data.put(floatData).position(0);
69

  
70
        mVBO = new int[1];
71
        GLES30.glGenBuffers(1, mVBO, 0);
72
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mVBO[0]);
73
        GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, mBufferLength, data, GLES30.GL_STATIC_READ);
74

  
75
        mTBO = new int[1];
76
        GLES30.glGenBuffers(1, mTBO, 0);
77
        GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mTBO[0]);
78
        GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mBufferLength, null, GLES30.GL_STATIC_READ);
79
        }
80

  
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

  
83
    void render()
84
        {
85
        mFeedbackProgram.useProgram();
86

  
87
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mVBO[0]);
88
        GLES30.glVertexAttribPointer(mFeedbackProgram.mAttribute[0], 1, GLES30.GL_FLOAT, false, 4, 0);
89
        GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mTBO[0]);
90

  
91
        GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD);
92
        GLES30.glBeginTransformFeedback(GLES30.GL_POINTS);
93
        GLES30.glDrawArrays(GLES30.GL_POINTS, 0, mBufferLength);
94
        GLES30.glEndTransformFeedback();
95
        GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD);
96
        GLES30.glFlush();
97

  
98
        Buffer mappedBuffer =  GLES30.glMapBufferRange(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mBufferLength, GLES30.GL_MAP_READ_BIT);
99

  
100
        if (mappedBuffer!=null)
101
            {
102
            ByteBuffer bb = ((ByteBuffer) mappedBuffer);
103
            bb.order(ByteOrder.nativeOrder());
104
            FloatBuffer transformedData = bb.asFloatBuffer();
105

  
106
            Log.d( "TransformFeedback", String.format("output values = %f %f %f %f %f %f\n",
107
                    transformedData.get(), transformedData.get(), transformedData.get(),
108
                    transformedData.get(), transformedData.get(), transformedData.get() ));
109
            }
110

  
111
        GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER);
112
        }
113
}

Also available in: Unified diff