LWJGL Forum

Programming => OpenGL => Topic started by: Erestar on July 28, 2004, 19:09:42

Title: Texture clamping
Post by: Erestar on July 28, 2004, 19:09:42
Hey guys,
 I haven't messed with LWJGL since earler this year, and now I'm porting everything from .8 to .9. Aside from this, I've switched to Linux.

The problem I'm having now is that my skybox is ending up with lines around the quads, whereas before they were clamping fine all the way to the edges and blending seemlessly.

I was wondering if anyone could look at my texture loader and see if anything is obviously screwed up, as I've been away from this for so long I might be not remembering correctly, or I screwed something up in the LWJGL conversion.

/*
* TextureLoader.java
*
* Created on January 27, 2004, 2:19 AM
*/

package net.erestar.glutil;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.glu.GLU;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.nio.IntBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import org.lwjgl.util.vector.Vector3f;

/**
*
* @author  Jim Podroskey
*/
public final class TextureLoader {
 private TextureLoader() { }
 /* clamping support added to make skybox lookbetter */
 
 public static int load(String path) {
   return load(path, false);
 }
 
 public static int load(String path, boolean clamp) {
   BufferedImage img = null;
   
   try {
     img = ImageIO.read(new File(path));
   }
   catch(Exception e) {
     e.printStackTrace();
   }

   // Flip Image vertically because Java's coords system are different from openGls
   AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
   
   tx.translate(0, -img.getHeight(null));
   AffineTransformOp op = new AffineTransformOp(tx,
                                                AffineTransformOp.
                                                TYPE_NEAREST_NEIGHBOR);
   
   img = op.filter(img, null);

   
   ByteBuffer bb = ByteBuffer.allocateDirect(4 * img.getWidth() * img.getHeight());
   bb.clear();
   bb.put((byte[]) img.getRaster().getDataElements(0, 0, img.getWidth(), img.getHeight(), null));
   bb.rewind();
   
   
   
   IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
   GL11.glGenTextures(buf);
   GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
   
   GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
   
   if(clamp) {
     GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
     GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);
   }
   
   GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, 3, img.getWidth(), img.getHeight(),
       GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, bb);
   
   return buf.get(0);
 }
 
 public static void bind(int textureIndex) {
   GL13.glActiveTexture(GL13.GL_TEXTURE0);
   GL11.glEnable(GL11.GL_TEXTURE_2D);
   GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureIndex);
 }
 
 public static void bind2(int textureIndex) {
   GL13.glActiveTexture(GL13.GL_TEXTURE1);
   GL11.glEnable(GL11.GL_TEXTURE_2D);

   GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureIndex);
 }
}
Title: Texture clamping
Post by: princec on July 28, 2004, 19:58:40
Should be

      GL11.glTexParameteri(ARBTextureCubeMap.GL_TEXTURE_CUBE_MAP_ARB, GL11.GL_TEXTURE_WRAP_S, ARBTextureBorderClamp.GL_CLAMP_TO_BORDER_ARB);
      GL11.glTexParameteri(ARBTextureCubeMap.GL_TEXTURE_CUBE_MAP_ARB, GL11.GL_TEXTURE_WRAP_T, ARBTextureBorderClamp.GL_CLAMP_TO_BORDER_ARB);


Cas :)
Title: Texture clamping
Post by: Erestar on July 28, 2004, 20:13:20
Thanks for the reply Cas :)

Sadly, this didn't exactally fix anything... The black 'outline' is gone, but there's still a noticable line between each of the quads, though this one is a very faint whitish blue (which happens to be the main colors of the sky box... Not sure if that has anything to do with it) and when I put that change in the app got really clunky. I haven't tried to get my frame rate controller working yet, so I can't give specifics, but there's a noticable jerkiness that's not there when I change it back.

Any other ideas? Do I have to change anything when I bind to account for the new values I'm passing in?

Thanks,
 Ere
Title: Texture clamping
Post by: elias on July 28, 2004, 22:42:57
I'm pretty sure that GL12.GL_CLAMP_TO_EDGE should be used, not CLAMP_TO_BORDER.

- elias
Title: Texture clamping
Post by: Erestar on July 29, 2004, 16:15:54
Quote from: "elias"I'm pretty sure that GL12.GL_CLAMP_TO_EDGE should be used, not CLAMP_TO_BORDER.

- elias

You were right elias, thanks !

Here's the combination fo what ended up working:

     GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
     GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
Title: Texture clamping
Post by: princec on July 30, 2004, 09:41:18
Whoops :)
Found a bug in SPGL for me, too :P

Cas :)