I am trying to write a function that draws an image, given screen coordinates, and it places it in world coordinates in OpenGL.
glm::vec3 CImage::showImage(float x, float y)
{
float screenW = 1920.0f;
float screenH = 1080.0f;
float xPosition = x;
float yPosition = y;
xPosition = xPosition * 2 / screenW - 1;
yPosition = yPosition * (-2 / screenH) + 1;
glm::vec4 viewport = glm::vec4(0.0f, 0.0f, screenW, screenH);
glm::mat4 projectionMatrix = glm::ortho(0.0f, screenW, screenH, 0.0f, -1.0f, 1.0f);
glm::mat4 model = glm::mat4(1.0f);
glm::vec2 pixel_pos = glm::vec2(xPosition, screenH - yPosition);
glm::vec3 world = glm::unProject(glm::vec3(pixel_pos, 0), model, projectionMatrix, viewport);
return world;
}
been called by :
glm::mat4 transform = glm::mat4(1.0f);
transform = glm::translate(transform, showImage(100, 150));//screen coordinates
glUseProgram(shaderProgram);
unsigned int transformLoc = glGetUniformLocation(shaderProgram, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
This is the vertex shader :
#version 330
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
uniform mat4 transform;
void main()
{
gl_Position = transform * vec4(aPos, 1.0);
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
};
This is the fragment shader :
#version 330
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D texture_sampler;
void main()
{
FragColor = texture(texture_sampler, TexCoord);
};
The result is never the same depending on the image size, it kind of works if I place it at the middle (960, 540), but when I try to display at the left/upper corner or right/lower corner, it is a mess.
I know there are several of these questions around (glm::unproject
) but been trying for several days to solve it.