Hello guys, iam rendering text in opengl using code on the first link when searching for “text rendering opengl”(learnopengl com). In this code is used this vertex shader:
#version 330 core
layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex>
out vec2 TexCoords;
uniform mat4 projection;
void main()
{
gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);
TexCoords = vertex.zw;
}
… how can i make text staying at same position when resizing? i already correctly created vertex shaders for other things then text, and those shaders correctly keeps same positions when resizing, but with this shader its not the case. I tried this code:
#shader vertex
#version 330 core
layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex>
out vec2 TexCoords;
uniform mat4 normalizeOrtho; // normalizing position
uniform mat4 projection3; //correctly correcting text size, so its doesnt shrink for example
uniform vec2 u_Ratio; //should have keep text on same position when resizing
void main()
{
gl_Position = projection3 * normalizeOrtho * vec4(vertex.x * u_Ratio.x, vertex.y * u_Ratio.y, 0.0, 1.0);
TexCoords = vertex.zw;
}
//projection values:
float wRatio = (float)nowW / startW;
float hRatio = (float)nowH / startH;
glm::mat4 projection = glm::ortho(-wRatio, wRatio, -hRatio, hRatio, -1.0f, 1.0f);
glm::mat4 normalizeOrtho = glm::ortho(0.0f, static_cast<float>(nowW), 0.0f, static_cast<float>(nowH));
GLCall(textFlexibleShaderProgram->SetUniform2f("u_Ratio", wRatio, hRatio));
GLCall(textFlexibleShaderProgram->SetUniformMatrix4fv("normalizeOrtho", 1, GL_FALSE, glm::value_ptr(normalizeOrtho)));
GLCall(textFlexibleShaderProgram->SetUniformMatrix4fv("projection3", 1, GL_FALSE, glm::value_ptr(projection)));
problem is, that when i resize screen to left or right, text is correctly staying on its positions(what i mean by it, is that when text is rendered in the middle of the screen, text will be still in the middle of the screen after resizing), but when i resize screen up or down, text is moving to direction i resize to. I also tried to flip all kind of y values with “-”, but nothing works. Does anybody have idea how to solve it? Iam already looking into it for a long time and iam desperate. Thank you very much.