Ruth Guo Ruth Guo - 7 months ago 20
Java Question

Why can polygons be displayed but the point cannot

public class SimpleJOGL2 implements GLEventListener {

private float x = 0.5f;
private float y = -0.5f;
public static void main(String[] args) {
Frame frame = new Frame("Simple JOGL Application");
GLCanvas canvas = new GLCanvas();


canvas.addGLEventListener(new SimpleJOGL2());
frame.add(canvas);
frame.setSize(640, 640);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {

@Override
public void windowClosing(WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable() {

public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
// Center frame
frame.setLocationRelativeTo(null);
frame.setVisible(true);
animator.start();
}


And...

public void display(GLAutoDrawable drawable) {

GL gl = drawable.getGL();


float a[][] = {{x,y},{x,y-0.05f},{x-0.2f,y-0.15f},{x-0.22f, y-0.17f}};
float b[][] = {{x,y},{x,y-0.05f},{x+0.2f,y-0.15f},{x+0.22f, y-0.17f}};
float c[][]= {{x+0.03f,y-0.25f},{x-0.03f,y-0.25f},{x-0.03f,y-0.4f},{x+0.03f,y-0.4f}};
float col[] ={230/255f,84/255f,109/255f};
drawPolygon(gl,a,col);
drawPolygon(gl,b,col);
drawPolygon(gl,c,col);

gl.glLoadIdentity();
gl.glColor3f(0,0,0);
gl.glPointSize(4.0f);
gl.glBegin(GL.GL_POINT);
gl.glVertex2f(x+0.015f, y-0.33f);
gl.glEnd();
gl.glFlush();


}


I want to draw polygons and points. polygons can be displayed, but the point cannot.

public void drawPolygon(GL gl, float a[][], float col[]){
gl.glColor3f(col[0], col[1], col[2]);
gl.glBegin(GL.GL_POLYGON);
gl.glVertex2f(a[0][0], a[0][1]);
gl.glVertex2f(a[1][0], a[1][1]);
gl.glVertex2f(a[2][0], a[2][1]);
gl.glVertex2f(a[3][0], a[3][1]);

gl.glEnd();
}

}

Answer

Hi and welcome on Stackoverflow :)

That code is using deprecated OpenGL functions and it is highly suggested to not using those unless under special conditions, which I think it's not your case.

Current OpenGL actually allows three tipes of primitives:

  • GL_TRIANGLES
  • GL_POINTS
  • GL_LINES

If you want to read more on this, you can read the wiki.

Anyway, for beginners, it's preferred to start from a simple Hello Triangle, like these ones of mine and then using that as a base to build on.

So if you want to display any kind of polygon, you should divide it in triangles and render it passing triangles as primitive type

If you need further help, don't hesitate ask, good work!