I have tried to implement this matrix, but it doesn’t work for me. Am I doing anything wrong?
I have changed this function into:
public Matrix4f orthoMatrix(float left, float right, float bottom, float top, float near, float far) {
Matrix4f matrix = new Matrix4f();
float x_orth = 2 / (right - left);
float y_orth = 2 / (top - bottom);
float z_orth = -2 / (far - near);
float tx = -(right + left) / (right - left);
float ty = -(top + bottom) / (top - bottom);
float tz = -(far + near) / (far - near);
matrix.m00 = x_orth;
matrix.m10 = 0;
matrix.m20 = 0;
matrix.m30 = 0;
matrix.m01 = 0;
matrix.m11 = y_orth;
matrix.m21 = 0;
matrix.m31 = 0;
matrix.m02 = 0;
matrix.m12 = 0;
matrix.m22 = z_orth;
matrix.m32 = 0;
matrix.m03 = tx;
matrix.m13 = ty;
matrix.m23 = tz;
matrix.m33 = 1;
return matrix;
}
Matrix initialization (screen resolution - 800X600):
projectionMatrix = gCore.matrix.orthoMatrix(-400, 400, -300, 300, 0.1f, 1.1f);
Rendering (I am using scaleCoords and scaleScale functions to be able to input more programmer-friendly values, currently from range 0;100):
public void startDrawing() {
GL20.glUseProgram(activeProgram);
GL30.glBindVertexArray(vaoId);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
}
public void render(int texture, int shader, float x, float y, float scalex, float scaley, float angle) {
x = scaleCoords(x);
y = scaleCoords(y);
scalex = scaleScale(scalex);
scaley = scaleScale(scaley);
if (shader!=activeProgram) {
GL20.glUseProgram(shader);
projectionMatrixLocation = GL20.glGetUniformLocation(shader, "projectionMatrix");
viewMatrixLocation = GL20.glGetUniformLocation(shader, "viewMatrix");
modelMatrixLocation = GL20.glGetUniformLocation(shader, "modelMatrix");
activeProgram = shader;
}
modelAngle.z = angle;
modelPos.x = x;
modelPos.y = y;
modelScale.x = scalex;
modelScale.y = scaley;
viewMatrix = new Matrix4f();
modelMatrix = new Matrix4f();
Matrix4f.translate(cameraPos, viewMatrix, viewMatrix);
Matrix4f.translate(modelPos, modelMatrix, modelMatrix);
Matrix4f.scale(modelScale, modelMatrix, modelMatrix);
Matrix4f.rotate(this.degreesToRadians(modelAngle.z), new Vector3f(0, 0, 1), modelMatrix, modelMatrix);
//Matrix4f.rotate(this.degreesToRadians(modelAngle.y), new Vector3f(0, 1, 0), modelMatrix, modelMatrix);
//Matrix4f.rotate(this.degreesToRadians(modelAngle.x), new Vector3f(1, 0, 0), modelMatrix, modelMatrix);
projectionMatrix.store(matrix44Buffer); matrix44Buffer.flip();
GL20.glUniformMatrix4(projectionMatrixLocation, false, matrix44Buffer);
viewMatrix.store(matrix44Buffer); matrix44Buffer.flip();
GL20.glUniformMatrix4(viewMatrixLocation, false, matrix44Buffer);
modelMatrix.store(matrix44Buffer); matrix44Buffer.flip();
GL20.glUniformMatrix4(modelMatrixLocation, false, matrix44Buffer);
if (texture!=activeTexture) {
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
activeTexture = texture;
}
GL11.glDrawElements(GL11.GL_TRIANGLES, indicesCount, GL11.GL_UNSIGNED_BYTE, 0);
}
public void endRendering() {
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0);
GL20.glUseProgram(0);
}
private float scaleCoords(float scale) {
if (scale!=0) {
scale=scale/50;
}
scale--;
return scale;
}
private float scaleScale(float scale) {
if (scale!=0) {
scale=scale/50;
}
return scale;
}