display()似乎只在调整大小时被调用

时间:2015-03-18 23:28:48

标签: java opengl jogl

基本上,我实现的旋转仅在屏幕调整大小时被绘制到屏幕上。为了调试,我在display()函数中插入了一个print,输出了该角度。在运行程序时,这仅显示display()的2次迭代的输出,然后在屏幕的任何后续调整期间再次显示。 这让我相信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
    }

1 个答案:

答案 0 :(得分:0)

刚才意识到,我忘了在JOGL中驱动display()函数必须使用动画师。问题解决了