2014-11-06 89 views
0

我想創建一個多維數據集,因爲這是OpenGl 2.9中要做的事情。不過,我最初有一個立方體渲染,但我試圖讓我的設計更好一點,但我現在無法看到我的立方體。幫我看看我的魔方。代碼如下。我沒有得到任何錯誤,只是無法看到我的立方體。Java OpenGL Cube Not Drawing

import java.awt.*; 
import org.lwjgl.*; 
import javax.swing.*; 

public class Scene 
{ 
    private static boolean run = false; 
    private static int width = 640; 
    private static int height = 480; 
    long lastFrame; //time at last frame 
    int fps; 
    long lastFPS; 
    /** position of quad */ 
    float x = 1, y = 1, z = 1; //400,300,400 
    /** angle of quad rotation */ 
    float rotation = 0; 

    public void start() 
    { 
    try 
    { 
     Display.setDisplayMode(new DisplayMode(width, height)); 
     Display.create(); 
    } catch (LWJGLException e) 
    { 
     e.printStackTrace(); 
     System.exit(0); 
    } 

    initGL(); // init OpenGL 
    getDelta(); // call once before loop to initialise lastFrame 
    lastFPS = getTime(); // call before loop to initialise fps timer 

    while (run == true) 
    { 
     int delta = getDelta(); 

     update(delta); 
     renderGL(); 

     Display.update(); 
     Display.sync(60); // cap fps to 60fps 

     if (Keyboard.isKeyDown(Keyboard.KEY_Q)) 
     { 
     break; 
     } 
    } 
    Display.destroy(); 
    System.exit(0); 
    } 

    public void update(int delta) { 
    // rotate quad 
    rotation += 0.15f * delta; 

    if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) x -= 0.35f * delta; 
    if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) x += 0.35f * delta; 

    if (Keyboard.isKeyDown(Keyboard.KEY_UP)) y -= 0.35f * delta; 
    if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) y += 0.35f * delta; 

    // keep quad on the screen 
    if (x < 0) x = 0; 
    if (x > 800) x = 800; 
    if (y < 0) y = 0; 
    if (y > 600) y = 600; 

    updateFPS(); // update FPS Counter 
    } 

    public void renderGL() { 
    // Clear The Screen And The Depth Buffer 
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); 

    GL11.glColor3f(1, 0, 0); 

    // draw quad 
    GL11.glPushMatrix(); 
     GL11.glTranslatef(x, y, z); 
     GL11.glRotatef(rotation, 0f, 0f, 1f); 
     GL11.glTranslatef(-x, -y, z); 

     GL11.glBegin(GL11.GL_QUADS); 
     GL11.glColor3f(0.0f,1.0f,0.0f);  // Set The Color To Green 
     GL11.glVertex3f(x, y,-z);   // Top Right Of The Quad (Top) 
     GL11.glVertex3f(-x, y,-z);   // Top Left Of The Quad (Top) 
     GL11.glVertex3f(-x, y, z);   // Bottom Left Of The Quad (Top) 
     GL11.glVertex3f(x, y, z);   // Bottom Right Of The Quad (Top) 

     GL11.glColor3f(1.0f,0.5f,0.0f);  // Set The Color To Orange 
     GL11.glVertex3f(x, -y, z);   // Top Right Of The Quad (Bottom) 
     GL11.glVertex3f(-x, -y, z);   // Top Left Of The Quad (Bottom) 
     GL11.glVertex3f(-x, -y,-z);   // Bottom Left Of The Quad (Bottom) 
     GL11.glVertex3f(x,-y,-z);   // Bottom Right Of The Quad (Bottom) 

     GL11.glColor3f(1.0f,0.0f,0.0f);  // Set The Color To Red 
     GL11.glVertex3f(x, y, z); // Top Right Of The Quad (Front) 
     GL11.glVertex3f(-x, y, z); // Top Left Of The Quad (Front) 
     GL11.glVertex3f(-x,-y, z); // Bottom Left Of The Quad (Front) 
     GL11.glVertex3f(x,-y, z); // Bottom Right Of The Quad (Front) 

     GL11.glColor3f(1.0f,1.0f,0.0f);  // Set The Color To Yellow 
     GL11.glVertex3f(x,-y,-z); // Bottom Left Of The Quad (Back) 
     GL11.glVertex3f(-x,-y,-z); // Bottom Right Of The Quad (Back) 
     GL11.glVertex3f(-x, y,-z); // Top Right Of The Quad (Back) 
     GL11.glVertex3f(x, y,-z); // Top Left Of The Quad (Back) 

     GL11.glColor3f(0.0f,0.0f,1.0f);  // Set The Color To Blue 
     GL11.glVertex3f(-x, y, z); // Top Right Of The Quad (Left) 
     GL11.glVertex3f(-x, y,-z); // Top Left Of The Quad (Left) 
     GL11.glVertex3f(-x,-y,-z); // Bottom Left Of The Quad (Left) 
     GL11.glVertex3f(-x,-y, z); // Bottom Right Of The Quad (Left) 

     GL11.glColor3f(1.0f,0.0f,1.0f);  // Set The Color To Violet 
     GL11.glVertex3f(x, y,-z); // Top Right Of The Quad (Right) 
     GL11.glVertex3f(x, y, z); // Top Left Of The Quad (Right) 
     GL11.glVertex3f(x,-y, z); // Bottom Left Of The Quad (Right) 
     GL11.glVertex3f(x,-y,-z); // Bottom Right Of The Quad (Right) 

//  GL11.glVertex2f(x - 50, y - 50); 
//  GL11.glVertex2f(x + 50, y - 50); 
//  GL11.glVertex2f(x + 50, y + 50); 
//  GL11.glVertex2f(x - 50, y + 50); 

    GL11.glEnd(); 
    GL11.glPopMatrix(); 
    } 
    /** 
    * Calculate how many milliseconds have passed 
    * since last frame. 
    * 
    * @return milliseconds passed since last frame 
    */ 
    public int getDelta() { 
     long time = getTime(); 
     int delta = (int) (time - lastFrame); 
     lastFrame = time; 

     return delta; 
    } 

    /** 
    * Calculate the FPS and set it in the title bar 
    */ 
    public void updateFPS() { 
    if (getTime() - lastFPS > 1000) { 
     Display.setTitle("FPS: " + fps); 
     fps = 0; 
     lastFPS += 1000; 
    } 
    fps++; 
    } 

    /** 
    * Get the accurate system time 
    * 
    * @return The system time in milliseconds 
    */ 
    public long getTime() { 
     return (Sys.getTime() * 1000)/Sys.getTimerResolution(); 
    } 

    public void initGL() { 
    GL11.glMatrixMode(GL11.GL_PROJECTION); 
    GL11.glLoadIdentity(); 
    GL11.glOrtho(0, 800, 0, 600, 1, -1); 
    GL11.glMatrixMode(GL11.GL_MODELVIEW); 
    } 

    public static void main(String[] Args) 
    { 
    final JFrame info = new JFrame(); 
    JButton button = new JButton(); 
    JLabel infoText1 = new JLabel(); 
    JLabel infoText2 = new JLabel(); 
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 

    button.setText("Acknowledge"); 
    button.setSize(20, 10); 
    infoText1.setText("Press Q to quit"); 
    infoText2.setText("Click Acknowledge to start simulation"); 
    info.setTitle("Operator's Manual"); 

    info.setLayout(new FlowLayout()); 
    info.add(infoText1); 
    info.add(infoText2); 
    info.setSize(240, 120); 
    info.setLocation(dim.width/2-info.getSize().width/2, dim.height/2-info.getSize().height/2); 
    info.add(button); 
    info.setVisible(true); 

    button.addActionListener(new java.awt.event.ActionListener() 
     { 
     @Override 
     public void actionPerformed(java.awt.event.ActionEvent acknowledge) 
     { 
      run = true; 
      Scene sceneStart = new Scene(); 
      sceneStart.start(); 
     } 
     }); 
    } 
} 

回答

0

編輯︰你的近和遠值爲glOrtho是倒退。請注意,稍後7個z單位的轉換也會將您的對象移動到由glOrtho定義的體積之外,這將導致它不被繪製。

您正在取消模型視圖矩陣中的自己的翻譯。

GL11.glTranslatef(1.5f, 0.0f, -7.0f); 
    GL11.glLoadIdentity(); // << reloads identity matrix, overriding the translate. 

正確的是:

// Ensure model view matrix is starting is nice fresh state. 
    GL11.glLoadIdentity(); 
    // Put the quad where you want it. 
    GL11.glTranslatef(1.5f, 0.0f, -7.0f); 
+0

謝謝大家的響應!我提出了修改建議,還有更多,我現在看到了我的對象,但它非常失真。它也似乎是2D,這意味着它不能繪製我所有的頂點,你可以幫忙嗎? – vector 2014-11-06 03:16:19

+0

您正在使用[正射投影](http://en.wikipedia.org/wiki/Orthographic_projection),它不提供透視效果。 – MtRoad 2014-11-06 03:20:59

+0

那麼我會用什麼呢?只是ModelView?對不起,我對此很新穎。 – vector 2014-11-06 03:26:27