Project

General

Profile

« Previous | Next » 

Revision baa47ae9

Added by Leszek Koltunski over 7 years ago

Transform Feedback Example.

View differences:

src/main/AndroidManifest.xml
45 45
        <activity android:name=".mirror.MirrorActivity"/>
46 46
        <activity android:name=".blur.BlurActivity"/>
47 47
        <activity android:name=".multiblur.MultiblurActivity"/>
48
        <activity android:name=".feedback.FeedbackActivity"/>
48 49
    </application>
49 50
</manifest>
src/main/java/org/distorted/examples/TableOfContents.java
63 63
import org.distorted.examples.mirror.MirrorActivity;
64 64
import org.distorted.examples.blur.BlurActivity;
65 65
import org.distorted.examples.multiblur.MultiblurActivity;
66
import org.distorted.examples.feedback.FeedbackActivity;
66 67

  
67 68
///////////////////////////////////////////////////////////////////////////////////////////////////
68 69

  
......
346 347
      activityMapping.put(i++, MultiblurActivity.class);
347 348
   }
348 349

  
350
   {
351
      final Map<String, Object> item = new HashMap<>();
352
      item.put(ITEM_IMAGE, R.drawable.icon_example_feedback);
353
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_feedback));
354
      item.put(ITEM_SUBTITLE, getText(R.string.example_feedback_subtitle));
355
      data.add(item);
356
      activityMapping.put(i++, FeedbackActivity.class);
357
   }
358

  
349 359
   final SimpleAdapter dataAdapter = new SimpleAdapter(this, data, R.layout.toc_item, new String[] {ITEM_IMAGE, ITEM_TITLE, ITEM_SUBTITLE}, new int[] {R.id.Image, R.id.Title, R.id.SubTitle});
350 360
   setListAdapter(dataAdapter);  
351 361
      
src/main/java/org/distorted/examples/feedback/FeedbackActivity.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.app.Activity;
23
import android.os.Bundle;
24

  
25
import org.distorted.library.Distorted;
26

  
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
public class FeedbackActivity extends Activity
30
{
31
    private FeedbackSurfaceView mView;
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

  
35
    @Override
36
    protected void onCreate(Bundle icicle)
37
      {
38
      super.onCreate(icicle);
39
      mView = new FeedbackSurfaceView(this);
40
      setContentView(mView);
41
      }
42

  
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
    
45
    @Override
46
    protected void onPause() 
47
      {
48
      mView.onPause();
49
      super.onPause();
50
      }
51

  
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
    
54
    @Override
55
    protected void onResume() 
56
      {
57
      super.onResume();
58
      mView.onResume();
59
      }
60
    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
    
63
    @Override
64
    protected void onDestroy() 
65
      {
66
      super.onDestroy();
67
      }
68
    
69
}
src/main/java/org/distorted/examples/feedback/FeedbackRenderer.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.opengl.GLSurfaceView;
23

  
24
import javax.microedition.khronos.egl.EGLConfig;
25
import javax.microedition.khronos.opengles.GL10;
26

  
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
class FeedbackRenderer implements GLSurfaceView.Renderer
30
{
31

  
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

  
35
    FeedbackRenderer(GLSurfaceView v)
36
      {
37

  
38
      }
39

  
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
   
42
    public void onDrawFrame(GL10 glUnused) 
43
      {
44
      TransformFeedback tf = new TransformFeedback();
45
      }
46

  
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
    
49
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
50
      { 
51

  
52
      }
53

  
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
    
56
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
57
      {
58

  
59
      }
60
}
src/main/java/org/distorted/examples/feedback/FeedbackSurfaceView.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.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLSurfaceView;
26

  
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
class FeedbackSurfaceView extends GLSurfaceView
30
{
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32
   
33
    public FeedbackSurfaceView(Context context)
34
      {
35
      super(context);
36
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
37
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
38
      setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
39
      setRenderer(new FeedbackRenderer(this));
40
      }
41
}
42

  
src/main/java/org/distorted/examples/feedback/TransformFeedback.java
1
package org.distorted.examples.feedback;
2

  
3
import android.opengl.EGL14;
4
import android.opengl.GLES30;
5
import android.util.Log;
6

  
7
import java.nio.Buffer;
8
import java.nio.ByteBuffer;
9
import java.nio.ByteOrder;
10
import java.nio.FloatBuffer;
11

  
12
/**
13
 * Created by izzy on 6/24/15. TransformFeedback is a example of how
14
 *  to do transform feedback on Android using OpenGLES 3.0. Closely
15
 *  follows the example found at https://open.gl/feedback.
16
 *
17
 * Assumes you have a working instance of GLSurfaceVew. Remember to call
18
 *  setEGLContextClientVersion(3) to set the OpenGLES context to API 3.0
19
 *  and have the appropriate OpenGLES specified in the Android manifest file.
20
 */
21

  
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

  
24

  
25
public class TransformFeedback {
26
    private final String TAG = "TransformFeedback";
27

  
28

  
29
// Vertex shader
30
    private final String vertexShaderSrc =
31
            "#version 300 es \n" +
32
            "in float inValue;\n" +
33
            "out float outValue;\n" +
34

  
35
            "void main() {\n" +
36
            "    outValue = sqrt(inValue);\n" +
37
            "}";
38

  
39
// Need a fragmentShader or glLinkProgram will throw
40
    private final String fragmentShaderCode =
41
            "#version 300 es \n" +
42
            "precision mediump float;\n" +
43
            "out vec4 fragColor;\n" +
44
            "void main() {\n" +
45
            "  fragColor = vec4(1.0,1.0,1.0,1.0);\n" +
46
            "}";
47

  
48
    private final int mProgram;
49

  
50
    /**
51
     * The TransformFeedback constructor contains all the code to initialize
52
     *  and draw the TransformFeedback, so create an instance of TransformFeedback
53
     *  i.e. 'new TransformFeedBack()' in your 'GLRenderer.OnDrawFrame()' method.
54
     */
55
    public TransformFeedback(){
56

  
57
        // Compile shaders
58
        int vertexShader   = loadShader(GLES30.GL_VERTEX_SHADER  , vertexShaderSrc   );
59
        int fragmentShader = loadShader(GLES30.GL_FRAGMENT_SHADER, fragmentShaderCode);
60

  
61
        // Create program and attach shaders
62
        mProgram = GLES30.glCreateProgram();
63
        GLES30.glAttachShader(mProgram, vertexShader);
64
        GLES30.glAttachShader(mProgram, fragmentShader);
65

  
66
        // Tell GL where the feedbackVaryings are in the shader before Linking
67
        //  the shaders together to form a program.
68
        final String[] feedbackVaryings = { "outValue" };
69
        GLES30.glTransformFeedbackVaryings(mProgram, feedbackVaryings, GLES30.GL_INTERLEAVED_ATTRIBS);
70
        checkGlError(TAG + " glTransformFeedbackVaryings");
71

  
72
        // Link program and look for errors
73
        GLES30.glLinkProgram(mProgram);
74

  
75
        int[] linkSuccessful = new int[1];
76
        GLES30.glGetProgramiv(mProgram, GLES30.GL_LINK_STATUS, linkSuccessful, 0);
77

  
78
        if (linkSuccessful[0] != 1){
79
            Log.d(TAG, "glLinkProgram failed");
80
        }
81
        checkGlError(TAG + " glLinkProgram");
82

  
83
        /***********
84
         * Begin Rendering process
85
         *  everything before this can be moved to an initialization stage
86
         ***********/
87

  
88
        // Bring the program into use
89
        GLES30.glUseProgram(mProgram);
90
        checkGlError(TAG + " glUseProgram");
91

  
92
        // Create data to fill VBO
93
        float[] floatData = { 1.0f, 4.0f, 9.0f, 16.0f, 25.0f, 100.0f };
94
        int bufferLength = floatData.length * 4;
95
        FloatBuffer data = ByteBuffer.allocateDirect(bufferLength)
96
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
97
        data.put(floatData).position(0);
98

  
99
        // Create VBO and fill with data
100
        int[] vbo = new int[1];
101
        GLES30.glGenBuffers(1, vbo, 0);
102
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, vbo[0]);
103
        GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, bufferLength, data, GLES30.GL_STATIC_READ);
104
        checkGlError(TAG + " glBufferData GL_ARRAY_BUFFER");
105

  
106
        // Link created VBO to shader attribute "inValue"
107
        int inputAttrib = GLES30.glGetAttribLocation(mProgram, "inValue");
108
        GLES30.glEnableVertexAttribArray(inputAttrib);
109
        GLES30.glVertexAttribPointer(inputAttrib, 1, GLES30.GL_FLOAT, false, 4, 0);
110
        checkGlError(TAG + " glVertexAttribPointer");
111

  
112
        // Create transform feedback buffer object and bind to transform feedback
113
        //  this creates space in a buffer object for the TransformFeedback.
114
        int[] tbo = new int[1];
115
        GLES30.glGenBuffers(1, tbo, 0);
116
        GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, tbo[0]);
117
        GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, bufferLength, null, GLES30.GL_STATIC_READ);
118
        GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tbo[0]); //very important
119

  
120
        checkGlError(TAG + " glBindBufferBase");
121

  
122
        // Disable Rasterizer if you just need the data
123
        GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD);
124

  
125
        // Start the TransformFeedback, Draw the Arrays, then End the Transform Feedback
126
        GLES30.glBeginTransformFeedback(GLES30.GL_POINTS);
127
        GLES30.glDrawArrays(GLES30.GL_POINTS, 0, bufferLength);
128
        checkGlError(TAG + " glDrawArrays");
129
        GLES30.glEndTransformFeedback();
130

  
131
        // Reenable Rasterizer if you need it, which you do if you are drawing anything.
132
        GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD);
133

  
134
        // Flush out anything in GL before mapping the buffer.
135
        GLES30.glFlush();
136
        checkGlError(TAG + " pre-glMapBufferRange ");
137

  
138
        // Map the transform feedback buffer to local address space.
139
        Buffer mappedBuffer =  GLES30.glMapBufferRange(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER,
140
                0, bufferLength, GLES30.GL_MAP_READ_BIT);
141
        checkGlError(TAG + " glMapBufferRange");
142

  
143
        // Read out the data, here we write to logcat.
144
        if (mappedBuffer!=null){
145
            ByteBuffer bb = ((ByteBuffer) mappedBuffer);
146
            bb.order(ByteOrder.nativeOrder());
147
            FloatBuffer transformedData = bb.asFloatBuffer();
148

  
149
            Log.d(TAG, String.format("output values = %f %f %f %f %f %f\n",
150
                    transformedData.get(), transformedData.get(), transformedData.get(),
151
                    transformedData.get(), transformedData.get(), transformedData.get() ));
152
        }
153
        // Don't forget to Unmap the Transform Feeback Buffer.
154
        GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER);
155
    }
156

  
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

  
159
    private static void checkGlError(String op)
160
      {
161
      int error = GLES30.glGetError();
162

  
163
      if (error != GLES30.GL_NO_ERROR)
164
        {
165
        String msg = op + ": glError 0x" + Integer.toHexString(error);
166
        throw new RuntimeException(msg);
167
        }
168
      }
169

  
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

  
172
    private static int loadShader(final int shaderType, final String shaderSource)
173
      {
174
      int shaderHandle = GLES30.glCreateShader(shaderType);
175

  
176
      if (shaderHandle != 0)
177
        {
178
        GLES30.glShaderSource(shaderHandle, shaderSource);
179
        GLES30.glCompileShader(shaderHandle);
180
        final int[] compileStatus = new int[1];
181
        GLES30.glGetShaderiv(shaderHandle, GLES30.GL_COMPILE_STATUS, compileStatus, 0);
182

  
183
        if (compileStatus[0] != GLES30.GL_TRUE )
184
          {
185
          GLES30.glDeleteShader(shaderHandle);
186
          shaderHandle = 0;
187
          }
188
        }
189

  
190
      if (shaderHandle == 0)
191
        {
192
        String error = GLES30.glGetShaderInfoLog(shaderHandle);
193
        android.util.Log.e("Program", "error compiling :"+error);
194
        }
195

  
196
      return shaderHandle;
197
      }
198
}
src/main/res/values/strings.xml
138 138
    <string name="example_blur_subtitle">Postprocessing effect: Blur.</string>
139 139
    <string name="example_multiblur">Multiblur</string>
140 140
    <string name="example_multiblur_subtitle">Blur multiple objects which obstruct each other.</string>
141
    <string name="example_feedback">Transform Feedback</string>
142
    <string name="example_feedback_subtitle">Transform Feedback test.</string>
141 143

  
142 144
    <string name="example_movingeffects_toast">Click on \'RESET\' and define your path by touching the screen. Then click on one of the effects and see it move along your path.</string>
143 145
    <string name="example_rotate_toast">Rotate the scene by swiping the screen</string>

Also available in: Unified diff