I am drawing a 3d cube in LWJGL 3.It renders fine with the window size being 600 * 480.It renders a perfect cube.
But if i increase the window's width to 900 while the height remains the same. The cube have a rectangular shape.
In 640 * 480 resolution the front face of cube is :
In 900 * 480 resolution the front face of cube is :
I want to render a perfect square without it resembling a rectangle.
if required the, The render loop is as follows :
private void loop() {
GL.createCapabilities();
// Set the clear color
glClearColor(0f, 0.0f, 0.0f, 0.0f);
float x = 0.5f;
float y = 0.5f;
float z = 0.5f;
float[] vertices = new float[] {
// VO
-x, y, z,
// V1
-x, -y, z,
// V2
x, -y, z,
// V3
x, y, z,
// V4
-x, y, -z,
// V5
x, y, -z,
// V6
-x, -y, -z,
// V7
x, -y, -z, };
float[] colours = new float[] {
0.5f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f,
0.0f, 0.0f, 0.5f,
0.0f, 0.5f, 0.5f,
0.5f,0.0f, 0.0f,
0.0f, 0.5f, 0.0f,
0.0f, 0.0f, 0.5f,
0.0f, 0.5f, 0.5f, };
int[] indices = new int[] {
// Front face
0, 1, 3, 3, 1, 2,
// Top Face
4, 0, 3, 5, 4, 3,
// Right face
3, 2, 7, 5, 3, 7,
// Left face
6, 1, 0, 6, 0, 4,
// Bottom face
2, 1, 6, 2, 6, 7,
// Back face
7, 6, 4, 7, 4, 5, };
RawModel model = Loader.loadRawModel(vertices, indices, colours);
ShaderProgram shader = null;
Transformation transformation = new Transformation();
try {
shader = new ShaderProgram(Utils.loadTextFile("src/shaders/vertexShader.txt"),
Utils.loadTextFile("src/shaders/fragmentShader.txt"));
shader.link();
shader.createUniform("projectionMatrix");
shader.createUniform("worldMatrix");
} catch (Exception e) {
e.printStackTrace();
}
float rotation = 0;
while (!glfwWindowShouldClose(window)) {
glEnable(GL11.GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.bind();
shader.setUniform("projectionMatrix", transformation.getProjectionMatrix((float) Math.toRadians(60),
(float) (width / height), 0.1f, 1000f));
shader.setUniform("worldMatrix", transformation.getWorldMatrix(new Vector3f(0, 0, -3f),
new Vector3f(rotation, rotation, rotation), 1));
model.render();
shader.unBind();
glfwSwapBuffers(window);
glfwPollEvents();
}
shader.dispose();
Loader.dispose();
}
Thanks to @Derhass the problem is solved now.The problem was that i calculated the aspect ration as :
float aspectRatio = (float)(900/480);
which equals to 1.0f .
But when i added f to both width and height :
float aspectRatio = (float)(900f/480f);
It gave the right aspectRatio
of 1.875.
now the cube appears as square.