2016-12-24 73 views
0

中渲染多個形狀,只是對lwjgl做一些修改並製作一些2D形狀。不知道我做錯了什麼,但我無法展示超過屏幕上的第一個綠色方塊。這裏是我的窗口循環功能代碼:LWJGL:不能在opengl

private void loop() { 
// 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(); 
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); 
glOrtho(0, vidmode.width(),0, vidmode.height(),-1,1); 

// Set the clear color 
glClearColor(.0f, 0.6f, 0.6f, 0.0f); 


// Run the rendering loop until the user has attempted to close 
// the window or has pressed the ESCAPE key. 
while (!glfwWindowShouldClose(window) && !dataH.isGameOver()) { 


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer 

    glBegin(GL_QUADS); 
    { 
     glColor3f(0.1f, 1.0f, 0.1f); // Green 
     glVertex2d(xMapCentre - 100,yMapCentre - 100); 
     glVertex2d(xMapCentre - 100,yMapCentre + 100); 
     glVertex2d(xMapCentre + 100,yMapCentre + 100); 
     glVertex2d(xMapCentre + 100,yMapCentre - 100); 

     glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray 
     glVertex2f(-0.9f, -0.7f); 
     glColor3f(1.0f, 1.0f, 1.0f); // White 
     glVertex2f(-0.5f, -0.7f); 
     glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray 
     glVertex2f(-0.5f, -0.3f); 
     glColor3f(1.0f, 1.0f, 1.0f); // White 
     glVertex2f(-0.9f, -0.3f); 
    } 
    glEnd(); 



    glBegin(GL_TRIANGLES);   // Each set of 3 vertices form a triangle 
    { 
    glColor3f(0.0f, 0.0f, 1.0f); // Blue 
    glVertex2f(0.1f, -0.6f); 
    glVertex2f(0.7f, -0.6f); 
    glVertex2f(0.4f, -0.1f); 

    glColor3f(1.0f, 0.0f, 0.0f); // Red 
    glVertex2f(0.3f, -0.4f); 
    glColor3f(0.0f, 1.0f, 0.0f); // Green 
    glVertex2f(0.9f, -0.4f); 
    glColor3f(0.0f, 0.0f, 1.0f); // Blue 
    glVertex2f(0.6f, -0.9f); 
    } 
    glEnd(); 


    glfwSwapBuffers(window); // swap the color buffers 
    // Poll for window events. The key callback above will only be 
    // invoked during this call. 
    glfwPollEvents(); 
} 
} 

任何幫助將不勝感激,讓我知道如果我太模糊。謝謝。

回答

1

通常,在3D空間中渲染2D結構時,應禁用背面剔除glDisable(GL_CULL_FACE)。否則,你只能從一側看到它們。在着色器中,您必須注意正常的方向。

在2D唯一的情況下,檢查該三角形的繞組順序,如果你想擁有背面或正面剔除,對於逆時針分別順時針纏繞順序設置glFontFace(GL_CCW)glFrontFace(GL_CW)。 (我不能確認當使用glVertex2f時,纏繞順序是否會在2D中產生差異)

也嘗試diable深度測試和深度編寫glDisable(GL_DEPTH_TEST)glDepthMask(GL_FALSE)