【问题标题】:lwjgl 3 with projectionMatrix: nothing renderlwjgl 3 与 projectionMatrix:没有渲染
【发布时间】:2016-09-30 13:14:45
【问题描述】:

当我尝试启用 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();
}
}

顶点着色器

#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 图像渲染正常。通过转换Floatbuffer中的值来统一加载projectionMatrix。

ShaderProgram - setUniform() 方法

public void setUniform(String uniformName, Matrix4f value) {

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

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

【问题讨论】:

    标签: java matrix lwjgl


    【解决方案1】:

    我发现了错误:

    渲染器

    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;
    }
    }
    

    【讨论】:

    • 在静态变量中修改宽高int变量解决。
    猜你喜欢
    • 1970-01-01
    • 2015-01-12
    • 2015-09-15
    • 2022-07-30
    • 1970-01-01
    • 2020-09-08
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多