I have a problem with projection matrix in opengl. I use LWJGL 2.9.3, with java 7 and when i draw a quad without the projection matrix, it works but when i draw with the projection matrix, nothing happens.
Here's my code : for create projection matrix :
private void createProjectionMatrix()
{
float aspectRatio = (float) Display.getWidth() / (float) Display.getHeight();
float y_scale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio);
float x_scale = y_scale / aspectRatio;
float frustum_length = FAR_PLANE - NEAR_PLANE;
projectionMatrix = new Matrix4f();
projectionMatrix.m00 = x_scale;
projectionMatrix.m11 = y_scale;
projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
projectionMatrix.m23 = -1;
projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
projectionMatrix.m33 = 0;
}
for render quad :
public void renderVAOObject(VAOObject object)
{
GL30.glBindVertexArray(object.getVaoID());
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, object.getIndexBufferId());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2);
GL11.glDrawElements(drawMode, object.getVerticesCount(), GL11.GL_UNSIGNED_INT, 0);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}
public void render(GameObject object, BasicShader shader)
{
shader.bind();
TexturedModel model = object.getModel();
VAOObject vao = model.getVAOObject();
model.getTexture().bind();
Matrix4f transform = MatrixUtils.createTransformationMatrix(object.getPosition(), object.getRotation(), object.getScale());
shader.loadTransformationMatrix(transform);
renderVAOObject(vao);
model.getTexture().unbind();
shader.unbind();
}
(VAOObject is just a class for a vertex array buffer)
and my vertex shader :
#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in vec4 color;
layout(location = 2) in vec2 textureCoords;
out vec4 vColor;
out vec2 vTextureCoords;
uniform mat4 transform;
uniform mat4 projectionMatrix;
void main()
{
vColor = color;
vTextureCoords = textureCoords;
gl_Position = projectionMatrix * transform * vec4(position, 1);
}
If you want more code or precisions, say it.