Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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] How to invoke display() - react on changed value  (Read 1191 times)
0 Members and 1 Guest are viewing this topic.
Offline Jaquelin

Junior Newbie





« Posted 2009-04-17 19:51:20 »

Hello Together,

this might be a stupid question but due to the fact that I am new to JOGL and 3D I hope you will forgive give me.

Imagine I have a value which is the result of a calculation in a different class.

I want to move my scene to the left every time this value (lets called it moveToLeft) changes. But since moveToLeft is not a user input ( mouse/addMouseListener() or keyboard/addKeyListener() ) I have no idea how to achieve this.

So my question is: How do I invoke the display() method which includes gl.glTranslatef(moveToLeft, 0.0f, 0.0f)?

Is it at least the right way to invoke display() directly or is there a complete different way how to achieve this? Or is there a listener which a have to add which I had overlooked?

I really appreciate any help, code fragment etc.

Thanks
Jaquelin
Offline Cakey

Junior Devvie





« Reply #1 - Posted 2009-04-17 22:47:05 »

So all your trying to do is after a function/method call change a variable which moves your "scene" around?

I always thought Display was automaticly called every "frame"...

If your post some of your code I can help you figure out what your problem is, but you shouldn't have to invoke display() as it's already being invoked. Much like "paint()" of the graphics class(I think).

Offline lhkbob

JGO Knight


Medals: 32



« Reply #2 - Posted 2009-04-18 01:27:12 »

Calling display() causes the opengl rendering to happen (by calling any added GLEventListeners).  You can use an Animator to automatically call display(), but that's not really related to the post.

The best way of handling this is to have a main loop that renders the frame over and over.  Then within your display(drawable) method, just look-up moveToLeft or other properties and change the drawing as needed.  If you didn't want to render all the time, you could have a boolean needsRender, and after calculating moveToLeft, set it to true.  Then the main loop checks needsRender, and then displays everything.

IMPORTANT WARNING: display() is not the same as display(drawable).  display() is a method on a GLCanvas or GLJPanel to start the rendering process.  display(drawable) is part of the GLEventListener and should never be called directly because the GL object won't be ready.

Also, it's not advisable to render from within a mouse or key listener.  It's better to have a property that gets updated by the listener (like mouseX, mouseY, or shiftPressed=true), and then check those values as I described above.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jaquelin

Junior Newbie





« Reply #3 - Posted 2009-04-19 12:01:13 »

@Cakey + Ihkbob

Thanks for your quick. Yesterday I played around the whole day with your suggestions. Everything works fine.

@ Ihkbob
I even wrote some lines with an animator. To rise my learning curve even a bit more! Cheesy Wink → See below. Thanks for mention it.

Again, thanks you two. It helped me very much.

Jaquelin


In case someone is interested in the animator solution Ihkbob mentioned see this code. Take a look at moveToLeftAccess() and how the other two classes use this method.

My “Main-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  
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  
112  
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLJPanel;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.sun.opengl.util.FPSAnimator;

@SuppressWarnings("serial")
public class TriangleGL extends JFrame {

   //private static int DEFAULT_FPS = 80;

   private static final int JPANEL_WIDTH = 512; // initial size of panel
   private static final int JPANEL_HEIGHT = 512;
 
   private TriangleGLListener triangleGLListener;
   
   private GLJPanel jCanvas;

   private static ValueChanger valueChanger;
   
   private FPSAnimator animator;
   private static int DEFAULT_FPS = 40;
   
   private static float moveToLeft = 0;
   public static final int SET = 1;
   public static final int GET = 2;
   
   public TriangleGL() {
     
      super("Frame for my frist triangle");
     
      Container container = this.getContentPane();
      container.setLayout(new BorderLayout());
      this.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            animator.stop();
            valueChanger.interrupt();
            System.exit(0);
         }
      });
     
      JPanel renderJPanel = makeRenderPanel();
      container.add(renderJPanel, BorderLayout.CENTER);
     
      pack();
      setVisible(true);
     
      animator.start();
     
   }
   
   public static void main(String[] args) {
     
      valueChanger = new ValueChanger();
      valueChanger.start();
      new TriangleGL();
     
   }

   private JPanel makeRenderPanel() {
   
      JPanel renderJPanel = new JPanel();
      renderJPanel.setLayout(new BorderLayout());
      renderJPanel.setPreferredSize(new Dimension(JPANEL_WIDTH, JPANEL_HEIGHT));
     
      GLCapabilities glCapabilities = new GLCapabilities();
      glCapabilities.setAlphaBits(8);
      jCanvas = new GLJPanel(glCapabilities);
     
      triangleGLListener = new TriangleGLListener(this);
      jCanvas.addGLEventListener(triangleGLListener);
     
      animator = new FPSAnimator(jCanvas, DEFAULT_FPS, true);
     
      renderJPanel.add(jCanvas, BorderLayout.CENTER);
     
      return renderJPanel;
     
   }
   
   /**
    * Thread-save method to read and write moveToLeft
    *
    * @param mode GET or SET value
    * @param newValue New value.
    * @return
    */

   public static synchronized float moveToLeftAccess(int mode, float newValue) {
     
      switch(mode) {
     
      case (SET):
         moveToLeft = newValue;
         break;
     
      case (GET):
         // return value at end of method (see below)
         break;
      }
     
      return moveToLeft;
     
   }
     
}


My GLListener implementation:

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  
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

public class TriangleGLListener implements GLEventListener {

   private GLU glu;

   public TriangleGLListener(TriangleGL triangle) {

   }

   // ----------------- listener callbacks -----------------------------

   public void display(GLAutoDrawable drawable) {

      // System.out.println("display called");

      GL gl = drawable.getGL();

      // Clear screen and depth buffer
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

      // Reset view (x = 0, y = 0, z = 0)
      gl.glLoadIdentity();

      // Move view - C A L L moveToLeftAccess()!!!
      gl.glTranslatef(TriangleGL.moveToLeftAccess(TriangleGL.GET, 0.0f), 0.0f, -6.0f);

      gl.glBegin(GL.GL_TRIANGLES);
      gl.glVertex3f(0.0f, 1.5f, 0.0f); // Top
      gl.glVertex3f(-1.0f, 0.0f, 0.0f); // Bottom Left
      gl.glVertex3f(1.0f, 0.0f, 0.0f); // Bottom Right
      gl.glEnd();

      gl.glFlush();

   }

   public void reshape(GLAutoDrawable drawable, int x, int y, int width,
         int height) {

      // System.out.println("reshape called");

      GL gl = drawable.getGL();

      // To avoid division by 0 in aspect ration below (gluPerspective()).
      if (height == 0)
         height = 1;

      // Resizes the current viewport
      gl.glViewport(x, y, width, height);

      // Select the projection matrix
      gl.glMatrixMode(GL.GL_PROJECTION);
      // Reset the projection matrix
      gl.glLoadIdentity();

      glu.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);

      // Select the ModelView Matrix
      gl.glMatrixMode(GL.GL_MODELVIEW);
      // Reset the ModelView Matrix
      gl.glLoadIdentity();

   }

   public void init(GLAutoDrawable drawable) {

      // System.out.println("init called");

      // don't make this gl a global!
      GL gl = drawable.getGL();

      // this is okay as a global, but only use in callbacks
      glu = new GLU();

      // Enables Smooth Shading
      gl.glShadeModel(GL.GL_SMOOTH);

      gl.glEnable(GL.GL_DEPTH_TEST);

   }

   public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
         boolean deviceChanged) {
      // not implemented yet.
   }

}


This class simulates my class which actually calculates some stuff on which changeToLeft depends.

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  
public class ValueChanger extends Thread {

   private float moveToLeftInternal;
   
   public ValueChanger() {
      moveToLeftInternal = 0;
   }
   
   public void run() {
     
      while(true) {
         if(isInterrupted()) {
            break;
         }
         changeMoveToLeft();
      }
     
   }
   
   public void changeMoveToLeft() {
     
      if(moveToLeftInternal > -2.0f) {
         moveToLeftInternal = moveToLeftInternal - 0.1f;
      } else {
         // reset value
         moveToLeftInternal = 0;
      }
      TriangleGL.moveToLeftAccess(TriangleGL.SET, moveToLeftInternal);
     
      try {
         Thread.sleep(500);
      } catch (InterruptedException ie) {
         interrupt();
      }
   }

}



Offline Cakey

Junior Devvie





« Reply #4 - Posted 2009-04-19 15:28:10 »

Nicely structured code, also thanks for sharing your solution rather then just saying you figured it out Cheesy

Offline DzzD
« Reply #5 - Posted 2009-04-20 19:28:41 »

you can also perform active rendering and call display whenever you need

http://www.java-gaming.org/topics/active-rendering-with-jogl/17661/view.html

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.

toopeicgaming1999 (71 views)
2014-11-26 15:22:04

toopeicgaming1999 (60 views)
2014-11-26 15:20:36

toopeicgaming1999 (14 views)
2014-11-26 15:20:08

SHC (27 views)
2014-11-25 12:00:59

SHC (25 views)
2014-11-25 11:53:45

Norakomi (31 views)
2014-11-25 11:26:43

Gibbo3771 (25 views)
2014-11-24 19:59:16

trollwarrior1 (38 views)
2014-11-22 12:13:56

xFryIx (77 views)
2014-11-13 12:34:49

digdugdiggy (55 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!