简体   繁体   中英

Lwjgl 3D Cube Transparency Issue

I have run into a bit of a strange issue. I am working on a 3d program. (I am just dabbling into ljwgl). I have made a simple cube. But when I try to texture it, it is almost a transparent type thing. Not only that, but I am having an issue with the movement. It is extremely choppy, and I am not sure why. Thanks in advance.

Here is the main class.

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();
}

}

Here is the Camera class.

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;
}
}

Finally here is a picture of the result at this link.

http://imgur.com/nzKWjJi

You probably want to enable depth testing.

Without depth testing, polygons always overwrite polygons that were previously rendered. So if the back square happened to be drawn after the front square, you'll see the back square "on top of" the front square, exactly as in your picture.

With depth testing, the graphics system will only overwrite pixels that are farther away from the camera.

As far as I know, all LWJGL display modes include a depth buffer already (which is required for depth testing to work). So all you need to do is add:

GL11.glEnable(GL11.GL_DEPTH_TEST);

to your initialization code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM