当前位置: 首页 > 文档资料 > JOGL 中文教程 >

着色(Coloring)

优质
小牛编辑
126浏览
2023-12-01

本章将教您如何使用JOGL将颜色应用于对象。 要将颜色应用于对象,请使用GL2 glColor()方法。 下面给出了使用glColor方法的语法。

语法 (Syntax)

gl.glColorXY(1f,0f,0f); 

Where,

  • X表示使用的颜色数,3(红色,绿色,蓝色)或4(红色,绿色,蓝色,alpha)。 要获得各种颜色组合,这些颜色的值将作为参数传递。 必须按该顺序维护颜色参数的顺序。

    Example

    如果将颜色值传递为(1,0,0),则会得到红色。 同样,(1,1,0)给你黄色。

  • Y表示接受诸如byte(b),double(d),float(f),int(i),short(s),ubyte(ub),uint(ui)和ushort(us)等参数的数据类型。

gl.glColor3f(1f,0f,0f);   //gives us red          
gl.glColor3f(0f,1f,0f);   //gives us green            
gl.glColor3f(0f,0f,1f);   //gives us blue

如果是三角形,您可以为每个顶点应用不同的颜色。

让我们通过程序将颜色应用于三角形 -

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 TriangleColor implements GLEventListener { 
   @Override 
   public void display( GLAutoDrawable drawable ) { 
      final GL2 gl = drawable.getGL().getGL2(); 
      gl.glBegin( GL2.GL_TRIANGLES );  
      // Drawing Using Triangles 
      gl.glColor3f( 1.0f, 0.0f, 0.0f );   // Red 
      gl.glVertex3f( 0.5f,0.7f,0.0f );    // Top 
      gl.glColor3f( 0.0f,1.0f,0.0f );     // green 
      gl.glVertex3f( -0.2f,-0.50f,0.0f ); // Bottom Left 
      gl.glColor3f( 0.0f,0.0f,1.0f );     // blue 
      gl.glVertex3f( 0.5f,-0.5f,0.0f );   // Bottom Right 
      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 ); 
      TriangleColor triangle = new TriangleColor(); 
      glcanvas.addGLEventListener( triangle ); 
      glcanvas.setSize( 400, 400 );   
      //creating frame 
      final JFrame frame = new JFrame (" Colored Triangle"); 
      //adding canvas to it 
      frame.getContentPane().add( glcanvas ); 
      frame.setSize( frame.getContentPane().getPreferredSize()); 
      frame.setVisible( true );   
   } //end of main
} //end of class 

编译并执行上述程序时,会得到以下彩色三角形 -

三角形颜色

将颜色应用于多边形

让我们通过程序将颜色应用于多边形 -

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 PolygonColor implements GLEventListener { 
   @Override 
   public void display( GLAutoDrawable drawable ) { 
      final GL2 gl = drawable.getGL().getGL2(); 
      gl.glColor3f( 1f,0f,0f ); //applying red  
      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(); 
   }
   @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 ); 
      PolygonColor polygon = new PolygonColor(); 
      glcanvas.addGLEventListener( polygon ); 
      glcanvas.setSize( 400, 400 ); 
      //creating frame 
      final JFrame frame = new JFrame ( "Colored Polygon" ); 
      //adding canvas to frame 
      frame.getContentPane().add( glcanvas ); 
      frame.setSize(frame.getContentPane().getPreferredSize() ); 
      frame.setVisible( true );    
   } //end of main 
 } //end of class 

编译并执行上述程序时,您将获得以下彩色多边形 -

多边形颜色