使用现代OpenGL进行正交渲染?

时间:2017-09-19 01:03:27

标签: opengl matrix rendering lwjgl

嗨所以我试图使用正交投影矩阵渲染四边形。当我将矩阵加载到着色器而不进行移调时,屏幕上没有显示任何内容,但是如果我进行转置,我会得到一条对角线,显示在屏幕上而不是四边形(见下图)(四边形渲染时效果很好)乘以正交矩阵)。我试过旋转物体,来回移动它并尝试了一种不同的方法来创建一个正交矩阵,没有任何接缝可以工作。我也没有做任何剔除。有没有人知道如何解决这个问题?我是否应该将所有传递到矩阵中的矩阵转置?关于这个问题,我听到过多个不同的答案。

我的目标是使用现代OpenGL创建正交投影,而不使用任何立即模式技术。

目前的结果: enter image description here

矩阵输出: 我假设问题与矩阵的结果有关但我已经完成了计算并且这个接缝是正确的吗?

0.0015625 0.0 0.0 -1.0
0.0 -0.0027777778 0.0 1.0
0.0 0.0 -1.0 -0.0
0.0 0.0 0.0 1.0

顶点着色器:

#version 400 core

in vec2 position;
in vec2 textureCoords;

out vec2 pass_textureCoords;

uniform mat4 transformationMatrix;
uniform mat4 orthographicMatrix;

void main(void) {
    gl_Position = orthographicMatrix * transformationMatrix * vec4(position.xy, 0, 1.0);
    pass_textureCoords = textureCoords;
}

Fragment Shader:

#version 400 core

in vec2 pass_textureCoords;

out vec4 out_Color;

uniform sampler2D textureSampler;

void main(void) {
    out_Color = texture(textureSampler, pass_textureCoords);
}

Matrix Creation:

orthographicMatrix = Maths.createOrthoProjectionMatrix(0f, (float) Display.getWidth(), 0f, (float) Display.getHeight(), -1.0f, 1.0f);

public static Matrix4f createOrthoProjectionMatrix(float left, float right, float top, float bottom, float near, float far) {
    Matrix4f m = new Matrix4f();

    m.m00 = 2.0f / (right - left);
    m.m01 = 0.0f;
    m.m02 = 0.0f;
    m.m03 = 0.0f;

    m.m10 = 0.0f;
    m.m11 = 2.0f / (top - bottom);
    m.m12 = 0.0f;
    m.m13 = 0.0f;

    m.m20 = 0.0f;
    m.m21 = 0.0f;
    m.m22 = -2.0f / (far - near);
    m.m23 = 0.0f;

    m.m30 = -(right + left) / (right - left);
    m.m31 = -(top + bottom) / (top - bottom);
    m.m32 = -(far + near) / (far - near);
    m.m33 = 1.0f;

    return m;
}

渲染代码:

public void render(Entity entity, StaticShader shader) {
    TexturedModel texturedModel = entity.getTexturedModel();
    RawModel model = texturedModel.getRawModel();
    GL30.glBindVertexArray(model.getVaoID());
    GL20.glEnableVertexAttribArray(0);
    GL20.glEnableVertexAttribArray(1);
    Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
    shader.loadTransformationMatrix(transformationMatrix);
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturedModel.getTexture().getTextureID());
    GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
    GL20.glDisableVertexAttribArray(0);
    GL20.glDisableVertexAttribArray(1);
    GL30.glBindVertexArray(0);
}

转型矩阵:

public static Matrix4f createTransformationMatrix(Vector2f translation, float rx, float ry, float rz, float scale) {
    Matrix4f matrix = new Matrix4f();
    matrix.setIdentity();
    Matrix4f.translate(translation, matrix, matrix);
    Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1, 0, 0), matrix, matrix);
    Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0, 1, 0), matrix, matrix);
    Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0, 0, 1), matrix, matrix);
    Matrix4f.scale(new Vector3f(scale, scale, 1), matrix, matrix);
    return matrix;
}

四边形的顶点,指数和纹理坐标:

public static final float[] BOX_VERTICES = {
        -0.5f, 0.5f,
        -0.5f, -0.5f,
        0.5f, -0.5f,
        0.5f, 0.5f,
};

public static final int[] BOX_INDICES = {
        0, 1, 3,
        3, 1, 2
};

public static final float[] BOX_TEXTURE_COORDS = {
        0, 0,
        0, 1,
        1, 1,
        1, 0
};

VAO和VBO的创建:

public RawModel loadToVAO(float[] positions, float[] textureCoords, int[] indices) {
    int vaoID = createVAO();
    bindIndicesBuffer(indices);
    storeDataInAttributeList(0, positions);
    storeDataInAttributeList(1, textureCoords);
    unbindVAO();
    return new RawModel(vaoID, indices.length);
}

private int createVAO() {
    int vaoID = GL30.glGenVertexArrays();
    vaos.add(vaoID);
    GL30.glBindVertexArray(vaoID);
    return vaoID;
}

private void storeDataInAttributeList(int attributeNumber, float[] data) {
    int vboID = GL15.glGenBuffers();
    vbos.add(vboID);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
    FloatBuffer buffer = storeDataInFloatBuffer(data);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
    GL20.glVertexAttribPointer(attributeNumber, 2, GL11.GL_FLOAT, false, 0, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}

private void bindIndicesBuffer(int[] indices) {
    int vboID = GL15.glGenBuffers();
    vbos.add(vboID);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID);
    IntBuffer buffer = storeDataInIntBuffer(indices);
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
}

private IntBuffer storeDataInIntBuffer(int[] data) {
    IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
    buffer.put(data);
    buffer.flip();
    return buffer;
}

private FloatBuffer storeDataInFloatBuffer(float[] data) {
    FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
    buffer.put(data);
    buffer.flip();
    return buffer;
}

private void unbindVAO() {
    GL30.glBindVertexArray(0);
}

着色器类:

public class StaticShader extends ShaderProgram {

    private int location_transformationMatrix;
    private int location_orthographicMatrix;

    public StaticShader() {
        super("src/com/robert/game/shaders/staticVertexShader.glsl", "src/com/robert/game/shaders/staticFragmentShader.glsl");
    }

    @Override
    protected void bindAttributes() {
        super.bindAttribute(0, "position");
        super.bindAttribute(1, "textureCoords");
    }

    @Override
    protected void getAllUniformLocations() {
        location_transformationMatrix = super.getUniformLocation("transformationMatrix");
        location_orthographicMatrix = super.getUniformLocation("orthographicMatrix");
    }

    public void loadTransformationMatrix(Matrix4f matrix) {
        super.loadMatrix(location_transformationMatrix, matrix);
    }

    public void loadOrthographicMatrix(Matrix4f matrix) {
        super.loadMatrix(location_orthographicMatrix, matrix);
    }
}

着色器加载矩阵: 转置参数最初为false,导致屏幕上没有显示任何内容。

private static FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);

protected void loadMatrix(int location, Matrix4f matrix) {
    matrix.store(matrixBuffer);
    matrixBuffer.flip();
    GL20.glUniformMatrix4(location, true, matrixBuffer);
}

public Renderer(StaticShader shader) {
    orthographicMatrix = Maths.createOrthoProjectionMatrix(0f, (float) Display.getWidth(), 0f, (float) Display.getHeight(), -1.0f, 1.0f);
    System.out.println(orthographicMatrix);
    shader.start();
    shader.loadOrthographicMatrix(orthographicMatrix);
    shader.stop();
}

0 个答案:

没有答案