Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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] Texture is flickering, help?  (Read 446 times)
0 Members and 1 Guest are viewing this topic.
Offline tdegroot96

Senior Newbie





« Posted 2012-10-31 19:52:52 »

My texture is flickering, it keeps showing up normal and after 5 seconds it starts to flicker again and then it shows up normal again. Is it my game loop or what is it?

Source 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  
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  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Main {

   public boolean finished = false;
   Texture texture;
   float x = 50.0f;
   float y = 50.0f;

   public Main() {

   }

   private void init(boolean fullscreen) {
      try {

         Display.setTitle("Test LWJGL");
         Display.setFullscreen(fullscreen);
         Display.setDisplayMode(new DisplayMode(480, 600));
         Display.setVSyncEnabled(true);
         Display.create();

         texture = getTexture("texture");

         GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
         GL11.glMatrixMode(GL11.GL_MODELVIEW);

         GL11.glMatrixMode(GL11.GL_PROJECTION);
         GL11.glLoadIdentity();
         GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
         GL11.glMatrixMode(GL11.GL_MODELVIEW);
         GL11.glEnable(GL11.GL_TEXTURE_2D);
      } catch (LWJGLException e) {
         e.printStackTrace();
      }
   }

   private Texture getTexture(String key) {
      try {
         return TextureLoader.getTexture("PNG", new FileInputStream(
               new File("res/img/" + key + ".png")));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      return null;
   }

   public void start() {
      init(false);
      run();
   }

   public void run() {
      while (!finished) {
         Display.update();
         if (Display.isCloseRequested()) {
            finished = true;
         } else {
            tick();
            render();
         }
      }
   }

   private void render() {
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

      draw();

      Display.update();
      Display.sync(60);
   }

   private void draw() {
      texture.bind();

      GL11.glBegin(GL11.GL_QUADS);
      GL11.glTexCoord2f(0, 0);
      GL11.glVertex2f(x, y);
      GL11.glTexCoord2f(1, 0);
      GL11.glVertex2f(x + texture.getTextureWidth(), y);
      GL11.glTexCoord2f(1, 1);
      GL11.glVertex2f(x + texture.getTextureWidth(),
            y + texture.getTextureHeight());
      GL11.glTexCoord2f(0, 1);
      GL11.glVertex2f(x, y + texture.getTextureHeight());
      GL11.glEnd();
   }

   private void tick() {

   }

   public static void main(String args[]) {
      Main main = new Main();
      main.start();
   }

}
Offline KittenKoder

Senior Member


Medals: 7
Projects: 1



« Reply #1 - Posted 2012-10-31 20:13:51 »

For one thing, you should use tris and not quads, graphics cards prefer those. Also, if I remember correctly, you only bind a texture once, though I could be wrong, but I think that's your problem.

I am no one else.
Offline tdegroot96

Senior Newbie





« Reply #2 - Posted 2012-10-31 20:19:52 »

This is the only texture I use. And Triangles just split it up. Or did you mean something else with tri?
<a href="http://www.youtube.com/v/pxiv6mqfrqs?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/pxiv6mqfrqs?version=3&amp;hl=en_US&amp;start=</a>
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline KittenKoder

Senior Member


Medals: 7
Projects: 1



« Reply #3 - Posted 2012-10-31 20:28:43 »

Triangle strip, basically.

I think I just realized what could be wrong, it's not the texture flickering but the entire scene. It's been so long since I coded raw GL that I forgot how to fix this, but I do remember having that same problem. There's a wait method for something that will fix it.

I am no one else.
Offline davedes
« Reply #4 - Posted 2012-10-31 20:33:29 »

Read the tutorials and wiki. There are a few things wrong:

- You call Display.update at the beginning of your loop, and then again during render(). You should only call this once at the end of your loop (before sync), and probably not inside your render() method. This is likely the cause of the flickering.
- You aren't using proper texcoords. See here.
- You are loading from a file, which won't work if you try packaging your resources into a JAR. You should be using MyGame.class.getResource().

Regarding triangles; true that the GPU will prefer triangles (since that's what it will be in the end), but that's the least of your worries. Right now you're using old-school immediate mode and fixed function; ideally you should be learning more modern OpenGL through shaders and VBOs. Understandably though, the "hello world" quad is a good starting point for newbies.

If you find these topics difficult to grasp, then you would be better off using a library like LibGDX which will cover the lower-level stuff for you.

Offline tdegroot96

Senior Newbie





« Reply #5 - Posted 2012-10-31 20:43:17 »

Read the tutorials and wiki. There are a few things wrong:

- You call Display.update at the beginning of your loop, and then again during render(). You should call this at the end of your loop (before sync), and probably not inside your render() method.
- You aren't using proper texcoords. See here.
- You are loading from a file, which won't work if you try packaging your resources into a JAR. You should be using MyGame.class.getResource().

Regarding triangles; true that the GPU will prefer triangles (since that's what it will be in the end), but that's the least of your worries. Right now you're using old-school immediate mode and fixed function; ideally you should be learning more modern OpenGL through shaders and VBOs. Understandably though, the "hello world" quad is a good starting point for newbies.

If you find these topics difficult to grasp, then you would be better off using a library like LibGDX which will cover the lower-level stuff for you.
Thank you! It was the Display.update() which made this fault. I just started with OpenGL, but I have alot of experience in making 2D and 3D games with the built-in graphics class. Thanks for this file tip as well, I just made this messy piece of code in about 15 mins. And I know, it's messy.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (78 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (121 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (179 views)
2013-05-12 17:10:36

kutucuk (179 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.214 seconds with 20 queries.