我遇到了一个奇怪的问题。我正在研究一个3D程序。 (我只是涉足ljwgl)。我做了一个简单的立方体。但是当我尝试纹理时,它几乎是一种透明的东西。不仅如此,我对这个运动有一个问题。它非常不稳定,我不知道为什么。提前谢谢。
这是主要课程。
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;
}
}
最后这里是这个链接的结果图片。
答案 0 :(得分:1)
您可能想要启用深度测试。
如果没有深度测试,多边形总是会覆盖先前渲染的多边形。因此,如果正方形正好在正方形之后绘制,那么您将看到正方形“在正方形顶部”的正方形,就像在图片中一样。
通过深度测试,图形系统只会覆盖距离相机较远的像素。
据我所知,所有LWJGL显示模式都已包含深度缓冲(深度测试工作需要)。所以你需要做的就是添加:
GL11.glEnable(GL11.GL_DEPTH_TEST);
到您的初始化代码。