Search code examples
javaopengllwjgl

OpenGL generic error 1282, issue drawing GL_QUADS


I'm attempting to draw a red square with LGJWL (native OpenGL bindings for Java) in Java. I receive a 1282 error from OpenGL and my square does not appear.

Below is (what I believe to be) the basic implementation for what I'm trying to do.

// 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();

Following is what I'm trying to do in practice.

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();
    }
}

is an extension of...

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();
}

and I'm initializing this all with...

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

Solution

  • In begin:

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

    You cannot call glTranslate in between glBegin and glEnd; move it to before.