如何使用OpenGL在中心的按钮上绘制方形?

时间:2016-12-25 16:14:06

标签: c++ opengl

如何在中心的按钮上画一个正方形(如停止按钮)?

在这段代码中我尝试它就像一个矩形并且充满了按钮:

void ButtonDraw(Button *b)
    {
        if(b)
        {
            /*
             *  We will indicate that the mouse cursor is over the button by changing its
             *  colour.
             */
            if (b->highlighted) 
                glColor3f(0.7f,0.7f,0.8f);
            else 
                glColor3f(0.6f,0.6f,0.6f);
            /*
             *  draw background for the button.
             */
            glBegin(GL_QUADS);
                glVertex2i( b->x     , b->y      );
                glVertex2i( b->x     , b->y+b->h );
                glVertex2i( b->x+b->w, b->y+b->h );
                glVertex2i( b->x+b->w, b->y      );
            glEnd();

           /*draw red square on the button*/
            glBegin(GL_QUADS);
            glColor3f(1.0f, 0.0f, 0.0f);
            glVertex2i(b->x, b->y);
            glVertex2i(b->x+b->w, b->y);
            glVertex2i(b->x+b->w, b->y + b->h);
            glVertex2i(b->x, b->y + b->h);
            glEnd();

            /*
             *  Draw an outline around the button with width 3
             */
            glLineWidth(3);


        }
    }

1 个答案:

答案 0 :(得分:1)

显而易见的方法是不要绘制与按钮大小相同的红色矩形,而是绘制一个正方形。以下是执行此操作的最基本方法:

public class Main {

    public static void main(String[] args) {
        AgentWrapper agentWrapper = new AgentWrapper(new Agent1(), false, 1);
        System.out.println("isWalkable: " + agentWrapper.isWalkable());
        System.out.println("position: " + agentWrapper.getPosition());
        agentWrapper.getAgent().doSomething();
    }
}

interface Agent {
    void doSomething();
}

class Agent1 implements Agent {

    public void doSomething() {
        System.out.println("Agent1");
    }
}

class Agent2 implements Agent {

    public void doSomething() {
        System.out.println("Agent1");
    }
}

class AgentWrapper {

    private final Agent agent;  //Wrapped instance.
    private final boolean isWalkable;
    private final int position;

    public AgentWrapper(Agent agent, boolean isWalkable, int position) {
        this.agent = agent;
        this.isWalkable = isWalkable;
        this.position = position;
    }

    public Agent getAgent() {
        return agent;
    }

    public boolean isWalkable() {
        return isWalkable;
    }

上面的代码计算按钮的中间,然后围绕它绘制一个正方形。对于生产代码,您当然要检查您是否与按钮的边缘重叠,并使用浮动来处理舍入问题等。