How to apply transform matrix to shader

Hi all, my problem is how to set a simple uniform transform matrix (translation) to my shader. Before I create the shader and a buffer binding:

shader = loadShader("frag.glsl", "vert.glsl");

// generate a buffer binding
IntBuffer resultBuffer = ByteBuffer.allocateDirect(1 * INTEGER_SIZE).order(ByteOrder.nativeOrder()).
PGL pgl = beginPGL();
pgl.genBuffers(1, resultBuffer); // generates a buffer, places its id in resultBuffer[0]
starsBufferObjectName = resultBuffer.get(0); // grab our buffer name
endPGL();

Then each draw I do:

// bind and update array buffer data 
pgl.bindBuffer(PGL.ARRAY_BUFFER, starsBufferObjectName);
pgl.bufferData(PGL.ARRAY_BUFFER, stars.length * 8 * FLOAT_SIZE, starsData, PGL.DYNAMIC_DRAW);

shader.bind();

int vertexLocation = pgl.getAttribLocation(shader.glProgram, "vertex");
int colorLocation = pgl.getAttribLocation(shader.glProgram, "color");
pgl.enableVertexAttribArray(vertexLocation);
pgl.enableVertexAttribArray(colorLocation);
pgl.vertexAttribPointer(vertexLocation, 3, PGL.FLOAT, false, 8 * FLOAT_SIZE, 0);
pgl.vertexAttribPointer(colorLocation, 4, PGL.FLOAT, false, 8 * FLOAT_SIZE, 3 * FLOAT_SIZE);

// set point size, disable texture map
gl.glEnable(GL2.GL_BLEND);
gl.glEnable(GL2.GL_POINT_SPRITE);
gl.glEnable(GL2.GL_POINT_SMOOTH);
gl.glDisable(GL2.GL_TEXTURE_2D);

// draw points
pgl.drawArrays(PGL.POINTS, 0, stars.length);

// unbind
pgl.disableVertexAttribArray(vertexLocation);
pgl.disableVertexAttribArray(colorLocation);
shader.unbind();
pgl.bindBuffer(PGL.ARRAY_BUFFER, 0);

My vert.glsl file is:

uniform mat4 transform;

attribute vec4 vertex;
attribute vec4 color;
attribute float size;

varying vec4 vertColor;

void main() {
  gl_Position = transform * vertex;
  gl_PointSize = size;

  vertColor = color;
}

I simply want to translate each star (points) setting uniform transform matrix. How can I do?

I tried this before or after shader.bind() but it doesn't work:

PMatrix3D matrix = new PMatrix3D();
matrix.translate(width / 2, height / 2, 0);
shader.set("transform", matrix, false);

I tried this but the result is very strange (points are all aligned), where transformMatrix is a FloatBuffer:

float[] matv = new float[]{ matrix.m00, matrix.m01, matrix.m02, matrix.m03,
                    matrix.m10, matrix.m11, matrix.m12, matrix.m13,
                    matrix.m20, matrix.m21, matrix.m22, matrix.m23,
                    matrix.m30, matrix.m31, matrix.m32, matrix.m33 };
transformMatrix.put(matv);
transformMatrix.rewind();
int transformLocation = pgl.getUniformLocation(shader.glProgram, "transform");
pgl.uniformMatrix4fv(transformLocation, 1, false, transformMatrix);

Any ideas? Thanks in advance.

Tagged:
Sign In or Register to comment.