放置在屏幕中间的粒子(OpenGL ES 2 / Android)

似乎我的每个粒子被放置在(0, 0, 0)即使我为他们设置不同的位置(debugging器也证实了这一点)。 这一定是真的很愚蠢,我错过了。 我的意图是在屏幕上的随机位置上绘制250个粒子。 这是我的代码:

 @Override public void onDrawFrame(GL10 gl) { if(time++ == MAXTIME) { time = 0; } //my own helper methods GLUtils.sendBufferData(a_Position, 3, position); GLUtils.sendBufferData(a_Velocity, 3, velocity); GLUtils.sendBufferData(a_Offset, 1, offset); Matrix.setIdentityM(mModelMatrix, 0); Matrix.multiplyMM(mMVPMatrix, 0, camera.getViewMatrix(), 0, mModelMatrix, 0); Matrix.multiplyMM(mMVPMatrix, 0, camera.getProjectionMatrix(), 0, mMVPMatrix, 0); GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); GLES20.glUniform1f(u_Time, time); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture); GLES20.glUniform1i(u_Texture, 0); GLES20.glDrawArrays(GLES20.GL_POINTS, 0, particlesCount); //particlesCount = 250 } @Override protected void initData() { float[] positions = new float[3 * count]; float[] velocities = new float[3 * count]; float[] offsets = new float[count]; int index = 0; for(int i = 0; i < count; i++) { offsets[i] = MathUtils.random() * 60 + 20; positions[index] = MathUtils.random() * 2 - 1; velocities[index] = -0.025f; index++; positions[index] = MathUtils.random() * 2 - 1; velocities[index] = -0.035f; index++; positions[index] = 0; velocities[index] = 0; index++; } //my own helper method texture = GLUtils.loadTexture(mContext, R.drawable.snowflake); position = ByteBuffer.allocateDirect(positions.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); velocity = ByteBuffer.allocateDirect(velocities.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); offset = ByteBuffer.allocateDirect(offsets.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); position.put(positions).position(0); velocity.put(velocities).position(0); offset.put(offsets).position(0); } private static final String vertexShader() { return "" + "uniform mat4 u_MVPMatrix;\n" + "uniform float u_Lifetime;\n" + "uniform float u_CurrentTime;\n" + "\n" + "attribute vec4 a_Position;\n" + "attribute vec3 a_Velocity;\n" + "attribute float a_Offset;\n" + "\n" + "void main() {\n" + " gl_PointSize = 10.0;\n" + " gl_Position = u_MVPMatrix * a_Position;\n" + "}"; } private static final String fragmentShader() { return "precision mediump float;\n" + "\n" + "uniform sampler2D u_Texture;\n" + "\n" + "void main() {\n" + " gl_FragColor = texture2D(u_Texture, gl_PointCoord);\n" + "}"; } 

注:我故意从顶点着色器中删除了一些行,因为我不认为它们在这种情况下是相关的。

编辑:这可能是gl_PointCoord总是设置为0.我现在正在调查。

你分配和初始化位置和速度,但从来没有填写分配的缓冲区:

 position = ByteBuffer.allocateDirect(positions.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); velocity = ByteBuffer.allocateDirect(velocities.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); offset = ByteBuffer.allocateDirect(offsets.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); position.put(positions); velocity.put(velocities); offset.put(offsets);