0

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

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

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

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

顶点32.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;
}

片段32.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
4

1 回答 1

1

我没有看到您告诉 OpenGLin_Position属性 0 和属性 1 的部分。有问题的教程使用语法in_Color在着色器中执行该部分。layout(location)

如果您不能或不会使用该语法,那么您需要在链接着色器之前通过glBindAttribLocation调用来执行此操作。

于 2013-04-27T12:08:16.353 回答