在Opengl中移动了很多形状

时间:2014-10-30 01:34:06

标签: c++ opengl

this post中,矩形通过鼠标移动。我想添加一个三角形并通过鼠标移动像矩形。

三角函数如下:

void drawTriangle(float x,float y,float size){
glPushMatrix();
glTranslatef( x, y, 0.0f );
glScalef( size, size, 1.0f );
glBegin( GL_TRIANGLES );
glColor3ub( 255, 255, 255 );
glVertex2f(  -1,  1 );
glVertex2f(  1, -1 );
glVertex2f(  1,  1 );
glEnd();
glPopMatrix();

矩形和三角形一起移动。但我想把它与众不同。那我错了什么?

1 个答案:

答案 0 :(得分:1)

你必须维护一个Shape对象数组并测试每个对象的鼠标碰撞,并跟踪 形状你正在拖动:

#include <GL/glut.h>
#include <vector>

using namespace std;

struct Shape
{
    float mX, mY;
    float mSize;
    bool mIsRectangle;

    bool PointInside( const float x, const float y ) const
    {
        return
            mX - mSize <= x && x <= mX + mSize
            &&
            mY - mSize <= y && y <= mY + mSize;
    }
};

vector< Shape > objects;
Shape* dragging = NULL;
void mouse( int button, int state, int x, int y )
{
    if( GLUT_DOWN == state )
    {
        dragging = NULL;
        for( Shape& obj : objects )
        {
            if( obj.PointInside( x, y ) )
            {
                dragging = &obj;
                glutPostRedisplay();
                break;
            }
        }
    }
    else
    {
        dragging = NULL;
    }
}

void motion( int x, int y )
{
    if( dragging )
    {
        dragging->mX = x;
        dragging->mY = y;
        glutPostRedisplay();
    }
}

void drawRect( float x, float y, float size )
{
    glPushMatrix();
    glTranslatef( x, y, 0.0f );
    glScalef( size, size, 1.0f );
    glBegin( GL_QUADS );
    glColor3ub( 255, 255, 255 );
    glVertex2f( -1, -1 );
    glVertex2f(  1, -1 );
    glVertex2f(  1,  1 );
    glVertex2f( -1,  1 );
    glEnd();
    glPopMatrix();
}

void drawTriangle( float x, float y, float size )
{
    glPushMatrix();
    glTranslatef( x, y, 0.0f );
    glScalef( size, size, 1.0f );
    glBegin( GL_TRIANGLES );
    glColor3ub( 255, 255, 255 );
    glVertex2f(  -1,  1 );
    glVertex2f(  1, -1 );
    glVertex2f(  1,  1 );
    glEnd();
    glPopMatrix();
}

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

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

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    for( const Shape& obj : objects )
    {
        if( obj.mIsRectangle )
            drawRect( obj.mX, obj.mY, obj.mSize );
        else
            drawTriangle( obj.mX, obj.mY, obj.mSize );
    }

    glutSwapBuffers();
}

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

    Shape temp;
    temp.mSize = 50;

    temp.mX = temp.mY = 100;
    temp.mIsRectangle = true;
    objects.push_back( temp );

    temp.mX = temp.mY = 200;
    temp.mIsRectangle = false;
    objects.push_back( temp );

    glutMainLoop();
    return 0;
}