(LWJGL)openGL arraylist rendering problem
Code :
import static org.lwjgl.opengl.GL11.*;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class Main {
private static final String TITLE = "LWJGL program test";
public static final int WIDTH = 600;
public static final int HEIGHT = 400;
public static boolean running = true;
public List<Box> shapes = new ArrayList<Box>(16);
public Main() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.setTitle(TITLE);
Display.setResizable(false);
Display.create();
} catch (LWJGLException e) {
System.err.println(e);
}
Start();
running = true;
}
public void Start() {
openGL();
init();
render();
}
private void openGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
private void init() {
shapes.add(new Box (15,15));
shapes.add(new Box (100,100));
}
private void keyHandler(){
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
Display.destroy();
System.exit(0);
}
}
public void mouseHandler(){
int mouseX = Mouse.getX();
int mouseY = Mouse.getY();
int mouseDX = Mouse.getDX();
int mouseDY = Mouse.getDY();
if(Mouse.isButtonDown(0)){
//left click listener
}
}
private void render() {
while (!Display.isCloseRequested()) {
for(Box box: shapes){
box.draw();
}
keyHandler();
mouseHandler();
Display.update();
Display.sync(60);
}
if (Display.isCloseRequested()) {
Display.destroy();
System.exit(0);
}
}
public static void main(String[] args) {
new Main();
}
}
Code :
import static org.lwjgl.opengl.GL11.*;
import java.util.Random;
public class Box {
private static float R,G,B;
private static int x, y;
private static Random random = new Random();
public Box(int x, int y){
this.x = x;
this.y = y;
randomColor();
}
public static void randomColor(){
R = random.nextFloat();
G = random.nextFloat();
B = random.nextFloat();
}
public void draw(){
glColor3f(R,G,B);
glBegin(GL_QUADS);
glVertex2i(x,y);
glVertex2i(x + 25,y);
glVertex2i(x + 25,y + 25);
glVertex2i(x,y + 25);
glEnd();
}
}
basicaly i want to render as many boxes as i can (up to 16 as my array list shows) but every time i add a new box it only renders the last box that i created(the box with the integers 100,100), if you need any more info just post in the coments, thanks.
Re: (LWJGL)openGL arraylist rendering problem
Based on this LWJGL tutorial, it appears you are missing this line:
Code Java:
// Clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
....
This code apparently should be at the beginning of the draw() method for your Box class.
Since that method clears the screen and depth buffer, that might be why only the last box you create is rendered. I could be wrong though.
Hope that helps! If not, some of the other tutorials on the LWJGL wiki might have the answer.
Re: (LWJGL)openGL arraylist rendering problem
thanks for the input :) but this doesnt seem to work :/
Re: (LWJGL)openGL arraylist rendering problem
Alright, after examining the rendering code for this Example Java Space Invaders game, I think you need to add the following methods to your Box.Draw() method:
Code Java:
//Add this to beginning of draw() method
GL11.glPushMatrix();
....
//Add this to end of draw() method
GL11.glPopMatrix();
I hope that works!
Re: (LWJGL)openGL arraylist rendering problem
darn it, doesnt seem to work either not having much luck :/ thanks thought
Re: (LWJGL)openGL arraylist rendering problem
I recommend reading up on the static keyword as well as how to make classes. The variables that should describe each instance of a Box - variables that define position and color - are static...meaning you can have as many boxes as you wish, but every one of them will have the same color and location. I presume this is not the behavior you intend (and will result in the behavior you see)