2015-06-07 67 views
0

我需要使3D對象以同樣的方式面對,無論它們在攝像頭上的位置如何。正如如果相機是非常非常遠,在放大這裏是我的意思(看看柱子和牆壁):http://youtu.be/Pn9fh93oV-cLWJGL讓對象始終正視

這是我到目前爲止有:

package pack; 

import org.lwjgl.LWJGLException; 
import org.lwjgl.input.Keyboard; 
import org.lwjgl.opengl.Display; 
import org.lwjgl.opengl.DisplayMode; 
import org.lwjgl.opengl.GL11; 

import java.util.Random; 

import static org.lwjgl.opengl.GL11.*; 
import static org.lwjgl.util.glu.GLU.gluPerspective; 

public class Main { 

float viewx, viewy; 
float angle; 

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

private void start() { 
    try { 
     Display.setDisplayMode(new DisplayMode(640, 480)); 
     Display.setTitle("Three Dee Demo"); 
     Display.create(); 
    } catch (LWJGLException e) { 
     e.printStackTrace(); 
     Display.destroy(); 
     System.exit(1); 
    } 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective((float) 30, 640f/480f, 0.001f, 100); 
    glMatrixMode(GL_MODELVIEW); 
    glEnable(GL_DEPTH_TEST); 
    glTranslatef(0, 0, -20); 

    while (!Display.isCloseRequested()) { 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

     update(); 
     render(); 

     Display.update(); 
     Display.sync(60); 
    } 

    Display.destroy(); 
    System.exit(0); 
} 

private void render() { 

    GL11.glBegin(GL11.GL_QUADS);  
    GL11.glColor3f(1.0f,1.0f,0.0f);   
    GL11.glVertex3f(1.0f, 1.0f,-1.0f);   
    GL11.glVertex3f(-1.0f, 1.0f,-1.0f);   
    GL11.glVertex3f(-1.0f, 1.0f, 1.0f); 
    GL11.glVertex3f(1.0f, 1.0f, 1.0f); 
    GL11.glColor3f(1.0f,0.5f,0.0f);    
    GL11.glVertex3f(1.0f,-1.0f, 1.0f); 
    GL11.glVertex3f(-1.0f,-1.0f, 1.0f); 
    GL11.glVertex3f(-1.0f,-1.0f,-1.0f); 
    GL11.glVertex3f(1.0f,-1.0f,-1.0f); 
    GL11.glColor3f(1.0f,0.0f,0.0f); 
    GL11.glVertex3f(1.0f, 1.0f, 1.0f); 
    GL11.glVertex3f(-1.0f, 1.0f, 1.0f); 
    GL11.glVertex3f(-1.0f,-1.0f, 1.0f); 
    GL11.glVertex3f(1.0f,-1.0f, 1.0f); 
    GL11.glColor3f(1.0f,1.0f,0.0f); 
    GL11.glVertex3f(1.0f,-1.0f,-1.0f); 
    GL11.glVertex3f(-1.0f,-1.0f,-1.0f); 
    GL11.glVertex3f(-1.0f, 1.0f,-1.0f); 
    GL11.glVertex3f(1.0f, 1.0f,-1.0f); 
    GL11.glColor3f(0.0f,0.0f,1.0f); 
    GL11.glVertex3f(-1.0f, 1.0f, 1.0f); 
    GL11.glVertex3f(-1.0f, 1.0f,-1.0f); 
    GL11.glVertex3f(-1.0f,-1.0f,-1.0f); 
    GL11.glVertex3f(-1.0f,-1.0f, 1.0f); 
    GL11.glColor3f(1.0f,0.0f,1.0f); 
    GL11.glVertex3f(1.0f, 1.0f,-1.0f); 
    GL11.glVertex3f(1.0f, 1.0f, 1.0f); 
    GL11.glVertex3f(1.0f,-1.0f, 1.0f); 
    GL11.glVertex3f(1.0f,-1.0f,-1.0f); 
GL11.glEnd();  

} 

private void update() { 
    if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { 
     viewx -= 0.1f*(float)Math.sin(Math.toRadians(angle)); 
     viewy -= 0.1f*(float)Math.cos(Math.toRadians(angle)); 
    } 

    if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { 
     viewx += 0.1f*(float)Math.sin(Math.toRadians(angle)); 
     viewy += 0.1f*(float)Math.cos(Math.toRadians(angle)); 
    } 
    if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { 
     viewx+= 0.1f*(float)Math.sin(Math.toRadians(angle-90)); 
     viewy+=0.1f*(float)Math.cos(Math.toRadians(angle-90)); 
    } 

    if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { 
     viewx+= 0.1f*(float)Math.sin(Math.toRadians(angle+90)); 
     viewy+=0.1f*(float)Math.cos(Math.toRadians(angle+90)); 
    } 

    if (Keyboard.isKeyDown(Keyboard.KEY_Q)) { 
     angle += 1; 
    } 


    if (Keyboard.isKeyDown(Keyboard.KEY_E)) { 
     angle -= 1; 
    } 

    glLoadIdentity(); 
    glRotatef (angle, 0, 0, 1); 
    glTranslated(viewx, viewy, -20); 

    GL11.glBegin(GL11.GL_QUADS);  
    GL11.glColor3f(1.0f,1.0f,0.0f);   
    GL11.glVertex3f(-viewx-0.2f, -viewy-0.2f,0);   
    GL11.glVertex3f(-viewx+0.2f, -viewy-0.2f, 0);   
    GL11.glVertex3f(-viewx+0.2f, -viewy+0.2f, 0); 
    GL11.glVertex3f(-viewx-0.2f, -viewy+0.2f, 0); 
    glEnd();  

} 
} 
+0

1.你有問題嗎? 2.這些柱子和牆壁不是「面向」的......它們只是普通的3D對象。唯一改變的就是相機。 – immibis

+0

@immibis我很難解釋,但他們沒有像往常一樣渲染3d。一開始角色向右走時,柱子保持不變,你看不到它的一面。然而,在「真正的」3D遊戲(以及上面的代碼)中,當您將相機移動到右側時,您也可以看到立方體的右側牆。 –

回答

1

那場比賽外出使用正投影 - 注意距離相機較遠的東西是不是更小。這就是爲什麼線條的方向不取決於相機的位置。

這個畫面的左半部分示出了具有透視投影呈現板條箱的網格。右半部分顯示了使用正交投影渲染的同一網格。

請注意,正投影的情況下,無論它們的位置如何,所有的箱子看起來都是相同的,無論它們與攝像機的距離如何 - 只有它們的方位很重要。

在OpenGL固定功能管線中,您可以使用glOrtho設置正交投影。如果您正在使用可編程管道,那麼這取決於您的特定程序;在一般情況下,你會想要在fo中使用投影矩陣