Login  Register

Need help getting started with glTranslate

Posted by zonski on Aug 20, 2011; 8:59am
URL: https://forum.jogamp.org/Need-help-getting-started-with-glTranslate-tp3270178.html

Hi,

I need a little kick-start to get me passed a very simple newbie problem. I have successfully created a triangle using JOGL 2 (win 64bit) using these useful tutorials: https://sites.google.com/site/justinscsstuff/jogl-tutorials

Now I want to apply a translation to it. When I apply an x and y translation it all works as I had hoped but when I apply a z-translation things don't go so well. If my z is between -1.0 and 1.0 it seems to have no affect, then the moment I go outside that range (say -2.0), my triangle just disappears and I see a blank screen. I was hoping it would shrink off into the distance?

I assume it is something to do with my viewport or something I am not doing in the init or reshape routines, but I have no idea what. I have tried a lot of different options and been through all the tutorials I can find but without luck.

Any help would be appreciated!

My simple code is this:

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestApp implements GLEventListener
{
    public static void main(String[] args)
    {
        GLProfile.initSingleton(true);

        GLProfile glp = GLProfile.getDefault();
        GLCapabilities caps = new GLCapabilities(glp);
        GLCanvas canvas = new GLCanvas(caps);

        TestApp testApp = new TestApp();
        canvas.addGLEventListener(testApp);

        Frame frame = new Frame("AWT Window Test");
        frame.add(canvas);
        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        frame.setSize(800, 640);
        frame.setVisible(true);
    }

    public void dispose(GLAutoDrawable drawable) { }

    public void init(GLAutoDrawable drawable) { }

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

    public void display(GLAutoDrawable drawable)
    {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);

        gl.glLoadIdentity();

        // if z is between -1 and 1, I see my triangle, otherwise just a blank screen
        float z = -2.0f;
        gl.glTranslatef(0.0f, 0.0f, z);

        gl.glBegin(GL.GL_TRIANGLES);
        gl.glVertex2f(0, 1);
        gl.glVertex2f(1, -1);
        gl.glVertex2f(-1, -1);
        gl.glEnd();
    }
}