Jump to content
  • Advertisement
Sign in to follow this  
Smit

[java] JOGL GLCanvas setup

This topic is 4132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to use OpenGL in a Java project so I've downloaded installed jogl and the following code compiles fine but it seems OpenGL is never setup. MainWindow which sets up OpenGL.
[source lang='Java']

import java.awt.*;
import javax.swing.*;
import javax.media.opengl.GL;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLDrawableFactory;
    
public class MainWindow extends JFrame
{
    private Renderer mainRenderer;
    private GLCanvas mainCanvas;
    
    public void createMainWindow( )
    {
        setSize( 500, 500 );
        setTitle( "Test" );
        setLocation( 20, 200 );
        
        //Display the window.
        show();
        
        setupOpenGL( );
    }
    
    public void setupOpenGL( )
    {
        GLCapabilities caps = new GLCapabilities();
        caps.setHardwareAccelerated( true );
        caps.setDoubleBuffered( true );

        mainCanvas = new GLCanvas( caps, null, null, null );
        mainRenderer = new Renderer();
        
        mainCanvas.setSize( 320, 240 );
        mainCanvas.addGLEventListener( mainRenderer );
        
        Container con = getContentPane();
        con.add( mainCanvas );
        
        mainCanvas.display();
    }
    
    public void update()
    {
        mainCanvas.display();
    }
}

Renderer
[source lang='Java']
import javax.media.opengl.GL;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;

public class Renderer implements GLEventListener
{
private static final GLU glu = new GLU();
    public void display( GLAutoDrawable glDrawable )
    {
        final GL gl = glDrawable.getGL();
        
        gl.glClear( GL.GL_COLOR_BUFFER_BIT );
        gl.glClear( GL.GL_DEPTH_BUFFER_BIT );
        
        gl.glLoadIdentity();
        gl.glTranslatef( 0.0f, 0.0f, -5.0f );
        
        gl.glBegin( GL.GL_QUADS );
            gl.glColor3f( 1.0f, 0.0f, 0.0f );
            gl.glVertex3f( -1.0f, 1.0f, 0.0f );
            gl.glVertex3f( -1.0f, -1.0f, 0.0f );
            gl.glVertex3f( 1.0f, -1.0f, 0.0f );
            gl.glVertex3f( 1.0f, 1.0f, 0.0f );
        gl.glEnd();
    }
    
    public void displayChanged( GLAutoDrawable glDrawable, boolean modeChanged, boolean deviceChanged )
    {
  
    }
    
    public void init( GLAutoDrawable glDrawable )
    {
        final GL gl = glDrawable.getGL();
       gl.glShadeModel(GL.GL_SMOOTH);
       gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
       gl.glClearDepth(1.0f);
       gl.glEnable(GL.GL_DEPTH_TEST);
       gl.glDepthFunc(GL.GL_LEQUAL);
       gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
    }
    
    public void reshape( GLAutoDrawable glDrawable, int x, int y, int width, int height ) 
    {
        final GL gl = glDrawable.getGL();
       if(height <= 0) {
           height = 1;
       }
       final float h = (float)width / (float)height;
       gl.glMatrixMode(GL.GL_PROJECTION);
       gl.glLoadIdentity();
       glu.gluPerspective(50.0f, h, 1.0, 1000.0);
       gl.glMatrixMode(GL.GL_MODELVIEW);
       gl.glLoadIdentity();
    }
    
}

Main Loop
[source lang='Java']
while( bQuit == false )
{
     mainWindow.update();
}

It compiles fine and I get a window but I don't get any OpenGL stuff at all. Every gl/java tut/article seems to setup in a completly different way each time and I can't seem to get any of them working. Thanks for looking!

Share this post


Link to post
Share on other sites
Advertisement
You create your Renderer class, but you never call init() or reshape() on it.


public void setupOpenGL( )
{
GLCapabilities caps = new GLCapabilities();
caps.setHardwareAccelerated( true );
caps.setDoubleBuffered( true );

mainCanvas = new GLCanvas( caps, null, null, null );
mainRenderer = new Renderer();

//////////////////////////
//never called
//
//mainRenderer.init();
//mainRenderer.reshape();
//////////////////////////

mainCanvas.setSize( 320, 240 );
mainCanvas.addGLEventListener( mainRenderer );

Container con = getContentPane();
con.add( mainCanvas );

mainCanvas.display();
}


Share this post


Link to post
Share on other sites
Quote:
Original post by CaptainJester
You create your Renderer class, but you never call init() or reshape() on it.

*** Source Snippet Removed ***
It has been a long time since I worked with JOGL but I believe those are handled automatically by GLEventListener, so you don't need to explicitly call them (well at least I never did in anything I made with JOGL but could well be wrong).

As for your code, the only thing I can see that might be a problem is the way you add the canvas to your JFrame. I would give the container a BorderLayout and add the canvas at BorderLayout.CENTER

eg.
Container con = getContentPane();
con.setLayout(new BorderLayout());
con.add( mainCanvas, BorderLayout.CENTER );
Hope this helps,
ViLiO

Share this post


Link to post
Share on other sites
Everything I've read seems to indicate that the methods in Renderer are called auotmatically.

I tried the border layout too but I still get a blank window. Could someone try the code above on their machine? Thanks for the help!

Share this post


Link to post
Share on other sites
Copied your code into a project, compiled and run. Works fine. I see a litle red square on a black background. You are only drawing to a part of the window though. Should setup viewport with glViewport in reshape.

The only other thing I did was putting a Thread.sleep(100); in the main loop so the app do not eat all of the CPU.

What is the results you are getting?

/Lizard

Share this post


Link to post
Share on other sites
From the documentation of Container.add(Component):

* Note: If a component has been added to a container that
* has been displayed, <code>validate</code> must be
* called on that container to display the new component.


From a working JOGL application:

class Main extends Frame implements GLEventListener
{
Animator animator;

Main ()
{
GLCapabilities capabilities = new GLCapabilities ();
GLCanvas canvas = new GLCanvas (capabilities);
animator = new Animator (canvas);
canvas.addGLEventListener (this);
add (canvas, BorderLayout.CENTER);
animator.start ();
}
...
public static void main (String[] args)
{
Main main = new Main ();
main.pack ();
main.setVisible (true);
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!