我正在尝试创建一个400x300屏幕,其中有一条从点(180,15)到点(10,145)的线。下面是我创建行的显示函数:
@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.glLoadIdentity(); // reset the model-view matrix
gl.glColor3f(1.0f, 0.0f, 0.0f); //set color of line
gl.glTranslatef(0.0f, 0.0f, -6.0f); // translate into the screen
gl.glBegin(GL_LINES);
gl.glVertex2f(180, 15); //specify line-segment geometry must use relative values
gl.glVertex2f(10,145);
gl.glEnd();
gl.glFlush();
}这是init函数,我首先定义屏幕的正交参数:
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = (GL2) drawable.getGL(); // get the OpenGL graphics context
drawable.setGL(new DebugGL2(gl)); //set debugger. This will come in handy.
glu = new GLU(); // get GL Utilities
gl.glClearDepth(1.0f); // set clear depth value to farthest
gl.glEnable(GL_DEPTH_TEST); // enables depth testing
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // sets base color for glClear()
gl.glDepthFunc(GL_LEQUAL); // the type of depth test to do
gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // best perspective correction
gl.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out lighting
//orthographic parameters set here
glu.gluOrtho2D(0, 200, 0, 150);
}这是一种重塑功能(据我所知),每当调整屏幕大小时,就会调用它。
@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;
glu = new GLU();
// 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
glu.gluOrtho2D(0, 200, 0, 150); //set 2-D orthographic context
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
}我也曾尝试使用:
gl.glMatrixMode(GL_PROJECTION);
gl.glOrtho(0,200,150,0,-1,1);和
gl.glMatrixMode(GL_PROJECTION);
gl.glOrtho(-1,1,-1,1,-1,1);使用相对比例尺然而,我所做的一切似乎都没有用。
发布于 2014-01-30 08:21:46
glOrtho定义的框。
注意,(0, 0)当前位于左下角。如果您正在做任何GUI内容,那么在我们从左到右、从下读时,翻转这个并使原点左上角非常有用。没有什么能阻止你在绘制过程中改变一半--用一些任意的坐标系做一些3D的事情,然后画一个HUD并设置投影来匹配像素。https://stackoverflow.com/questions/21442242
复制相似问题