我正在为游戏引擎的地图编辑器开发基本的GUI。一切正常,除了程序仅将某些四边形着色为我想要的颜色(灰色),并将其他四边形着色为黑色,我不知道为什么。
这是问题的图片。所有按钮(均为方形)应为灰色,但不是。



这是我绘制它们的代码。

import static org.lwjgl.opengl.GL11.*;

import java.awt.Font;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.EventObject;
import java.util.List;

import org.newdawn.slick.Color;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.opengl.Texture;

public class MapEditorUI
{
private int cols;
private float dHeight, dWidth, bHeight, bWidth;
private Color backgroundColor;
private Vector2f startPos;
private Vector2f borderStartPos;
private GUI editorGUI = new GUI();

public GUI.Element clickedElement;

MapEditorUI()
{
    //int width = 70, xStart = 800, yStart = 330, height = 20;

    editorGUI.addElement(0, new Vector2f(910, 330), new Vector2f(1020, 350), "Fill Layer");
    editorGUI.addElement(0, new Vector2f(910, 360), new Vector2f(1020, 380), "Clear Layer");

    editorGUI.addElement(0, new Vector2f(800, 330), new Vector2f(870, 350), "Ground");
    editorGUI.addElement(0, new Vector2f(800, 360), new Vector2f(870, 380), "Mask");
    editorGUI.addElement(0, new Vector2f(800, 390), new Vector2f(870, 410), "Mask2");
    editorGUI.addElement(0, new Vector2f(800, 420), new Vector2f(870, 440), "Fringe");
    editorGUI.addElement(0, new Vector2f(800, 450), new Vector2f(880, 470), "Fringe2");


}

public void initUI(int cols, Color color)
{
    this.cols = cols;
    this.backgroundColor = color;
}

public void setLoc(float startX, float startY, float width, float height)
{ startPos = new Vector2f(startX, startY); dHeight = height; dWidth = width; }
public void setBorderLocs(float borderStartX, float borderStartY, float width, float height)
{ borderStartPos = new Vector2f(borderStartX, borderStartY); bWidth = width; bHeight = height; }

public void draw()
{
    glDisable(GL_TEXTURE_2D);

    drawBackground();
    drawBorder();
    editorGUI.draw();
}

public boolean checkMouseInUI(float x, float y)
{
    if(x > startPos.x && y > startPos.y)
        return true;
    else if(x < borderStartPos.x && x > borderStartPos.x + bWidth)
        return true;

    return false;
}

public int doMouseActions(float x, float y)
{
    clickedElement = editorGUI.checkClick(x, y);
    if(clickedElement != null) // Something was actually clicked
    {
//          System.out.println("CLICKED: " + clickedElement.text);
        return 0; // Element Clicked, have main class handle it from there
    }
    return -1;
}

private void drawBorder()
{
    backgroundColor.bind();
    glBegin(GL_QUADS);
        glVertex2f(borderStartPos.x, borderStartPos.y);
        glVertex2f(borderStartPos.x + bWidth, borderStartPos.y);
        glVertex2f(borderStartPos.x + bWidth, borderStartPos.y + bHeight);
        glVertex2f(borderStartPos.x, borderStartPos.y + bHeight);
    glEnd();
}

private void drawBackground()
{
    backgroundColor.bind();
    glBegin(GL_QUADS);
        glVertex2f(startPos.x, startPos.y);
        glVertex2f(startPos.x + dWidth, startPos.y);
        glVertex2f(startPos.x + dWidth, startPos.y + dHeight);
        glVertex2f(startPos.x, startPos.y + dHeight);
    glEnd();
}


class GUI
{
    private Text guiText = new Text();
    private List<Element> Elements = new ArrayList<Element>();

    GUI()
    {
        guiText.init("Courier", Font.PLAIN, 16);
    }

    public Element checkClick(float x, float y)
    {
        for(Element i : Elements)
        {
            if(i != null)
            {
                if(i.checkClick(x, y) == true)
                {
                    i.clicked();
                    return i;
                }
            }
        }

        return null;
    }

    public void draw()
    {
        for(Element i : Elements)
        {
            if(i != null)
                i.draw();
        }
    }

    public void addElement(int type, Vector2f startPos, Vector2f endPos, String text)
    {Elements.add(new Element(type, startPos, endPos, text));}
    public void addElement(int type, Vector2f startPos, Vector2f endPos, String text, Color buttonBackgroundColor)
    {Elements.add(new Element(type, startPos, endPos, text, buttonBackgroundColor));}

    class Element
    {
        public int type;
        private Vector2f startPos, endPos;
        public String text;
        private byte state;
        private long timeClick;
        private Color buttonColor;

        Element(int type, Vector2f startPos, Vector2f endPos, String text)
        {this.type = type; this.startPos = startPos; this.endPos = endPos; this.text = text;}
        Element(int type, Vector2f startPos, Vector2f endPos, String text, Color buttonColor)
        {this.type = type; this.startPos = startPos; this.endPos = endPos; this.text = text; this.buttonColor = buttonColor;}

        public void draw()
        {
            checkTime();

            switch(type)
            {
            case 0: // Button

                switch(state)
                {
                case 0: // Unclicked
//                      if(buttonColor != null)
//                          buttonColor.bind();
//                      else
                        Color.gray.bind();
                    break;

                case 1: // Clicked
//                      if(buttonColor != null)
//                          new Color(buttonColor.r-50, buttonColor.g-50, buttonColor.b-50).bind();
//                      else
                        Color.red.bind();
                    break;
                }

//                  Color.green.bind();


//                  System.out.println(glGetFloat(GL_CURRENT_COLOR)[2]);
                float Minv[]=new float[16];
                ByteBuffer temp = ByteBuffer.allocateDirect(64);
                temp.order(ByteOrder.nativeOrder());
                glGetFloat(GL_MODELVIEW_MATRIX, (FloatBuffer)temp.asFloatBuffer());
                temp.asFloatBuffer().get(Minv);

                for(int i = 0; i < 16; ++i)
                    System.out.println(text + "| " + i + ": " + Minv[i]);

                glBegin(GL_QUADS);
                    glVertex2f(startPos.x, startPos.y);
                    glVertex2f(endPos.x, startPos.y);
                    glVertex2f(endPos.x, endPos.y);
                    glVertex2f(startPos.x, endPos.y);
                glEnd();

                guiText.drawString(startPos.x + 2, startPos.y - 2, text, Color.white);
                glDisable(GL_BLEND);
                break;
            }
        }

        private void checkTime()
        {
            long curTime = System.currentTimeMillis();
            long timePassed = curTime - timeClick;

            if(timePassed > 50)
                state = 0;
        }

        public boolean checkClick(float x, float y)
        {
            if((x > startPos.x && y > startPos.y) && (x < endPos.x && y < endPos.y))
            {
                return true;
            }

            return false;
        }

        public void clicked()
        {
            timeClick = System.currentTimeMillis();
            state = 1; // Clicked
        }

        public void setType(int type)
        {this.type = type;}

        public void setButtonColor(Color buttonColor)
        {this.buttonColor = buttonColor;}
    }
}
}


这是我的drawString代码:

public void drawString(float x, float y, String text, Color color)
{
    // Make sure alpha blending is enabled, it might not be
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    font.drawString(x, y, text, color);
}

最佳答案

您必须在glEnable(GL_TEXTURE_2D);之前输入font.drawString();,然后在其后输入glDisable(GL_TEXTURE_2D);

10-02 23:39