0
基本上,我實現的旋轉只在屏幕被調整大小期間被繪製到屏幕上。爲了調試,我在display()函數中插入了一個print,輸出角度。在運行程序時,它只顯示display()的兩次迭代的輸出,然後在隨後調整屏幕大小的過程中再次顯示輸出。 這使我相信display()只會被調用兩次? 我已經在下面發佈了相關回調,但是讓我知道是否需要其他代碼。display()似乎只能在調整大小時調用
float angle = 0.0f;
float increment = 0.5f;
@Override
public void init(GLAutoDrawable drawable)
{
GL2 gl = drawable.getGL().getGL2(); // get the OpenGL graphics context
glu = new GLU(); // get GL Utilities
glut = new GLUT();
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // set background (clear) color
gl.glClearDepth(1.0f); // set clear depth value to farthest
gl.glEnable(GL_DEPTH_TEST); // enables depth testing
gl.glDepthFunc(GL_LEQUAL); // the type of depth test to do
gl.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out lighting
calculateSpecks(noSpecks);
}
@Override
public void dispose(GLAutoDrawable drawable)
{
}
@Override
public void display(GLAutoDrawable drawable)
{
GL2 gl = drawable.getGL().getGL2(); // get the OpenGL 2 graphics context
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers
gl.glColor3f(1.0f, 0.0f, 0.0f);
gl.glLoadIdentity();
glu.gluLookAt(1.0, 0.0, 3.0, 0.0, 0.0, -0.5, 0.0, 1.0, 0.0);
gl.glRotatef(angle, 1.0f, 0.0f, 0.0f);
angle+=increment;
//setLighting(gl);
glut.glutWireSphere(1, 32, 32);
gl.glColor3f(0.0f, 0.0f, 0.0f);
for(int i = 0; i < noSpecks; i++)
{
gl.glBegin(GL.GL_POINTS);
gl.glVertex3i(specks[i][0], specks[i][1], specks[i][2]);
gl.glEnd();
}
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height)
{
GL2 gl = drawable.getGL().getGL2(); // get the OpenGL 2 graphics context
if (height == 0) height = 1; // prevent divide by zero
float aspect = (float)width/height;
// Set the view port (display area) to cover the entire window
gl.glViewport(0, 0, width, height);
// Setup perspective projection, with aspect ratio matches viewport
gl.glMatrixMode(GL_PROJECTION); // choose projection matrix
gl.glLoadIdentity(); // reset projection matrix
glu.gluPerspective(45.0, aspect, 0.1, 100.0); // fovy, aspect, zNear, zFar
// Enable the model-view transform
gl.glMatrixMode(GL_MODELVIEW);
gl.glLoadIdentity(); // reset
}
這不是強制性的,但這是一個很好的做法,以避免重新發明車輪。某些引擎不使用動畫製作者,直接調用GLAutoDrawable.display()來運行GLEventListener實例:http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/opengl/GLAutoDrawable。 html#display()如果您不使用動畫器,並且您不調用GLAutoDrawable.display(),則JOGL不會呈現任何內容。當窗口或畫布被實現或調整大小時調用此方法,這就解釋了爲什麼在您將代碼添加到動畫之前它被調用了幾次。 – gouessej 2015-03-20 09:36:34
啊謝謝你,這是非常有用的:) – Sammdahamm 2015-03-21 10:50:58