Rendering glitch with multiple cubes + aliasing

I have a map of 3D cubes and everything seems good except for two things.

First I have a problem that when I move my VBO of block up and down I get at some places little lines that flicker when I move.

The other problem I have Is about aliasing some textures flicker allot.

I know I can fix this one using linear filter for my texture but the problem is that the linear filter cause texture bleeding since it blend the closest colors together or something like that. glShadeModel(GL_SMOOTH) or changing the pixelformat of LWJGL isnt helping since the problem is texture based and not polygon based.

One last thing, if I change the pixel format for anything else than 0 I get again little lines between my cubes I don’t know if this one I a texture bleeding problem too.
http://postimg.org/image/i3a6bller/

–Ps I dont wan’t to use shaders if possible. I in the range of the possible I don’t want to limit old computers.

Try enabling vsync. Should fix some flickering.

Thank you for your answer but vsync is already enabled.

Texture bleeding. Use GL_NEAREST. Is your texture power of 2? Are you using mipmaps?

No I don’t use mipmaps and I don’t know how to implement them. would it be a good idea for only tiles ??

yes I use power of two textures 512*512 for my tiles of 32 pixel.

Hmm… Can we see some code?

Not that I am an expert, but I somehow seem to associate this with depth issues.
Do you have depth testing enabled? And if you do have depth testing enabled,
Do you have two faces exactly on top of each other?

I might be wrong here, but I think it’s worth checking out.

I have not yet optimized my map yes there’s faces at the same place it will probably fix some problems but is there a solution for the texture aliasing since gl linear create texture bleeding?
Ps yes dept testing is enabled.
Pps . sorry I’m on my phone.

If he didn’t have depth testing enabled all you would see is a bunch of vertices rendered in front of each other. Depth testing should not affect texture bleeding. I agree, show us some code.

what I think is important in the main
[spoiler]

public void init(){
		try {
			PF = new PixelFormat(8, 16, 0, 16);
			Display.setDisplayMode(new DisplayMode(width, height));
			Display.setVSyncEnabled(true);
			Display.create(PF);
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
		lastFps = getTime();
		setupGL();
		screen.init();
		splashscreen.init();
	}

public void setupGL(){
		glMatrixMode(GL_PROJECTION);
		//glOrtho(0, width, height, 0, 1, -1);
		glViewport(0, 0, Display.getWidth(), Display.getHeight());
		gluPerspective(50f, (float)800 / (float)600, 1, 1000);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glRotatef(-35f, 1.0f, 0.0f, 0.0f);
		glEnable(GL_DEPTH_TEST);
		glEnable(GL_CULL_FACE);
		glEnable(GL_TEXTURE_2D);
		
	}

[/spoiler]

my vbo class I know Its a mess sorry
[spoiler]

public class Quad3DVBO extends VBO{

	protected int instances = 0;
	
	public Quad3DVBO(){
		
	}
	
	public Quad3DVBO(Color color){
		this.color = color;
		colored = true;
		numberOfID += 1;
	}
	
	public Quad3DVBO(SpriteSheet sheet, Textures texture){
		this.sheet = sheet;
		this.texture = texture;
		textured = true;
		numberOfID += 1;
	}
	
	public void add(float[] coords){
		setVertices(coords);

		if(colored){
			setColors();
		}
		
	}
	
	public void add(Block block){
		
		for(float[] v : block.getVerices()){
			
			setVertices(v);
			
			if(colored){
				setColors();
			}
			
			if(textured){
				setTextures();
			}
		}
	}
	
	public void add(Block block, float x, float y, float z){
		for(float[] v : block.getVerices(x, y, z)){
			setVertices(v);
		}
			if(colored){
				setColors();
			}
			
			
		for(float[] t  : block.getTextures()){
			if(textured){
				setTextures(t);
			}
		}
			
	}
	
	public void add(float[] coords, float[] cols){
		setVertices(coords);
		if(colored){
			setColors(cols);
		}
	}
	
	public void init() {
		buffIds = BufferUtils.createIntBuffer(numberOfID);
		glGenBuffers(buffIds);
		vertexId = buffIds.get(0);
		indexId = buffIds.get(1);
		if(colored){
			colorId = buffIds.get(2);
		}
		if(textured){
			textureCoordsId = buffIds.get(2);
		}
		
			vertexBuff = BufferUtils.createFloatBuffer(12*instances);
			for(float[] v : vertices){
				vertexBuff.put(v);
			}
			
			indexBuff = BufferUtils.createShortBuffer(6*instances);
			for(short[] i : indices){
				indexBuff.put(i);
			}
			
			if(colored){
				colorBuff = BufferUtils.createFloatBuffer(16*instances);
				
				for(float[] c : colors){
					colorBuff.put(c);
				}
				
				colorBuff.flip();
				glBindBuffer(GL_ARRAY_BUFFER, colorId);
				glBufferData(GL_ARRAY_BUFFER, colorBuff,  GL_STATIC_DRAW);
				glBindBuffer(GL_ARRAY_BUFFER, 0);
			}
			
			if(textured){
				textureBuff = BufferUtils.createFloatBuffer(8 * instances);
				
				for(float[] t : textures){
					textureBuff.put(t);
				}
				
				textureBuff.flip();
				glBindBuffer(GL_ARRAY_BUFFER, textureCoordsId);
				glBufferData(GL_ARRAY_BUFFER, textureBuff,  GL_STATIC_DRAW);
				glBindBuffer(GL_ARRAY_BUFFER, 0);
			}
			
			vertexBuff.flip();
			glBindBuffer(GL_ARRAY_BUFFER, vertexId);
			glBufferData(GL_ARRAY_BUFFER, vertexBuff,  GL_STATIC_DRAW);
			glBindBuffer(GL_ARRAY_BUFFER, 0);
			
			indexBuff.flip();
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexId);
			glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuff, GL_STATIC_DRAW);
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	}

	public void render() {
		
		glPushMatrix();
		//glShadeModel(GL_SMOOTH);
		if(textured){
			glBindTexture(GL_TEXTURE_2D, sheet.getTextureID());
		}
		glCullFace(GL_FRONT);
		glTranslatef(x, y, z);
		glEnableClientState(GL_VERTEX_ARRAY);
		glBindBuffer(GL_ARRAY_BUFFER, vertexId);
		glVertexPointer(3, GL_FLOAT, 0, 0);
			if(colored){
				glEnableClientState(GL_COLOR_ARRAY);
				glBindBuffer(GL_ARRAY_BUFFER, colorId);
				glColorPointer(4, GL_FLOAT, 0, 0);
			}
			if(textured){
				glEnableClientState(GL_TEXTURE_COORD_ARRAY);
				glBindBuffer(GL_ARRAY_BUFFER, textureCoordsId);
				glTexCoordPointer(2, GL_FLOAT, 0, 0l);
			}
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexId);
			glDrawElements(GL_TRIANGLES, indexBuff.capacity(), GL_UNSIGNED_SHORT, 0);
			glBindBuffer(GL_ARRAY_BUFFER, 0);
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
			if(colored){
				glDisableClientState(GL_COLOR_ARRAY);
			}
			if(textured){
				glDisableClientState(GL_TEXTURE_COORD_ARRAY);
			}
		glDisableClientState(GL_VERTEX_ARRAY);
		if(textured){
			glBindTexture(GL_TEXTURE_2D, 0);
		}
		glPopMatrix();
	}

	public void dispose(){
		buffIds.put(0, vertexId);
		buffIds.put(1, indexId);
		if(colored){
			buffIds.put(2, colorId);
		}
		if(textured){
			buffIds.put(2, textureCoordsId);
		}
		glDeleteBuffers(buffIds);
	}
	
	public void translate(float x, float y, float z){
		this.x += x;
		this.y += y;
		this.z += z;
	}
	
	
	
	
	
	
	
	
	
	
	private void setVertices(float[] vert){
		if(vert.length == 12)
		{
			short indpos = (short)(instances*4);
			vertices.add(vert);
			indices.add(new short[]{(short)(indpos+2), (short)(indpos+1), indpos, indpos, (short)(indpos+3), (short)(indpos+2)});
			instances++;
		}else{
			System.out.println("ERROR - The array must containt 12 variables.");
		}
	}
	
	private void setColors(){
		float[] vertexCols = new float[16];
		float[] toArray = color.getColors();
		for(int i = 0; i <= 3; i++){
			vertexCols[i*4] = toArray[0];
			vertexCols[i*4+1] = toArray[1];
			vertexCols[i*4+2] = toArray[2];
			vertexCols[i*4+3] = toArray[3];
		}
		colors.add(vertexCols);
	}
	
	private void setColors(float[] cols){
		float[] vertexCols = new float[16];
		for(int i = 0; i <= 3; i++){
			vertexCols[i*4] = cols[0];
			vertexCols[i*4+1] = cols[1];
			vertexCols[i*4+2] = cols[2];
			vertexCols[i*4+3] = cols[3];
		}
		colors.add(vertexCols);
	}
	
	private void setTextures(){
		float[] toArray = texture.getCoords();
	
		textures.add(toArray);
	}
	
	private void setTextures(float[] coords){

		textures.add(coords);
	}

}

[/spoiler]

initializing my sprite sheet
[spoiler]

public void init(){
		textureID = glGenTextures();
		glBindTexture(GL_TEXTURE_2D, textureID); //Bind texture ID
        
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);


        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
       glBindTexture(GL_TEXTURE_2D, 0);
        
	}

[/spoiler]

Unrelated, but i strongly recommend against have a buffer object for every cube, which it seems you are doing right now. And to be honest, buffer objects aren’t that great for this type of geometry anyway. Use vertex arrays or even display lists, if you’re ok with using deprecated GL.
Edit: Never mind it seems like thats how you render chunks. I also found something else, glTranslatef. Don’t use this for moving your chunks when you can just specify starting coordinates for the chunk and pass over expensive matrix calls…

But how can I move my vbos then?

[quote]and pass over expensive matrix calls…
[/quote]
What do you mean?

If you have static chunks, why would you ever translate them using more expensive matrix math when you can simply set a start point for your chunk, and then send the vertices to your renderer along with the chunk offset?

[quote]If you have static chunks, why would you ever translate them using more expensive matrix math when you can simply set a start point for your chunk, and then send the vertices to your renderer along with the chunk offset?
[/quote]
Because the map has to move when the player is moving to to make the “camera effect” ??

I don’t know if it can help but I definitely get texture bleeding with pixel format other than 0.
http://s24.postimg.org/ffo13rrwh/Untitled.png

This is irrelevant. Default OpenGL automatically precomputes the gl_ModelViewProjectionMatrix, so it’s still just a single matrix multiplication for the GPU.

You’re creating your pixel format using [icode]new PixelFormat(8, 16, 0, 16);[/icode]. That’s 8 bits of alpha (not really usable for 3D), 16 bits of depth and 16 samples per pixel. 16x multisampling isn’t supported by any hardware, but on Nvidia hardware at least 16x multisampling is mapped to 2x2 supersampling (double resolution rendering) plus 4x multisampling. It’s the most expensive anti aliasing setting you can enable from OpenGL, around 4x to 8x as expensive as no multisampling. Not only is it expensive, it’s also screwing up your texture coordinates.

Multisampling works by computing the color of a pixel once, but computing depth and coverage for each sample. With 4x MSAA you get the following layout. The green point is the point for which the color is computed.

http://www.nordichardware.se/skrivelser_img_swe/215/nv4x.jpg

At edges only some of the 4 samples will be covered by the triangle. Consider a triangle that barely overlaps the pixel’s top (red) sample but not the green center point. Despite the fact that the green center point isn’t covered, the color will be calculated as if it was covered, but the result will only be written to the top sample. That means that all the vertex attributes, including texture coordinates and colors, will be extrapolated (as opposed to interpolated when the triangle covers the center point), and the texture coordinates generated can be outside the range that the vertices specify. When using sprite sheets, it essentially introduces a new source of texture bleeding.

Solutions:

  • Like you said, disable all multisampling: [icode]new PixelFormat(8, 16, 0, 0);[/icode]
  • If you’re okay with using shaders and OpenGL 3, create a custom texture coordinate varying and set its interpolation qualifier to centroid to prevent extrapolation.
  • Use a 2D texture array with one sprite per layer instead of a sprite sheet. Since that’ll prevent all texture bleeding between sprites you can use everything from bilinear filtering and mipmaps to multisampling without any bleeding.

[quote]Like you said, disable all multisampling: new PixelFormat(8, 16, 0, 0);
[/quote]
Thats kinda sad.

[quote]If you’re okay with using shaders and OpenGL 3
[/quote]
I’m not, at least for a global solution. I will probably implement them for peoples that suport opengl 3+ but I want a solution for those who don’t support opengl3 + and shaders seems to be an other world that I haven’t discovered yet.

[quote]- Use a 2D texture array with one sprite per layer instead of a sprite sheet. Since that’ll prevent all texture bleeding between sprites you can use everything from bilinear filtering and mipmaps to multisampling without any bleeding.
[/quote]
I know that recent GFXcards have some magic way to play with texture array that doesn’t make them slow but isn’t a bad solution for older GFXcards?

Texture arrays shouldn’t be slower than normal textures. The filtering cost is exactly the same. The only difference is the amount of data you can sample from, which isn’t any worse than when using 3D textures which have been supported since forever. Sadly texture arrays are just like the centroid interpolation qualifier limited to OGL3 hardware. In other words, you can’t have accurate antialiasing with sprite sheets used as you use them. Traditional 3D textures can also be used, but they won’t handle mipmaps correctly since they’ll blend between layers as well. You could add a border with the same color as the edge pixels of each sprite, but that won’t help if you’re using mipmaps.

As you can see, there’s no perfect solution without OGL3. You can get two of three of mipmaps, antialiasing and filtering. If you’re willing to forsake mipmaps (which will look like crap when the sprites are small on the screen), you can use 3D textures or add borders around the sprites in the sprite sheet.

I decided to add a border around the sprites but I am wondering something let say I have a 512X512 spritesheet would it make slower if I add the border around every sprites while loading. I’m guessing Ill have to make every sprites 2X2 pixel smaller so my texture is a power of two. Do you think this is a good idea if I double the pixels for every edges of the sprites instead of doing this manualy when putting them into the bytebuffer?

I have had this issue and I had it from rounding to early , check you are using doubles for your positioning not integers, round too early is easy to do.

When you add the border is largely irrelevant. The only thing it affects is load time. Nowadays GPUs don’t require power-of-2 textures, but if you decide to make your sprites 2 pixels smaller to fit the border you’ll need to adjust your texture coordinates so that they don’t include the border.