java - lwjgl 无法显示具有 OpenGL 3 兼容性的三角形

标签 java opengl lwjgl

我有一个非常简单的 lwjgl 程序:

package com.github.fabioticconi;

import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryUtil;

import java.nio.FloatBuffer;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
import static org.lwjgl.opengl.GL30.glGenVertexArrays;
import static org.lwjgl.system.MemoryUtil.NULL;

/**
 * Author: Fabio Ticconi
 * Date: 10/03/18
 */
public class Main
{
    public static void main(final String[] args)
    {
        // Setup an error callback. The default implementation
        // will print the error message in System.err.
        GLFWErrorCallback.createPrint(System.err).set();

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

        glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

        // IF I ENABLE THE BELOW, I DON'T SEE THE TRIANGLE!
        // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
        // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

        // Open a window and create its OpenGL context
        final long window = glfwCreateWindow(1024, 768, "Test", NULL, NULL);

        if (window == NULL)
            throw new RuntimeException("Failed to create the GLFW window");

        glfwMakeContextCurrent(window);

        // This line is critical for LWJGL's interoperation with GLFW's
        // OpenGL context, or any context that is managed externally.
        // LWJGL detects the context that is current in the current thread,
        // creates the GLCapabilities instance and makes the OpenGL
        // bindings available for use.
        GL.createCapabilities();

        // Ensure we can capture the escape key being pressed below
        glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

        final float[] vertices = new float[] { 0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f };

        int         vaoId;
        final int   vboId;
        FloatBuffer verticesBuffer = null;
        try
        {
            verticesBuffer = MemoryUtil.memAllocFloat(vertices.length);
            verticesBuffer.put(vertices).flip();

            // Create the VAO and bind to it
            vaoId = glGenVertexArrays();
            glBindVertexArray(vaoId);

            // Create the VBO and bint to it
            vboId = glGenBuffers();
            glBindBuffer(GL_ARRAY_BUFFER, vboId);
            glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
            // Define structure of the data
            glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);

            // Unbind the VBO
            glBindBuffer(GL_ARRAY_BUFFER, 0);

            // Unbind the VAO
            glBindVertexArray(0);
        } finally
        {
            if (verticesBuffer != null)
            {
                MemoryUtil.memFree(verticesBuffer);
            }
        }

        do
        {
            // Swap buffers
            glfwSwapBuffers(window);
            glfwPollEvents();

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            // Bind to the VAO
            glBindVertexArray(vaoId);
            glEnableVertexAttribArray(0);

            // Draw the vertices
            glDrawArrays(GL_TRIANGLES, 0, 3);

            // Restore state
            glDisableVertexAttribArray(0);
            glBindVertexArray(0);

        } // Check if the ESC key was pressed or the window was closed
        while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && !glfwWindowShouldClose(window));
    }
}

当我运行它时,我看到一个白色三角形。这正是我期望看到的。

但是,如果我取消注释此行:

// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

我没有看到三角形,只看到黑色的窗口。尝试了一下,看起来每当我将上下文版本设置为 3 或更高时,除非我使用 GLFW_OPENGL_COMPAT_PROFILE,否则它不会显示三角形。

我使用的是 Linux,nvidia 专有驱动程序显然支持 OpenGL 4.5:

$ glxinfo | grep -i open
OpenGL vendor string: NVIDIA Corporation
OpenGL renderer string: GeForce GTX 970M/PCIe/SSE2
OpenGL core profile version string: 4.5.0 NVIDIA 384.111
OpenGL core profile shading language version string: 4.50 NVIDIA
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 4.5.0 NVIDIA 384.111
OpenGL shading language version string: 4.50 NVIDIA
OpenGL context flags: (none)
OpenGL profile mask: (none)
OpenGL extensions:
OpenGL ES profile version string: OpenGL ES 3.2 NVIDIA 384.111
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20
OpenGL ES profile extensions:

所以我真的不明白为什么 lwjgl 似乎在 OpenGL 3 或 4 兼容性方面默默失败。

这些行显然是支持 MacOS 所必需的(来 self 遇到过的所有 OpenGL 和 lwjgl 教程),总的来说,我想知道发生了什么。

有什么线索吗?

最佳答案

在 OpenGL 核心配置文件中,必须使用您自己的着色器。如果您不提供后备方案,则不会使用后备方案。

我也不相信这真的会默默地失败,因为您从未查询glGetError()。您很可能会从 glDrawArrays 行收到错误。

关于java - lwjgl 无法显示具有 OpenGL 3 兼容性的三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49212417/

相关文章:

java - Docker 外部数据库映射

java - 如何使用 ThreadPoolExecutor 和自定义任务实现 PriorityBlockingQueue

c++ - 跨平台硬件加速 2d C++ 应用程序?

c++ - 使用鼠标单击和鼠标拖动(Opengl)绕立方体

java - 线程中出现异常 "main"java.lang.UnsatisfiedLinkError : no lwjgl-devil in java. library.path

java - 具有浮点和字节的交错 VBO

Java while 循环计时

Java JOptionPane 单步执行数组

java - 使用 HSQLDB JDBC 驱动程序在 CSV 上执行 SQL

multithreading - 当在 RenderThread 上调用 SwapBuffers 时,Windows 上的 OpenGL 使用大量 CPU