Texture Loading BUG

Started by
14 comments, last by Ereinion 21 years, 7 months ago
Hello, I have a problem with texture loading. I load several textures and when I display them on rectangles, the last one is mapped on every rectangle. I am doing this with Java and gl4java but I already have had this bug in C. // Loading texture Textures = new int[3]; gl.glGenTextures(3, Textures); LoadTGATexture("newgame.tga", Textures[0]); LoadTGATexture("loadgame.tga", Textures[1]); LoadTGATexture("options.tga", Textures[2]); // displaying texture 1 gl.glBegin(GL_QUADS); gl.glBindTexture(GL_TEXTURE_2D, Textures[0]); gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex2i(0, 0); gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex2i(0, 40); gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex2i(100, 40); gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex2i(100, 0); gl.glEnd(); The texture displayed is the last one : "options.tga" I tried with PNG image as well and it does not work either. Waiting for your help, thanks Ereinion
Advertisement
You have to bind the correct texture before sending the OpenGL drawing commands, or it will simply use the last texture bound - which in your case seems to be the last image loaded.

It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
This is what I do with the line :
gl.glBindTexture(GL_TEXTURE_2D, Textures[0]);

Is that wrong ?

Ereinion
Move it outside the glBegin / glEnd pair.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
thanks for the advice,
I tried but it didn''t change anything

if you have any ideas, they''re welcome !
Well, in that case there''s probably something internal to the loading function that''s not quite kosher. I can''t really help you with that.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
The loading function has worked very well until now. I really dont understand what is going on ...
i think your loading function works right... else there must be a very strange error in it. ( because if a texture is loaded and you always just overgive a pointer to GLubyte i cant imagin how to overwrite other memory... such an error would be to stupid )

i would even think that you perhaps are doing a little mistake while binding the texture... perhaps you could post the whole thing in here.
OK Here is my whole code.
The loading part is in "init", the displaying part is in "display" and the loading function is called LoadTGATexture and is at the end of the code.
If you have any questions to understand the code (though I''d prefer answers to my problem ) please ask.

// GL4Java imports
import gl4java.*;
import gl4java.awt.*;
import gl4java.drawable.*;
import gl4java.utils.textures.*;

// standard imports
import java.awt.*;
import java.awt.event.*;

public class TextureTest extends Thread
{
private Frame frame;
private TestCanvas canvas;

// windows properties
private String FrameTitle;
private int Width;
private int Height;
private int Bpp;

public int NbrTextures = 3;
public int[] Textures;

TextureTest(String Title, int Width, int Height, int Bpp)
{
this.FrameTitle = Title;
this.Width = Width;
this.Height = Height;
this.Bpp = Bpp;

// main frame creation and settings
this.frame = new Frame(this.FrameTitle);
this.frame.setLayout(new BorderLayout());
this.frame.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
public void windowClosing(WindowEvent e)
{
windowClosed(e);
}
}
);


// sets the capabilities to support
GLCapabilities caps =
new GLCapabilities(true, false, true, 0, 0, 0, 0, 0);
caps.setAlphaBits(8);

this.canvas = new TestCanvas(caps, 800, 600);
this.frame.add(canvas, BorderLayout.CENTER);
this.frame.pack();
this.frame.show();
}

public void run()
{
canvas.requestFocus();
canvas.start();
}

public static void main(String args[])
{
new TextureTest("Test", 800, 600, 24).start();
}

public class TestCanvas extends GLAnimCanvas
{
TestCanvas(GLCapabilities caps, int width, int height)
{
super(caps, width, height);

Textures = new int[2];
}

public void init()
{
gl.glEnable(GL_TEXTURE_2D);

/// Texture Loading

gl.glGenTextures(NbrTextures, Textures);
LoadTGATexture("./data/textures/menu/newgame.tga",
Textures[0]);
LoadTGATexture("./data/textures/menu/loadgame.tga",
Textures[1]);
LoadTGATexture("./data/textures/menu/options.tga",
Textures[2]);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0);
gl.glDepthFunc(GL_LESS);
gl.glEnable(GL_DEPTH_TEST);
gl.glDisable(GL_BLEND);
gl.glShadeModel(GL_SMOOTH);
gl.glMatrixMode(GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, this.getWidth(), 0,
this.getHeight(), -1 ,1);
gl.glMatrixMode(GL_MODELVIEW);

}

public void reshape(int width, int height)
{
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, this.getWidth(), 0,
this.getHeight(), -1 ,1);
gl.glMatrixMode(GL_MODELVIEW);
}

public void display()
{
// ensure GL is initialised correctly
if (glj.gljMakeCurrent() == false)
return;

// clear the screen and the depth buffer
gl.glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
// resets the view
gl.glLoadIdentity();

/// Diplaying

gl.glBindTexture(GL_TEXTURE_2D, Textures[0]);

gl.glBegin(GL_QUADS);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex2i(0, 0);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex2i(0, 40);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex2i(100, 40);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex2i(100, 0);
gl.glEnd();

// swap buffers and frees the GLContext
glj.gljSwap();
glj.gljFree();
}

public void LoadTGATexture(String file, int store)
{
// object to load PNG images
TGATextureLoader texLoader =
new TGATextureLoader(gl, glu);
texLoader.readTexture(file);

if(texLoader.isOk())
{
// create texture
gl.glBindTexture(GL_TEXTURE_2D, store);
gl.glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
gl.glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST);
glu.gluBuild2DMipmaps(GL_TEXTURE_2D,
3,
texLoader.getImageWidth(),
texLoader.getImageHeight(),
GL_RGB,
GL_UNSIGNED_BYTE,
texLoader.getTexture());
}
else
System.out.println("Error loading texture");
}
}
}
Please someone answers, I really need help with this one.
Any idea is welcome, even the most "stupid" ones.

Ereinion

This topic is closed to new replies.

Advertisement