2012-06-29 61 views

回答

2

簡單的代碼來創建一個球體?(你可以請充分源代碼):

你會得到關於如何創建一個簡單的球體LWJGL

public static void main(String[] args) { 
    SphereDemo test = new SphereDemo(); 
    test.run(); 
} 

private void run() { 
    try { 
    init(); 
    render(); // render graphics 
    Display.sync(70); // sync to fps 
    Display.update(); // update the view/screen 
    } catch (Exception e) { 
      e.printStackTrace(); 
      System.exit(1); 
    } 
} 

private void render() { 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 
    glTranslatef(0.0f, 0.0f, zTranslation); 
    renderSphere(-2f, -0.5f, -1f); 
    renderSphere(-1f, -0.5f, -2f); 
    renderSphere(-0f, -0.5f, -3f); 
    renderSphere(1f, -0.5f, -4f); 
    renderSphere(2f, -0.5f, -5f); 
} 

private void renderSphere(float x, float y, float z) { 
    glPushMatrix(); 
    glTranslatef(x, y, z); 
    Sphere s = new Sphere(); 
    s.draw(0.4f, 16, 16); 
    glPopMatrix(); 
} 

private void init() { 
    createWindow(); 
    initGL(); 
} 

private void createWindow() { 
    try { 
     Display.setDisplayMode(DISPLAY_MODE); 
     Display.setTitle(WINDOW_TITLE); 
     Display.create(); 
    } catch (LWJGLException e) { 
     e.printStackTrace(); 
    } 
} 

private void initGL() { 
    glClearDepth(1.0f); // clear depth buffer 
    glEnable(GL_DEPTH_TEST); // Enables depth testing 
    glDepthFunc(GL_LEQUAL); // sets the type of test to use for depth 
    // testing 
    glMatrixMode(GL_PROJECTION); // sets the matrix mode to project 
    float fovy = 45.0f; 
    float aspect = DISPLAY_MODE.getWidth()/(float) DISPLAY_MODE.getHeight(); 
    float zNear = 0.1f; 
    float zFar = 100.0f; 
    GLU.gluPerspective(fovy, aspect, zNear, zFar); 
    glMatrixMode(GL_MODELVIEW); 
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 
} 
+0

一個基本思想是(翻譯+繪圖+翻譯方向相反)有過之而無不及(推+翻譯+繪圖+膨化)?我是爲單個球體的第一種方式使用,這對於許多球體來說可能更好? –

+0

如果您使用push/pop矩陣,它會更清潔更安全。 – Romejanic