Need little help getting startet

Hello, i hope i am right here…
i was coding c++ and actionscript, but for games i need an
openGL system…but i am not very expericened in the java.

i found jogl/java(eclipse) and installed it…first 2 example on the wiki
work perfekt but i got some litte questions about it.
But a little thing i cant find in the tutorials.

you see in the code, i try a bit helpless
to use an call for an renderer loop to the opengl thing,
but i dont know how to access/handle it .
i know that every opengl things must be in the->
“class SceneView implements GLEventListener”
but how can i implement a render loop call
to update the scene every frame.

thanks for any help :slight_smile: (and sorry for my bad english)


import javax.swing.*;
import javax.media.opengl.*;
import com.sun.opengl.util.GLUT;
import java.awt.geom.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import com.sun.opengl.util.texture.*;

import java.applet.*;
import java.awt.*;

public class fso extends JFrame 
{
  GLCanvas canvas;      

  static Thread t = null;
  static int i=0;
 
  
  public fso()
  {
    GLCapabilities cap = new GLCapabilities();
    
    canvas = new GLCanvas(cap);

    canvas.addGLEventListener(new SceneView());
   
    getContentPane().add(canvas);
   
    setTitle("Game Scene");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(900, 600);
    setVisible(true);  
    
    

    
  }

  public static void run()
  {
	
	    while(true)
	    {
	      i++;
	      renderer();  //  i want to refresh the opengl frame and this call is surly wrong
	  
	      try {
	        t.sleep(1000/30);
	      } catch (InterruptedException e) { ; }
	    }
	    
  }


  private static void renderer() {
// i think here i am wrong...

	// TODO Auto-generated method stub
}


class SceneView implements GLEventListener
  {

	 
	  
	public void init(GLAutoDrawable arg0) 
    {
      GL gl = arg0.getGL();
      float l_position[] = {100.0f, 100.0f, 200.0f, 1.0f};
  
      gl.glEnable(GL.GL_LIGHTING);
      gl.glEnable(GL.GL_LIGHT0);
      gl.glEnable(GL.GL_COLOR_MATERIAL);
      gl.glEnable(GL.GL_DEPTH_TEST);
      gl.glEnable(GL.GL_NORMALIZE);
      gl.glEnable(GL.GL_POLYGON_SMOOTH);
      gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, l_position, 0);  
  
      gl.glClearColor(0.7f, 0.7f, 0.7f, 0.0f);
                  
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glOrtho(-100, 100, -100, 100, -100, 100);
      int i=120;
  
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glRotatef(i, 1.0f, 0.0f, 0.0f);    // Rotation um die x-Achse
      gl.glRotatef(-25.0f, 0.0f, 1.0f, 0.0f);   // Rotation um die y-Achse
    }

	 public void renderer(GLAutoDrawable arg0) 
	    {
	// how can i access this renderer from a loop
            //to refresh here the opengl scene..or is there any
            //other predefinded thing in jogl for that?
	    }

	 
    public void display(GLAutoDrawable arg0) 
    {
      GL gl = arg0.getGL();
      GLUT glut =  new GLUT();
           
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
      gl.glColor3f(0.2f, 0.0f, 0.3f);
      glut.glutSolidTeapot( 50.0 ) ;
    }


    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) 
    {
    }


    public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) 
    {
    }
  }


    
  public static void main(String args[]) 
  {

	//init
    new fso(); 
    //Main Loop
    run();
    
    
  }
}


You don’t need to set up your own rendering loop in JOGL and for the beginning it is imho better not to try (It isn’t hard to do, but you have to have a little background on JOGL threading and the stability problems that can occur with some opengl drivers). You can simply use an Animator to refresh your scene periodically. Here is a template we use for our JOGL projects:


public class SimpleJOGL implements GLEventListener
{
	public static void main(String[] args)
	{
		Frame frame = new Frame("Simple JOGL Application");
		GLCanvas canvas = new GLCanvas();
		
		canvas.addGLEventListener(new SimpleJOGL());
		frame.add(canvas);
		frame.setSize(640, 480);
		final Animator animator = new Animator(canvas);
		frame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				// Run this on another thread than the AWT event queue to
				// make sure the call to Animator.stop() completes before
				// exiting
				new Thread(new Runnable()
				{
					public void run()
					{
						animator.stop();
						System.exit(0);
					}
				}).start();
			}
		});
		// Center frame
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		animator.start();
	}
	
	public void init(GLAutoDrawable drawable)
	{
		// Use debug pipeline
		// drawable.setGL(new DebugGL(drawable.getGL()));
		
		GL gl = drawable.getGL();
		System.err.println("INIT GL IS: " + gl.getClass().getName());
		
		// Enable VSync
		gl.setSwapInterval(1);
		
		// Setup the drawing area and shading mode
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
	}
	
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
	{
		GL gl = drawable.getGL();
		GLU glu = new GLU();

		if (height <= 0) // avoid a divide by zero error!
				height = 1;
		final float h = (float) width / (float) height;
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		glu.gluPerspective(45.0f, h, 1.0, 20.0);
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
	}
	
	public void display(GLAutoDrawable drawable)
	{
		GL gl = drawable.getGL();
		
		// Clear the drawing area
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		// Reset the current matrix to the "identity"
		gl.glLoadIdentity();
		
		// Move the "drawing cursor" around
		gl.glTranslatef(-1.5f, 0.0f, -6.0f);

		// Drawing Using Triangles
		gl.glBegin(GL.GL_TRIANGLES);		    
			gl.glColor3f(1.0f, 0.0f, 0.0f);    // Set the current drawing color to red
			gl.glVertex3f(0.0f, 1.0f, 0.0f);   // Top
			gl.glColor3f(0.0f, 1.0f, 0.0f);    // Set the current drawing color to green
			gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
			gl.glColor3f(0.0f, 0.0f, 1.0f);    // Set the current drawing color to blue
			gl.glVertex3f(1.0f, -1.0f, 0.0f);  // Bottom Right
		// Finished Drawing The Triangle
		gl.glEnd();
		
		// Move the "drawing cursor" to another position
		gl.glTranslatef(3.0f, 0.0f, 0.0f);
		// Draw A Quad
		gl.glBegin(GL.GL_QUADS);
			gl.glColor3f(0.5f, 0.5f, 1.0f);    // Set the current drawing color to light blue
			gl.glVertex3f(-1.0f, 1.0f, 0.0f);  // Top Left
			gl.glVertex3f(1.0f, 1.0f, 0.0f);   // Top Right
			gl.glVertex3f(1.0f, -1.0f, 0.0f);  // Bottom Right
			gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
		// Done Drawing The Quad
		gl.glEnd();
		
		// Flush all drawing operations to the graphics card
		gl.glFlush();
	}
	
	public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
	{}
}

If you want to limit the CPU load for rendering, you can use the FPSAnimator as an alternative (http://download.java.net/media/jogl/builds/nightly/javadoc_public/com/sun/opengl/util/FPSAnimator.html)

thanks for your reply :slight_smile:

i am still a bit confused,cause i think i am too new to this stuff.

i try tour code,but i didnt get it to run to see how it works

What’s the problem. It should work out of the box, when you have jogl.jar and gluegen-rt.jar on your classpath and set up the native libraries correctly. Post full stacktraces and error messages, when you still can’t get it to work.

thanks, i got it.

i call the FPSAnimator but the main loop
is in the display( )…

now i got it…thanks :slight_smile: