2014-12-07 92 views
0

我遇到了一些奇怪的問題。我正在研究一個3d程序。 (我只是涉足ljwgl)。我做了一個簡單的立方體。但是,當我試圖紋理它,它幾乎是一個透明類型的東西。不僅如此,但我對這個運動有一個問題。這是非常波濤洶涌,我不知道爲什麼。提前致謝。Lwjgl 3D立方體透明度問題

這是主要的課程。

package three.demensional.testing; 


import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.Vector; 

import org.lwjgl.LWJGLException; 
import org.lwjgl.Sys; 
import org.lwjgl.input.Keyboard; 
import org.lwjgl.input.Mouse; 
import org.lwjgl.opengl.Display; 
import org.lwjgl.opengl.DisplayMode; 
import org.lwjgl.opengl.GL11; 
import org.lwjgl.util.glu.GLU; 
import org.lwjgl.util.vector.Vector3f; 
import org.newdawn.slick.opengl.Texture; 
import org.newdawn.slick.opengl.TextureLoader; 


public class Main{ 
private static Texture texture; 
private static final int maxLookDown = -85; 
private static final int maxLookUp = 85; 
public static float speed = -1; 
public static Vector3f rotation = new Vector3f(0, 0, 0); 
public static double mouseSpeed = 0.1; 

public static void start(){ 

    CameraController camera = new CameraController(0.01f,0.01f,0.01f); 
     float dx  = 0.0f; 
     float dy  = 0.0f; 
     float dt  = 0.0f; //length of frame 
     float lastTime = 0.0f; // when the last frame was 
     float time  = 0.0f; 
     int object = 5; 
     float movementSpeed = 0.01f; 


    try { 
     Display.setDisplayMode(new DisplayMode(800,600)); 
     Display.create(); 
    } catch (LWJGLException e) { 
     e.printStackTrace(); 
     System.exit(0); 
    } 
    try { 
     texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("./src/res/Texture.png"))); 
    } catch (IOException e) { 
     System.out.println(e); 
    } 

    //Initialize OpenGL 
    GL11.glMatrixMode(GL11.GL_PROJECTION); 
    GL11.glLoadIdentity(); 
    GLU.gluPerspective((float) 30, 800f/600f, 0.001f, 100); 
    GL11.glMatrixMode(GL11.GL_MODELVIEW); 

    GL11.glEnable(GL11.GL_TEXTURE_2D); 
    GL11.glCullFace(GL11.GL_BACK); 

    while(!Display.isCloseRequested()) 
    { 
     GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); 
     time = Sys.getTime(); 
     dt = (time - lastTime)/100.0f; 
     lastTime = time; 



     dx = Mouse.getDX(); 
     dy = Mouse.getDY(); 

     camera.yaw((float) (dx * mouseSpeed)); 
     camera.pitch((float) -(dy * mouseSpeed)); 

     if(Keyboard.isKeyDown(Keyboard.KEY_W)) 
     { 
      camera.walkForward(movementSpeed * dt); 
     } 
     if(Keyboard.isKeyDown(Keyboard.KEY_S)) 
     { 
      camera.walkBackward(movementSpeed * dt); 
     } 
     if(Keyboard.isKeyDown(Keyboard.KEY_D)) 
     { 
      camera.strafeRight(movementSpeed * dt); 
     } 
     if(Keyboard.isKeyDown(Keyboard.KEY_A)) 
     { 
      camera.strafeLeft(movementSpeed * dt); 
     } 
     if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) 
     { 
      camera.flyUp(dt * movementSpeed); 
     } 
     if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) 
     { 
      camera.flyDown(movementSpeed * dt); 
     } 

     if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) 
     { 
      texture.release(); 
      Display.destroy(); 
      System.exit(1); 
     } 
     GL11.glLoadIdentity(); 
     camera.lookThrough(); 
     GL11.glPointSize(10f); 
     drawTexturedCube(0f, 0, 0f, 0.1f,texture); 
     Display.update(); 

    } 
    GL11.glDeleteLists(object, 1); 
    texture.release(); 
    Display.destroy(); 
} 
public static void drawCube(float x, float y, float z, float size) 
{ 
    drawSquare(x, y, z, size, "y"); 
    drawSquare(x, y - size, z, size, "y"); 
    drawSquare(x, y, z, size, "z"); 
    drawSquare(x, y, z - size, size, "z"); 
    drawSquare(x, y, z, size, "x"); 
    drawSquare(x - size, y, z, size, "x"); 
} 
public static void drawTexturedCube(float x, float y, float z, float size, Texture t) 
{ 
    drawTexturedSquare(x, y - size, z, size, "y", t); 
    drawTexturedSquare(x, y, z, size, "y", t); 
    drawTexturedSquare(x, y, z, size, "x", t); 
    drawTexturedSquare(x - size, y, z, size, "x", t); 
    drawTexturedSquare(x, y, z - size, size, "z", t); 
    drawTexturedSquare(x, y, z, size, "z", t); 
} 
public static void drawSquare(float x, float y, float z, float size, String side) 
{ 
    if(side == "y"){ 
     /* 
     A_________B 
     |   | 
     |   | 
     |   | 
     |_________| 
     C   D 
     */ 
     GL11.glBegin(GL11.GL_TRIANGLES); 
     GL11.glVertex3f(x, y, z);  // Vertex A 
     GL11.glVertex3f(x - size, y, z); // Vertex C 
     GL11.glVertex3f(x - size, y, z - size); // Vertex D 
     // Second Triangle 
     GL11.glVertex3f(x, y, z);    // Vertex A 
     GL11.glVertex3f(x, y, z - size);  // Vertex B 
     GL11.glVertex3f(x - size, y, z - size); // Vertex D 
     GL11.glEnd(); 
    } 
    if(side == "x") 
    { 
     GL11.glBegin(GL11.GL_TRIANGLES); 
     GL11.glVertex3f(x, y, z);    // Vertex A 
     GL11.glVertex3f(x, y - size, z);  // Vertex C 
     GL11.glVertex3f(x, y - size, z - size); // Vertex D 
     // Second Triangle 
     GL11.glVertex3f(x, y, z);    // Vertex A 
     GL11.glVertex3f(x, y, z - size);  // Vertex B 
     GL11.glVertex3f(x, y - size, z - size); // Vertex D 
     GL11.glEnd(); 
    } 
    if(side == "z") 
    { 
     GL11.glBegin(GL11.GL_TRIANGLES); 
     GL11.glVertex3f(x, y, z);    // Vertex A 
     GL11.glVertex3f(x, y - size, z);  // Vertex C 
     GL11.glVertex3f(x - size, y - size, z); // Vertex D 
     // Second Triangle 
     GL11.glVertex3f(x, y, z);    // Vertex A 
     GL11.glVertex3f(x - size, y, z);  // Vertex B 
     GL11.glVertex3f(x - size, y - size, z); // Vertex D 
     GL11.glEnd(); 



    } 
} 
public static void drawTexturedSquare(float x, float y, float z, float size, String side, Texture t) 
{ 
    if(side == "y"){ 
     t.bind(); 
     GL11.glBegin(GL11.GL_TRIANGLES); 
     GL11.glTexCoord2f(0, 0); 
     GL11.glVertex3f(x, y, z);  // Vertex A 
     GL11.glTexCoord2f(0, 1); 
     GL11.glVertex3f(x - size, y, z); // Vertex C 
     GL11.glTexCoord2f(1, 1); 
     GL11.glVertex3f(x - size, y, z - size); // Vertex D 
     // Second Triangle 
     GL11.glTexCoord2f(0, 0); 
     GL11.glVertex3f(x, y, z);    // Vertex A 
     GL11.glTexCoord2f(1, 0); 
     GL11.glVertex3f(x, y, z - size);  // Vertex B 
     GL11.glTexCoord2f(1, 1); 
     GL11.glVertex3f(x - size, y, z - size); // Vertex D 
     GL11.glEnd(); 
    } 
    if(side == "x") 
    { 
     t.bind(); 
     GL11.glBegin(GL11.GL_TRIANGLES); 
     GL11.glTexCoord2f(0, 0); 
     GL11.glVertex3f(x, y, z);    // Vertex A 
     GL11.glTexCoord2f(0, 1); 
     GL11.glVertex3f(x, y - size, z);  // Vertex C 
     GL11.glTexCoord2f(1, 1); 
     GL11.glVertex3f(x, y - size, z - size); // Vertex D 
     // Second Triangle 
     GL11.glTexCoord2f(0, 0); 
     GL11.glVertex3f(x, y, z);    // Vertex A 
     GL11.glTexCoord2f(1, 0); 
     GL11.glVertex3f(x, y, z - size);  // Vertex B 
     GL11.glTexCoord2f(1, 1); 
     GL11.glVertex3f(x, y - size, z - size); // Vertex D 
     GL11.glEnd(); 
    } 
    if(side == "z") 
    { 
     t.bind(); 
     GL11.glBegin(GL11.GL_TRIANGLES); 
     GL11.glTexCoord2f(0, 0); 
     GL11.glVertex3f(x, y, z);    // Vertex A 
     GL11.glTexCoord2f(0, 1); 
     GL11.glVertex3f(x, y - size, z);  // Vertex C 
     GL11.glTexCoord2f(1, 1); 
     GL11.glVertex3f(x - size, y - size, z); // Vertex D 
     // Second Triangle 
     GL11.glTexCoord2f(0, 0); 
     GL11.glVertex3f(x, y, z);    // Vertex A 
     GL11.glTexCoord2f(1, 0); 
     GL11.glVertex3f(x - size, y, z);  // Vertex B 
     GL11.glTexCoord2f(1, 1); 
     GL11.glVertex3f(x - size, y - size, z); // Vertex D 
     GL11.glEnd`enter code here`(); 
    } 
} 


public static void main(String[] args) 
{ 
    Main Main = new Main(); 
    Main.start(); 
} 

} 

這是Camera類。

package three.demensional.testing; 

import org.lwjgl.opengl.GL11; 
import org.lwjgl.util.vector.Vector3f; 

public class CameraController { 
public Vector3f position = null; 

private float yaw = 0.0f; 
private float pitch = 0.0f; 

public CameraController(float y, float x, float z) 
{ 
    position = new Vector3f(x, y, z); 
} 

public void yaw(float amount) 
{ 
    yaw += amount; 
} 

public void pitch(float amount) 
{ 
    pitch += amount; 
} 

public void lookThrough() 
{ 
    GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f); 
    GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f); 
    GL11.glTranslatef(position.x, position.y, position.z); 
} 

public void walkForward(float distance) 
{ 
    position.x -= distance * (float)Math.sin(Math.toRadians(yaw)); 
    position.z += distance * (float)Math.cos(Math.toRadians(yaw)); 
    position.z += distance * (float) Math.cos(Math.toRadians(yaw)); 
} 

public void walkBackward(float distance) 
{ 
    position.x += distance * (float)Math.sin(Math.toRadians(yaw)); 
    position.z -= distance * (float)Math.cos(Math.toRadians(yaw)); 
    position.z -= distance * (float) Math.cos(Math.toRadians(yaw)); 
} 

public void strafeLeft(float distance) 
{ 
    position.x -= distance * (float)Math.sin(Math.toRadians(yaw - 90)); 
    position.z += distance * (float)Math.cos(Math.toRadians(yaw - 90)); 
} 

public void strafeRight(float distance) 
{ 
    position.x -= distance * (float)Math.sin(Math.toRadians(yaw + 90)); 
    position.z += distance * (float)Math.cos(Math.toRadians(yaw + 90)); 
} 

public void flyUp(float distance) 
{ 
    position.y -= distance; 
} 
public void flyDown(float distance) 
{ 
    position.y += distance; 
} 
} 

最後,這裏是這個鏈接的結果圖片。

http://imgur.com/nzKWjJi

回答

1

你可能要啓用深度測試。

沒有深度測試,多邊形總是覆蓋先前渲染的多邊形。所以如果後廣場碰巧在前廣場後面畫,你會看到前廣場上的後廣場,就像你的照片一樣。

隨着深度測試,圖形系統將只覆蓋遠離相機的像素。

據我所知,所有LWJGL顯示模式都已經包含深度緩衝區(這是深度測試工作所必需的)。因此,您只需添加:

GL11.glEnable(GL11.GL_DEPTH_TEST); 

到您的初始化代碼。

+0

謝謝你的解決方案!這似乎是訣竅! – Ryanh 2014-12-07 02:54:21