Почему мои точки не рисуются там, где моя мышь находится в OpenGL? - PullRequest
2 голосов
/ 17 февраля 2020

Мои точки не начнут рисовать там, где находится моя мышь, если я не буду точно на половине высоты окна. Если я ниже этой линии на полпути, она будет рисоваться выше линии, если я выше, она будет рисоваться ниже.

В приложении есть GIF, если неясно: https://gyazo.com/407d679430c70c3235d9e5c43e521347

Я бы хотел, чтобы моя система координат начиналась в левом нижнем углу экрана (а также Нарисуйте точки, где моя мышь). Я разрабатываю на VirtualBox экземпляр Ubuntu, если это поможет.

#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);

}

1 Ответ

3 голосов
/ 17 февраля 2020

Вам нужно либо отразить 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 );
}
...