Texture size when using JOGL
In my next project, I'm trying to learn JOGL. The result should be a simple 2d animation.
I've heard, that a texture must be 2^n*2^n in size. Can I still use images which don't have this size, or do I have to edit all images in advance? What do I have to take care of, when using such textures?
As an example, I want to show a kind of progress bar, whose image is at the size of 1024*96px. Do I have to define a quad (eg.1024*1024 in size if you calculate the pixels) and use alpha blending to show only the "filled" part? Or are texture coordinates the way to go?
[Edit] Works, solution looks like this (using orthogonal projection):
@Override
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL(); // get GL
glu = new GLU();
//...
t = load(".\\TEXTURES\\ProgressBarBG.png");
//...
}
public Texture load(String fileName){
Texture text = null;
try{
text = TextureIO.newTexture(new File(fileName), false);
text.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
text.setTexPa开发者_JAVA百科rameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
}catch(Exception e){
System.out.println(e.getMessage());
System.out.println("Error loading texture " + fileName);
}
return text;
}
private void drawProgressBG(GL gl, int z) {
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL.GL_ALPHA_TEST);
gl.glAlphaFunc(GL.GL_GREATER, 0); // only render if alpha > 0
// don't show source alpha parts in the destination
gl.glEnable(GL.GL_TEXTURE_2D);
TextureCoords tc = t.getImageTexCoords();
t.enable();
t.bind();
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE,
GL.GL_REPLACE);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(tc.left(), tc.bottom()); gl.glVertex3f( 0f, 0f, z);
gl.glTexCoord2f(tc.right(), tc.bottom()); gl.glVertex3f( 1024f, 0f, z);
gl.glTexCoord2f(tc.right(), tc.top()); gl.glVertex3f( 1024f, 96f, z);
gl.glTexCoord2f(tc.left(), tc.top()); gl.glVertex3f( 0f, 96f, z);
gl.glEnd();
gl.glDisable(GL.GL_TEXTURE_2D);
// switch back to modulation of quad colours and texture
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE,
GL.GL_MODULATE);
gl.glDisable(GL.GL_ALPHA); // switch off transparency
gl.glDisable(GL.GL_BLEND);
gl.glPopMatrix();
}
[/EDIT]
I don't think it has to be 2^n anymore. You could test it easily nehe.gamedev.net has great opengl tutorials and some also in JOGL. Using only 9% of your 1024*1024 texture is a big waste of video ram. You'd better edit it. Use texture coordinates if you only need a portion of your texture, that's why we have them.
精彩评论