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();
}
}
1.你有問題嗎? 2.這些柱子和牆壁不是「面向」的......它們只是普通的3D對象。唯一改變的就是相機。 – immibis
@immibis我很難解釋,但他們沒有像往常一樣渲染3d。一開始角色向右走時,柱子保持不變,你看不到它的一面。然而,在「真正的」3D遊戲(以及上面的代碼)中,當您將相機移動到右側時,您也可以看到立方體的右側牆。 –