2013-04-27 58 views
0

我給Archsynthetics'底漆和你好三角章節一讀,並決定跳進LWJGL。當我第一次嘗試後沒有在屏幕上看到任何東西時,我再次嘗試從另一個GL 3.x tutorial上移植一些C++代碼,但無濟於事。簡單的LWJGL程序什麼都沒有顯示

據我所知,我有所有的部分在一起,但屏幕仍然是黑色的。我理解這些概念,但我確信我在這裏錯過了一些簡單的東西。

我已經儘可能簡化了這一點。請注意,以下類使用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

」*只需要1.30版本*「這不需要任何*版本的桌面GL。事實上,在桌面GL中,它確實沒有*。 – 2013-04-27 12:03:51

+0

@NicolBolas着色器被直接複製/粘貼。不是我的評論,嘿。 – Qix 2013-04-27 12:07:03

+0

爲什麼從一個*不同的教程*複製和粘貼着色器,而不是你說你正在從事的工作? – 2013-04-27 12:08:10

回答

0

我不明白,你告訴OpenGL是in_Position是屬性0和in_Color是屬性1.有問題的教程做的部分與layout(location)語法着色。

如果您不能或不會使用該語法,那麼您需要在之前使用glBindAttribLocation calls來鏈接着色器。 「

+0

還沒有(查看更新的代碼)。 – Qix 2013-04-27 12:18:21