JOGL入门例子(十二)----------JOGL2作地球围绕太阳公转

由于出了JOGL 2.0,代码应用上和JOGL 1.0多少有点区别,研究了一下,做了个基于JOGL2的例子

 

 

JOGL 2.0 for netbeans 下载地址,感谢网友wei495715356提供的地址和包

http://projectkenai.com/projects/netbeans-opengl-pack/downloads/directory/early-access

 


其实JOGL2.0在代码上和JOGL1.0相差不大.

 

图一:

 

 

图二:
JOGL入门例子(十二)----------JOGL2作地球围绕太阳公转

 

 

窗体类:

package org.yourorghere;               //要是想使用默认包,请去掉这行

/**
 *
 * @欢迎来到子瑜IT博客空间
 *
 */



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
import javax.media.opengl.awt.GLCanvas;

public class SolarMain extends JFrame {
	GLRender listener=new GLRender();
	static FPSAnimator animator=null;
	public SolarMain() throws HeadlessException {
		super("太阳与地球");
		setSize(600,480);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GLCapabilities glcaps=new GLCapabilities(null);          //这里和之前章节的代码有区别.
		GLCanvas canvas=new GLCanvas(glcaps);
		canvas.addGLEventListener(listener);
		//canvas.addMouseListener(listener);
		getContentPane().add(canvas, BorderLayout.CENTER);
		animator=new FPSAnimator(canvas,60,true);
                
		centerWindow(this);		
	}	
	private void centerWindow(Component frame) { // 居中窗体
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.width > screenSize.width)
			frameSize.width = screenSize.width;
		if (frameSize.height > screenSize.height)
			frameSize.height = screenSize.height;
		frame.setLocation((screenSize.width - frameSize.width) >> 1,
				(screenSize.height - frameSize.height) >> 1);

	}
	
	public static void main(String[] args) {
		final SolarMain app = new SolarMain();
		// 显示窗体
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				app.setVisible(true);
			}
		});
		// 动画线程开始
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				animator.start();
			}
		});
	}
}

 

GL控制类:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.yourorghere;


import com.sun.opengl.util.gl2.GLUT;              //这里类所在包改变了
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;                //这里类所在包改变了


/**
 *
 * @欢迎来到子瑜IT博客空间
 * 
 */
public class GLRender implements GLEventListener {

    GL2 gl;
    GLU glu;
    GLUT glut;
    float whiteLight[] = {0.2f, 0.2f, 0.2f, 1.0f};
    float sourceLight[] = {0.8f, 0.8f, 0.8f, 1.0f};
    float lightPos[] = {0.0f, 0.0f, 0.0f, 1.0f};


	 float fMoonRot = 0.0f;
	float fEarthRot = 0.0f;

    public void init(GLAutoDrawable drawable) {
        gl =  drawable.getGL().getGL2();    //主要就是这里发生变化
        glu = new GLU();
        glut=new GLUT();


        // Light values and coordinates
        gl.glEnable(GL.GL_DEPTH_TEST);	// 启用深度测试
        gl.glFrontFace(GL.GL_CCW);		//设置CCW方向为“正面”,CCW即CounterClockWise,逆时针
        gl.glEnable(GL.GL_CULL_FACE);		//用GL_CULL_FACE符号常量调用glEnable函数表示开启多边形表面剔除功能

        // 启用灯光
        gl.glEnable(GL2.GL_LIGHTING);

        // 设置灯光
        gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, whiteLight, 0);
        gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, sourceLight, 0);
        gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPos, 0);
        gl.glEnable(GL2.GL_LIGHT0);

        // 启用材质
        gl.glEnable(GL2.GL_COLOR_MATERIAL);

        // 决定对物体的正面还是反面,对环境光、镜面光还是漫射光进行颜色跟踪。
        //第一个参数可以取GL_FRONT、GL_BACK、GL_FRONT_AND_BACK中的任意一种,
        //第二个参数可以取GL_AMBIENT、GL_DIFFUSE、GL_AMBIENT_AND_DIFFUSE、GL_SPECULAR中的任意一种。
        gl.glColorMaterial(GL.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE);

         // 黑色背景
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);



    }

    public void display(GLAutoDrawable drawable) {



	// 填充背景为背景颜色
	gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

	// 保存矩阵状态和做旋转
	gl.glMatrixMode(GL2.GL_MODELVIEW);
	gl.glPushMatrix();

	// 画图点移动到Z里300的位置,负为向里,正为向程序员.
	gl.glTranslatef(0.0f, 0.0f, -300.0f);

	// Set material color, Red
	// 太阳
        gl.glDisable(GL2.GL_LIGHTING);
	gl.glColor3ub((byte)255, (byte)255, (byte)0);
	glut.glutSolidSphere(15.0f, 30, 17);
        gl.glEnable(GL2.GL_LIGHTING);

	// 重新设置灯光到太阳的位置
	gl.glLightfv(GL2.GL_LIGHT0,GL2.GL_POSITION,lightPos,0);

	// 围绕Y轴旋转矩阵
	gl.glRotatef(fEarthRot, 0.0f, 1.0f, 0.0f);

	// 画地球
	gl.glColor3ub((byte)0,(byte)200,(byte)255);
	gl.glTranslatef(105.0f,0.0f,0.0f);
	glut.glutSolidSphere(15.0f, 30, 17);


	//围绕Y轴旋转矩阵,当前旋转中心为地球,被旋转的是月球
	gl.glColor3ub((byte)200,(byte)200,(byte)200);
	gl.glRotatef(fMoonRot,0.0f, 1.0f, 0.0f);
	gl.glTranslatef(30.0f, 0.0f, 0.0f);
	fMoonRot+= 15.0f;
	if(fMoonRot > 360.0f)
		fMoonRot = 0.0f;

	glut.glutSolidSphere(6.0f, 30, 17);

	// 还原状态矩阵
	gl.glPopMatrix();	// Modelview matrix


	//地球轨道每步5度,旋转度数
	fEarthRot += 2.0f;
	if(fEarthRot > 360.0f)
		fEarthRot = 0.0f;
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

        float fAspect;

        // 防止为零
        if (height == 0) {
            height = 1;
        }

        //视口设置为窗口尺寸
        gl.glViewport(0, 0, width, height);

        fAspect = (float) width / height;

        // Reset projection matrix stack
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();



        //透视投影 眼角度,比例,近可视,远可视
        glu.gluPerspective(53.0f, fAspect, 1.0, 400.0);

        // 重置模型观察矩阵堆栈
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();

    }



    public void dispose(GLAutoDrawable arg0) {
        
    }
}

 
 

顺便发上JOGL2 FOR eclipse插件 jogl-2.0-windows-i586.zip

分享自己的技术论坛http://www.itneng.com/forum.php

你可能感兴趣的:(eclipse,swing,sun,Netbeans,360)