lwjgl 3 with projectionMatrix:没有渲染

时间:2016-09-30 13:14:45

标签: java matrix lwjgl

当我尝试启用projectionMatrix时,渲染图像消失,我只看到黑屏。控制台不会返回任何错误。

问题出在哪里?

渲染

package ms.renderer;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;

import org.joml.Matrix4f;

import ms.Display;
import ms.renderer.VertexArrayObject.Vertex;
import ms.shaders.ShaderProgram;
import ms.utils.FileLoader;
import ms.utils.TextureLoader;

public class Renderer {

VertexArrayObject vertices;
ShaderProgram shaders;
Display display;
Matrix4f projectionMatrix;

private float FOV = (float) Math.toRadians(60.0f);
private float Z_NEAR = 0.01f;
private float Z_FAR = 1000.0f;

public Renderer() {

    display = new Display();
    vertices = new VertexArrayObject();
}

public void init() throws Exception {

    shaders = new ShaderProgram();
    shaders.createVertexShader(FileLoader.loadResources("/ms/resources/shaders/vertexShader.vs"));
    shaders.createFragmentShader(FileLoader.loadResources("/ms/resources/shaders/fragmentShader.fs"));
    shaders.link();

    float aspectRation = (float) display.getWidth() / display.getHeight();
    projectionMatrix = new Matrix4f().perspective(FOV, aspectRation, Z_NEAR, Z_FAR);

    shaders.createUniforms("projectionMatrix");
    shaders.createUniforms("textureSampler");
}

public void render(Vertex vertex, TextureLoader texture) {

    clear();

    shaders.bindPorgram();

    shaders.setUniform("projectionMatrix", projectionMatrix);
    shaders.setUniform("textureSampler", 0);

    glBindVertexArray(vertex.getVaoID());
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture.getId());

    glDrawElements(GL_TRIANGLES, vertex.getVertexCount(), GL_UNSIGNED_INT,       0);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glBindVertexArray(0);

    shaders.unbindProgram();
}

public void clear() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

public void clenaUp() {

    vertices.cleanVAO();
    shaders.cleanUp();
}
}

VertexShader

#version 400 core

in vec3 position;
in vec2 textureCoords;

out vec2 outTextureCoords;

uniform mat4 projectionMatrix;

void main()
{
    gl_Position = projectionMatrix * vec4(position, 1.0);
    outTextureCoords = textureCoords;
}

MainClass - gameLoop()方法

public void gameLoop() throws Exception {

    isRunning = true;

    display.libVersion();
    renderer.init();

    float[] vertices = {
              -0.5f,  0.5f, -5.0f,
              -0.5f, -0.5f, -5.0f,
               0.5f, -0.5f, -5.0f,
               0.5f,  0.5f, -5.0f
            };

    int[] indices = {
              0,1,3,
              3,1,2
    };

    float[] textCoords = {
             0, 0,
             0, 1,
             1, 1,
             1, 0
    };

    Vertex model = loader.loadVAO(vertices, textCoords, indices);
    TextureLoader texture = new TextureLoader("image");

    while (isRunning && !display.windowShouldClose()) {

        update();
        renderer.render(model, texture);

        if(display.windowShouldClose()) {

            renderer.clenaUp();
            isRunning = false;
        }
    }
}

如果我删除projectionMatrix,图像渲染正常。 projectionMatrix通过转换Floatbuffer中的值来均匀加载。

ShaderProgram - setUniform()方法

public void setUniform(String uniformName, Matrix4f value) {

    FloatBuffer buffers = BufferUtils.createFloatBuffer(16);
    value.get(buffers);

    glUniformMatrix4fv(uniforms.get(uniformName), false, buffers);
}

1 个答案:

答案 0 :(得分:0)

我发现了错误:

<强>渲染

public void createProjectionMatrix() {

    Matrix4f projectionMatrix = transformation.getProjectionMatrix(
            FOV, 
            display.getWidth(), //Solve by indicate an int value
            display.getHeight(), //Solve by indicate an int value
            Z_NEAR, 
            Z_FAR);

    shaders.setUniform("projectionMatrix", projectionMatrix);
}

但是如果我调整窗口大小,图像就会移动到右上角。

如何解析getWidth()和getHeight()方法?

<强>显示

import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;

import ms.input.KeyboardInput;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;

public class Display {

GLFWKeyCallback keyCallback;
GLFWWindowSizeCallback windowSizeCallback;

private int width;
private int height;

private String title;

public long window;

public Display() {}

public Display(String title, int width, int height) {

    this.title = title;
    this.width = width;
    this.height = height;
}

public void init() {

    GLFWErrorCallback.createPrint(System.err).set();

    if ( !glfwInit() )
        throw new IllegalStateException("Unable to initialize GLFW");

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    window = glfwCreateWindow(width, height, title, NULL, NULL);
    if ( window == NULL )
        throw new RuntimeException("Failed to create the GLFW window");

    glfwSetWindowSizeCallback(window, windowSizeCallback = new GLFWWindowSizeCallback() {
        @Override
        public void invoke(long window, int width, int height) {

            Display.this.width = width;
            Display.this.height = height;
        }
    });

    glfwSetKeyCallback(window, keyCallback = new KeyboardInput());

    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(
        window,
        (vidmode.width() - width) / 2,
        (vidmode.height() - height) / 2
    );

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);
    glfwShowWindow(window);

    GL.createCapabilities();
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

public void libVersion() {

    System.out.println("LWJGL Version: " + Version.getVersion() + "!");
    System.out.println("GLFW Version: " + glfwGetVersionString() + "!");
    System.out.println("OpenGL Version: " + glGetString(GL_VERSION));
}

public boolean windowShouldClose() {
    return glfwWindowShouldClose(window);
}

public int getWidth() {
    return width;
}

public int getHeight() {
    return height;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}
}