因此,我正在关注我可以在此处找到的关于转换反馈的最简单示例之一,并将其转换为 Android/Java,但 Android 似乎缺少glGetBufferSubData
OpenGL ES 3.0 中可用的功能。
我已经尝试搜索Android Source以查看在 JNI 下调用另一个方法时是否使用了此方法,但我不确定我是否正在寻找正确的位置。
如果此功能不存在,我如何从转换反馈中读取数据?我看过glGetTransformfeedbackVarying
,glGetBufferPointerv
但glReadBuffer
似乎没有人能做我想做的事。
这是我的代码:
import com.harmonicprocesses.penelopefree.openGL.MyGLRenderer;
import android.opengl.GLES30;
import android.util.Log;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
/**
* Created by izzy on 6/24/15.
*/
public class TransformFeedback {
// Vertex shader
private final String vertexShaderSrc =
"in float inValue;\n" +
"out float outValue;\n" +
"void main() {\n" +
" outValue = sqrt(inValue);\n" +
"}\n";
private final int mProgram;
public TransformFeedback{
// Compile shader
int vertexShader = MyGLRenderer.loadShader(GLES30.GL_VERTEX_SHADER,
vertexShaderSrc);
// Create program and specify transform feedback variables
mProgram = GLES30.glCreateProgram();
GLES30.glAttachShader(mProgram, vertexShader);
//const GLchar* feedbackVaryings[] = { "outValue" };
GLES30.glTransformFeedbackVaryings(mProgram, 1, {"outValue"}, GLES30.GL_INTERLEAVED_ATTRIBS);
GLES30.glLinkProgram(mProgram);
GLES30.glUseProgram(mProgram);
// Create VAO
int[] vao = new int[1];
GLES30.glGenVertexArrays(1, vao, 0);
GLES30.glBindVertexArray(vao[0]);
// Create input VBO and vertex format
int bufferLength = 5 * 4; //5 floats 4 bytes each
ByteBuffer bb = ByteBuffer.allocateDirect(bufferLength);
FloatBuffer data = bb.asFloatBuffer();
float[] floatData = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
data.put(floatData);
data.position(0);
int[] vbo = new int[1];
GLES30.glGenBuffers(1, vbo, 0);
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, vbo[0]);
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, bufferLength, data, GLES30.GL_STATIC_DRAW);
int inputAttrib = GLES30.glGetAttribLocation(mProgram, "inValue");
GLES30.glEnableVertexAttribArray(inputAttrib);
GLES30.glVertexAttribPointer(inputAttrib, 1, GLES30.GL_FLOAT, false, 0, 0);
// Create transform feedback buffer
int[] tbo = new int[1];
GLES30.glGenBuffers(1, tbo, 0);
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, tbo[0]);
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, bufferLength, null, GLES30.GL_STATIC_READ);
// Perform feedback transform
GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD);
GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tbo[0]);
GLES30.glBeginTransformFeedback(GLES30.GL_POINTS);
GLES30.glDrawArrays(GLES30.GL_POINTS, 0, 5);
GLES30.glEndTransformFeedback();
GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD);
GLES30.glFlush();
// Fetch and print results
float feedback[] = new float[5];
GLES30.glGetBufferSubData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, bufferLength, feedback);
Log.e("TransformFeedback", String.format("%f %f %f %f %f\n", feedback[0], feedback[1], feedback[2], feedback[3], feedback[4]));
}
我的最终实现不需要读取数据,只需在下一次传递(反馈)的着色器中使用它。但这确实是第一步。