JAVA的OPENGL,JOGL入门例子----碰撞移动的正方体

开发环境:XP + eclipse + JOGL

第一步:到https://jogl.dev.java.net/  下载 jogl-1.1.2-pre-20080523-windows-i586.zip

解压缩到随意文件夹里,如:C:\Program Files\Java\jogl-1.1.2-pre-20080523-windows-i586

打开eclipse,

     点FILE-->new--->project--->project name 随意写如:My_opengl----FINISH

     建立项目以后在左边Workspace里找到刚新建的项目

      如:My_opengl,鼠标右键-->properties-->Java Build Path 选 libraries--->Add External JARs

       找到原解压缩文件夹 如:C:\Program Files\Java\jogl-1.1.2-pre-20080523-windows-i586 

        在里面找到lib,打开--->双击jogl.jar和gluegen-rt.jar  把这2个JAR包添加到我们的 libraries

         这时能在libraries里看到jogl.jar和gluegen-rt.jar 两包,选jogl.jar,

          ---> Native library location:---->Edit--->External Folder

           -->选择C:\Program Files\Java\jogl-1.1.2-pre-20080523-windows-i586\lib

           这样配置就完成了。



       

在My_opengl项目里,新建一个CLASS,CLASS NAME:Opengl_Bounce

先Ctrl+C复制下面代码到CLASS里,





import java.awt.BorderLayout;           

import java.awt.Frame;

import java.awt.HeadlessException;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;



import javax.media.opengl.GL;

import javax.media.opengl.GLAutoDrawable;

import javax.media.opengl.GLCanvas;

import javax.media.opengl.GLCapabilities;

import javax.media.opengl.GLEventListener;





public class Opengl_Bounce extends Frame implements GLEventListener, Runnable {



     float x1 = 100.0f;             //矩型的X坐标

     float y1 = 150.0f;             //矩型的y坐标



     long rsize = 50;               //为矩型宽度预留的位置或距离



     float xstep = 1.0f;            //沿X每步位移的距离,当然可以改大些

     float ystep = 1.0f;            //沿Y每步位移的距离



     float windowWidth;             //这里不是指窗体边框的的宽,而是视觉投影的右端

     float windowHeight;            //这里不是指窗体边框的的高,而是视觉投影的顶端



     GL gl;                         //OPENGL的主接口

     GLCanvas glcanvas;             //类似java.awt.Canvas, GLCanvas主要用来显示各种OPENGL的效果

     GLCapabilities capabilities;   //指定了一套OpenGL的功能:渲染内容必须支持,如色彩深度,以及立体是否已启用。



     Thread myThread = new Thread(this);



     public Opengl_Bounce() throws HeadlessException {      



         this.capabilities = new GLCapabilities();        //实例化capabilities

         this.glcanvas = new GLCanvas(capabilities);      //实例化glcanvas

         this.glcanvas.addGLEventListener(this);          //给glcanvas添加GL事件处理

         this.add(glcanvas, BorderLayout.CENTER);         //给窗体添加一个Component:glcanvas

         this.addWindowListener(new WindowAdapter() {     //给窗体添加关闭事件





              public void windowClosing(WindowEvent e) {

                   System.exit(0);

              }



         });





         myThread.start();                                //主线程开始





     }



     public static void main(String[] args) {            

         Opengl_Bounce f = new Opengl_Bounce();

         f.setTitle("移动的矩型");

         f.setSize(800, 600);

         f.setVisible(true);



     }



    



     public void display(GLAutoDrawable drawable) {



         if (x1 > windowWidth - rsize || x1 < 0) {

              xstep = -xstep;

         }

         if (y1 > windowHeight - rsize || y1 < 0) {

              ystep = -ystep;

         }



         if (x1 > windowWidth - rsize) {

              x1 = windowWidth - rsize;

         }

         if (y1 > windowHeight - rsize) {

              y1 = windowHeight - rsize;

         }



         x1 += xstep;

         y1 += ystep;







         gl.glClear(GL.GL_COLOR_BUFFER_BIT);

         gl.glColor3f(1.0f, 0.0f, 0.0f);



         gl.glRectf(x1, y1, x1 + rsize, y1 + rsize);



         gl.glFlush();

         glcanvas.swapBuffers();



     }



     public void displayChanged(GLAutoDrawable drawable, boolean arg1,

              boolean arg2) {



     }



     public void init(GLAutoDrawable drawable) {

         gl = drawable.getGL();

         gl.glClearColor(0.0f, 0.0f, 1.0f, 1.0f);



     }



     public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {

         if (h == 0) {

              h = 1;

         }

         gl.glViewport(0, 0, w, h);

         gl.glMatrixMode(GL.GL_PROJECTION);

         gl.glLoadIdentity();



         if (w <= h) {

              this.windowWidth = 250.0f;

              this.windowHeight = 250.0f * h / w;

         } else {

              this.windowWidth = 250.0f * w / h;

              this.windowHeight = 250.0f;



         }



         gl.glOrtho(0.0f, windowWidth, 0.0f, windowHeight, 1.0f, -1.0f);

         gl.glMatrixMode(GL.GL_MODELVIEW);

         gl.glLoadIdentity();



     }



     public void run() {

         while (true) {



              glcanvas.display();                  //主线程不停调用display()

              try {

                   myThread.sleep(20);             //每执行完一次休息20毫秒

              } catch (InterruptedException ex) {

                   ex.printStackTrace();

              }



         }



     }



}



先不谈Runnable接口,这是个多线程接口,

我们直接看到上面代码里我们 implements GLEventListener ,这用于声明一个客户端代码管理OpenGL渲染的事件.

GLEventListener 接口里有4个方法必须被重写(当然不是让你重新写一次,eclipse会自动提示你加载这些方法):

    public void init(GLAutoDrawable drawable)   程序初试化时被调用
    public void display(GLAutoDrawable drawable)  //作为画图的动作被调用,也就是主画图函数

    {



         if (x1 > windowWidth - rsize || x1 < 0) {    当矩型的X坐标大于视图的最右边或小于0时,沿X移动方向取反


       

    }

    public void displayChanged(GLAutoDrawable drawable, boolean arg1,boolean arg2)   //当DisplayMode被改变的时候自动被调用



    public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)

    {

      

    }  //当屏幕尺寸发生改变的时候调用,一开始就会至少会被调用一次.



最终效果










              xstep = -xstep;

         }

         if (y1 > windowHeight - rsize || y1 < 0) {   当矩型的y坐标大于视觉的最右边或小于0时,沿Y移动方向取反

              ystep = -ystep;

         }



         if (x1 > windowWidth - rsize) {              当矩型的X坐标到达视图的最右边,先停止

              x1 = windowWidth - rsize;

         }

         if (y1 > windowHeight - rsize) {             当矩型的Y坐标到达视图的最右边,先停止

              y1 = windowHeight - rsize;

         }



         x1 += xstep;                                 沿X坐标执行的位移距离                        

         y1 += ystep;                                 沿Y坐标执行的位移距离







         gl.glClear(GL.GL_COLOR_BUFFER_BIT);         在init()中曾把背景色设为蓝,在这里还要实际清除一下

         gl.glColor3f(1.0f, 0.0f, 0.0f);             设置画笔颜色为红



         gl.glRectf(x1, y1, x1 + rsize, y1 + rsize);   画矩型,坐标为(x1,y1)



         gl.glFlush();                               刷新

         glcanvas.swapBuffers();                     更新前景缓冲和背景缓冲


    {



         gl = drawable.getGL();                用drawable设备获取到GL接口,这句为必须,否则报错

         gl.glClearColor(0.0f, 0.0f, 1.0f, 1.0f);     把当前背景清除颜色设为蓝RGB(0,0,1),之前系统默认为黑





    }

你可能感兴趣的:(java,eclipse,多线程,windows,XP)