📜  JOGL基元

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

JOGL基元

作为一个开放的图形库,我们可以在JOGL中绘制不同的形状,例如圆形,三角形,正方形。因此,要绘制这些形状,Open GL提供了便于JOGL绘制2D和3D尺寸图形的图元。

OpenGL Primitive提供了各种内置参数,可以根据这些参数绘制不同的形状。每个参数在图形中都有特定的作用。

JOGL内置基元

以下是JOGL支持的各种OpenGL内置参数:-

Primitive Description
GL_LINES It treats each pair of vertices as line segment.
GL_LINES_STRIP Connects group of line segment with each other.
GL_LINES_LOOP Connects group of line segment in a loop i.e. from first to last and then back to the first.
GL_TRIANGLE Each vertices of triplet behaves as an independent triangle
GL_TRIANGLE_FAN Creates a connected group of triangle where each triangle is defined for a particular vertex presented after the first two vertex.
GL_TRIANGLE_STRIP Connects group of triangle with each other.
GL_QUADS Each group of four vertices behaves as an independent quadrilateral.
GL_QUAD_STRIP Connects group of quadrilateral with each other.
GL_POLYGON Draws a single, convex polygon.

Display()方法

display()方法包含用于绘制和显示形状的代码。请按照以下步骤系统地安排代码:-

  • 最初,使用display()方法作为参数传递GLAutoDrawable接口的对象。
public void display(GLAutoDrawable drawable)
  • 生成GL2接口的对象。
final GL2 gl = drawable.getGL().getGL2();
  • 将该对象与glBegin()方法相关联,并传递所需的原语类型作为其参数。
gl.glBegin (GL2.GL_LINES);

在这里,我们提供GL_LINES原语。

  • 现在,使用glVeretex2d()方法绑定GL2对象,并传递特定的坐标。
gl.glVertex2d(-0.60, 0.10);
gl.glVertex2d(0.60, 0.10);
  • 通过调用glEnd()方法结束该行。
gl.glEnd();

基本线示例

在此示例中,我们将学习如何使用基元绘制一条简单的线。

BasicLine.java

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

public class BasicLine implements GLEventListener {

    @Override
public void init(GLAutoDrawable arg0) 
  {
        
  }

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

gl.glBegin (GL2.GL_LINES);
gl.glVertex2d(-0.60, 0.10);
gl.glVertex2d(0.60, 0.10);
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);
BasicLine bl = new BasicLine();
gc.addGLEventListener(bl);
gc.setSize(400, 400);

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

输出: