Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Pallina 3D - Palline3D.java

Palline3D.java

Caricato da: Fraioveio
Scarica il programma completo

  1. package fraioveio.palline3d;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.ByteOrder;
  5. import java.nio.FloatBuffer;
  6. import org.lwjgl.LWJGLException;
  7. import org.lwjgl.input.Keyboard;
  8. import org.lwjgl.input.Mouse;
  9. import org.lwjgl.opengl.Display;
  10. import org.lwjgl.opengl.DisplayMode;
  11. import org.lwjgl.opengl.GL11;
  12. import org.lwjgl.util.glu.GLU;
  13. import org.lwjgl.util.glu.Sphere;
  14.  
  15. public class Palline3D {
  16.     public static final int width = 800, height = 600;
  17.    
  18.     public boolean close = false;
  19.     public float rotx = 0;
  20.     public float roty = 0;
  21.    
  22.     public float posx = 0;
  23.     public float posy = 0.5f;
  24.     public float posz = 0;
  25.    
  26.     public float vx = 0.05f;
  27.     public float vy = 0.08f;
  28.     public float vz = -0.07f;
  29.    
  30.     public static void main(String[] args) {
  31.         System.setProperty("org.lwjgl.librarypath",System.getProperty("user.dir") + "/natives/");
  32.        
  33.         Palline3D p = new Palline3D();
  34.        
  35.         p.run();
  36.     }
  37.    
  38.     public void run() {
  39.         initDisplay();
  40.         initGL();
  41.        
  42.         while(!close) {
  43.             input();
  44.             render();
  45.            
  46.             Display.update();
  47.             Display.sync(70);
  48.         }
  49.        
  50.         Display.destroy();
  51.     }
  52.    
  53.     public void initGL() {
  54.         GL11.glViewport(0, 0, width, height);
  55.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  56.         GL11.glLoadIdentity();
  57.         GLU.gluPerspective(45.0f, ((float) width / (float) height), 0.1f, 100.0f);
  58.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  59.         GL11.glLoadIdentity();
  60.            
  61.         GL11.glShadeModel(GL11.GL_SMOOTH);
  62.         GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  63.         GL11.glClearDepth(1.0f);
  64.         GL11.glEnable(GL11.GL_DEPTH_TEST);
  65.         GL11.glDepthFunc(GL11.GL_LEQUAL);
  66.         GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
  67.         GL11.glEnable(GL11.GL_COLOR_MATERIAL);
  68.        
  69.        
  70.         GL11.glEnable(GL11.GL_LIGHTING);
  71.        
  72.         GL11.glEnable(GL11.GL_LIGHT1);
  73.         GL11.glLoadIdentity();
  74.         GL11.glTranslatef(0, 0, 0);
  75.        
  76.         float lightAmbient[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  77.         float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  78.         float lightPosition[] = { 0.0f, 0.0f, 0.0f, 10.0f };
  79.  
  80.         ByteBuffer temp = ByteBuffer.allocateDirect(16);
  81.         temp.order(ByteOrder.nativeOrder());
  82.         GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip());
  83.         GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip());
  84.         GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());
  85.        
  86.         Mouse.setGrabbed(true);
  87.     }
  88.  
  89.     private void initDisplay() {
  90.         try {
  91.             Display.setDisplayMode(new DisplayMode(width, height));
  92.             Display.setTitle("Palline 3D by FraioVeio");
  93.            
  94.             Display.create();
  95.         } catch (LWJGLException ex) {
  96.             ex.printStackTrace();
  97.             System.exit(1);
  98.         }
  99.     }
  100.  
  101.     private void input() {
  102.         close = Display.isCloseRequested();
  103.        
  104.         if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
  105.             close = true;
  106.        
  107.         int rx = Mouse.getDX(), ry = Mouse.getDY();
  108.        
  109.         if(roty - ry / 2f <= 90 && roty - ry / 2f >= -90) {
  110.             roty -= ry / 2f;
  111.         }
  112.        
  113.         rotx += rx / 2f;
  114.     }
  115.    
  116.     private Sphere s = new Sphere();
  117.    
  118.     private void render() {
  119.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  120.        
  121.         GL11.glLoadIdentity();
  122.        
  123.         GL11.glRotatef(roty, 1, 0, 0);
  124.         GL11.glRotatef(rotx, 0, 1, 0);
  125.        
  126.         GL11.glTranslatef(0, -10, 0);
  127.        
  128.         GL11.glBegin(GL11.GL_QUADS);
  129.             // Basso
  130.             GL11.glColor3f(0, 0, 1);
  131.             GL11.glVertex3f(-10, 0, 10);
  132.             GL11.glVertex3f(10, 0, 10);
  133.             GL11.glVertex3f(10, 0, -10);
  134.             GL11.glVertex3f(-10, 0, -10);
  135.            
  136.             // Davanti
  137.             GL11.glColor3f(1, 0, 0);
  138.             GL11.glVertex3f(-10, 0, 10);
  139.             GL11.glVertex3f(10, 0, 10);
  140.             GL11.glVertex3f(10, 20, 10);
  141.             GL11.glVertex3f(-10, 20, 10);
  142.            
  143.             // Dietro
  144.             GL11.glColor3f(0, 1, 0);
  145.             GL11.glVertex3f(-10, 0, -10);
  146.             GL11.glVertex3f(10, 0, -10);
  147.             GL11.glVertex3f(10, 20, -10);
  148.             GL11.glVertex3f(-10, 20, -10);
  149.            
  150.             // Destra
  151.             GL11.glColor3f(1, 1, 0);
  152.             GL11.glVertex3f(10, 0, 10);
  153.             GL11.glVertex3f(10, 0, -10);
  154.             GL11.glVertex3f(10, 20, -10);
  155.             GL11.glVertex3f(10, 20, 10);
  156.            
  157.             // Sinistra
  158.             GL11.glColor3f(0, 1, 1);
  159.             GL11.glVertex3f(-10, 0, 10);
  160.             GL11.glVertex3f(-10, 0, -10);
  161.             GL11.glVertex3f(-10, 20, -10);
  162.             GL11.glVertex3f(-10, 20, 10);
  163.            
  164.             // Alto
  165.             GL11.glColor3f(1, 0, 1);
  166.             GL11.glVertex3f(-10, 20, 10);
  167.             GL11.glVertex3f(10, 20, 10);
  168.             GL11.glVertex3f(10, 20, -10);
  169.             GL11.glVertex3f(-10, 20, -10);
  170.         GL11.glEnd();
  171.        
  172.         /* Sfera */
  173.         GL11.glLoadIdentity();
  174.        
  175.         GL11.glRotatef(roty, 1, 0, 0);
  176.         GL11.glRotatef(rotx, 0, 1, 0);
  177.        
  178.         GL11.glTranslatef(posx, posy - 10, posz);
  179.        
  180.         GL11.glColor3f(1, 1, 1);
  181.         s.draw(0.5f, 20, 20);
  182.        
  183.         posx += vx;
  184.         posy += vy;
  185.         posz += vz;
  186.        
  187.         if(posx - 0.5f <= -10 || posx + 0.5f >= 10) {
  188.             vx = -vx;
  189.         }
  190.        
  191.         if(posy - 0.5f <= 0 || posy + 0.5f >= 20) {
  192.             vy = -vy;
  193.         }
  194.        
  195.         if(posz - 0.5f <= -10 || posz + 0.5f >= 10) {
  196.             vz = -vz;
  197.         }
  198.     }
  199. }