我正在尝试创建一个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);
即使用相对比例。但是,我所做的一切似乎都无法发挥作用。
答案 0 :(得分:1)
在要绘制的窗口中设置区域或视口
gl.glViewport(0, 0, width, height); //in pixels
设置投影,使OpenGL坐标以像素为单位(与视口的宽度/高度相匹配)
//we want to modify the projection matrix (without this, mesh normals will break)
gl.glMatrixMode(GL_PROJECTION);
//clear any previous transforms the projection matrix may contain (otherwise it would be combined with the following glOrtho matrix)
gl.glLoadIdentity();
//set the projection (could use glTranslate/glScale but this utility function is simpler)
gl.glOrtho(0, width, 0, height, -1, 1); //left,right,bottom,top,front,back
//common practice to leave modelview as the current matrix for editing
gl.glMatrixMode(GL_MODELVIEW);
现在观看量是由glOrtho
定义的框。
请注意,(0, 0)
目前位于左下方。如果你正在做任何GUI的东西,那么当我们从左到右阅读时,翻转它并使原点左上方是非常有用的。没有什么可以阻止你改变绘图的一半 - 用一些任意坐标系统做一些3D东西,然后画一个HUD并设置投影以匹配像素。
在你的显示功能中绘制你的线......
gl.glBegin(GL_LINES);
gl.glVertex2f(180, 15);
gl.glVertex2f(10,145);
gl.glEnd();
或,以演示使用翻译
gl.glLoadIdentity(); //remove the following translate that's still in the modelview matrix from the previous display() call
gl.glTranslatef(180, 15); //modifies the modelview matrix
gl.glBegin(GL_LINES);
gl.glVertex2f(0, 0);
gl.glVertex2f(-170, 130);
gl.glEnd();
您可以考虑将视图向下移动到左侧,或者将模型(线)向上移动到右侧
< / LI> 醇>