【问题标题】:Simple LWJGL program displays nothing简单的 LWJGL 程序不显示任何内容
【发布时间】:2013-04-27 11:35:37
【问题描述】:

我阅读了 Archsynthetics' 入门和 Hello triangle 章节,并决定加入 LWJGL。当我第一次尝试后屏幕上没有任何内容时,我再次尝试从另一个 GL 3.x tutorial 移植一些 C++ 代码,但无济于事。

据我所知,我已经将所有部分放在一起,但屏幕仍然是黑色的。我理解这些概念,但我确定我在这里遗漏了一些简单的东西。

我已经尽可能简单地减少了。请注意,以下类使用this shader helper,据我所知,它按预期工作(除了缺少错误检查 - 但是,我已确保着色器编译)。

public class HelloTriangle31 {
    public static void main(String[] args) throws LWJGLException, InterruptedException, IOException
    {
        // Setup display mode (size)
        Display.setDisplayMode(new DisplayMode(800, 600));

        // Set context settings
        //  Basically forces 3.1
        ContextAttribs contextAttributes = new ContextAttribs(3, 2)
        .withForwardCompatible(true)
        .withProfileCompatibility(false)
        .withProfileCore(true);
        Display.create(new PixelFormat(), contextAttributes);
        Display.setResizable(false);
        Display.setVSyncEnabled(true);

        // Log some stuff
        System.out.println("OpenGL version: " + GL11.glGetString(GL11.GL_VERSION));     

        // Setup
    String vertexStr = readEntireFile(new File("vertex32.gl"));
    int vertexID = ShaderUtils.makeShader(vertexStr, GL20.GL_VERTEX_SHADER);
    String fragStr = readEntireFile(new File("fragment32.gl"));
    int fragID = ShaderUtils.makeShader(fragStr, GL20.GL_FRAGMENT_SHADER);
    int program = GL20.glCreateProgram();
        GL20.glAttachShader(program, vertexID);
        GL20.glAttachShader(program, fragID);
        GL20.glBindAttribLocation(program, 0, "in_Position");
        GL20.glBindAttribLocation(program, 1, "in_Color");
        GL20.glLinkProgram(program);

        FloatBuffer vertexFloats = BufferUtils.createFloatBuffer(9);
        assert(vertexFloats.capacity() == 9);
        vertexFloats.put(new float[]{
            -0.3f, 0.5f, 0f,
            -0.8f, -0.5f, 0f,
            0.2f, -0.5f, 0f
        });

        FloatBuffer colorFloats = BufferUtils.createFloatBuffer(9);
        assert(colorFloats.capacity() == 9);
        colorFloats.put(new float[]{
            1.0f, 0.0f, 0.0f,
            0.0f, 1.0f, 0.0f,
            0.0f, 0.0f, 1.0f
        });

        IntBuffer vertexArrayInts = BufferUtils.createIntBuffer(1);
        assert(vertexArrayInts.capacity() == 1);
        GL30.glGenVertexArrays(vertexArrayInts);
        GL30.glBindVertexArray(vertexArrayInts.get(0));

        IntBuffer vertexBufferInts = BufferUtils.createIntBuffer(2);
        assert(vertexBufferInts.capacity() == 2);
        GL15.glGenBuffers(vertexBufferInts);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferInts.get(0));
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexFloats, GL15.GL_STATIC_DRAW);
        GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
        GL20.glEnableVertexAttribArray(0);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferInts.get(1));
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorFloats, GL15.GL_STATIC_DRAW);
        GL20.glVertexAttribPointer(1, 3, GL11.GL_FLOAT, false, 0, 0);
        GL20.glEnableVertexAttribArray(1);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL30.glBindVertexArray(0);

        GL11.glViewport(0, 0, 800, 600);

        GL20.glUseProgram(program);

        // Main loop
        while(!Display.isCloseRequested())
        {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            GL30.glBindVertexArray(vertexArrayInts.get(0));
            GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
            GL30.glBindVertexArray(0);

            Display.update();
        }

        Display.destroy();
    }

    private static String readEntireFile(File file) throws IOException
    {
        // Open input stream
        FileInputStream fis = new FileInputStream(file);
        try
        {
            byte[] buffer = new byte[(int) file.length()];
            int len = fis.read(buffer);
            return new String(buffer, 0, len);
        }finally{
            if(fis != null) fis.close();
        }
    }
}

vertex32.gl

#version 140

in  vec3 in_Position;
in  vec3 in_Color;
out vec3 ex_Color;

void main(void)
{
        gl_Position = vec4(in_Position, 1.0);
        ex_Color = in_Color;
}

fragment32.gl

#version 140

precision highp float; // needed only for version 1.30

in  vec3 ex_Color;
out vec4 out_Color;

void main(void)
{
        out_Color = vec4(ex_Color,1.0);
}

我看不到哪里出错了。没有错误,唯一的输出是版本字符串(它正确显示了 OpenGL 3.2 - ,我尝试过使用和不使用任何类型的显式上下文属性)。

在我遵循的所有教程中,令我感到奇怪的是,没有使用例如glOrtho 设置投影矩阵。我在 LWJGL 中的应用程序(使用 GL 1.1 功能)可以与 glOrtho 一起工作,但现在我正在尝试升级/重新构建/完善我的 GL 知识,我又回到了第一阶段。

我错过了什么?

编辑:定义 VM 参数 -Dorg.lwjgl.util.Debug=true 产生:

[LWJGL] Initial mode: 1920 x 1080 x 32 @60Hz
[LWJGL] MemoryUtil Accessor: AccessorUnsafe
[LWJGL] GL_ARB_gpu_shader_fp64 was reported as available but an entry point is missing
[LWJGL] GL_ARB_shader_subroutine was reported as available but an entry point is missing
[LWJGL] GL_ARB_vertex_attrib_64bit was reported as available but an entry point is missing
OpenGL version: 3.2.0

【问题讨论】:

  • "仅适用于 1.30 版" 任何 版本的桌面 GL 不需要。事实上,在桌面 GL 中,它什么都不做
  • @NicolBolas 着色器实际上是复制/粘贴的。不是我的评论,呵呵。
  • 您为什么要复制并粘贴来自不同教程的着色器而不是您所说的工作?
  • 这是我正在研究的那个......
  • “工作”,如“写作”或“阅读”?

标签: java opengl lwjgl


【解决方案1】:

我没有看到你告诉 OpenGL in_Position 是属性 0 和 in_Color 是属性 1 的部分。有问题的教程使用 layout(location) 语法在着色器中进行。

如果您不能或不会使用该语法,则需要在链接着色器之前使用glBindAttribLocation calls

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 2016-08-19
    • 2012-12-30
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多