Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
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  
  [LWJGL Texture fail]  (Read 1321 times)
0 Members and 1 Guest are viewing this topic.
Offline GabrielBailey74
« Posted 2013-01-27 21:52:15 »

Hey JGO, this isn't the first time this has happened to me.

LWJGL has a issue of 'keeping data' even after I shutdown / cleanup my LWJGL application..
I'd assume that once I close my application, all the data pertaining to the application while it's running is disposed of. (But it's not..)

For example:
If I load a texture and bind it, and upon program exit I delete the texture, the next time I start my program (Even if I don't bind my texture) it still binds the texture...?
(And people wonder why I stick to Java2D :L)

I've even tried booting up the application and right after OpenGL initialization deleting the textures, it still chooses to bind them..

Any feedback will be helpful guys, I'm fairly new to OpenGL/LWJGL.

Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #1 - Posted 2013-01-27 22:00:59 »

It does not really make sense, but why would you even rely on this?
Post some code, it makes things more clear Smiley
Offline GabrielBailey74
« Reply #2 - Posted 2013-01-27 22:35:00 »

Here you go.
Textures are being loaded via PNGDecoder and Slick-Util.

Psuedo code:
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  
   public void loop() {
      /* Initialization */
      while (!Display.isCloseRequested()) {
         /* Update */
         /* Render */
         /* OpenGL Update */
      }
      /* Destroy */
   }
   
   public void render() {
      /* Clear the screen */
      glPushMatrix();
      texture.bind();
         glBegin(GL_QUADS); {
            glTexCoord2f(0, 0);
            glVertex2f(x, y);
            glTexCoord2f(1, 0);
            glVertex2f(x + w, y);
            glTexCoord2f(1, 1);
            glVertex2f(x + w, y + h);
            glTexCoord2f(0, 1);
            glVertex2f(x, y + h);
         } glEnd();
      glPopMatrix();  
   }
   
   public void destroyGL() {
      glDeleteTextures(texture.getTextureID());
   }

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #3 - Posted 2013-01-27 22:43:30 »

Make sure you call Display.destroy() when you are done. Also, I do not think it is possible for something like what you are saying to happen unless you got drivers made by a rock or something. If it is driver issues you can't blame opengl but yourself for not updating them.

Also, opengl is state based. So if you bind a texture, it will stay bound until you bind something else. The slick utils are kinda bad from what I understand so maybe make your own texture loader and texture class so you know what is going on.

All these issues you post with opengl I never had when learning it and many of them show signs of a lack of general programing knowledge. This is fine but do not blame a library that has been use by many many people if your code does not work.  Clueless

Offline GabrielBailey74
« Reply #4 - Posted 2013-01-27 22:49:51 »

Make sure you call Display.destroy() when you are done. Also, I do not think it is possible for something like what you are saying to happen unless you got drivers made by a rock or something. If it is driver issues you can't blame opengl but yourself for not updating them.

Also, opengl is state based. So if you bind a texture, it will stay bound until you bind something else. The slick utils are kinda bad from what I understand so maybe make your own texture loader and texture class so you know what is going on.

All these issues you post with opengl I never had when learning it and many of them show signs of a lack of general programing knowledge. This is fine but do not blame a library that has been use by many many people if your code does not work.  Clueless

All of these tutorials for LWJGL are ass backwards, half of the variables passed through methods are labeled incorrectly.
Horrible way for a beginner to start LWJGL... (Surfing through old ass tutorials with backwards variables)


'A Lack of general programming knowledge' lolz.
I've been with Java for a few years now mate.
I can decode a few ass backward parameters XD.
In my example I didn't include 'Display.destroy' because it is 'Psuedo code'.

If you want to look at the actual methods not psuedo code enjoy:
(See if you spot what you're calling my 'Lack of general programming knowledge' Tongue)
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  
   @Override
   protected void renderGL() {  
      glClear(GL_COLOR_BUFFER_BIT);
      glPushMatrix();
      texture.bind();
         glBegin(GL_QUADS); {
            glTexCoord2f(0, 0);
            glVertex2f(x, y);
            glTexCoord2f(1, 0);
            glVertex2f(x + w, y);
            glTexCoord2f(1, 1);
            glVertex2f(x + w, y + h);
            glTexCoord2f(0, 1);
            glVertex2f(x, y + h);
         } glEnd();
      glPopMatrix();
   }

   @Override
   protected void initGL() {
      try {
         texture = TextureLoader.getTexture("png", new FileInputStream(new File("res/texture.png")));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      glMatrixMode(GL_PROJECTION);
      glEnable(GL_TEXTURE_2D);
      glClearColor(0.0f, 0.0f, 0.0f, 0.0f);          
       
      // enable alpha blending
     glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   
      glViewport(0, 0, 800, 600);
      glLoadIdentity();
      glOrtho(0, 800, 600, 0, 1, -1);
      glMatrixMode(GL_MODELVIEW);

   }

   public void startGL() {
      initGL();
      getDelta();
      lastFPS = Misc.getTime();
      while (!windowClosed) {
         int delta = getDelta();
         update(delta);
         updateFPS();
         renderGL();
         Display.sync(60);
         Display.update();
         /* Have to listen for our parent window to close */
         if (windowClosing && !windowClosed) {
            windowClosed = true;
            destroyGL();
         }
      }
   }

   /* Called before Display.destroy() */
   protected void destroyGL() {
      glDeleteTextures(texture.getTextureID());
   }

Offline StumpyStrust
« Reply #5 - Posted 2013-01-27 22:58:10 »

Just because you have been with java for a few years does not mean you are pro. I am by no means pro but some of the questions you post on here are well...noobish. I agree that the tutorials for lwjgl are by no means great but you cant just blame everything else when YOUR code does not work.

Now let me get all things right. You start app and everything works. Then you end it. (No longer in processes.) But this time when you start it you don't bind or load a texture anymore and it still shows the one from the previous app. Even if this is try this should not really be a problem as you bind your textures every time you render. Or if you are using an atlas, you bind one then give different texture coords. Also, giving Psuedo code is kinda of an ass move when trying to get people to help you as it is not 'actual' code which could have the problem.

I would also check your driver version as that can be the culprit for when opengl calls are not doing what they are suppose to do. 

Offline GabrielBailey74
« Reply #6 - Posted 2013-01-27 23:05:21 »

*gasp*
Pseudo code was commented well.
Basically the same format as the real code lolz.

Now we're comparing personal experience in Java, never said I was a 'pro' D:
Just because you have more experience in a certain area of Java than I do doesn't mean you wouldn't be likely to come across some of the same issues / questions as me if you didn't have experience in the area Cheesy, anyways.

Now let me get all things right. You start app and everything works. Then you end it. (No longer in processes.) But this time when you start it you don't bind or load a texture anymore and it still shows the one from the previous app.
Correct.

I would also check your driver version as that can be the culprit for when opengl calls are not doing what they are suppose to do.
Meh, my computer's a dinosaur, that could be the issue here if you're suggesting it has any relevance.

Offline StumpyStrust
« Reply #7 - Posted 2013-01-27 23:10:34 »

*sigh* Not going to respond to the first part.  Undecided

Even if your computer is super duper old it should still be able to do the glBegin and basic texturing. Still though, even if this really is not your fault and it is your drivers, it should not be an issue. Why would you start your app, close it, change code, and start it again? Just don't do that can keep coding. Should be fine. I can't think of anytime where you would need it to not do that.


Offline GabrielBailey74
« Reply #8 - Posted 2013-01-27 23:14:54 »

That's what brings me here. Lips Sealed

I started my program.
Binded my texture, checked out how it looked.
Closed the program.

I than went into the code.
Removed the texture.
Started the program to see what it looked like without the texture again. (Texture's still there)

If I have to 'cover up' for this issue by calling glColor3f (Paint over the texture..) that's seriously lame..

Hope this makes sense.

Offline theagentd
« Reply #9 - Posted 2013-01-27 23:16:42 »

Give us an example program we can run that reproduces the problem.

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #10 - Posted 2013-01-27 23:21:27 »

Give us an example program we can run that reproduces the problem.

Wow, I thought closing my IDE and booting it back up with the textures removed would fix the problem but it didn't. Shocked Shocked Shocked
(Code for initializing textures via Slick-Util is still there, no binding)

Edit:
Yeah, if I remove:
1  
texture = TextureLoader.getTexture("png", new FileInputStream(new File("res/img.png")));

and rerun the program, the issue doesn't occur. (Should of tried this first D:)
Maybe Slick-Util is automatically binding? >.<

Guess this thread can be considered solved, thanks for all the feedback guys.
I'm one of the moreslower people on JGO so I don't mind that my problems/questions are considered 'noobish', I program for fun not for income.

I'll look into making my own Utils vs. 3rd party if I choose to use LWJGL.

Offline StumpyStrust
« Reply #11 - Posted 2013-01-27 23:36:54 »

Man should have guessed it was that. I am lazy and use the string rending there and have run into issues with it. This is why you should probably do things yourself once or look into the documentation of what libraries you are using.


At least it is not your drivers.  Cheesy

Offline Danny02
« Reply #12 - Posted 2013-01-27 23:37:27 »

if you don't want textures, disable them: glDisable(GL_TEXTURE_2D)

also I would bet with you that you are indeed binding the texture somewhere, even when you don't know about it.

"just" use shaders, no strange opengl state bullshit^^
Offline davedes
« Reply #13 - Posted 2013-01-27 23:42:59 »

Old versions of SlickUtil have a bug where you might need to do this after loading textures:
1  
2  
.. load textures ..
TextureImpl.bindNone(); //clear SlickUtil cache


In general Slick will always call glBindTexture for you. You should never enable/disable textures or glBindTexture directly. Instead, you would do this:
1  
2  
3  
4  
5  
//calls glBindTexture for you, enables texturing, and caches this texture to reduce redundant driver calls
tex.bind();

//glDisable textures and clears the last cached texture
TextureImpl.bindNone();


SlickUtil is pretty outdated, and as are many of the LWJGL tutorials. They might be OK for your very first "Hello, World" type of application, but generally speaking it's better to understand what is going on rather than relying on SlickUtil (which doesn't really allow for much control over GL state, and tries to do a lot "under the hood" for you like caching texture binds).

If you want to learn OpenGL from the lowest level, IMHO you should write your own texture loader. Then you can either learn to write your own sprite batcher, or you can use a 3rd party lib.
https://github.com/mattdesl/lwjgl-basics/wiki

If you just want to learn OpenGL concepts (like textures, shaders, meshes, etc) you would probably be better off using a 3rd party library like LibGDX rather than struggling with the piss-poor documentation and tutorials of modern GL.

Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #14 - Posted 2013-01-27 23:44:59 »

Edit:
Yeah, if I remove:
1  
texture = TextureLoader.getTexture("png", new FileInputStream(new File("res/img.png")));

and rerun the program, the issue doesn't occur. (Should of tried this first D:)
Maybe Slick-Util is automatically binding? ....

Creating an texture involves binding it first, and sending data after, thats why its bound by default lol Smiley

Edit:
Just use glBindTexture(GL_TEXTURE_2D, -1); to unbind it (faster than enabling / disabling all the time).
Offline GabrielBailey74
« Reply #15 - Posted 2013-01-28 00:16:18 »

Just use glBindTexture(GL_TEXTURE_2D, -1); to unbind it (faster than enabling / disabling all the time).

Argh, exactly what I mean XD, thanks mate, every tutorial on LWJGL I've seen says to unbind call:
1  
glBindTexture(GL_TEXTURE_2D, 0); 

(Ass backwards tutorials on LWJGL everywhere)
>.< (I've only used the call above one time, that was before I found 'TextureImpl.bindNone()')

@davedes:
I totally forgot about TextureImpl.bindNone(), only used that call once previous to this post and it did as you said.

Offline davedes
« Reply #16 - Posted 2013-01-28 02:01:26 »

You should not call the following:
1  
glBindTexture(GL_TEXTURE_2D, 0);


Nor should you bind texture "-1." There are a number of reasons for this:

  • Texture "0" is undefined. In most cases it will be white, but on rare drivers it may contain garbage pixels or could be transparent black. And since it's not very commonly used, it hasn't been as thoroughly tested.
  • Texture "-1" is also undefined; maybe it points to a texture, or maybe it will cause an error with the driver, or maybe it will just point to texture unit 0. You should never assume that it's a valid texture name.
  • If you want to disable texturing, use glDisable(GL_TEXTURE_2D)
  • If you want to texture with opaque white (i.e. for a seemingly "untextured" rectangle), then you should use a small texture region of opaque white picked from your game's sprite sheet (explained here). This allows for more batching and does not need a state change with glBindTexture
  • As I discussed in my last post, you should not be calling glBindTexture directly if you are using SlickUtil. Instead, you should use texture.bind().
  • In modern GL we no longer need to enable/disable texturing, or worry about a "default texture"
Like I keep repeating... ditch SlickUtil and start learning OpenGL "properly."

Offline GabrielBailey74
« Reply #17 - Posted 2013-01-28 02:12:05 »

Alrighty.

Seems every tutorial has their preferences, what's the most common way of texture handling 'properly'?

http://www.lwjgl.org/wiki/index.php?title=The_Quad_textured

Offline davedes
« Reply #18 - Posted 2013-01-28 02:53:56 »

"Properly" would involve any method that uses modern GL (i.e. no immediate mode) and does not rely on something defunct like SlickUtil. Using BufferedImage is convenient, but some may argue PNGDecoder is superior (i.e. no dependency on AWT).

I've explained Textures in detail here:
https://github.com/mattdesl/lwjgl-basics/wiki/Textures

The particular LWJGL wiki article you linked is good if you are looking to learn GL 3.0+. Unfortunately by using 3.0+ you are limiting your audience. The article also falls short on explaining the basics, and will probably leave you with a lot of unanswered questions ("What is a shader?", "What is a vertex?").

This is why I started writing my own series. You will learn things like GLSL and shaders without the complex boilerplate of OpenGL (VBOs, shader creation, etc). It is compatible with GL 2.0, which means that your shader code will also work with GL ES (iOS, Android, WebGL).

If after following the tutorials you feel comfortable with the OpenGL pipeline, you can then drop down to a lower level and start poking around the source code to get an idea of how it all comes together.

There is also plenty of other "modern GL" tutorials floating around the web. The real trick is understanding how to translate C/C++ code to Java, and understanding how to translate GL 3.0+ concepts to a 2.0 context (or GL ES).

http://www.arcsynthesis.org/gltut/ (Java ports)
http://www.opengl-tutorial.org/
http://open.gl/
http://tomdalling.com/blog/category/modern-opengl/

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.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (10 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (107 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!