Jake Gabb
Jake Gabb

Reputation: 385

opengl only drawing final quad out of a list of many in java

I'm making a simple 2D game with LWJGL and OpenGl in Java and I can't seem to render more than one quad.

I have an arraylist of Squares in my main class and if I have more than one Square in the list it only renders the last one in the list, failing to render the others (I debugged and printed the Square names, which showed it was iterating through them properly, but just decides not to render any of them but the last for some reason). So the only thing I can think of is that either there's a problem with my Square classes Draw method (since I use that method solely to render my quads), or my OpenGL settings code.

If it helps, my code looks very much like what's in this video since I've been largely following this and the wiki pages on lwjgl: http://www.youtube.com/watch?v=EjbOjio_pC4

Square class:

package dasting;

import org.lwjgl.*; //lwjgl engine
import org.lwjgl.opengl.*; //opengl
import static org.lwjgl.opengl.GL11.*; //Dunno yet, youtube said so
import org.lwjgl.LWJGLException; //Allows tries and catches with exception handling for LWJGL (IMPORTANT SHIT)
import java.util.Random; 

public class Square {

private int x1, x2, y1, y2, roomHeight, roomWidth;

//constructor takes the position values and width and height of room for boundary checks
Square(int X1, int X2, int Y2, int Y1, int rmWidth, int rmHeight) { 
    x1 = X1; //initialising the point values
    x2 = X2;
    y2 = Y2;
    y1 = Y1;
    roomHeight = rmHeight;
    roomWidth = rmWidth;
}

public void draw() { //draw method
    //Rendering random stuff example code. Also moves the square
    glClear(GL_COLOR_BUFFER_BIT);

    //Render quad
    glBegin(GL_QUADS);
        glVertex2i(x1, y1);
        glVertex2i(x2, y1);
        glVertex2i(x2, y2);
        glVertex2i(x1, y2);
    glEnd();
    }
}

And here's the openGl settings I think may be problematic in my main class:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, roomWidth, roomHeight, 0, 1, -1); 
glMatrixMode(GL_MODELVIEW);

Upvotes: 1

Views: 372

Answers (1)

Apples
Apples

Reputation: 3225

You should be calling glClear every frame, not every time you draw a Square. Move the call to glClear out of the draw function and put it at the start of your drawing loop.

Upvotes: 2

Related Questions