Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved, mostly] Texture binding not updating current texture  (Read 961 times)
0 Members and 1 Guest are viewing this topic.
Offline goblinJoel

Junior Member


Medals: 2



« Posted 2013-12-30 15:36:17 »

UPDATE: Actually, if I try to render the norm TextureRgba, I get the equivalent problem. It looks like once I use texRgbaLoad(), it ignores Texture.bind() and only pays attention to glBindTexture(). I can swap out the return TextureRgba.loadTexture() in texRgbaLoad() with return null, and I don't get this problem. So it's definitely something in my TextureRgba.loadTexture() method. It's not the return new TextureRgba() line at the end, as commenting that out doesn't fix it. However, if I comment out both that line and the glBindTexture() in the same method and the glTexImage2D() line, the problem does go away. But of course, then I can't use the custom texture loading that TextureRgba.loadTexture() accomplishes. If I try to render the norm TextureRgba, it draws the door texture instead, scaled to the dimensions of norm.

I'm having a weird problem. If I load a Texture using slick's TextureLoader.getTexture(), all is well. However, if I afterwards load a different texture using a custom loader designed to create them from BufferedImages, I can no longer draw the one I loaded from slick unless I specifically use glBindTexture(GL_TEXTURE_2D, texvar.getTextureID()). Simply using texvar.bind() no longer seems to have any effect. Instead, it just draws the one I loaded earlier with my custom loader, but scaling to the dimensions of the texvar texture.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
public class GLTest {

private Texture door;
private int texW;
private int texH;
private int w = 1280;
private int h = 1024;
public GLTest() {
    //make display

    door = loadTexture("normal-example");
    TextureRgba foo = texRgbaLoad();

    //lwjgl setup, loop begins
   while(!Display.isCloseRequested()) {
    glClear(GL_COLOR_BUFFER_BIT);
    door.bind();
    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
        renderTexture(door, 100, 200);
    glEnd();
    //rest of loop
   }

    //relevant methods
   private TextureRgba texRgbaLoad() {
        BufferedImage img; int imgw = 0, imgh = 0; int[] rgba;
        int[] colors;
        try {
            img = ImageIO.read(new File(GameBase.assetsDir + "normal-example.png"));
            imgw = img.getWidth(); imgh = img.getHeight();
            rgba = new int[imgw * imgh];
            colors = img.getRGB(0, 0, imgw, imgh, null, 0, imgw);
            for(int y = 0; y < imgh; y++) {
                for(int x = 0; x < imgw; x++) {
                    Color c = new Color(colors[y*imgw + x]);
                    rgba[y*imgw + x] = c.getRGB();
                }
            }
            return TextureRgba.loadTexture(rgba, imgw, imgh);
        } catch(IOException ex) {
            Logger.getLogger(GLTest.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
   
    private Texture loadTexture(String key) {
        try {
            System.out.println("loading: assets/" + key + ".png");
            return TextureLoader.getTexture("PNG", new FileInputStream(new File("assets/" + key + ".png")));
        } catch(FileNotFoundException ex) {
            Logger.getLogger(GLTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch(IOException ex) {
                Logger.getLogger(GLTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        return null;
    }
   
    private void renderTexture(Texture t, float x, float y) {
        int tw = t.getTextureWidth();
        int th = t.getTextureHeight();
        glTexCoord2f(0,0);
        glVertex2f(x, y);
        glTexCoord2f(1,0);
        glVertex2f(x+tw, y);
        glTexCoord2f(1, 1);
        glVertex2f(x+tw, y+th);
        glTexCoord2f(0, 1);
        glVertex2f(x, y+th);
    }
   
    public static void main(String[] args) {
        new GLTest();
    }
   
}

Note that this code never actually does anything with the custom foo texture. All it does is load it. If I put that one line, TextureRgba foo = texRgbaLoad();, before door = loadTexture("door"); instead of after it, or if I just comment it out, everything works fine. Here is the TextureRgba.loadTexture() code, in another class, in another package:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
    private TextureRgba(int id, int imgWidth, int imgHeight, int texWidth, int texHeight) {
        this.id = id;
        this.imgWidth = imgWidth;
        this.imgHeight = imgHeight;
        this.texWidth = texWidth;
        this.texHeight = texHeight;
    }
    public static TextureRgba loadTexture(int[] rgba, int sourceW, int sourceH) {
        int textureID = glGenTextures();
       
        //determine texture dimensions
       //put the pixels from the provided rgba array into a ByteBuffer named buffer and flip it
       
        //"Bind the texture handle to the context"
       glBindTexture(GL_TEXTURE_2D, textureID);
       
        //"Set the scaling filtering"
       //specifies how it is handled when minified and magnified
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
       
        //"Send the buffer to OpenGL"
       //makes the texture
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texW, texH, 0,
                GL_RGBA, GL_UNSIGNED_BYTE, buffer);
       
        return new TextureRgba(textureID, sourceW, sourceH, texW, texH);
    }


Anyone got any ideas? I guess I could work around it by using the glBindTexture() instead of the Texture.bind(), or by extending the Texture class to override its bind() method to use glBindTexture()... but it's always nice to figure out what I'm doing wrong in the first place.

Thanks in advance for any insight.
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2013-12-30 16:44:50 »

I believe you have to call
1  
TextureImpl.release()[code] (a slick function) to release the slick textures, and then you can draw yours. Why are you trying to load textures using two different libraries anyway? It's useless, and will only cause problems like this. Just stick to one or the other.
[/code]

Offline goblinJoel

Junior Member


Medals: 2



« Reply #2 - Posted 2013-12-30 17:21:21 »

I'm loading from two different libraries because slick lets me get them from the hard drive, but I needed to also be able to create them from BufferedImages that have had their pixel date manipulated at runtime, so I coded a version to do that.

Update:
Ok, tried your suggestion. I found TextureImpl.unbind() and TextureImpl.bindNone(), and both seem to work to get my texture out of the way and allow the slick textures to bind, so long as I call one and then bind the slick texture before glBegin(). Looks like it does the reverse as well. So, thanks! That seems to have fixed it. Odd though, as I'd made my own TextureRgba as an implementer of slick's Texture. Looking at the docs for the TextureImpl class you pointed out, maybe I should subclass that instead. Or maybe that will break things again. Suppose I'll have to try it.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (22 views)
2014-07-24 01:59:36

Riven (20 views)
2014-07-23 21:16:32

Riven (17 views)
2014-07-23 21:07:15

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (52 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!