Why is it that when I make the object on my screen move, it leaves a trail?

I’m just studying how to display images on a screen and make it move using a gamepad.

I’ve done that, but for some reason, when the object moves, it leaves a trail where it passed.

Anyone have any idea why? Here’s my code. Thanks!

import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import net.java.games.input.*;

public class FuzzyMover {
	
	Controller[] ca;
	int gp=-1; // To store controller number of the gamepad
	private Texture texture; // The texture that will hold the image details
	float xMove=0, yMove=0, xPos=100,yPos=100;	
	
	public void start(){
		
		/* Get all controller instances attached to the machine */
		ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
		
		/* Get the name of each controller and pick the gamepad out of all controllers */
		for(int i =0;i<ca.length;i++){            
             System.out.println(ca[i].getName());
             System.out.println("Type: "+ca[i].getType().toString());	             
             if(ca[i].getType().toString().indexOf("Gamepad")>-1){
            	 gp=i;
             }
    	}
    	
    	initGL(800, 600); // initialize Open GL
    	init(); // initialize other resources
    	
    	/* Do the following repeatedly until the app window is closed */
    	while (!Display.isCloseRequested()) {       
        	render();            
            pollInput();
            Display.update();
        }
        
        Display.destroy();    	
	}
	
	private void initGL(int width, int height) {
		try {
			Display.setDisplayMode(new DisplayMode(width,height));
			Display.setLocation(43, 12);
			Display.create();
			Display.setVSyncEnabled(true);
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
		}

		GL11.glEnable(GL11.GL_TEXTURE_2D);               
        
		GL11.glClearColor(255.0f, 255.0f, 255.0f, 255.0f);
        
		/* Enable alpha blending */
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);        
        GL11.glViewport(0,0,width,height);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, width, height, 0, 1, -1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
	}
	
	public void render() {
		Color.white.bind();
		texture.bind();
				
		xPos=xPos+xMove;
		yPos=yPos+yMove;
		
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2f(0,0);
		GL11.glVertex2f(xPos,yPos);
		GL11.glTexCoord2f(1,0);
		GL11.glVertex2f(xPos+texture.getImageWidth(),yPos);
		GL11.glTexCoord2f(1,1);
		GL11.glVertex2f(xPos+texture.getImageWidth(),yPos+texture.getImageHeight());
		GL11.glTexCoord2f(0,1);
		GL11.glVertex2f(xPos,yPos+texture.getImageHeight());
		GL11.glEnd();
	}
	
public void init() {		
		try {
			// load texture from PNG file
			texture = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream("img/fuzzy.jpg"));

			System.out.println("Texture loaded: "+texture);
			System.out.println(">> Image width: "+texture.getImageWidth());
			System.out.println(">> Image height: "+texture.getImageHeight());
			System.out.println(">> Texture width: "+texture.getTextureWidth());
			System.out.println(">> Texture height: "+texture.getTextureHeight());
			System.out.println(">> Texture ID: "+texture.getTextureID());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void pollInput() {
		if(gp>-1){
   			ca[gp].poll();
	    	EventQueue queue = ca[gp].getEventQueue();
	   	 	Event event = new Event();
	
	   	 	while(queue.getNextEvent(event)) {
		   	 	StringBuffer buffer = new StringBuffer(ca[gp].getName());
		        buffer.append(" at ");
		        buffer.append(event.getNanos()).append(", ");
		        Component comp = event.getComponent();
		        buffer.append(comp.getName()).append(comp.getIdentifier()).append(" changed to ");
		        float value = event.getValue(); 
		        if(comp.isAnalog()) {
		           buffer.append(value);
		        } else {
		           if(value==1.0f) {
		              buffer.append("On");
		           } else {
		              buffer.append("Off");
		           }
		        }
		        	        
		        if(comp.getName().equals("X Axis")){
		        	System.out.println(buffer.toString());
		        	if(event.getValue()>0.1 || event.getValue()<-0.1)
		        		xMove=event.getValue();
		        	else
		        		xMove=0;
		        } else if(comp.getName().equals("Y Axis")){
		        	if(event.getValue()>0.1 || event.getValue()<-0.1)
		        		yMove=event.getValue();
		        	else
		        		yMove=0;
		        }
	   	 	}
    	}		
	}
	
	public static void main(String args[]){
		FuzzyMover fm = new FuzzyMover();
		fm.start();
	}
}

For what it’s worth, this is what I’m talking about:

wkKDVMOttfM

Crap, sorry I just realized I should have posted this on the “Newbie & Debugging Questions” subforum… :frowning:

That’s odd. The only thing I notice is that you’re giving 0-255 values to glClearColor. OpenGL wants these in 0 - 1 range (always does if it asks for colours as floats). According to the docs however, this will just be clamped to 1 anyway so I’m really not sure what the problem could be.
Again from the docs: [quote]The pixel ownership test, the scissor test, dithering, and the buffer writemasks affect the operation of glClear. The scissor box bounds the cleared region. Alpha function, blend function, logical operation, stenciling, texture mapping, and depth-buffering are ignored by glClear.
[/quote]
. But I can’t see you doing any of that in the code.
Best to check if OpenGL is throwing any errors otherwise I am stumped.

Even funnier… When it is clamped to 1, it should clear everything white, but it’s not white… it’s black…

Are you actually operating on the wrong framebuffer somehow? But I can’t see anything like that happening… ???

Sorry! I commented out this line for the video:

//		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

To better illustrate my problem with a black background.

With that line not commented out, the background is indeed white.

OF COURSE your object leaves a trail when you don’t clear the screen!!!
That is the purpose of glClear :smiley:

Good lord, what the hell was I thinking, I mixed and messed up things in my head.

Sorry for this thread, it was a total brain fart.

:emo: