Sunday 15 August 2010

java - LWJGL OpenGL projection matrix -



java - LWJGL OpenGL projection matrix -

i using projection matrix found on lwjgl wiki vbo cube.

it works fine part, when comes think aspect ratio, things start acting funny.

my app window resolution 640 480. when run programme happens cube...

the face see should square, instead, seems getting stretched aspect ratio.

here code projection matrix.

public static matrix4f perspective(float aspect, float fov, float near, float far) { matrix4f matrix = new matrix4f(); float y_scale = cotan(degtorad(fov/2f)); float x_scale = y_scale / aspect; float frustum = far-near; matrix.m00 = x_scale; matrix.m11 = y_scale; matrix.m22 = -((far+near)/frustum); matrix.m23 = -1; matrix.m32 = -((2 * far * near)/frustum); matrix.m33 = 0; homecoming matrix; }

here modify , translate matrices.

matrix4f.translate(camera.position, camera.matrix(camera.viewmatrix), matrix4f.scale(obj.scale, obj.matrix(), obj.matrix()); matrix4f.translate(obj.position, obj.matrix(), obj.matrix()); matrix4f.rotate(degtorad(obj.rotation.x), new vector3f(1f,0f,0f), obj.matrix(), obj.matrix()); matrix4f.rotate(degtorad(obj.rotation.y), new vector3f(0f,1f,0f), obj.matrix(), obj.matrix()); matrix4f.rotate(degtorad(obj.rotation.z), new vector3f(0f,0f,1f), obj.matrix(), obj.matrix());

obj.matrix() modelview matrix obj.rotation, object.position, , object.scale vector3f's camera.matrix(matrix type) either homecoming projection matrix or view matrix

p.s. degtorad , cotan methods similar math.toradians , math.tan

and here vertex shader.

#version 150 core in vec4 in_position; in vec4 in_color; in vec2 in_texturecoord; uniform mat4 projection; uniform mat4 view; uniform mat4 model; out vec4 pass_color; out vec2 pass_texturecoord; void main(void) { gl_position = projection * view * model * in_position; pass_color = in_color; pass_texturecoord = in_texturecoord; }

leave comment if need other parts of code.

java opengl glsl lwjgl projection-matrix

No comments:

Post a Comment