📜  JOGL着色

📅  最后修改于: 2021-01-05 00:40:15             🧑  作者: Mango

JOGL颜色

在JOGL中,可以用不同类型的颜色填充图形。着色只是增强了人物的外观和感觉。

为了指定颜色的类型,JOGL使用GL2接口的glColor3f()方法。此方法遵循RGB(红色,绿色,蓝色)颜色模型。在此模型中,每种颜色都用0到1之间的值表示,其中0表示没有该颜色,而1表示该颜色的最大值。

注-需要将所有三种颜色的值作为glColor3f()方法的参数传递。

JOGL颜色代码列表

以下是一些常用颜色的代码列表:

Color Red Green Blue
Red 1 0 0
Green 0 1 0
Blue 1 1 0
Yellow 1 1 0
Orange 1 0.5 0
Purple 1 0 1
Cyan 0 1 1

JOGL单色着色示例

这是一个简单的示例,其中三角形仅填充一种(绿色)颜色。

JColor.java

package com.javatpoint.jogl;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

public class JColor implements GLEventListener {

    @Override
public void init(GLAutoDrawable arg0) 
  {
        
  }

   @Override
public void display(GLAutoDrawable drawable) {

    final GL2 gl = drawable.getGL().getGL2();
    
    gl.glBegin(GL2.GL_TRIANGLES);     
               //Green Color
    gl.glColor3f( 0.0f,1.0f,0.0f ); 
    gl.glVertex2d(0,0.5);
    gl.glVertex2d(-0.5,-0.5);
    gl.glVertex2d(0.5,-0.5);
    
    gl.glEnd();
    }

   @Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) 
   {    

   }
   @Override
public void dispose(GLAutoDrawable arg0) 
   {

   }

public static void main(String[] args) {

final GLProfile gp = GLProfile.get(GLProfile.GL2);
GLCapabilities cap = new GLCapabilities(gp);

final GLCanvas gc = new GLCanvas(cap);
JColor jc = new JColor();
gc.addGLEventListener(jc);
gc.setSize(400, 400);

final JFrame frame = new JFrame("JOGL Coloring");
frame.add(gc);
frame.setSize(500,400);
frame.setVisible(true);  
   }    
}

输出:

JOGL混合着色示例

在此示例中,一个三角形填充了三种不同的颜色。

JMColor.java

package com.javatpoint.jogl;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

public class JMColor implements GLEventListener {

    @Override
public void init(GLAutoDrawable arg0) 
  {
        
  }

   @Override
public void display(GLAutoDrawable drawable) {

    final GL2 gl = drawable.getGL().getGL2();
    
    
    
    gl.glBegin(GL2.GL_TRIANGLES);   
               //Yellow Color
    gl.glColor3f(1.0f, 1.0f, 0f);
    gl.glVertex2d(0,0.5);
               //Red Color
    gl.glColor3f(1f,0f,0f); 
    gl.glVertex2d(-0.5,-0.5);
                //Blue Color
    gl.glColor3f(0f,0f,1f); 
    gl.glVertex2d(0.5,-0.5);
    
    gl.glEnd();
    }

   @Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) 
   {    

   }
   @Override
public void dispose(GLAutoDrawable arg0) 
   {

   }

public static void main(String[] args) {

final GLProfile gp = GLProfile.get(GLProfile.GL2);
GLCapabilities cap = new GLCapabilities(gp);

final GLCanvas gc = new GLCanvas(cap);
JMColor jc = new JMColor();
gc.addGLEventListener(jc);
gc.setSize(400, 400);

final JFrame frame = new JFrame("JOGL Mixed Coloring");
frame.add(gc);
frame.setSize(500,400);
frame.setVisible(true);  
   }    
}

输出: