Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: (LWJGL)openGL arraylist rendering problem

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Red face (LWJGL)openGL arraylist rendering problem

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

    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.
    Last edited by DanielJamesCollier; April 5th, 2012 at 06:43 AM.


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Location
    Missouri, United States
    Posts
    17
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default Re: (LWJGL)openGL arraylist rendering problem

    Based on this LWJGL tutorial, it appears you are missing this line:
    // 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.
    Last edited by Gigggas; April 7th, 2012 at 05:32 PM.

  3. The Following User Says Thank You to Gigggas For This Useful Post:

    DanielJamesCollier (April 7th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: (LWJGL)openGL arraylist rendering problem

    thanks for the input but this doesnt seem to work :/

  5. #4
    Junior Member
    Join Date
    Apr 2012
    Location
    Missouri, United States
    Posts
    17
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default 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:
    //Add this to beginning of draw() method
    GL11.glPushMatrix();
    ....
    //Add this to end of draw() method
    GL11.glPopMatrix();

    I hope that works!

  6. The Following User Says Thank You to Gigggas For This Useful Post:

    DanielJamesCollier (April 10th, 2012)

  7. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: (LWJGL)openGL arraylist rendering problem

    darn it, doesnt seem to work either not having much luck :/ thanks thought

  8. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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)

  9. The Following User Says Thank You to copeg For This Useful Post:

    DanielJamesCollier (April 11th, 2012)

Similar Threads

  1. [SOLVED] Problem with OpenGL via LWJGL
    By paulo.carabuena in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 6th, 2011, 02:52 PM
  2. about making rubick with openGL
    By crimx in forum Java Theory & Questions
    Replies: 0
    Last Post: December 6th, 2011, 10:20 AM
  3. OpenGL Texture won't draw
    By nivangerow in forum Android Development
    Replies: 1
    Last Post: November 26th, 2011, 06:03 AM
  4. 3D picking in opengl
    By eshvartz in forum Java Theory & Questions
    Replies: 0
    Last Post: November 21st, 2011, 03:52 AM
  5. Problem in JSP rendering
    By srkumarj2ee@gmail.com in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: September 13th, 2009, 02:26 PM

Tags for this Thread