JOGL无法弄清楚为什么绘制的线条没有出现

时间:2012-03-01 00:27:42

标签: java opengl jogl

我正在尝试学习JOGL,但是首先在画布上绘制线条(如边框)​​,但我无法弄清楚为什么线条没有显示!我猜我没有正确设置我的视口。

以下是一些代码段:

private static final float DISTANCE_FROM_EDGE = 25.0f; 

@Override
public void init(GLAutoDrawable drawable) 
{
  GL2 gl = drawable.getGL().getGL2();
  drawable.setGL(new DebugGL2(gl.getGL2()));

  m_glu = new GLU();

  establishProjectionMatrix(gl , getWidth(), getHeight());

  gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);        
  gl.glEnable(GL2.GL_TEXTURE_2D);
}

@Override
public void reshape(GLAutoDrawable drawable, 
                  int x, 
                  int y, 
                  int width,
                  int height) 
{
  GL2 gl = drawable.getGL().getGL2();
  establishProjectionMatrix(gl , width, height);  
}

public void establishProjectionMatrix(GL2 gl,int width,int height)
{

  gl.glViewport(0, 0, width, height);

  gl.glMatrixMode(GL2.GL_PROJECTION);
  gl.glLoadIdentity();

  m_glu.gluOrtho2D(0,width,height,0);
}

@Override
public void display(GLAutoDrawable drawable) 
{
  update();
  GL2 gl = drawable.getGL().getGL2();
  gl.glClear( GL2.GL_COLOR_BUFFER_BIT );
  gl.glMatrixMode( GL2.GL_MODELVIEW );
  gl.glLoadIdentity();
  drawBoundary(gl);

}

public void drawBoundary(GL2 gl)
{
  gl.glPushMatrix();
  gl.glTranslatef(10.0f, 10.0f, 0.0f); 
  gl.glColor3f(0.0f, 0.1f, 0.0f);
  //Draw left edge
  drawLine(gl, -DISTANCE_FROM_EDGE, -DISTANCE_FROM_EDGE, -DISTANCE_FROM_EDGE,    DISTANCE_FROM_EDGE);
  //Draw top edge
  drawLine(gl, -DISTANCE_FROM_EDGE, DISTANCE_FROM_EDGE, DISTANCE_FROM_EDGE, DISTANCE_FROM_EDGE);
  //Draw right edge
  drawLine(gl, DISTANCE_FROM_EDGE, DISTANCE_FROM_EDGE, DISTANCE_FROM_EDGE, -DISTANCE_FROM_EDGE);
  //Draw bottom edge
  drawLine(gl, DISTANCE_FROM_EDGE, -DISTANCE_FROM_EDGE, -DISTANCE_FROM_EDGE, -DISTANCE_FROM_EDGE);
  gl.glPopMatrix();
}

private void drawLine(GL2 gl, float x1,float y1, float x2, float y2)
{
  gl.glBegin(GL.GL_LINES);
  gl.glVertex2f((x1), (y1));
  gl.glVertex2f((x2), (y2));
  gl.glEnd();
}

1 个答案:

答案 0 :(得分:1)

是否启用了双缓冲?在渲染后尝试调用drawable.swapBuffers()