LWJGL投影矩阵 - 一切都消失了

时间:2016-02-04 00:22:06

标签: java opengl lwjgl projection-matrix

我最近开始使用OpenGL开发Java项目,而不是我第一次尝试以自己的风格编写代码而不是直接使用教程。尝试应用投影矩阵时遇到了问题。我能够创建和显示三角形,我甚至可以使用转换矩阵(工作正常)。这是我的代码:
我的主要课程包含大部分模型信息。

public class Main{
public static Screen screen;
public static Loader loader = new Loader();
public static void main(String[] args){
    screen = new Screen();
    ModelData data = OBJFileLoader.loadOBJ("firstTest");
    BasicShader shader = new BasicShader();
    float f = 0.5f;
    RawModel rawModel = loader.loadToVAO(new float[] {
    -f, f, 0, -f, -f, 0, f, -f, 0, f, f, 0
    }, data.getTextureCoords(), data.getNormals(), new int[] {
    0, 1, 3, 3, 1, 2
    });
    // RawModel rawModel1 = loader.loadToVAO(data.getVertices(),
    // data.getTextureCoords(), data.getNormals(), data.getIndices());
    ModelTexture texture = new ModelTexture(loader.loadTexture("firstTest"));
    TexturedModel model = new TexturedModel(rawModel, texture);
    Entity entity = new Entity(model, new Vector3f(0, 0, 0), 0, 0, 0, 1);
    shader.start();
    shader.loadProjectionMatrix();
    shader.stop();
    //GL11.glEnable(GL11.GL_CULL_FACE);
    //GL11.glCullFace(GL11.GL_BACK);
    while(!Display.isCloseRequested()){
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glClearColor(0.5f, 0, 1, 1);
        shader.start();
        shader.loadTransformation(Maths.createTransformationMatrix(entity.position, entity.rx, entity.ry, entity.rz, entity.scale));
        GL30.glBindVertexArray(model.getRawModel().getVaoID());
        GL20.glEnableVertexAttribArray(0);
        GL11.glDrawElements(GL11.GL_TRIANGLES, model.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
        GL20.glDisableVertexAttribArray(0);
        GL30.glBindVertexArray(0);
        shader.stop();
        screen.updateDisplay();
    }
    shader.cleanUp();
    loader.cleanUp();
    Display.destroy();
}

我的顶点着色器:

#version 400 core

in vec3 position;

out vec3 color;
out vec2 gridPosition;

uniform mat4 projectionMatrix;
uniform mat4 transformationMatrix;

void main(void){
gl_Position = projectionMatrix * transformationMatrix * vec4(position, 1.0);
color = vec3(1.0, 1.0, 0.0);
}

我的屏幕和投影矩阵的创建:

public class Screen{
public static final float FOV = 70, NEAR_PLANE = 0.1f, FAR_PLANE = 1000;
private static final int WIDTH = 1000, HEIGHT = 600, FPS_CAP = 120;
public static Matrix4f projectionMatrix;
public Screen(){
    createProjectionMatrix();
    ContextAttribs attribs = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true);
    try{
        Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
        Display.create(new PixelFormat(), attribs);
        Display.setTitle("Screen Test");
    }catch (LWJGLException e){
        e.printStackTrace();
    }
    GL11.glViewport(0, 0, WIDTH, HEIGHT);
}
public void updateDisplay(){
    Display.sync(FPS_CAP);
    Display.update();
}
private void createProjectionMatrix(){
    float aspectRatio = (float) Display.getWidth() / (float) Display.getHeight();
    float y_scale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio);
    float x_scale = y_scale / aspectRatio;
    float frustum_length = FAR_PLANE - NEAR_PLANE;
    projectionMatrix = new Matrix4f();
    projectionMatrix.m00 = x_scale;
    projectionMatrix.m11 = y_scale;
    projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
    projectionMatrix.m23 = -1;
    projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
    projectionMatrix.m33 = 0;
}

我已经确认矩阵均匀被加载,gl_Position z介于-1和1之间,并且背面剔除没有做任何事情。我确信我已经忽略了一些简单的事情,但我已经在这一天做了一两天,并决定寻求一些帮助,如果有任何其他来源是必要的,我会发布。

0 个答案:

没有答案