在上一章中,我们学习了如何使用JOGL绘制基本线.我们通过将预定义字段 Gl_lines 传递给 glBegin()方法来绘制线条.
本章提供了绘制三角形等形状的示例,菱形和房子,使用glBegin()方法和GL_Lines.
让我们通过一个程序来绘制一个三角形,使用GL_LINES :
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 Triangle implements GLEventListener { @Override public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glBegin (GL2.GL_LINES); //drawing the base gl.glBegin (GL2.GL_LINES); gl.glVertex3f(-0.50f, -0.50f, 0); gl.glVertex3f(0.50f, -0.50f, 0); gl.glEnd(); //drawing the right edge gl.glBegin (GL2.GL_LINES); gl.glVertex3f(0f, 0.50f, 0); gl.glVertex3f(-0.50f, -0.50f, 0); gl.glEnd(); //drawing the lft edge gl.glBegin (GL2.GL_LINES); gl.glVertex3f(0f, 0.50f, 0); gl.glVertex3f(0.50f, -0.50f, 0); gl.glEnd(); gl.glFlush(); } @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); Triangle l = new Triangle(); glcanvas.addGLEventListener(l); glcanvas.setSize(400, 400); //creating frame final JFrame frame = new JFrame ("Triangle"); //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;
如果编译并执行上述程序,则会生成以下输出.
它显示使用GL_LINES glBegin()方法绘制的三角形.
让我们通过一个程序来绘制菱形,使用GL_LINES :
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; 公共类Rhombus实现GLEventListener { @Override public void display(GLAutoDrawable drawable){ final GL2 gl = drawable.getGL(). getGL2(); //edge1 gl.glBegin(GL2.GL_LINES); gl.glVertex3f(0.0f,0.75f,0); gl.glVertex3f(-0.75f,0f,0); gl.glEnd(); //edge2 gl.glBegin(GL2.GL_LINES); gl.glVertex3f(-0.75f,0f,0); gl.glVertex3f(0f,-0.75f,0); gl.glEnd(); //edge3 gl.glBegin(GL2.GL_LINES); gl.glVertex3f(0f,-0.75f,0); gl.glVertex3f(0.75f,0f,0); gl.glEnd(); //edge4 gl.glBegin(GL2.GL_LINES); gl.glVertex3f(0.75f,0f,0); gl.glVertex3f(0.0f,0.75f,0); gl.glEnd(); gl.glFlush(); } @Override public void dispose(GLAutoDrawable arg0){ //方法体 } @Override public void init(GLAutoDrawable arg0){ //方法体 } @Override public void reshape(GLAutoDrawable arg0,int arg1,int arg2,int arg3,int arg4){ //方法体 } public static void main(String [] args){ //获取GL2配置文件的功能对象 final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities功能=新的GLCapabilities(配置文件); //画布 final GLCanvas glcanvas = new GLCanvas(capabilities); Rhombus rhombus = new Rhombus(); glcanvas.addGLEventListener(菱形); glcanvas.setSize(400,400); //创建框架 final JFrame frame = new JFrame("Rhombus"); //将画布添加到帧 frame.getContentPane().add(glcanvas); frame.setSize(frame.getContentPane().getPreferredSize()); frame.setVisible(true); } }
如果编译并执行上述程序,则会得到以下输出.它显示了使用 glBegin()方法的GL_LINES生成的菱形.
让我们通过一个程序来绘制一个房子,使用GL_LINES :
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; 公共类House实现GLEventListener { @Override public void display(GLAutoDrawable drawable){ final GL2 gl = drawable.getGL(). getGL2(); //绘制顶部 gl.glBegin(GL2.GL_LINES); gl.glVertex3f(-0.3f,0.3f,0); gl.glVertex3f(0.3f,0.3f,0); gl.glEnd(); //绘制底部 gl.glBegin(GL2.GL_LINES); gl.glVertex3f(-0.3f,-0.3f,0); gl.glVertex3f(0.3f,-0.3f,0); gl.glEnd(); //绘制右边缘 gl.glBegin(GL2.GL_LINES); gl.glVertex3f(-0.3f,0.3f,0); gl.glVertex3f(-0.3f,-0.3f,0); gl.glEnd(); //绘制左边缘 gl.glBegin(GL2.GL_LINES); gl.glVertex3f(0.3f,0.3f,0); gl.glVertex3f(0.3f,-0.3f,0); gl.glEnd(); //建筑屋顶 //建筑物lft dia gl.glBegin(GL2.GL_LINES); gl.glVertex3f(0f,0.6f,0); gl.glVertex3f(-0.3f,0.3f,0); gl.glEnd(); //建筑rt dia gl.glBegin(GL2.GL_LINES); gl.glVertex3f(0f,0.6f,0); gl.glVertex3f(0.3f,0.3f,0); gl.glEnd(); //建筑门 //图纸顶部 gl.glBegin(GL2.GL_LINES); gl.glVertex3f(-0.05f,0.05f,0); gl.glVertex3f(0.05f,0.05f,0); gl.glEnd(); //绘制左边缘 gl.glBegin(GL2.GL_LINES); gl.glVertex3f(-0.05f,0.05f,0); gl.glVertex3f(-0.05f,-0.3f,0); gl.glEnd(); //绘制右边缘 gl.glBegin(GL2.GL_LINES); gl.glVertex3f(0.05f,0.05f,0); gl.glVertex3f(0.05f,-0.3f,0); gl.glEnd(); } @Override public void dispose(GLAutoDrawable arg0){ //方法体 } @Override public void init(GLAutoDrawable arg0){ //方法体 } @Override public void reshape(GLAutoDrawable arg0,int arg1,int arg2,int arg3,int arg4){ //方法体 } public static void main(String [] args){ //获取GL2配置文件的功能对象 final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities功能=新的GLCapabilities(配置文件); //画布 final GLCanvas glcanvas = new GLCanvas(capabilities); House house = new House(); glcanvas.addGLEventListener(house); glcanvas.setSize(400,400); //创建框架 final JFrame frame = new JFrame("House"); //将画布添加到帧 frame.getContentPane().add(glcanvas); frame.setSize(frame.getContentPane().getPreferredSize()); frame.setVisible(true); }//主要结束 }//类结束
如果你编译和执行上面的程序,你得到以下输出.它显示了使用GL_LINES()方法生成的房屋图.