Reputation: 41
I have an issue with my 2D Game in C++/OpenGL.
I need to somehow get the pixel coordinates (preferably the glfw coords) of a texture that has been rendered on the screen. The final goal is to be able to click on a texture in the window and get the correct texture name. To be able to perform per pixel collision with the mouse click, I need to know where exactly the texture is located in the window.
However, I am super confused about the various coordinate systems and how to get to the coord form I am looking for.
Can anyone help me on how to get started on this, or did someone maybe even have the same issue as me?
Now to the facts: This is my mvp matrix:
glm::mat4 Game::calculateMvpMatrix(glm::vec3 position, float zRotationInDegrees, glm::vec3 scale){
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, position);
model = glm::scale(model, scale);
model = glm::rotate(model, glm::radians(zRotationInDegrees), glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 90.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f));
int windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
glm::mat4 projection = glm::ortho(0.0f, 8.0f, 0.0f, 4.5f, 0.1f, 100.0f);
return projection * view * model; }
This is how I use it in the shader:
layout (location = 0) in vec3 aPos; ...
uniform mat4 mvp;
void main() {
gl_Position = mvp * vec4(aPos, 1.0); ...
Then I have the vertex positions of the texture in my Renderer:
data.vertexPositions[0] = {0.5f, -0.5f, 0.0f, 1.0f};
data.vertexPositions[1] = {-0.5f, -0.5f, 0.0f, 1.0f};
data.vertexPositions[2] = {-0.5f, 0.5f, 0.0f, 1.0f};
data.vertexPositions[3] = {0.5f, 0.5f, 0.0f, 1.0f};
which I then transform in my Renderer:
void BatchRenderer::DrawQuad(const glm::vec3& position, float rotation, const glm::vec2& size, unsigned int textureID,
const glm::vec4& texColor, float texAlpha)
{
...
glm::mat4 transform = glm::translate(glm::mat4(1.0f), {position.x, position.y, 0.0f}) *
glm::rotate(glm::mat4(1.0f), glm::radians(rotation), {0.0f, 0.0f, 1.0f}) *
glm::scale(glm::mat4(1.0f), {size.x, size.y, 1.0f});
data.quadBufferPtr->Position = transform * data.vertexPositions[0];
data.quadBufferPtr->Color = texColor;
data.quadBufferPtr->TexCoords = {0.0f, 0.0f};
data.quadBufferPtr->TexID = texIndex;
data.quadBufferPtr->TexAlpha = texAlpha;
data.quadBufferPtr++;
...
Does anybody know how I can get the glfw pixel coordinates or even just normalized screen coordinates out of these information?
Upvotes: 0
Views: 69