为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
Why are my points are not drawing where my mouse is in OpenGL?
除非我正好是窗口高度的一半,否则我的点不会画出鼠标所在的位置。如果我在这半程线以下,它将画在这条线之上,如果我在上面,它将画在下面。
附上一个 GIF,以防不清楚:https://gyazo.com/407d679430c70c3235d9e5c43e521347
我希望我的坐标系从屏幕的左下角开始(以及鼠标所在的绘制点(。如果有帮助的话,我正在Ubuntu的VirtualBox实例上进行开发。
#include <iostream>
#include <vector>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "Point.h"
const int w = 640;
const int h = 480;
// Prototypes
void display(void);
void myInit(void);
void myMouse(int button, int state, int x, int y);
// Globals
std::vector<Point> points;
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(w, h);
glutInitWindowPosition(0, 100);
glutCreateWindow("My First Attempt");
glutDisplayFunc(display);
glutMouseFunc(myMouse);
myInit();
glutMainLoop();
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
// for each point in points, draw a point
for(Point p : points)
glVertex2i(p._x,p._y);
glEnd();
glFlush();
}
void myMouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
Point p;
p._x = x;
p._y = y;
points.push_back(p);
std::cout << p._x << " " << p._y << std::endl;
glutPostRedisplay();
}
if(button == GLUT_LEFT_BUTTON && state == GLUT_UP){
}
}
void myInit(void){
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0,0,0);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f,w, 0,h);
}
您需要 Y 形翻转鼠标坐标或调整投影矩阵以匹配其坐标系:
// broken
gluOrtho2D( 0.0f, w, 0, h );
// works
gluOrtho2D( 0.0f, w, h, 0 );
一起:
#include <iostream>
#include <vector>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
struct Point
{
int _x, _y;
};
const int w = 640;
const int h = 480;
// Prototypes
void display( void );
void myInit( void );
void myMouse( int button, int state, int x, int y );
// Globals
std::vector<Point> points;
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE );
glutInitWindowSize( w, h );
glutInitWindowPosition( 0, 100 );
glutCreateWindow( "My First Attempt" );
glutDisplayFunc( display );
glutMouseFunc( myMouse );
myInit();
glutMainLoop();
}
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT );
glBegin( GL_POINTS );
// for each point in points, draw a point
for( Point p : points )
glVertex2i( p._x, p._y );
glEnd();
glFlush();
}
void myMouse( int button, int state, int x, int y )
{
if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
{
Point p;
p._x = x;
p._y = y;
points.push_back( p );
std::cout << p._x << " " << p._y << std::endl;
glutPostRedisplay();
}
}
void myInit( void )
{
glClearColor( 1.0, 1.0, 1.0, 0.0 );
glColor3f( 0, 0, 0 );
glPointSize( 4.0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0f, w, h, 0 );
}
相关文章:
- 使用 dlib 记录鼠标位置
- 获取窗口中的鼠标位置
- 将文本添加到鼠标位置时,请单击“给出分割故障”
- 在鼠标位置移动精灵
- 如何在Mac OSX上的窗口客户端内获取真实的鼠标位置?
- 如何始终获得QT鼠标位置
- QT QgraphicView和Qgraphicscene鼠标位置
- QT:QGraphicsScene鼠标位置始终(0,0)
- SFML鼠标位置和碰撞从文件加载
- 根据鼠标位置缩小到窗口中
- 如何根据鼠标位置旋转精灵
- OpenGL获取离当前鼠标位置最近的世界三维点的三维坐标
- C++ SDL 根据鼠标位置绘制动态矩形
- 如何检索WM_COMMAND中的鼠标位置
- Qt mouseMoveEvent-跟踪鼠标位置
- OpenGl获取鼠标位置
- 获取RGBDemo的鼠标位置坐标
- 通过互联网发送鼠标位置
- 如何使用可可获得鼠标位置差异的变化
- 鼠标位置并单击 sfml 中的检测