📜  JOGL-基本模板的API

📅  最后修改于: 2020-11-13 04:52:04             🧑  作者: Mango


使用JOGL编程,可以绘制各种图形形状,例如直线,三角形,3D形状,包括特殊效果,例如旋转,照明,颜色等。要在JOGL中绘制对象,首先我们必须构造一个基本的JOGL框架。下面给出的是构造基本框架所需的类。

GLEventListener接口

为了使您的程序能够使用JOGL图形API,您需要实现GLEventListener接口。您可以在javax.media.opengl包中找到GLEventListener接口。

下表提供了GLEventListener接口的各种方法和描述的详细信息-

Sr.No. Methods and Descriptions
1

Void display(GLAutoDrawable drawable)

It is called by the object of GLAutoDrawable interface to initiate OpenGL rendering by the client. i.e., this method contains the logic used to draw graphical elements using OpenGL API.

2

Void dispose(GLAutoDrawable drawable)

This method signals the listener to perform the release of all OpenGL resources per each GLContext, such as memory buffers and GLSL programs.

3

Void init(GLAutoDrawble drawable)

It is called by the object of GLAutoDrawable interface immediately after the OpenGL context is initialized.

4

Void reshape(GLAutoDrawble drawble, int x, int y, int width, int height)

It is called by the object of GLAutoDrawable interface during the first repaint after the component has been resized. It is also called whenever the position of the component on the window, is changed.

GLEventListener的所有方法都需要GLAutoDrawable接口的对象作为参数。

GLAutoDrawable接口

此接口提供了用于执行OpenGL渲染的基于事件的机制(GLEventListener)GLAutoDrawable会在对象的生存期内自动创建与GLAutoDrawable关联的主渲染上下文。

下表提供了GLAutoDrawable接口的各种方法和描述的详细信息-

Sr.No Methods and Descriptions
1

GL getGL()

Returns the GL pipeline object that is used by the current object of GLAutoDrawable interface.

2

void addGLEventListener(GLEventListener Listener)

Adds the given listener to the end of current drawable queue.

3

void addGLEventListener(int index, GLEventListener listener)

Adds the given listener at the given index of this drawable queue.

4

void destroy()

Destroys all resources associated with this object of GLAutoDrawable interface, including the GLContext.

注意-此包中还有其他方法。在此界面中仅讨论了与模板有关的一些重要方法。

GLCanvas类

GLCanvasGLJpanel是实现GLAutoDrawable接口的JOGL GUI的两个主要类,可以用作OpenGL命令的绘图表面。

GLCanvas是一个重量级的AWT组件,它提供OpenGL渲染支持。这是AWTAutoGLDrawable接口的主要实现。它还继承了java.awt.Canvas类。由于它是重量级组件,因此在某些情况下, GLJCanvas可能无法正确地与回转组件集成。因此,在与Swing一起使用时必须小心。每当遇到GLJCanvas问题时,就必须使用GLJPanel类。

GLCanvas的层次结构图可以如下所示-

帆布

  • GLEventistener接口与GLCanvas类一起使用。它响应GLCanvas类中的更改以及它们所做出的绘图请求。

  • 每当实例化GLCanvas类时,就会调用GLEventListenerinit()方法。您可以重写此方法以初始化OpenGL状态。

  • 每当GLCanvas最初被绘制(实例化)或调整大小时,就会执行GLEventListenerreshape()方法。它用于初始化OpenGL视口和投影矩阵。每当更改组件的位置时,也会调用它。

  • GLEventListener的display()方法包含用于渲染3D场景的代码。每当调用GLCanvas的display()方法时,都会调用它。

下面给出的是实例化GLCanvas类所需的构造函数。

Sr.No Constructor and Description
1

GLCanvas()

It creates a new GLCanvas component with a default set of OpenGL capabilities, using the default OpenGL capabilities selection mechanism, on the default screen device.

2

GLCanvas(GLCapabilitiesImmutable)

It creates a new GLCanvas component with the requested set of OpenGL capabilities using the default OpenGL capabilities selection mechanism on the default screen device.

下面给出的是用于GLCanvas类的事件处理的方法。

Sr. No. Methods and Description
1

void addGLEventListener(GLEventListener listener)

Adds the given listener to the end of this drawable queue.

2

void addGLEventListener(int indexGLEventListener listener)

Adds the given listener at the given index of this drawable queue.

要实例化GLCanvas类,您需要GLCapabilitiesImmutable接口的对象,该对象指定一组不变的OpenGL功能。

获取CapabilitiesImmutable接口对象的一种方法是实例化GLCapabilities类,该类实现了该接口。 GLCapabilities类的实例可用于实现此目的。

GLCapabilities类

此类指定一组OpenGL功能。它以GLCapabilities对象为参数。 GLCapabilities类描述了渲染上下文必须支持的所需功能,例如OpenGL配置文件。

下面给出的是一个实例化GLCapabilities类的构造函数

Sr. No. Methods and Description
1

GLCapabilities(GLProfile glprofile)

It creates a GLCapabilities object.

要实例化GLCanvas类,您需要一个GLCapabilitiesImmutable接口的对象,该对象指定一组不变的OpenGL功能。

获取CapabilitiesImmutable接口的对象的一种方法是实例化GLCapabilities类,该类实现了该接口。 GLCapabilities类的实例可用于实现此目的。

GLCapabilities类又需要一个GLProfile对象。

GLProfile类别

由于发布了多个版本的OpenGL API;您需要为Java虚拟机(JVM)指定程序中使用的OpenGL API的确切版本。这是使用GLProfile类完成的。

此类的get()方法接受不同的预定义String对象作为参数。每个String对象都是一个接口的名称,并且每个接口都支持某些版本的OpenGL。如果将此类初始化为静态和单例,则它将为每个可用的JOGL配置文件提供单例GLProfile对象。

下面给出的是GLProfile类的get方法的原型。

Sr.No. Method and Description
1

Static GLProfile get(String profile)

Uses the default device.

由于这是静态方法,因此您需要使用类名来调用它,并且它需要预定义的静态字符串变量作为参数。此类中有12个此类变量,每个变量代表GL接口的单独实现。

GLProfile.get(GLProfile.GL2);

get()方法的参数

Sr.No Predefined String value (Interface name) and Description
1

GL2

This interface contains all OpenGL [1.0 … 3.0] methods as well as most of its extensions defined at the time of this specification.

2

GLES1

This interface contains all OpenGL ES [1.0 … 1.1] methods as well as most of its extensions defined at the time of this specification.

3

GLES2

This interface contains all OpenGL ES 2.0 methods as well as most of its extensions defined at the time of this specification.

4

GLES3

This interface contains all OpenGL ES 3.0 methods as well as most of its extensions defined at the time of this specification.

5

GL2ES1

This Interface contains the common subset of GL2 and GLES1.

6

GL2ES2

This Interface contains the common subset of GL3, GL2, and GLES2.

7

GL2GL3

This Interface contains the common subset of core GL3 (OpenGL 3.1+) and GL2.

8

GL3

This interface contains all OpenGL [3.1 … 3.3] core methods as well as most of its extensions defined at the time of this specification.

9

GL3bc

This interface contains all OpenGL [3.1 … 3.3] compatibility methods, as well as most of its extensions defined at the time of this specification.

10

GL3ES3

This interface contains the common subset of core GL3 (OpenGL 3.1+) and GLES3 (OpenGL ES 3.0).

11

GL4

This interface contains all OpenGL [4.0 … 4.3] core methods, as well as most of its extensions defined at the time of this specification.

12

GL4bc

This interface contains all OpenGL [4.0 … 4.3] compatibility profile, as well as most of its extensions defined at the time of this specification.

13

GL4ES3

Interface containing the common subset of core GL4 (OpenGL 4.0+) and GLES3 (OpenGL ES 3.0).

GLJPanel类别

它是一个轻量级的Swing组件,提供OpenGL渲染支持。提供它是为了与Swing兼容。

GLJPanel类层次结构

下图显示了GLJPanel类的类层次结构。

GJPanel

下面给出了各种构造函数GLJPanel类。

Sr. No. Constructors and Description
1

GJPanel()

It creates a new GLJPanel component with a default set of OpenGL capabilities.

2

(GLCapabilitiesImmutable)

It creates a new GLJPanel component with the requested set of OpenGL capabilities.

3

GLJPanel(GLCapabilitiesImmutable userCapsRequest, GLCapabilitiesChooser chooser)

It creates a new GLJPanel component.

下面给出了GLJPanel类的方法。

Sr.No. Methods and Description
1

void addGLEventListener(GLEventListener listener)

This method adds the given listener to the end of this drawable queue.

2

void addGLEventListener(int indexGLEventListener listener)

This method adds the given listener at the given index of this drawable queue.