绘制一个矩形的新闻和释放的鼠标,opengl [英] Drawing a rectangle on press and release of mouse, opengl

查看:143
本文介绍了绘制一个矩形的新闻和释放的鼠标,opengl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从用户输入绘制矩形,从左击收集x1,y1坐标,然后指定x2,y2从左击发布。我基于该鼠标动作成功地保存坐标(基于cout<<确认坐标被保存的语句),但是在释放左按钮后不显示任何内容。

I'm trying to draw a rectangle from user input that collects x1,y1 coordinates from left click, and then specifies x2,y2 from release of the left click. I save the coordinates based on this mouse action successfully (based on cout<< statements confirming the coords are saved) but nothing is displayed after the left button is released.

这里是我试过的:(记住,没有崩溃,所以有一个逻辑错误)

Here is what I've tried: (keep in mind, nothing crashes, so there is a logic error)

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glColor3f(1,1,0);
    glBegin(GL_POLYGON);
        glVertex2f(clip_start.x_coord,clip_start.y_coord);
        glVertex2f(clip_finish.x_coord,clip_start.y_coord);
        glVertex2f(clip_finish.x_coord,clip_finish.y_coord);
        glVertex2f(clip_start.x_coord,clip_finish.y_coord);
    glEnd();
    glPopMatrix();
        glutSwapBuffers();
        glutPostRedisplay();
    drawit();
}

void mouse(int button, int state, int x, int y){
    if(c_pressed){
        switch(button){
        case GLUT_LEFT_BUTTON:

            if(state==GLUT_DOWN){
                clip_start.x_coord=x; //x1
                clip_start.y_coord=y; //y1          
                cout<<"Initial pos set"<<endl;                  
            }
            if(state==GLUT_UP){
                clip_finish.x_coord=x; //x2
                clip_finish.y_coord=y; //y2
                cout<<"Final pos set"<<endl;  

            }
            break;  
        }
        clip_ready==true;
    }
}
void keyboard(unsigned char key, int x, int y){
    if(key == 'c'){
        c_pressed = true;
        cout<<"c pressed"<<endl;
    }
}
int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(ImageW,ImageH);
    glutInitWindowPosition(100,100);
    glutCreateWindow("");
    init(); 
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}


推荐答案

GL_PROJECTION 矩阵。我不知道默认是什么(可能的单位矩阵),但它肯定不是 Y翻转,像素尺度坐标系统,你假设在 display )

You didn't set a GL_PROJECTION matrix. I'm not sure what the default is (probably the identity matrix) but it certainly isn't the Y-flipped, pixel-scale coordinate system you are assuming in display().

请尝试这样:

#include <GL/glut.h>

struct Position
{
    Position() : x(0), y(0) {}
    float x, y;
};

Position start, finish;
void mouse( int button, int state, int x, int y )
{
    if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
    {
        start.x = finish.x = x;
        start.y = finish.y = y;
    }
    if( button == GLUT_LEFT_BUTTON && state == GLUT_UP)
    {
        finish.x=x;
        finish.y=y;
    }
    glutPostRedisplay();
}

void motion( int x, int y )
{
    finish.x = x;
    finish.y = y;
    glutPostRedisplay();
}

void display()
{
    glClearColor( 0, 0, 0, 1 );
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    glOrtho( 0, w, h, 0, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glPushMatrix();
    glColor3f(1,1,0);
    glBegin(GL_QUADS);
        glVertex2f(start.x,start.y);
        glVertex2f(finish.x,start.y);
        glVertex2f(finish.x,finish.y);
        glVertex2f(start.x,finish.y);
    glEnd();
    glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,100);
    glutCreateWindow("GLUT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

这篇关于绘制一个矩形的新闻和释放的鼠标,opengl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆