Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  OpenGL - glClear() not clearing... possibly  (Read 551 times)
0 Members and 1 Guest are viewing this topic.
Offline samdeman22

Junior Member


Projects: 1



« Posted 2013-12-14 18:51:39 »

I'm trying to animate a quad in 2D space that has a texture applied to it, but its as if its not clearing (glClear is DEFINITELY being called before each render) or maybe its making a new quad each time, I don't know.

anyway, instead of a moving square, I get this:



here is my run() method:

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  
private void run() {
      isRunning = true;

      int frames = 0;
      long frameCounter = 0;

      final double frameTime = 1.0 / FRAME_CAP;

      long lastTime = Time.getTime();
      double unprocessedTime = 0;

      while (isRunning) {

         boolean render = false;

         long startTime = Time.getTime();
         long passedTime = startTime - lastTime;
         lastTime = startTime;

         unprocessedTime += passedTime / (double) Time.SECOND;
         frameCounter += passedTime;

         while (unprocessedTime > frameTime) {
            render = true;
            unprocessedTime -= frameTime;

            if (Window.isCloseRequested()) {
               stop();
            }

            Time.setDelta(frameTime);
            Input.update();

            game.input(); // IMPORTANT
           game.update(); // IMPORTANT

            if (frameCounter >= Time.SECOND) {
               System.out.println(frames);
               frames = 0;
               frameCounter = 0;
            }
         }

         if (render) {
            render(); // IMPORTANT
           frames++;
         } else {
            try {
               Thread.sleep(1);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
         }
      }

      cleanUp();
   }


here is my RenderUtil class, which contains static methods such as clearScreen() and initGraphics():

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public static void clearScreen(){
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER); //Although it is 2D, I might want to add some depth later on ;)
  }
   
   public static void initGraphics(int WIDTH, int HEIGHT){
      glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
     
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
      glMatrixMode(GL_MODELVIEW);
      glEnable(GL_TEXTURE_2D);
   }
   
   public static String getOpenGLVersion(){
      return glGetString(GL_VERSION);
   }


and here is what I do in render():

1  
2  
3  
4  
5  
6  
private void render() {
      clearScreen();
      game.render();
      Window.render(); // quite simply, Display.update();

   }

this is a part of my Game class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void render(){
      sprite.render();
   }
   
   public void update(){
      if (Input.getKey(Input.KEY_A)){
         sprite.setPos(sprite.getPos().getX() - 0.25f, sprite.getPos().getY());
      }
      if (Input.getKey(Input.KEY_D)){
         sprite.setPos(sprite.getPos().getX() + 0.25f, sprite.getPos().getY());
      }
      if (Input.getKey(Input.KEY_S)){
         sprite.setPos(sprite.getPos().getX(), sprite.getPos().getY() - 0.25f);
      }
      if (Input.getKey(Input.KEY_W)){
         sprite.setPos(sprite.getPos().getX(), sprite.getPos().getY() + 0.25f);
      }


And crucially, the actual rendering, the Sprite class.

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  
private Vector2f pos;
   private int size;
   
   //which animation the sprite has
  private int i;
   private int j;
   
   private Texture texture;
   
   public Sprite(String name, int size){
      i = 0;
      j = 0;
      this.size = size;
      pos = new Vector2f(0,0);
     
      try{
         texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + name + ".png")));
      } catch (IOException e){
         e.printStackTrace();
      }
     
   }
   
   public void render(){
     
                //IMPORTANT
               //should I be doing glBegin --coordinate stuff-- glEnd every render()?

      texture.bind();
     
      glBegin(GL_QUADS);

                // this is just a bit of square math to determine which tile of a spritesheet the quad should display.
     
         //BL
        glTexCoord2f(i * 0.25f, (j+1) * 0.25f);
//         glTexCoord2f(0, 1); of the tile
        glVertex2f(pos.getX(), pos.getY());
         //BR
        glTexCoord2f((i+1) * 0.25f, (j+1) * 0.25f);
//         glTexCoord2f(1,1); of the tile
        glVertex2f(pos.getX() + size, pos.getY());
         //TR
        glTexCoord2f((i+1) * 0.25f, j * 0.25f);
//         glTexCoord2f(1,0); of the tile
        glVertex2f(pos.getX() + size, pos.getY() + size);
         //TL
        glTexCoord2f(i * 0.25f, j * 0.25f);
//         glTexCoord2f(0,0); of the tile
        glVertex2f(pos.getX(), pos.getY() + size);
         
      glEnd();

   }
   
   public Vector2f getPos(){
      return pos;
   }
   
   public void setPos(Vector2f r){
      pos = r;
   }
   
   public void setPos(float x, float y){
      pos.setX(x);
      pos.setY(y);
   }

   public int getI() {
      return i;
   }

   public void setI(int i) {
      this.i = i;
   }

   public int getJ() {
      return j;
   }

   public void setJ(int j) {
      this.j = j;
   }



so when rendering my "sprite" should I be doing glBegin --coordinates-- glEnd every render cycle? Does this create an entirely new quad as well as the old one? that's the only possible problem that I can see.

If in doubt go flat out - Collin McRae
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2013-12-14 18:54:23 »

Yes, unless you're using display lists (which upload the data once to the GPU), the you will need to call glBegin and end every frame. The other thing you could do, which would probably be better to do, would be to use glTranslatef and move your vertices instead of creating a new quad every frame.

Offline samdeman22

Junior Member


Projects: 1



« Reply #2 - Posted 2013-12-14 18:58:06 »

Yes, unless you're using display lists (which upload the data once to the GPU), the you will need to call glBegin and end every frame. The other thing you could do, which would probably be better to do, would be to use glTranslatef and move your vertices instead of creating a new quad every frame.

right, I didn't think about that, I made my own Vector engine you see, so I thought I would relate my own vectors to that ^ current system. But unless I want to do a lot of background openGL work, I think that would be a good idea XD.

If in doubt go flat out - Collin McRae
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx
« Reply #3 - Posted 2013-12-15 22:12:34 »

Does removing GL_DEPTH_BUFFER from your clear call fix it?

Offline samdeman22

Junior Member


Projects: 1



« Reply #4 - Posted 2013-12-18 12:20:07 »

Does removing GL_DEPTH_BUFFER from your clear call fix it?

it does! Why would that be? I know its not atm. but it might be necessary in the future to use 3D space.

EDIT: is it because I'm using glVertex2f in a depth space? If I used glVertex3f with any old z component this wouldn't have happened would it?

If in doubt go flat out - Collin McRae
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-12-18 12:37:26 »

Does removing GL_DEPTH_BUFFER from your clear call fix it?

it does! Why would that be? I know its not atm. but it might be necessary in the future to use 3D space.
Just taking a stab in the dark but clearing the buffer bit sorts the geometry along the Z axis, so maybe OpenGL thought that you were drawing those 2D quads on top of each other and wanted to sort them? If you look, the quad drawn most recently overlaps the quad drawn before it. But I don't know!

Offline trollwarrior1
« Reply #6 - Posted 2013-12-18 12:46:11 »

I think I know why. You could try looking for error after you clear GL_DEPTH_BUFFER. I think it gives you an error. The argument is invalid probably. What you should be doing is
1  
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #7 - Posted 2013-12-18 20:50:27 »

I think I know why. You could try looking for error after you clear GL_DEPTH_BUFFER. I think it gives you an error. The argument is invalid probably. What you should be doing is
1  
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

The problem was with him clearing the depth buffer in the first place. I don't of any reason why you would need a depth buffer for "true" 2D (I.E. not isometric or 2.5D)

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2013-12-19 09:30:46 »

Damn, it took 6 posts for someone to notice it was GL_DEPTH_BUFFER_BIT not GL_DEPTH_BUFFER. One is used for a bit-field and the other is a standalone name. That's how tricky OpenGL is Smiley

Anyway, that error caused your glClear call to fail. You were never actually clearing the screen Wink

Offline quew8

JGO Coder


Medals: 29



« Reply #9 - Posted 2013-12-19 15:11:04 »

Moral of the story is: make sure you check OpenGL for errors. You would have picked up on this much sooner.

Edit: But don't feel bad. The actual mistake you made was minor and you are far from alone with not checking for OpenGL errors.
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 (11 views)
2014-07-24 01:59:36

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

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

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

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

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

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

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

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

Riven (50 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!