如何在使用glm::ortho的2d游戏中将像素坐标转换为opengl坐标
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;
这假设鼠标Y在屏幕下方增加(OpenGL坐标在屏幕上方增加(。
(这是用于身份模型转换(
相关文章:
- OpenGL 4.3 错误地将第 4 个纹理坐标映射到与第 3 个纹理坐标相同的位置
- 在 OpenGL 中将笛卡尔世界坐标转换为球面局部坐标
- 如何使用OpenGL知道所有顶点的当前坐标
- 在 openGL 中,模型坐标应该在我的 CPU 上计算还是在 GPU 上使用 OpenGL 调用计算?
- 检查刻度中的坐标不起作用(OpenGL)
- 将 OpenGL 坐标系统转换为自定义并返回
- OpenGL屏幕坐标到世界坐标
- 如何将鼠标坐标正确转换为 opengl 坐标?
- 在 OpenGL 3.3 中使用整数坐标绘制点?
- 如何在OpenGL中绘制一组坐标?
- OpenGL 纹理坐标不起作用
- OpenGL从窗口点到3D坐标
- 给定两组 xyz 坐标和一个 sag (a) 的值,我可以在 OpenGL 中的这些点之间绘制一个悬链线
- Opengl 土壤纹理坐标无法识别
- C++OpenGL坐标阵列来绘制线条/边界和填充矩形
- 如何在 opengl 中放置坐标> 100.0f?
- 在OpenGL 2.1中,即使在2D空间(具有2D纹理),也可以安全使用3D TEX坐标
- OpenGL获取离当前鼠标位置最近的世界三维点的三维坐标
- 在球形坐标(OpenGL、C++、GLUT)之间跨步
- 传递纹理坐标(OpenGL)时,不会显示任何内容