English 中文(简体)
JOGL - 3D Basics
  • 时间:2024-09-17

JOGL - 3D Basics


Previous Page Next Page  

In previous chapters we have seen how to create 2d objects, apply effects to it, and transform the object. This chapter teaches you how to draw a pne with 3rd dimension, and some shapes.

Let us draw a simple pne with z-axis and see the difference between 2D and 3D pnes. Draw a simple pne first, then draw the second pne 3 units into the window.

Let us go through the program to draw a 3D pne −

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabipties;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;

import javax.swing.JFrame;
   
pubpc class Line3d implements GLEventListener {
   private GLU glu = new GLU();
	
   @Override
   
   pubpc void display( GLAutoDrawable drawable ) {
      final GL2 gl = drawable.getGL().getGL2();
      gl.glTranslatef( 0f, 0f, -2.5f );
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( -0.75f,0f,0 );
      gl.glVertex3f( 0f,-0.75f, 0 );
      gl.glEnd();
      
      //3d pne
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( -0.75f,0f,3f );// 3 units into the window
      gl.glVertex3f( 0f,-0.75f,3f );
      gl.glEnd();
   }
   
   @Override
   pubpc void dispose( GLAutoDrawable arg0 ) {
      //method body
   }
   
   @Override
   pubpc void init( GLAutoDrawable arg0 ) {
      // method body
   }
   
   @Override
   pubpc void reshape( GLAutoDrawable drawable, int x, int y, int width, int height ) {
	
      GL2 gl = drawable.getGL().getGL2();
      
      if( height <= 0 )
         height = 1;
			
      final float h = ( float ) width / ( float ) height;
      gl.glViewport( 0, 0, width, height );
      gl.glMatrixMode( GL2.GL_PROJECTION );
      gl.glLoadIdentity();
		
      glu.gluPerspective( 45.0f, h, 1.0, 20.0 );
      gl.glMatrixMode( GL2.GL_MODELVIEW );
      gl.glLoadIdentity();
   }
   
   pubpc static void main( String[] args ) {
	
      //getting the capabipties object of GL2 profile
      final GLProfile profile = GLProfile.get( GLProfile.GL2 );
      GLCapabipties capabipties = new GLCapabipties(profile);
          
      // The canvas
      final GLCanvas glcanvas = new GLCanvas( capabipties );
      Line3d pne3d = new Line3d();
      glcanvas.addGLEventListener( pne3d );
      glcanvas.setSize( 400, 400 );
       
      //creating frame
      final JFrame frame = new JFrame (" 3d pne");
          
      //adding canvas to it
      frame.getContentPane().add( glcanvas );
      frame.setSize(frame.getContentPane().getPreferredSize() );
      frame.setVisible( true );
   }//end of main
	
}//end of class

When you compile and execute the above program, the following output is generated −

3D Line

3D shapes can be drawn by giving non-zero values to z quadrant of the glVertex3f() method, which generates the above view. Now joining the remaining pnes will lead to a 3D edge.

Now in the same way let us develop an edge with 3rd dimension.

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabipties;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;

import javax.swing.JFrame;

pubpc class Edge1 implements GLEventListener {
   private GLU glu = new GLU();
	
   @Override
   pubpc void display(GLAutoDrawable drawable) {
   
      // TODO Auto-generated method stub
      final GL2 gl = drawable.getGL().getGL2();
      gl.glTranslatef(0f, 0f, -2.5f);
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glEnd();

      //3d pne
      gl.glBegin(GL2.GL_LINES);

      //3 units in to the window
      gl.glVertex3f(-0.75f,0f,3f);
      gl.glVertex3f(0f,-0.75f,3f);
      gl.glEnd();

      //top
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glVertex3f(-0.75f,0f,3f);
      gl.glEnd();

      //bottom
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glVertex3f(0f,-0.75f,3f);
      gl.glEnd();
   }

   @Override
   pubpc void dispose(GLAutoDrawable arg0) {
      //method body
   }
	
   @Override
   pubpc void init(GLAutoDrawable arg0) {
      // method body
   }
   
   @Override
   pubpc void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
	
      // TODO Auto-generated method stubfinal
      GL2 gl = drawable.getGL().getGL2();
      if(height <= 0)
         height = 1;
			
      final float h = (float) width / (float) height;
      gl.glViewport(0, 0, width, height);
      gl.glMatrixMode(GL2.GL_PROJECTION);
      gl.glLoadIdentity();
		
      glu.gluPerspective(45.0f, h, 1.0, 20.0);
      gl.glMatrixMode(GL2.GL_MODELVIEW);
      gl.glLoadIdentity();

   }
   
   pubpc static void main(String[] args) {
   
      //getting the capabipties object of GL2 profile
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabipties capabipties = new GLCapabipties(profile);

      // The canvas
      final GLCanvas glcanvas = new GLCanvas(capabipties);
      Edge1 b = new Edge1();
      glcanvas.addGLEventListener(b);
      glcanvas.setSize(400, 400);

      //creating frame
      final JFrame frame = new JFrame (" 3d edge");

      //adding canvas to it
      frame.getContentPane().add(glcanvas);
      frame.setSize(frame.getContentPane().getPreferredSize());
      frame.setVisible(true);
   }//end of main
	
}//end of class

When you compile and execute the above program, the following output is generated −

3D Edge

In the same way, by developing 3D edges to corresponding sides of any 2D quadrilateral and joining the adjacent vertices, you can get a 3D quadrilateral.

Below given is a program to draw a rhombus using JOGL.

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabipties;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;

import javax.swing.JFrame;

pubpc class Rhombus implements GLEventListener {
   private GLU glu = new GLU();
    
   @Override
   pubpc void display(GLAutoDrawable drawable) {
	
      final GL2 gl = drawable.getGL().getGL2();
      gl.glTranslatef(0f, 0f, -2.5f);

      //drawing edge1.....
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,3f); // 3 units into the window
      gl.glVertex3f(0f,-0.75f,3f);
      gl.glEnd();

      //top
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glVertex3f(-0.75f,0f,3f);
      gl.glEnd();

      // bottom
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glVertex3f(0f,-0.75f,3f);
      gl.glEnd();
   
      // edge 2....
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glVertex3f(0.75f,0f, 0);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0f,-0.75f, 3f);
      gl.glVertex3f(0.75f,0f, 3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glVertex3f(0f,-0.75f, 3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0.75f,0f, 0);
      gl.glVertex3f(0.75f,0f, 3f);
      gl.glEnd();

      //Edge 3.............
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f( 0.0f,0.75f,0);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f( 0.0f,0.75f,3f);
      gl.glVertex3f(-0.75f,0f,3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f( 0.0f,0.75f,0);
      gl.glVertex3f( 0.0f,0.75f,3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glVertex3f(-0.75f,0f,3f);
      gl.glEnd();

      //final edge
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0.75f,0f, 0);
      gl.glVertex3f( 0.0f,0.75f,0);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0.75f,0f,3f);
      gl.glVertex3f( 0.0f,0.75f,3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0.75f,0f, 0);
      gl.glVertex3f(0.75f,0f,3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f( 0.0f,0.75f,0);
      gl.glVertex3f( 0.0f,0.75f,3f);
      gl.glEnd();
   }
   
   @Override
   pubpc void dispose(GLAutoDrawable arg0) {
      //method body
   }
	
   @Override
   pubpc void init(GLAutoDrawable arg0) {
      // method body
   }
	
   @Override
   pubpc void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
   
      // TODO Auto-generated method stub final
      GL2 gl = drawable.getGL().getGL2();
      if(height lt;= 0)
         height = 1;
			
      final float h = (float) width / (float) height;
      gl.glViewport(3, 6, width, height);
      gl.glMatrixMode(GL2.GL_PROJECTION);
      gl.glLoadIdentity();
		
      glu.gluPerspective(45.0f, h, 1.0, 20.0);
      gl.glMatrixMode(GL2.GL_MODELVIEW);
      gl.glLoadIdentity();
   }
   
   pubpc static void main(String[] args) {
	
      //getting the capabipties object of GL2 profile
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabipties capabipties = new GLCapabipties(profile);

      // The canvas
      final GLCanvas glcanvas = new GLCanvas(capabipties);
      Rhombus b = new Rhombus();
      glcanvas.addGLEventListener(b);
      glcanvas.setSize(400, 400);

      //creating frame
      final JFrame frame = new JFrame (" Rhombus 3d");

      //adding canvas to it
      frame.getContentPane().add(glcanvas);
      frame.setSize(frame.getContentPane().getPreferredSize());
      frame.setVisible(true);
   }//end of main
	
}//end of classimport javax.media.opengl.GL2;

When you compile and execute the above program, the following output is generated. It shows a rhombus drawn using 3D pnes.

Rhombus 3D

The predefined parameters of glBegin() method can be used for drawing 3D shapes.

Advertisements