LWJGL OpenGL投影矩阵问题

问题描述 投票:0回答:1

我正在用java编写一个游戏,最近我尝试添加投影矩阵,所以我能够用Z轴移动物体,实际上看到物体变小,有FOV,等等。

现在的问题是,当我引入这一点时,我的对象只是不渲染。我一直在遵循一个不错的教程系列,我相信我做的一切都是正确的,并仔细检查了两个小时,尝试新的解决方案,然后我放弃了,并查看了LWJGL文档。

这是我的vertexShader代码。

#version 150 core

in vec3 position;
in vec2 textureCoords;

out vec3 color;

out vec2 pass_textureCoords;

uniform mat4 projectionMatrix;
uniform mat4 transformationMatrix;

void main(void) {
    gl_Position = projectionMatrix * transformationMatrix * vec4(position, 1.0);

    pass_textureCoords = textureCoords;

    color = vec3(position.x+0.5,0.0,position.y+0.5);
}

这是我的fragmentShader代码

#version 150 core

in vec2 pass_textureCoords;

out vec4 out_Color;

uniform sampler2D textureSampler;

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

这是我的渲染器类。

package me.purplex.jgame.renderer;

import me.purplex.jgame.entity.Entity;
import me.purplex.jgame.model.RawModel;
import me.purplex.jgame.model.TexturedModel;
import me.purplex.jgame.shaders.program.impl.StaticShader;
import me.purplex.jgame.utils.MathUtils;
import org.lwjgl.opengl.*;
import org.lwjgl.util.vector.Matrix4f;

public class Renderer {

    private Matrix4f projectionMatrix;

    public static final float FOV = 70;

    public static final float NEAR_PLANE = 0.1f;

    /**
     * VIEW DISTANCE
     */
    public static final float FAR_PLANE = 1000;

    public Renderer(StaticShader shader) {
        createProjectionMatrix();
        shader.start();
        shader.loadProjectionMatrix(projectionMatrix);
        shader.stop();
    }

    public void prepare() {
        GL11.glClearColor(1, 0, 0, 1);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    }

    public void render(Entity entity, StaticShader shader) {
        TexturedModel model = entity.getModel();
        RawModel rawModel = model.getRawModel();
        GL30.glBindVertexArray(rawModel.getVaoID());
        GL20.glEnableVertexAttribArray(0);
        GL20.glEnableVertexAttribArray(1);
        Matrix4f transformationMatrix = MathUtils.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, model.getModelTexture().getTextureID());
        GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
        GL20.glDisableVertexAttribArray(0);
        GL20.glDisableVertexAttribArray(1);
        GL30.glBindVertexArray(0);
    }

    private void createProjectionMatrix() {
        final float width = Display.getWidth();
        final float height = Display.getHeight();

        final float fieldOfView = Renderer.FOV;
        final float aspectRatio = width / height;
        final float nearPlane = Renderer.NEAR_PLANE;
        final float farPlane = Renderer.FAR_PLANE;

        final float yScale = (float) coTanget(Math.toRadians(fieldOfView / 2f));
        final float xScale = yScale / aspectRatio;
        final float frustumLength = farPlane - nearPlane;

        projectionMatrix = new Matrix4f();
        projectionMatrix.m00 =xScale;
        projectionMatrix.m11 = yScale;
        projectionMatrix.m22 = -((farPlane + nearPlane) / frustumLength);
        projectionMatrix.m23 = -1;
        projectionMatrix.m32 = -((2 * nearPlane * farPlane) / frustumLength);
        projectionMatrix.m33 = 0;
    }

    private double coTanget(double rads) {
        return (1.0 / Math.tan(rads));
    }
}

这是我的主循环

        DisplayManager.createDisplay();
        float[] vertices = {
                -0.5f, 0.5f, 0f,
                -0.5f, -0.5f, 0f,
                0.5f, -0.5f, 0.0f,
                0.5f, 0.5f, 0.0f
        };
        int[] indices = {
                0, 1, 3,
                3, 1, 2
        };
        float[] textureCoords = {
                0, 0,
                0, 1,
                1, 1,
                1, 0
        };
        ModelLoader modelLoader = new ModelLoader();

        StaticShader shader = new StaticShader();

        Renderer renderer = new Renderer(shader);

        RawModel model = modelLoader.loadToVAO(vertices, textureCoords, indices);

        TexturedModel staticModel = new TexturedModel(model,new ModelTexture(modelLoader.loadTexture("image")));

        Entity entity = new Entity(staticModel, new Vector3f(0, 0, -1), 0, 0, 0, 1);
        while (!Display.isCloseRequested()) {
            renderer.prepare();
            shader.start();
            renderer.render(entity, shader);
            shader.stop();
            DisplayManager.updateDisplay();
        }
        shader.cleanUp();
        modelLoader.cleanUp();
        DisplayManager.closeDisplay();

为什么?我有点恼火.在我看来 vertexShader 当我把 projectionMatrix 部分,一切都很好,但我不能有一个投影矩阵!当我添加它时,我的对象根本没有渲染,我看到的只是我的红色背景,如果有人能帮助它将意味着世界!谢谢你。

我看到的只是我的红色背景,但没有图像。

java opengl matrix lwjgl projection
1个回答
0
投票

透视投影矩阵可以由一个定义为 . 距离 left, right, bottomtop,是近平面上从视图中心到甲壳侧面的距离。nearfar 规定了到壳的近平面和远平面的距离。

r = right, l = left, b = bottom, t = top, n = near, f = far

x:    2*n/(r-l)      0              0                0
y:    0              2*n/(t-b)      0                0
z:    (r+l)/(r-l)    (t+b)/(t-b)    -(f+n)/(f-n)    -1
t:    0              0              -2*f*n/(f-n)     0

如果投影是对称的,视线是甲壳的对称轴,矩阵可以简化。

a  = w / h
ta = tan( fov_y / 2 );

2 * n / (r-l) = 1 / (ta * a)
2 * n / (t-b) = 1 / ta
(r+l)/(r-l)   = 0
(t+b)/(t-b)   = 0

对称透视投影矩阵是:

x:    1/(ta*a)  0      0              0
y:    0         1/ta   0              0
z:    0         0     -(f+n)/(f-n)   -1
t:    0         0     -2*f*n/(f-n)    0

因此你的计算结果是 yScale 是错误的。

yScale = (float) coTanget(Math.toRadians(fieldOfView / 2f));

yScale = 1.0f / (float)coTanget(Math.toRadians(fieldOfView / 2f));

0
投票

我想澄清一下问题已经解决了,我忘了调用一个方法来启动所有的统一变量,我很惊讶有些东西还能用。 谢谢你的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.