有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Lwjgl 3D多维数据集透明度问题

我遇到了一个有点奇怪的问题。我正在做一个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();
}

}

这是相机课

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) 个答案

  1. # 1 楼答案

    您可能希望启用深度测试

    在没有深度测试的情况下,多边形总是覆盖以前渲染的多边形。因此,如果后方格恰好是在前方格之后绘制的,您将看到前方格“顶部”的后方格,与您的图片完全相同

    通过深度测试,图形系统将只覆盖距离相机较远的像素

    据我所知,所有LWJGL显示模式都已包含深度缓冲区(深度测试需要该缓冲区)。因此,您只需添加:

    GL11.glEnable(GL11.GL_DEPTH_TEST);
    

    你的初始化代码