Hey guys, iam very new to opengl coding, and i have problem with rotating my simply generated triangle. When i rotate triangle, it deforms its size(base of a triangle gets narrower, when its rotated for 90.0 or -90.0 degrees) also when i have width and height of screen bigger(for example 1400x1000), triangle rotates slower, then when screen is smaller(for example 800x600), its rotates quicker. Can somobedoy help me out?
Rotating code:
float vertices[] = {
-0.5f, 0.5f, // Top-left
0.5f, 0.5f, // Top-right
0.0f, -0.0f, // Bottom-right
};
GLuint VAO;
GLCall(glGenVertexArrays(1, &VAO));
GLCall(glBindVertexArray(VAO));
GLuint VBO;
GLCall(glGenBuffers(1, &VBO));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, VBO));
GLCall(glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), nullptr, GL_DYNAMIC_DRAW));
GLCall(glEnableVertexAttribArray(0));
GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0));
float degree = 0.01f;
bool quit = false;
while (!quit) {
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
GLCall(glClear(GL_COLOR_BUFFER_BIT));
GLCall(glDrawArrays(GL_TRIANGLES, 0, 3));
glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(degree), glm::vec3(0.0f, 0.0f, 1.0f));
for (int i = 0; i < 3; i += 2) {
glm::vec4 vertex(vertices[i], vertices[i + 1], 0.0f, 1.0f);
vertex = rotationMatrix * vertex;
vertices[i] = vertex.x;
vertices[i + 1] = vertex.y;
}
Shader vertex code:
#shader vertex
#version 330 core
layout(location = 0) in vec2 position;
uniform mat4 u_ProjectionMatrix;
void main()
{
gl_Position = u_ProjectionMatrix * vec4(position.x, position.y, 0.0, 1.0);
};
Btw. the u_ProjectionMatrix is keeping content on the same place, in case a resize window.
Function which handles window resize:
void changeViewport(int w, int h, int wStart, int wHeight, ShaderProgram& shaderProgram) {
glViewport(0, 0, w, h);
float wRatio = (float)w / wStart;
float hRatio = (float)h / wHeight;
// Pomocí nového poměru můžete vytvořit projekční matici pro zachování stejného zobrazení.
glm::mat4 projection = glm::ortho(-wRatio, wRatio, -hRatio, hRatio, -1.0f, 1.0f);
shaderProgram.SetUniformMatrix4fv("u_ProjectionMatrix", 1, GL_FALSE, glm::value_ptr(projection));
}
Thanks for the help guys.