OpenGL泛型错误1282,发布绘图GL_QUADS

时间:2015-12-31 01:47:13

标签: java opengl lwjgl

我试图用Java中的LGJWL(Java的原生OpenGL绑定)绘制红色正方形。我从OpenGL收到1282错误,我的方块没有出现。

以下是(我相信的)我尝试做的基本实现。

// begin drawing quads
glBegin(GL11.GL_QUADS);

// set color
glColor4d(255, 0, 0, 255);

// draw rectangle vertices
glVertex2i(0, 0);
glVertex2i(100, 0);
glVertex2i(100, 100);
glVertex2i(0, 100);

glEnd();

以下是我在实践中尝试做的事情。

public class WindowComponent extends Component {
    /** Window color */
    private Color color = new Color();

    /**
     * Sets the window's color
     * @param r red
     * @param b blue
     * @param g green
     * @param a alpha
     */
    public void setColor(int r, int g, int b, int a) {
        color.set(r, g, b, a);
    }

    /**
     * @see Component#renderComponent()
     */
    @Override
    protected void renderComponent() {
        // begin drawing quads
        begin(GL11.GL_QUADS);

        // set color
        GL11.glColor4ub(color.getRedByte(), color.getGreenByte(), color.getBlueByte(), color.getAlphaByte());

        // draw rectangle vertices
        GL11.glVertex2i(0, 0);
        GL11.glVertex2i(getWidth(), 0);
        GL11.glVertex2i(getWidth(), getHeight());
        GL11.glVertex2i(0, getHeight());

        end();
    }
}

是...的扩展名

public abstract class Component {
    /** Dimension of component */
    private Dimension size = new Dimension();
    /** Position of component */
    private Vector2f position = new Vector2f();

    /**
     * Gets width
     * @return width
     */
    public int getWidth() {
        return size.getWidth();
    }
    /**
     * Gets height
     * @return height
     */
    public int getHeight() {
        return size.getHeight();
    }
    /**
     * Gets size
     * @return size
     */
    public Dimension getSize() {
        return size;
    }

    /**
     * Gets X position
     * @return X position
     */
    public float getX() {
        return position.getX();
    }
    /**
     * Gets Y position
     * @return Y position
     */
    public float getY() {
        return position.getY();
    }
    /**
     * Gets position vector
     * @return position vector
     */
    public Vector2f getPosition() {
        return position;
    }

    /**
     * Sets width
     * @param width width
     */
    public void setWidth(int width) {
        size.setWidth(width);
    }
    /**
     * Sets height
     * @param height height
     */
    public void setHeight(int height) {
        size.setHeight(height);
    }
    /**
     * Sets size
     * @param width width
     * @param height height
     */
    public void setSize(int width, int height) {
        size.setSize(width, height);
    }

    /**
     * Sets X position
     * @param x X position
     */
    public void setX(float x) {
        position.setX(x);
    }

    /**
     * Sets Y position
     * @param y Y position
     */
    public void setY(float y) {
        position.setY(y);
    }

    /**
     * Sets position
     * @param x X position
     * @param y Y position
     */
    public void setPosition(float x, float y) {
        position.set(x, y);
    }

    /**
     * Begins drawing and does parent translations
     * @param mode drawing mode
     */
    protected void begin(int mode) {
        GL11.glDisable(GL11.GL_TEXTURE_2D);
        GL11.glBegin(mode);
        GL11.glTranslatef(getX(), getY(), 0);
    }

    /**
     * Ends drawing and does parent translations
     */
    protected void end() {
        GL11.glTranslatef(-getX(), -getY(), 0);
        GL11.glEnd();
        GL11.glEnable(GL11.GL_TEXTURE_2D);
    }

    /**
     * Applies pre-rendering checks then renders the component
     */
    public void render() {
        renderComponent();
    }

    /**
     * Component rendering code here
     */
    protected abstract void renderComponent();
}

我正在用...初始化这个...

    WindowComponent window = new WindowComponent();
    window.setSize(100, 100);
    window.setPosition(100, 100);
    window.setColor(255, 0, 0, 255);
    window.render();

1 个答案:

答案 0 :(得分:2)

begin

GL11.glBegin(mode);
GL11.glTranslatef(getX(), getY(), 0);

您无法在glTranslateglBegin之间致电glEnd;把它移到以前。