[英]How do I convert pixel coordinates to opengl coordinates In my 2d game that uses glm::ortho?
我一直在尝试找出如何将像素坐标转换为opengl坐标,以便可以在游戏中使用鼠标进行某些操作,尝试过很多操作,但每次都失败,这是我的代码:
glm::mat4 anim;
glm::mat4 model;
model = glm::translate(glm::mat4(1.0f), glm::vec3(-camerax + temp_sprites[id].xpos, -cameray + temp_sprites[id].ypos, temp_sprites[id].zindex));
anim = glm::rotate(glm::mat4(1.0f), glm::radians(0.0f), axis_y);
model *= glm::scale(glm::mat4(1.0f), glm::vec3(temp_sprites[id].width, temp_sprites[id].height, 0.0f));
glm::mat4 projection = glm::ortho(-50.0f * gaspect, 50.0f * gaspect, -50.0f, 50.0f, -1.0f, 1.0f);
mvp = projection * model * anim;
glUniformMatrix4fv(uniform_mvp, 1, GL_FALSE, glm::value_ptr(mvp));
你需要知道表面的像素宽度和高度surfaceWidth
和surfaceHeight
并且在像素的左上角的坐标(如果不是零) topLeftX
和topLeftY
。
根据您的直拨电话,界限如下:
float left = -50.0f * gaspect;
float right = 50.0f * gaspect;
float bottom = -50.0f;
float top = 50.0f;
如果您有一个像素坐标mouseX
, mouseY
,请减去左上角,然后像这样计算GL坐标:
float x = (((mouseX - topLeftX) / surfaceWidth) * (right - left)) + left;
float y = (((mouseY - topLeftY) / surfaceHeight) * (bottom - top)) + top;
这假定mouseY在屏幕上向下增加(OpenGL坐标在屏幕上向上增加)。
(这是用于身份模型转换的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.