setting up 3D in OpenGL

This seems like a really stupid problem that I don’t think should be in the more advanced forum…

I’ve searched everywhere and through many tutorials and I still can’t draw a polygon - they’re always too complex with multiple methods or too C-driven. I can use gluOrtho2D and draw a 2D polygon, but I just can’t get perspective/3D to work. I need someone to tell me flat-out what’s wrong.

import java.awt.Frame; // frame of window
import javax.media.opengl.GLCanvas; // screen
import javax.media.opengl.GLEventListener; // ogl events
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLDrawable; // for processevents method
import javax.media.opengl.GL;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.glu.GLU;

public class TestGL
{
  GLCanvas canvas;
  EventHandler eventHandler;
  final int WIDTH = 500;
  final int HEIGHT = 500;
  
  public TestGL()
  {
    Frame frame = new Frame("TestGL");
    GLCapabilities caps = new GLCapabilities();
    caps.setDoubleBuffered(true);
    caps.setHardwareAccelerated(true);
    canvas = new GLCanvas(caps);
    canvas.setBounds(0,0,WIDTH,HEIGHT); // set canvas size
    canvas.setIgnoreRepaint(true);
    eventHandler = new EventHandler();
    canvas.addGLEventListener(eventHandler); // gl event listener
    frame.add(canvas);
    frame.setResizable(false);
    frame.setSize(506,532);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null); // center window
    canvas.display();
  }
  public static void main(String[] args)
  {
    TestGL t = new TestGL();
  }
  private class EventHandler implements GLEventListener
  {
    private GLU glu = new GLU();
    public void displayChanged(GLAutoDrawable glDrawable, boolean modeChanged, boolean deviceChanged){}
    public void reshape(GLAutoDrawable drawable, int xstart, int ystart, int width, int height){}
    public void processEvents(GLDrawable glDrawable){}
    public void init(GLAutoDrawable glDrawable)
    {
      GL gl = glDrawable.getGL();
      gl.glShadeModel(GL.GL_SMOOTH); // smooth color shading mode to create gradient colour
      gl.glClearColor(0.5f,0.5f,0.5f,0.5f); // clearing colour
      gl.glEnable(GL.GL_DEPTH_TEST); // enable depth test
      gl.glClearDepth(300.0);  // set depth buffer to the most distant value
      gl.glDepthFunc(GL.GL_LEQUAL); // type of depth test
      gl.glViewport(0, 0, WIDTH, HEIGHT);
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
      glu.gluPerspective(45.0f,(float)WIDTH/(float)HEIGHT,0.1f,300.0f);
      //glu.gluOrtho2D(0,WIDTH,0,HEIGHT);
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
    }
    public void display(GLAutoDrawable glDrawable)
    {
      GL gl = glDrawable.getGL();
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // clear the buffers
      gl.glLoadIdentity();
      
      // render displayable objects
      gl.glColor3f(1.0f,0.0f,0.0f);
      //gl.glColor3i(255,0,0);
      gl.glBegin(GL.GL_POLYGON);
      gl.glVertex3i(259,25,-200);
      gl.glVertex3i(239,245,-200);
      gl.glVertex3i(5,235,-200);
      gl.glEnd();
      
      gl.glFlush();
    }
  }
}

If I swap the perspective and ortho2d lines and change the vertices to 2D, it draws.

A bit unrelated, how do you use glColor3i? I can’t find anything useful about it and anything I try gives me black.

You’re only allowed to call me an idiot if you fix this! ;D

gluPerspective sets up a coordinate-system with the range of -1 to 1, so your values for the glVertex calls are too high.

Thanks, it makes sense now and it works!