Как правильно рендерить объекты вокруг камеры ?

 
 
 
Сообщения:1
Добрый день. Впервые занимаюсь созданием 3д пространства при помощи lwjgl на java. Столкнулся с такой вот проблемой: У меня есть 2 куба, первым рендерится синий, вторым красный. В игре все выглядит так, что последний рендерящийся объект(красный) виден сквозь все предыдущие.



Как возможно решить эту проблему?

P.s. использую lwjgl

Материалы:

Код самой камеры
Main.java
package test3D;
 
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.glu.GLU;
import ru.xalva.tctrpg.pckg.EulerCamera;
import ru.xalva.tctrpg.render.Cube;
 
import static org.lwjgl.opengl.GL11.*;
 
/**
 * Created by Xalva on 07.11.2016.
 */
public class Main {
 
    static EulerCamera camera;
 
    public static void main(String[] strs){
        try {
            setupDisplay();
            setupGL();
            init();
            render();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }
    }
 
    public static void render(){
        Cube blue = new Cube(100, 100, 100, new org.lwjgl.util.Color(1, 1, 255)).setTexture("world/cells/grass");
        Cube red = new Cube(100, 100, 100, new org.lwjgl.util.Color(255, 1, 1)).setTexture("world/cells/grass");
        while (true) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glLoadIdentity();
 
            camera.applyTranslations();
 
            //DRAW
            glTranslatef(-100, 0, -400);
            blue.draw();
            glTranslatef(0, 0, -200);
            red.draw();
 
 
            glLoadIdentity();
            input();
            Display.update();
        }
    }
 
    public static void input(){
        camera.processMouse(1, 80, -80);
        camera.processKeyboard(16, 1.0F, 1.0F, 1.0F);
        if (Mouse.isButtonDown(0)) {
            Mouse.setGrabbed(true);
        } else if (Mouse.isButtonDown(1)) {
            Mouse.setGrabbed(false);
        }
    }
 
    public static void init(){
        camera = new EulerCamera.Builder().setAspectRatio((float) Display.getWidth() / Display.getHeight())
                .setRotation(0f, 0f, 0f).setPosition(0, 0, 0).setFieldOfView(60).build();
        camera.applyOptimalStates();
        camera.applyPerspectiveMatrix();
    }
 
    public static void setupGL(){
        glViewport(0, 0, 800, 600);                       // Reset The Current Viewport And Perspective Transformation
        glMatrixMode(GL_PROJECTION);                           // Select The Projection Matrix
        glLoadIdentity();                                      // Reset The Projection Matrix
        GLU.gluPerspective(45.0f, 800 / 600, 0.1f, 100.0f);  // Calculate The Aspect Ratio Of The Window
        glMatrixMode(GL_MODELVIEW);                            // Select The Modelview Matrix
        glLoadIdentity();
 
        glShadeModel(GL_SMOOTH);                            //Enables Smooth Color Shading
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);               //This Will Clear The Background Color To Black
        glClearDepth(1.0);                                  //Enables Clearing Of The Depth Buffer
        glEnable(GL_DEPTH_TEST);                            //Enables Depth Testing
        glDepthFunc(GL_LEQUAL);                             //The Type Of Depth Test To Do
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    }
 
 
    public static void setupDisplay() throws LWJGLException {
        Display.setDisplayMode(new DisplayMode(800, 600));
        Display.setTitle("TEST");
        Display.create();
        Keyboard.create();
    }
}

Camera.java (шаблон)
public interface Camera {
 
    /** Processes mouse input and converts it in to camera movement. */
    public void processMouse();
 
    /**
     * Processes mouse input and converts it in to camera movement.
     *
     * @param mouseSpeed the speed (sensitivity) of the mouse, 1.0 should suffice
     */
    public void processMouse(float mouseSpeed);
 
    /**
     * Processes mouse input and converts it into camera movement.
     *
     * @param mouseSpeed the speed (sensitivity) of the mouse, 1.0 should suffice
     * @param maxLookUp the maximum angle in degrees at which you can look up
     * @param maxLookDown the maximum angle in degrees at which you can look down
     */
    public void processMouse(float mouseSpeed, float maxLookUp, float maxLookDown);
 
    /**
     * Processes keyboard input and converts into camera movement.
     *
     * @param delta the elapsed time since the last frame update in milliseconds
     *
     * @throws IllegalArgumentException if delta is 0 or delta is smaller than 0
     */
    public void processKeyboard(float delta);
 
    /**
     * Processes keyboard input and converts into camera movement.
     *
     * @param delta the elapsed time since the last frame update in milliseconds
     * @param speed the speed of the movement (normal = 1.0)
     *
     * @throws IllegalArgumentException if delta is 0 or delta is smaller than 0
     */
    public void processKeyboard(float delta, float speed);
 
    /**
     * Processes keyboard input and converts into camera movement.
     *
     * @param delta the elapsed time since the last frame update in milliseconds
     * @param speedX the speed of the movement on the x-axis (normal = 1.0)
     * @param speedY the speed of the movement on the y-axis (normal = 1.0)
     * @param speedZ the speed of the movement on the z-axis (normal = 1.0)
     *
     * @throws IllegalArgumentException if delta is 0 or delta is smaller than 0
     */
    public void processKeyboard(float delta, float speedX, float speedY, float speedZ);
 
    /**
     * Move in the direction you're looking. That is, this method assumes a new coordinate system where the axis you're
     * looking down is the z-axis, the axis to your left is the x-axis, and the upward axis is the y-axis.
     *
     * @param dx the movement along the x-axis
     * @param dy the movement along the y-axis
     * @param dz the movement along the z-axis
     */
    public void moveFromLook(float dx, float dy, float dz);
 
    /**
     * Sets the position of the camera.
     *
     * @param x the x-coordinate of the camera
     * @param y the y-coordinate of the camera
     * @param z the z-coordinate of the camera
     */
    public void setPosition(float x, float y, float z);
 
    /**
     * Sets GL_PROJECTION to an orthographic projection matrix. The matrix mode will be returned it its previous value
     * after execution.
     */
    public void applyOrthographicMatrix();
 
    /** Enables or disables OpenGL states that will enhance the camera appearance. */
    public void applyOptimalStates();
 
    /**
     * Sets GL_PROJECTION to an perspective projection matrix. The matrix mode will be returned it its previous value
     * after execution.
     */
    public void applyPerspectiveMatrix();
 
    /** Applies the camera translations and rotations to GL_MODELVIEW. */
    public void applyTranslations();
 
    /**
     * Sets the rotation of the camera.
     *
     * @param pitch the rotation around the x-axis in degrees
     * @param yaw the rotation around the y-axis in degrees
     * @param roll the rotation around the z-axis in degrees
     */
    public void setRotation(float pitch, float yaw, float roll);
 
    /** @return the x-coordinate of the camera */
    public float x();
 
    /** @return y the y-coordinate of the camera */
    public float y();
 
    /** @return the z-coordinate of the camera */
    public float z();
 
    /** @return the pitch of the camera in degrees */
    public float pitch();
 
    /** @return the yaw of the camera in degrees */
    public float yaw();
 
    /** @return the roll of the camera in degrees */
    public float roll();
 
    /** @return the fov of the camera in degrees in the y direction */
    public float fieldOfView();
 
    /**
     * Sets the field of view angle in degrees in the y direction. Note that this.applyPerspectiveMatrix() must be
     * applied in order to see any difference.
     *
     * @param fov the field of view angle in degrees in the y direction
     */
    public void setFieldOfView(float fov);
 
    /**
     * Sets the aspect ratio of the camera. Note that, to see any effect, you must call applyPerspectiveMatrix or
     * applyOrthographicMatrix.
     *
     * @param aspectRatio the aspect ratio of the camera
     *
     * @throws IllegalArgumentException if aspectRatio is 0 or less
     */
    public void setAspectRatio(float aspectRatio);
 
    /** @return the aspect ratio of the camera */
    public float aspectRatio();
 
    /** @return the distance from the camera to the near clipping pane */
    public float nearClippingPane();
 
    /** @return the distance from the camera to the far clipping pane */
    public float farClippingPane();
}
Изменен:07 ноя 2016 21:09
 
Модераторы:Нет
Сейчас эту тему просматривают:Нет