c++ - 在 Opengl 中移动大量形状

标签 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();

矩形和三角形一起移动。但我想改变它。那我哪里错了?

最佳答案

您必须维护一个 Shape 对象数组并测试每个对象的鼠标碰撞,并跟踪您拖动的是哪个 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;
}

关于c++ - 在 Opengl 中移动大量形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26643607/

相关文章:

c++ - 不能将 std::unique_ptr<T> 与 T 一起使用为前向声明

c++ - 动态二维数组分配

c++ - OpenGL 游戏的主菜单

c++ - 任何使用 visual studio 2008 环境的基本 C++ 网站/书籍?

c++ - 获取 CAN 比特率

具有多个显示上下文的 OpenGL 纹理

c++ - Assimp 不在 OpenGL 中的 MD5 模型 Bob 的节点中加载骨骼名称

opengl - 多次调度相同的计算着色器

c - OpenGL 应用程序崩溃

c++ - 在 C++ 中的运行时选择模板参数