Need help getting started with glTranslate

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Need help getting started with glTranslate

zonski
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();
    }
}






Reply | Threaded
Open this post in threaded view
|

Re: Need help getting started with glTranslate

Wade Walker
Administrator
Hi zonski,

You're probably experiencing view frustum clipping  You can check out links like http://www.lighthouse3d.com/tutorials/view-frustum-culling/ to see examples of how to change it. Essentially, OpenGL doesn't draw anything outside a truncated pyramid with its point on the camera.
Reply | Threaded
Open this post in threaded view
|

Re: Need help getting started with glTranslate

zonski
Hi Wade,

Thanks for the tip and the link. It got me to my solution and I now have it working! I'm sure I'd tried each of the bits of the puzzle but that article helped me understand it and put it all together. Nice one!

For future reference, my working code looks like this:

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

public class TestJoglApp implements GLEventListener
{
    private GLU glu;

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

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

        TestJoglApp testJoglApp = new TestJoglApp();
        canvas.addGLEventListener(testJoglApp);

        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)
    {
        glu = new GLU();
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
    {
        GL2 gl = drawable.getGL().getGL2();

        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        float aspect = width / height;
        glu.gluPerspective(50.0, aspect, 1.0, 100.0);

        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
        glu.gluLookAt(
                0.0, 0.0, 10.0,
                0.0, 0.0, 0.0,
                0.0, 1.0, 0.0);
    }

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

        gl.glPushMatrix();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        float z = -5.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();

        gl.glPopMatrix();
    }
}

Now I can move on to some harder stuff.
Reply | Threaded
Open this post in threaded view
|

Re: Need help getting started with glTranslate

Wade Walker
Administrator
Thanks for posting your final code -- having examples like this in the forum will definitely help others in the future.

You had already done 99% of the work with your experiments to see what translations made the object disappear. I think this sort of experimentation is crucial to OpenGL programming, since there are so many things that can be wrong even in a very short OpenGL program. Half the time when I write a program from scratch (as opposed to modifying a working program) nothing shows up on screen at all when I first run it