📜  JOGL基本体形状

📅  最后修改于: 2021-01-05 00:37:30             🧑  作者: Mango

JOGL基本形状

在前面的示例中,我们已经了解到在JOGL中可以借助GL_LINES参数绘制任何类型的形状。但是除此之外,JOGL还包含其他各种参数,通过这些参数我们可以直接绘制特定形状,例如三角形,四边形,多边形等(即,以更简单的方式)。

JOGL基本三角形示例

在此示例中,我们将看到如何在基本三角形参数(GL_TRIANGLES)的帮助下创建三角形。

Triangle.java

package com.javatpoint.jogl;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

public class Triangle implements GLEventListener {

    @Override
public void init(GLAutoDrawable arg0) 
  {
        
  }

   @Override
public void display(GLAutoDrawable drawable) {

    final GL2 gl = drawable.getGL().getGL2();
    
    gl.glBegin(GL2.GL_TRIANGLES);       
    
    gl.glVertex2d(0,0.5);
    gl.glVertex2d(-0.5,-0.5);
    gl.glVertex2d(0.5,-0.5);
    
    gl.glEnd();
    }

   @Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) 
   {    

   }
   @Override
public void dispose(GLAutoDrawable arg0) 
   {

   }

public static void main(String[] args) {

final GLProfile gp = GLProfile.get(GLProfile.GL2);
GLCapabilities cap = new GLCapabilities(gp);

final GLCanvas gc = new GLCanvas(cap);
      Triangle tr = new Triangle();
gc.addGLEventListener(tr);
gc.setSize(400, 400);

final JFrame frame = new JFrame("JOGL Triangle");
frame.add(gc);
frame.setSize(500,400);
frame.setVisible(true);  
   }    
}

输出:

JOGL基本四边形示例

在此示例中,我们将看到如何在基本四边形参数(GL_QUADS)的帮助下创建四边形。

JQuad.java

package com.javatpoint.jogl;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

public class JQuad implements GLEventListener {

    @Override
public void init(GLAutoDrawable arg0) 
  {
        
  }

   @Override
public void display(GLAutoDrawable drawable) {

    final GL2 gl = drawable.getGL().getGL2();
    
    
    gl.glBegin(GL2.GL_QUADS);
    
    gl.glVertex3f( 0.0f,0.5f,0);
    gl.glVertex3f(-0.5f,0f,0);
    gl.glVertex3f(0f,-0.5f,0);
    gl.glVertex3f(0.5f,0f,0);
    
    gl.glEnd();
    
    
    }

   @Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) 
   {    

   }
   @Override
public void dispose(GLAutoDrawable arg0) 
   {

   }

public static void main(String[] args) {

final GLProfile gp = GLProfile.get(GLProfile.GL2);
GLCapabilities cap = new GLCapabilities(gp);

final GLCanvas gc = new GLCanvas(cap);
JQuad jq = new JQuad();
gc.addGLEventListener(jq);
gc.setSize(400, 400);

final JFrame frame = new JFrame("JOGL Primitive Quadrilateral");
frame.add(gc);
frame.setSize(500,400);
frame.setVisible(true);  
   }    
}

输出: