Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  glBindTexture(GL_TEXTURE_2D, number); for numbers bigger than 1  (Read 3464 times)
0 Members and 1 Guest are viewing this topic.
Offline Mac70
« Posted 2012-11-24 01:42:10 »

Like in topic. Is it possible to use glBindTexture(GL_TEXTURE_2D, number) for numbers bigger than 1? Currently when I try to do this, I can see only untextured quads.

Here is my texture binding and usage code:
(TextureHolder is just class with texture and two int values)

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  
public class Texture {
   
    public static void create(int number, TextureHolder tex) {
        glBindTexture(GL_TEXTURE_2D, number);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.texture);
    }
   
    public static void load(int number) {
        glBindTexture(GL_TEXTURE_2D, number);
    }
   
    public static void draw(TextureHolder tex, float x, float y, float angle) {
        glPushMatrix();
            glTranslated(x, y, 0);
            glRotated(angle/(Math.PI/180), 0, 0, 1);
            glBegin(GL_QUADS);
                glColor4f(1,1,1,1);
                glTexCoord2f(0,1); glVertex2f(-tex.height/2, -tex.width/2);
                glTexCoord2f(0,0); glVertex2f(-tex.height/2, tex.width/2);
                glTexCoord2f(1,0); glVertex2f(tex.height/2, tex.width/2);
                glTexCoord2f(1,1); glVertex2f(tex.height/2, -tex.width/2);
            glEnd();
        glPopMatrix();
    }
   
}


How I use it:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public void createTexture(String path) {
        textureLoader = Tools.TextureLoader.create(path);
        Texture.create(20, textureLoader);
    }

@Override public void Draw() {
        Texture.load(20);
        Texture.draw(textureLoader, x, y, angleRad);
    }

Check out my Devblog! Smiley
Offline davedes
« Reply #1 - Posted 2012-11-24 02:17:12 »

glGenTextures will give you a "name" (aka: ID) that you can use for that texture.

Here is a simple texture wrapper example.

Offline Mac70
« Reply #2 - Posted 2012-11-24 13:06:21 »

Is it possible to "load" empty texture numbers into memory for later use? I am using enum for storing texture numbers to have better control of them.

I have tried this, but it does not work (I still can't use texture numbers bigger than 1):

1  
2  
3  
4  
5  
6  
        int i = 1;
        int texAmount = 0;
        while (i<=30) {
            texAmount = glGenTextures();
            i++;
        }

Check out my Devblog! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-11-24 13:21:18 »

The problems is that enums are determined at compile time, while the values returned by glGenTextures cannot be assumed to have any pattern. Therefore trying to get texture IDs into enums is the wrong approach (by definition).

Why not simply make an int[], and store the texture IDs in them. Then use the enums to refer to a slot in the array.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
int[] textureHandles = new int[30];
for(int i=0; i<textureHandles.length; i++) {
   textureHandles[i] = glGenTextures();
}

public enum TextureID {
   GROUND, WATER, ROCK, AIR
}

glBindTexture(GL_TEXTURE_2D, textureHandles[ROCK.ordinal()]);

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Mac70
« Reply #4 - Posted 2012-11-24 13:41:10 »

The problems is that enums are determined at compile time, while the values returned by glGenTextures cannot be assumed to have any pattern. Therefore trying to get texture IDs into enums is the wrong approach (by definition).

Why not simply make an int[], and store the texture IDs in them. Then use the enums to refer to a slot in the array.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
int[] textureHandles = new int[30];
for(int i=0; i<textureHandles.length; i++) {
   textureHandles[i] = glGenTextures();
}

public enum TextureID {
   GROUND, WATER, ROCK, AIR
}

glBindTexture(GL_TEXTURE_2D, textureHandles[ROCK.ordinal()]);


Unfortunately, this approach does not work too.

Check out my Devblog! Smiley
Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2012-11-24 13:46:24 »

It works just fine. Show us more of your code, so we can see what goes wrong there.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2012-11-24 13:49:40 »

You should be using glGenTextures(); like this:

1  
2  
3  
4  
5  
public static int create(TextureHolder tex) {
    int uniqueId = glGenTextures();
    glBindTextures(GL_TEXTURE_2D, uniqueId);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.texture);
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private int texId;
public void createTexture(String path) {
        textureLoader = Tools.TextureLoader.create(path);
        texId = Texture.create(textureLoader);
    }

@Override public void Draw() {
        Texture.load(texId);
        Texture.draw(textureLoader, x, y, angleRad);
    }

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Mac70
« Reply #7 - Posted 2012-11-24 14:37:24 »

You should be using glGenTextures(); like this:

1  
2  
3  
4  
5  
public static int create(TextureHolder tex) {
    int uniqueId = glGenTextures();
    glBindTextures(GL_TEXTURE_2D, uniqueId);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.texture);
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private int texId;
public void createTexture(String path) {
        textureLoader = Tools.TextureLoader.create(path);
        texId = Texture.create(textureLoader);
    }

@Override public void Draw() {
        Texture.load(texId);
        Texture.draw(textureLoader, x, y, angleRad);
    }


Results:



I don't know where the problem is, so there is my full code: https://dl.dropbox.com/u/67758055/src.zip

I will be very grateful if someone will tell me where the problem is. Wink

Check out my Devblog! Smiley
Offline deathpat
« Reply #8 - Posted 2012-11-24 15:00:47 »

hi,
if you want some help on an international forum like here, you absolutely have to take the habit to write your code in English. I was willing to help here but when I saw that your code was in a language I don't understand, I just stopped trying as it would have been too painful ... and I think it will be the same for most people of this forum.

work in progress : D A E D A L U S
Offline Mac70
« Reply #9 - Posted 2012-11-24 16:02:12 »

Translated code and all resources, it is possible to run project in IDE: https://dl.dropbox.com/u/67758055/Silent_Space_English.zip

I hope that there will be no trouble understanding this code. Wink

Check out my Devblog! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline deathpat
« Reply #10 - Posted 2012-11-24 16:24:27 »

ok great Smiley
I had a look, the problem here is the Texture class. The textureId that is generated by glGenTextures() is used as an index in the static array "textures" instead of being used directly :

1  
2  
3  
    public static void load(int number) {
        glBindTexture(GL_TEXTURE_2D, textures[number]);
    }


"textures" is never modified in your code, meaning that it is initially filled with 0, so here you're always binding texture id 0 which is quite the same as disabling textures Smiley

just replacing the load method with :

1  
2  
3  
    public static void load(int number) {
        glBindTexture(GL_TEXTURE_2D, number);
    }


should fix your issue as long as "number" is always generated by glGenTextures()

Hope this helps Wink

EDIT :
something else, you have to add this when creating the texture (in the Texture class, after glTexImage2D(...)) :

1  
2  
3  
4  
   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_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

work in progress : D A E D A L U S
Offline sproingie

JGO Kernel


Medals: 202



« Reply #11 - Posted 2012-11-26 18:14:57 »

you're always binding texture id 0 which is quite the same as disabling textures Smiley

Texture 0 is a perfectly valid texture id, and refers to the single texture unit from OpenGL 1.0.  It's just not usually a very good to use it.  It's most definitely not the same thing as disabling textures, only glDisable does that.


Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #12 - Posted 2012-11-26 18:22:16 »

you're always binding texture id 0 which is quite the same as disabling textures Smiley

Texture 0 is a perfectly valid texture id, and refers to the single texture unit from OpenGL 1.0.  It's just not usually a very good to use it.  It's most definitely not the same thing as disabling textures, only glDisable does that.

probably true, but srsly, are you really calling glDisable() everytime you want to disable texturing? You have to admit you are using glBindTexture(GL_TEXTURE_2D, 0); Tongue

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #13 - Posted 2012-11-26 18:23:03 »

you're always binding texture id 0 which is quite the same as disabling textures Smiley

Texture 0 is a perfectly valid texture id, and refers to the single texture unit from OpenGL 1.0.  It's just not usually a very good to use it.  It's most definitely not the same thing as disabling textures, only glDisable does that.

http://en.wikipedia.org/wiki/OpenGL#Version_History
Quote
The first version of OpenGL, version 1.0, was released in January 1992 by Mark Segal and Kurt Akeley.

Seriously?

Matheus23: +1

Myomyomyo.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #14 - Posted 2012-11-26 21:51:16 »

probably true, but srsly, are you really calling glDisable() everytime you want to disable texturing? You have to admit you are using glBindTexture(GL_TEXTURE_2D, 0); Tongue

You cannot count on texture 0 having any particular default data in it.  Typically it's white, but on some GPUs/drivers, people have told me it's black.  I don't usually have much need to disable texturing.  For times when I do need a solid color, I generate a white pixmap or I work a white square into the atlas, which is more so I can treat that solid block like any other tile.  Otherwise it's not that expensive to disable as long as you're not doing it a zillion times per frame.
Offline davedes
« Reply #15 - Posted 2012-11-26 22:55:45 »

When do you really need to disable texturing in fixed function? Using a 1x1 white TextureRegion from your current sprite sheet has the same effect.

You shouldn't need to use glDisable (which can lead to performance problems) or bind texture 0 (which might have unusual results on shitty drivers). Just bind your white texture region whenever you want a "solid" or "untextured" primitive. And as a bonus, you won't even need to flush your sprite batch. Smiley

Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #16 - Posted 2012-11-26 22:57:16 »

You mean, exactly what sproingie was saying?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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.

Riven (19 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

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

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

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

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

ctomni231 (60 views)
2014-07-18 06:55:21
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!