我正在開發一個使用LWJGL 2.8.5的3D可視化應用程序。 在閱讀項目主頁的第一篇教程之後,我還深入分析了閱讀OpenGL書籍。 我看到OpenGL中的典型過程是在init函數中繪製場景,然後簡單地調用循環中的顯示更新。LWJGL - 渲染週期的屏幕閃爍
但是,當我嘗試使用LWJGL時,我在顯示屏上產生閃爍效果。 消除閃爍的唯一方法是在顯示更新週期中重新繪製場景。 這是怎麼回事?
爲了更好地解釋我的問題,我創建了一個簡單的類來重現問題。 它只是在屏幕中心畫一個四邊形,然後進入無盡的屏幕更新循環。
請注意,如果我取消註釋週期內的繪圖調用,則閃爍消失一切正常。 爲什麼?
我的期望是否有任何錯誤只繪製一次物體,只是移動相機以獲得靜態場景的不同視圖?
這是代碼:
package test;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import static org.lwjgl.opengl.GL11.*;
public class DisplayTest
{
public static void initGL()
{
GL11.glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
GLU.gluPerspective(45.0f, 640f/480f,0.1f, 100.0f);
draw();
}
public static void draw()
{
glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity(); // Reset The Current Modelview Matrix
GL11.glTranslatef(0, 0, -6.0f);//Place at the center at -6 depth units
//Start drawing a quad
//--------------------------------------------------
GL11.glBegin(GL11.GL_QUADS);
int size=1;
GL11.glColor3f(.3f, .5f, .8f);
GL11.glVertex3f(-size/2f,-size/2f,+size/2f);
GL11.glVertex3f(+size/2f,-size/2f,+size/2f);
GL11.glVertex3f(+size/2f,+size/2f,+size/2f);
GL11.glVertex3f(-size/2f,+size/2f,+size/2f);
glEnd();
}
public static void main(String[] args)
{
try
{
// Sets the width of the display to 640 and the height to 480
Display.setDisplayMode(new DisplayMode(640, 480));
// Sets the title of the display
Display.setTitle("Drawing a quad");
// Creates and shows the display
Display.create();
}
catch (LWJGLException e)
{
e.printStackTrace();
Display.destroy();
System.exit(1);
}
initGL();
// While we aren't pressing the red button on the display
while (!Display.isCloseRequested())
{
//draw();
// Update the contents of the display and check for input
Display.update();
// Wait until we reach 60 frames-per-second
Display.sync(60);
}
// Destroy the display and render it invisible
Display.destroy();
System.exit(0);
}
}
你能更具體嗎?也就是說,我應該如何修改上面張貼的簡單代碼? – Matteo
用一些複製粘貼的示例代碼編輯答案 –
謝謝,它工作正常!但是現在,它對視角的變化沒有反應(通過在循環中使用不同的值調用GLU.gluLookAt(eyeX,eyeY,eyeZ,centerX,centerY,centerZ,upX,upY,upZ)) – Matteo