jogl的 代码
配置 jogl
拷贝 jogl_lib和natives 到工作路径。
jogl_lib里面的文件添加到classpath里面。
下面是代码,亲测可用。
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
public class My3dConponent implements GLEventListener, Runnable {
private float angle = 0.0f;
@Override
public void run() {
}
@Override
public void display(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glColor3f(0.2f, 0.9f, 0.1f);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glRotated(angle, 0.0f, 1.0f, 0.0f);
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
gl.glEnable(GL2.GL_NORMALIZE);
float[] ambientLight = { 1.0f, 1.0f, 1.0f, 0.0f };
float[] diffuseLight = { 1.0f, 1.0f, 1.0f, 0.0f};
float[] specularLight={0.1f, 0.2f, 0.6f, 0.0f};
float[] positionLight = { -0.9f, 0.9f, 0.9f, 1.0f };
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, ambientLight, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuseLight, 0);
gl.glLightfv(GL2.GL_LIGHT0,GL2.GL_SPECULAR, specularLight,0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, positionLight, 0);
float[] ambientModel = { 0.4f, 0.4f, 0.4f, 1.0f };
float[] viewLmodel = { 0.0f, 0.0f, 0.0f, 1.0f };
gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, ambientModel, 0);
gl.glLightModelfv(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER, viewLmodel, 0);
gl.glBegin(GL2.GL_POLYGON);
gl.glNormal3f(0.0f,0.0f, 1.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.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();
gl.glFlush();
angle += 3.0f;
}
@Override
public void dispose(GLAutoDrawable arg0) {
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(0.392f, 0.584f, 0.929f, 1.0f);
}
@Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
int arg4) {
}
public static void main(String[] args) {
Rectangle windowBounds=new Rectangle(100,100,600,600);
final GLProfile proifile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(proifile);
final GLCanvas glCanvas = new GLCanvas(capabilities);
My3dConponent lightingPolygon = new My3dConponent();
glCanvas.addGLEventListener(lightingPolygon);
glCanvas.setSize(windowBounds.width, windowBounds.height);
final JFrame frame = new JFrame("带光照效果的旋转多边形");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.getContentPane().add(glCanvas);
frame.setBounds(windowBounds);
frame.setVisible(true);
final FPSAnimator animator = new FPSAnimator(glCanvas, 100, true);
animator.start();
}
}