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.res.Resources;
|
23
|
import android.opengl.GLES30;
|
24
|
import android.opengl.GLSurfaceView;
|
25
|
|
26
|
import org.distorted.library.Distorted;
|
27
|
import org.distorted.library.program.DistortedProgram;
|
28
|
|
29
|
import java.io.InputStream;
|
30
|
import java.nio.Buffer;
|
31
|
import java.nio.ByteBuffer;
|
32
|
import java.nio.ByteOrder;
|
33
|
import java.nio.FloatBuffer;
|
34
|
|
35
|
import javax.microedition.khronos.egl.EGLConfig;
|
36
|
import javax.microedition.khronos.opengles.GL10;
|
37
|
|
38
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39
|
|
40
|
class FeedbackRenderer implements GLSurfaceView.Renderer
|
41
|
{
|
42
|
private final int BYTESFLOAT = 4;
|
43
|
private final int POSITION = 3;
|
44
|
|
45
|
private final boolean USEPOINTS=false;
|
46
|
|
47
|
private GLSurfaceView mView;
|
48
|
private DistortedProgram mFeedbackProgram;
|
49
|
private int mVertices;
|
50
|
private int[] mVBO, mTBO;
|
51
|
|
52
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53
|
|
54
|
FeedbackRenderer(GLSurfaceView v)
|
55
|
{
|
56
|
mView = v;
|
57
|
}
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
public void onDrawFrame(GL10 glUnused)
|
62
|
{
|
63
|
mFeedbackProgram.useProgram();
|
64
|
|
65
|
int numVertices = (USEPOINTS ? mVertices : 3*(mVertices-2));
|
66
|
|
67
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mVBO[0]);
|
68
|
GLES30.glVertexAttribPointer(mFeedbackProgram.mAttribute[0], USEPOINTS ? 1:POSITION, GLES30.GL_FLOAT, false, 0, 0);
|
69
|
GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mTBO[0]);
|
70
|
|
71
|
GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD);
|
72
|
GLES30.glBeginTransformFeedback( USEPOINTS ? GLES30.GL_POINTS : GLES30.GL_TRIANGLES);
|
73
|
GLES30.glDrawArrays( USEPOINTS ? GLES30.GL_POINTS : GLES30.GL_TRIANGLE_STRIP, 0, USEPOINTS ? mVertices*POSITION : mVertices);
|
74
|
GLES30.glEndTransformFeedback();
|
75
|
GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD);
|
76
|
GLES30.glFlush();
|
77
|
|
78
|
Buffer mappedBuffer = GLES30.glMapBufferRange(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, numVertices*POSITION*BYTESFLOAT, GLES30.GL_MAP_READ_BIT);
|
79
|
FloatBuffer fb = ((ByteBuffer) mappedBuffer).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
80
|
String msg = "";
|
81
|
|
82
|
for(int i=0; i<numVertices*POSITION; i++) msg += (" "+fb.get(i));
|
83
|
|
84
|
android.util.Log.d( "Feedback", msg);
|
85
|
|
86
|
GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER);
|
87
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
|
88
|
}
|
89
|
|
90
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91
|
|
92
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
93
|
{
|
94
|
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
100
|
{
|
101
|
try
|
102
|
{
|
103
|
Distorted.onCreate(mView.getContext());
|
104
|
}
|
105
|
catch(Exception ex)
|
106
|
{
|
107
|
android.util.Log.e("Feedback", ex.getMessage() );
|
108
|
}
|
109
|
|
110
|
final Resources resources = mView.getContext().getResources();
|
111
|
final InputStream vertStream = resources.openRawResource(org.distorted.library.R.raw.feedback_vertex_shader);
|
112
|
final InputStream fragStream = resources.openRawResource(org.distorted.library.R.raw.feedback_fragment_shader);
|
113
|
|
114
|
int glsl = 300;
|
115
|
String header= "#version 300 es\n";
|
116
|
String[] feedback = { "outValue" };
|
117
|
|
118
|
if(USEPOINTS)
|
119
|
{
|
120
|
header += "#define USEPOINTS 1\n";
|
121
|
}
|
122
|
else
|
123
|
{
|
124
|
header += "#define USEPOINTS 0\n";
|
125
|
}
|
126
|
|
127
|
try
|
128
|
{
|
129
|
mFeedbackProgram = new DistortedProgram(vertStream,fragStream, header, header, glsl, feedback );
|
130
|
}
|
131
|
catch(Exception ex)
|
132
|
{
|
133
|
android.util.Log.e("Feedback", "exception creating feedback program: "+ex.getMessage());
|
134
|
}
|
135
|
|
136
|
float[] floatData = { 1.0f,4.0f,9.0f, 16.0f,25.0f,36.0f, 49.0f,64.0f,81.0f, 100.0f,121.0f,144.0f};
|
137
|
mVertices = floatData.length/POSITION;
|
138
|
FloatBuffer data = ByteBuffer.allocateDirect(mVertices*POSITION*BYTESFLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
139
|
data.put(floatData).position(0);
|
140
|
|
141
|
mVBO = new int[1];
|
142
|
GLES30.glGenBuffers(1, mVBO, 0);
|
143
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mVBO[0]);
|
144
|
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, mVertices*POSITION*BYTESFLOAT, data, GLES30.GL_STATIC_READ);
|
145
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
|
146
|
|
147
|
int numVertices = (USEPOINTS ? mVertices : 3*(mVertices-2));
|
148
|
|
149
|
mTBO = new int[1];
|
150
|
GLES30.glGenBuffers(1, mTBO, 0);
|
151
|
GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mTBO[0]);
|
152
|
GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, numVertices*POSITION*BYTESFLOAT, null, GLES30.GL_STATIC_READ);
|
153
|
GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0);
|
154
|
}
|
155
|
}
|