📜  JOGL-预定义形状

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


在前面的章节中,我们学习了如何使用JOGL绘制线,三角形,菱形等形状。我们通过将预定义的字段Gl_lines传递glBegin()方法来绘制线条。

除了GL_LINES之外glBegin()方法还接受八个参数。您可以使用它们绘制不同的形状。它们的用法与GL_LINES相同。

下表显示了glBegin()方法参数及其说明-

Sr.No Parameters and Description
1

GL_LINES

Creates each pair of vertices as an independent line segment.

2

GL_LINE_STRIP

Draws a connected group of line segments from the first vertex to the last.

3

GL_LINE_LOOP

Draws a connected group of line segments from the first vertex to the last, again back to the first.

4

GL_TRIANGLES

Treats each triplet of vertices as an independent triangle.

5

GL_TRIANGLE_STRIP

Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices.

6

GL_TRIANGLE_FAN

Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices.

7

GL_QUADS

Treats each group of four vertices as an independent quadrilateral.

8

GL_QUAD_STRIP

Draws a connected group of quadrilaterals. One quadrilateral is defined for each pair of vertices presented after the first pair.

9

GL_POLYGON

Draws a single, convex polygon. Vertices 1,…,n define this polygon.

让我们来看一些使用glBegin()参数的示例。

绘制线条的程序

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;

import javax.swing.JFrame;

public class LineStrip implements GLEventListener {

   @Override
   public void display(GLAutoDrawable drawable) {
   
      final GL2 gl = drawable.getGL().getGL2();
        
      gl.glBegin (GL2.GL_LINE_STRIP);
      gl.glVertex3f(-0.50f,-0.75f, 0);
      gl.glVertex3f(0.7f,0.5f, 0);
      gl.glVertex3f(0.70f,-0.70f, 0);
      gl.glVertex3f(0f,0.5f, 0);
      gl.glEnd();
   }
   
   @Override
   public void dispose(GLAutoDrawable arg0) {
      //method body
   }
   
   @Override
   public void init(GLAutoDrawable arg0) {
      // method body
   }
   
   @Override
   public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
      // method body
   }
   
   public static void main(String[] args) {
   
      //getting the capabilities object of GL2 profile
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabilities capabilities = new GLCapabilities(profile);
      
      // The canvas
      final GLCanvas glcanvas = new GLCanvas(capabilities);
      LineStrip r = new LineStrip();
      glcanvas.addGLEventListener(r);
      glcanvas.setSize(400, 400);
      
      //creating frame
      final JFrame frame = new JFrame ("LineStrip");
      
      //adding canvas to frame
      frame.getContentPane().add(glcanvas);
            
      frame.setSize(frame.getContentPane().getPreferredSize());
      frame.setVisible(true);
      
   }//end of main
    
}//end of classimport javax.media.opengl.GL2;

如果编译并执行上述代码,则会生成以下输出-

LineStrip

display()方法绘制线环的代码片段

public void display(GLAutoDrawable drawable) {

   final GL2 gl = drawable.getGL().getGL2();
   
   gl.glBegin (GL2.GL_LINE_LOOP);
   
   gl.glVertex3f( -0.50f, -0.75f, 0);
   gl.glVertex3f(0.7f, .5f, 0);
   gl.glVertex3f(0.70f, -0.70f, 0);
   gl.glVertex3f(0f, 0.5f, 0);
   
   gl.glEnd();
}

如果将任何基本模板程序的display()方法替换为上述代码,进行编译并执行,则会生成以下输出:

线路环

display()方法的代码段,以使用GL_TRIANGLES绘制三角形

public void display(GLAutoDrawable drawable) {

   final GL2 gl = drawable.getGL().getGL2();
   
   gl.glBegin(GL2.GL_TRIANGLES);        // Drawing Using Triangles
   
   gl.glVertex3f(0.5f,0.7f,0.0f);       // Top
   gl.glVertex3f(-0.2f,-0.50f,0.0f);    // Bottom Left
   gl.glVertex3f(0.5f,-0.5f,0.0f);      // Bottom Right
   
   gl.glEnd();
}

如果将任何基本模板程序的display()方法替换为上述代码,进行编译并执行,则会生成以下输出:

三角形

display()方法的代码片段,用于绘制三角带

public void display(GLAutoDrawable drawable) {

   final GL2 gl = drawable.getGL().getGL2();
   
   gl.glBegin (GL2.GL_TRIANGLE_STRIP);
   
   gl.glVertex3f(0f,0.5f,0);
   gl.glVertex3f(-0.50f,-0.75f,0);
   gl.glVertex3f(0.28f,0.06f,0);
   gl.glVertex3f(0.7f,0.5f,0);
   gl.glVertex3f(0.7f,-0.7f,0);
   
   gl.glEnd();
}

如果用上述代码替换任何基本模板程序的display()方法,并编译并执行,将生成以下输出:

三角带

用于显示四边形的display()方法的代码片段

public void display(GLAutoDrawable drawable) {

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

如果将任何基本模板程序的display()方法替换为上述代码,进行编译并执行,则会生成以下输出:

四边形

用于绘制多边形的display()方法的代码片段

public void display(GLAutoDrawable drawable) {

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

如果将任何基本模板程序的display()方法替换为上述代码,进行编译并执行,则会生成以下输出-

多边形