Re: Need help getting started with glTranslate
Posted by
zonski on
Aug 21, 2011; 2:50am
URL: https://forum.jogamp.org/Need-help-getting-started-with-glTranslate-tp3270178p3272063.html
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.