'OpenGL ES3 transfrom feedback messed with more than one output
A simple particle system with transform feedback. When only one output variable: "vPos" in glTransformFeedbackVaryings, it works correctly. But after adding another one: "vVel", the transform feedback output is messed.
I checked all places but cannot find anything wrong.
int program;
int[] vao;
int[] vbo;
int[] tfo;
int particles = 4;
int particlesLen = particles * 2; // 2 for x,y; 4 for x,y,vx,vy
int particlesBytes = particlesLen * 4;
float[] buf = new float[particlesLen]; // x,y,vx,vy
Random r = new Random();
float[] time = {0f, 0f, 0f}; // time,centerX,centerY
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
initAll();
program = ShaderUtils.loadProgramTransformFeedback();
ByteBuffer buffer = ByteBuffer
.allocateDirect(particlesBytes)
.order(ByteOrder.nativeOrder());
buffer.position(0);
buffer.asFloatBuffer().put(buf);
vao = new int[2];
glGenVertexArrays(2, vao, 0);
vbo = new int[2];
glGenBuffers(2, vbo, 0);
tfo = new int[2];
glGenTransformFeedbacks(2, tfo, 0);
for (int i = 0; i < 2; i++) {
glBindVertexArray(vao[i]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[i]);
glBufferData(GL_ARRAY_BUFFER, particlesBytes, buffer, GL_DYNAMIC_DRAW);
// Load the vertex data
glVertexAttribPointer(0, 2, GL_FLOAT, false, 2 * 4, 0);
glEnableVertexAttribArray(0);
// glVertexAttribPointer(1, 2, GL_FLOAT, false, 2 * 4, 2 * 4);
// glEnableVertexAttribArray(1);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, tfo[i]);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, vbo[i]);
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
glViewport(0, 0, width, height);
// glViewport(-(height - width) / 2, 0, height, height);
this.width = width;
this.height = height;
}
int width, height;
int i0 = 1;
int frames = 0;
@Override
public void onDrawFrame(GL10 gl) {
super.onDrawFrame(gl);
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(program);
time[0] += 0.01;
int loc = glGetUniformLocation(program, "time");
glUniform3fv(loc, 1, time, 0);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, tfo[i0]);
glBeginTransformFeedback(GL_POINTS);
glBindVertexArray(vao[1 - i0]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1 - i0]);
glDrawArrays(GL_POINTS, 0, particles);
glEndTransformFeedback();
if (frames++ < 10) {
ByteBuffer bb = (ByteBuffer) glMapBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, particlesBytes, GL_MAP_READ_BIT);
if (bb != null) {
FloatBuffer res = bb.order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer();
String s = "";
for (int i = 0; i < particlesLen; i++) {
float num = res.get(i);
s = s + " " + String.format("%.2f", num);
}
Log.d("chao data ", s);
} else {
Log.d("chao data", "null");
}
glUnmapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER);
}
// swap
i0 = 1 - i0;
}
void initAll() {
float f = 0.1f;
for (int i = 0; i < buf.length; i++) {
// buf[i] = (float)r.nextInt(1000) / 1000;
// buf[i] = (buf[i] - 0.5f) * 2;
if (i % 2 == 1) {
buf[i] = buf[i - 1];
} else {
buf[i] = f;
f += 0.1f;
}
}
}
}
#version 300 es
layout (location = 0) in vec2 vPosition;
//layout (location = 1) in vec2 vVelocity;
out vec2 vPos;
//out vec2 vVel;
uniform vec3 time;
uniform vec2 center;
void main() {
float x = vPosition.x + 0.01;
if (x > 1.0) {
x = -1.0;
}
float y = vPosition.y + 0.01;
if (y > 1.0) {
y = -1.0;
}
vPos = vec2(x, y);
vVel = vVelocity;
gl_PointSize = 5.0;
gl_Position = vec4(vPos, 0.0, 1.0);
The shader program works no error, but got the output data below. One line means one frame in vbo, adjacent 4 numbers means one vertex data.
0.11 0.11 0.20 0.20 0.21 0.21 0.30 0.30 0.31 0.31 0.40 0.40 0.41 0.41 0.50 0.50
0.11 0.11 0.20 0.20 0.21 0.21 0.30 0.30 0.31 0.31 0.40 0.40 0.41 0.41 0.50 0.50
0.13 0.13 0.20 0.20 0.21 0.21 0.21 0.21 0.22 0.22 0.21 0.21 0.22 0.22 0.22 0.22
0.13 0.13 0.20 0.20 0.21 0.21 0.21 0.21 0.22 0.22 0.21 0.21 0.22 0.22 0.22 0.22
0.15 0.15 0.20 0.20 0.21 0.21 0.21 0.21 0.22 0.22 0.21 0.21 0.22 0.22 0.22 0.22
0.15 0.15 0.20 0.20 0.21 0.21 0.21 0.21 0.22 0.22 0.21 0.21 0.22 0.22 0.22 0.22
0.17 0.17 0.20 0.20 0.21 0.21 0.21 0.21 0.22 0.22 0.21 0.21 0.22 0.22 0.22 0.22
0.17 0.17 0.20 0.20 0.21 0.21 0.21 0.21 0.22 0.22 0.21 0.21 0.22 0.22 0.22 0.22
0.19 0.19 0.20 0.20 0.21 0.21 0.21 0.21 0.22 0.22 0.21 0.21 0.22 0.22 0.22 0.22
0.19 0.19 0.20 0.20 0.21 0.21 0.21 0.21 0.22 0.22 0.21 0.21 0.22 0.22 0.22 0.22
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
