Model stretches when resizing OpenGL 3.2 window

I installed a window (in MFC) that contains the OpenGL 3.2 rendering context. Since it is with OpenGL 3.2, I want to use shaders ect, so I predict my projection and matrix viewing manually. I used this tutorial as an input to create them and transfer them to my shader.

The problem is that (even in the example tutorial) when you resize my window, the model is stretched.

This is the code that I use to create my matrices (I rebuild them and send them to my shader, each of which is updated).

View Matrix:

float zAxis[3], xAxis[3], yAxis[3]; 
float length, result1, result2, result3;

// zAxis = normal(lookAt - position)
zAxis[0] = lookAt[0] - m_position[0];
zAxis[1] = lookAt[1] - m_position[1];
zAxis[2] = lookAt[2] - m_position[2];
length = sqrt((zAxis[0] * zAxis[0]) + (zAxis[1] * zAxis[1]) + (zAxis[2] * zAxis[2]));
zAxis[0] = zAxis[0] / length;
zAxis[1] = zAxis[1] / length;
zAxis[2] = zAxis[2] / length;

// xAxis = normal(cross(up, zAxis))
xAxis[0] = (up[1] * zAxis[2]) - (up[2] * zAxis[1]);
xAxis[1] = (up[2] * zAxis[0]) - (up[0] * zAxis[2]);
xAxis[2] = (up[0] * zAxis[1]) - (up[1] * zAxis[0]);
length = sqrt((xAxis[0] * xAxis[0]) + (xAxis[1] * xAxis[1]) + (xAxis[2] * xAxis[2]));
xAxis[0] = xAxis[0] / length;
xAxis[1] = xAxis[1] / length;
xAxis[2] = xAxis[2] / length;

// yAxis = cross(zAxis, xAxis)
yAxis[0] = (zAxis[1] * xAxis[2]) - (zAxis[2] * xAxis[1]);
yAxis[1] = (zAxis[2] * xAxis[0]) - (zAxis[0] * xAxis[2]);
yAxis[2] = (zAxis[0] * xAxis[1]) - (zAxis[1] * xAxis[0]);

// -dot(xAxis, position)
result1 = ((xAxis[0] * m_position[0]) + (xAxis[1] * m_position[1]) + (xAxis[2] * m_position[2])) * -1.0f;

// -dot(yaxis, eye)
result2 = ((yAxis[0] * m_position[0]) + (yAxis[1] * m_position[1]) + (yAxis[2] * m_position[2])) * -1.0f;

// -dot(zaxis, eye)
result3 = ((zAxis[0] * m_position[0]) + (zAxis[1] * m_position[1]) + (zAxis[2] * m_position[2])) * -1.0f;

viewMatrix[0]  = xAxis[0];
viewMatrix[1]  = yAxis[0];
viewMatrix[2]  = zAxis[0];
viewMatrix[3]  = 0.0f;

viewMatrix[4]  = xAxis[1];
viewMatrix[5]  = yAxis[1];
viewMatrix[6]  = zAxis[1];
viewMatrix[7]  = 0.0f;

viewMatrix[8]  = xAxis[2];
viewMatrix[9]  = yAxis[2];
viewMatrix[10] = zAxis[2];
viewMatrix[11] = 0.0f;

viewMatrix[12] = result1;
viewMatrix[13] = result2;
viewMatrix[14] = result3;
viewMatrix[15] = 1.0f;

Projection Matrix:

float screenAspect = (float)rcClient.Width() / (float)rcClient.Height();
float fov = 3.14159265358979323846f / 4.0f;
float zfar = 1000.0;
float znear = 0.1f;

projectionMatrix[0]  = 1.0f / (screenAspect * tan( fov * 0.5f));
projectionMatrix[1]  = 0.0f;
projectionMatrix[2]  = 0.0f;
projectionMatrix[3]  = 0.0f;

projectionMatrix[4]  = 0.0f;
projectionMatrix[5]  = 1.0f / tan( fov * 0.5f);
projectionMatrix[6]  = 0.0f;
projectionMatrix[7]  = 0.0f;

projectionMatrix[8]  = 0.0f;
projectionMatrix[9]  = 0.0f;
projectionMatrix[10] = zfar / (zfar - znear);
projectionMatrix[11] = 1.0f;

projectionMatrix[12] = 0.0f;
projectionMatrix[13] = 0.0f;
projectionMatrix[14] = (-znear * zfar) / (zfar - znear);
projectionMatrix[15] = 0.0f;

And in my shader, I use them as follows:

#version 150

in vec3 inputPosition;
in vec3 inputColor;

out vec3 color;

uniform mat4 worldMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

void main(void)
{
    gl_Position = projectionMatrix * viewMatrix * worldMatrix * vec4(inputPosition, 1.0f);

    color = inputColor;
}

Does anyone have any idea what is going on here?

+4
1

glViewport, , .

:

float projectionMatrix[4][4];
projectionMatrix[0][0]  = 1.0f / (screenAspect * tan( fov * 0.5f));
projectionMatrix[0][1]  = 0.0f;
projectionMatrix[0][2]  = 0.0f;
projectionMatrix[0][3]  = 0.0f;

projectionMatrix[1][0]  = 0.0f;
projectionMatrix[1][1]  = 1.0f / tan( fov * 0.5f);
projectionMatrix[1][2]  = 0.0f;
projectionMatrix[1][3]  = 0.0f;

projectionMatrix[2][0]  = 0.0f;
projectionMatrix[2][1]  = 0.0f;
projectionMatrix[2][2] = (-znear -zfar) / (znear-zfar);
projectionMatrix[2][3] = 2.0f * zfar * znear / (znear-zfar);

projectionMatrix[3][0] = 0.0f;
projectionMatrix[3][1] = 0.0f;
projectionMatrix[3][2] = 1.0;
projectionMatrix[3][3] = 0.0f;

UVN, , glViewport. , GLM. , UVN, .. : glm , : http://ogldev.atspace.co.uk/

-1

Source: https://habr.com/ru/post/1530460/


All Articles